s3c2410fb: adds pixclock to s3c2410fb_display
[pv_ops_mirror.git] / net / mac80211 / ieee80211_sta.c
blob1641e8fe44b715e9b8d3832273d90d7df499b4a4
1 /*
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>
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.
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)
20 #include <linux/delay.h>
21 #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>
28 #include <net/iw_handler.h>
29 #include <asm/types.h>
31 #include <net/mac80211.h>
32 #include "ieee80211_i.h"
33 #include "ieee80211_rate.h"
34 #include "ieee80211_led.h"
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)
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)
54 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
57 #define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
59 #define ERP_INFO_USE_PROTECTION BIT(1)
61 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
62 u8 *ssid, size_t ssid_len);
63 static struct ieee80211_sta_bss *
64 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid);
65 static void ieee80211_rx_bss_put(struct net_device *dev,
66 struct ieee80211_sta_bss *bss);
67 static int ieee80211_sta_find_ibss(struct net_device *dev,
68 struct ieee80211_if_sta *ifsta);
69 static int ieee80211_sta_wep_configured(struct net_device *dev);
70 static int ieee80211_sta_start_scan(struct net_device *dev,
71 u8 *ssid, size_t ssid_len);
72 static int ieee80211_sta_config_auth(struct net_device *dev,
73 struct ieee80211_if_sta *ifsta);
76 /* Parsed Information Elements */
77 struct ieee802_11_elems {
78 /* pointers to IEs */
79 u8 *ssid;
80 u8 *supp_rates;
81 u8 *fh_params;
82 u8 *ds_params;
83 u8 *cf_params;
84 u8 *tim;
85 u8 *ibss_params;
86 u8 *challenge;
87 u8 *wpa;
88 u8 *rsn;
89 u8 *erp_info;
90 u8 *ext_supp_rates;
91 u8 *wmm_info;
92 u8 *wmm_param;
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;
108 u8 wmm_param_len;
111 enum ParseRes { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 };
113 static enum ParseRes ieee802_11_parse_elems(u8 *start, size_t len,
114 struct ieee802_11_elems *elems)
116 size_t left = len;
117 u8 *pos = start;
118 int unknown = 0;
120 memset(elems, 0, sizeof(*elems));
122 while (left >= 2) {
123 u8 id, elen;
125 id = *pos++;
126 elen = *pos++;
127 left -= 2;
129 if (elen > left) {
130 #if 0
131 if (net_ratelimit())
132 printk(KERN_DEBUG "IEEE 802.11 element parse "
133 "failed (id=%d elen=%d left=%d)\n",
134 id, elen, left);
135 #endif
136 return ParseFailed;
139 switch (id) {
140 case WLAN_EID_SSID:
141 elems->ssid = pos;
142 elems->ssid_len = elen;
143 break;
144 case WLAN_EID_SUPP_RATES:
145 elems->supp_rates = pos;
146 elems->supp_rates_len = elen;
147 break;
148 case WLAN_EID_FH_PARAMS:
149 elems->fh_params = pos;
150 elems->fh_params_len = elen;
151 break;
152 case WLAN_EID_DS_PARAMS:
153 elems->ds_params = pos;
154 elems->ds_params_len = elen;
155 break;
156 case WLAN_EID_CF_PARAMS:
157 elems->cf_params = pos;
158 elems->cf_params_len = elen;
159 break;
160 case WLAN_EID_TIM:
161 elems->tim = pos;
162 elems->tim_len = elen;
163 break;
164 case WLAN_EID_IBSS_PARAMS:
165 elems->ibss_params = pos;
166 elems->ibss_params_len = elen;
167 break;
168 case WLAN_EID_CHALLENGE:
169 elems->challenge = pos;
170 elems->challenge_len = elen;
171 break;
172 case WLAN_EID_WPA:
173 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
174 pos[2] == 0xf2) {
175 /* Microsoft OUI (00:50:F2) */
176 if (pos[3] == 1) {
177 /* OUI Type 1 - WPA IE */
178 elems->wpa = pos;
179 elems->wpa_len = elen;
180 } else if (elen >= 5 && pos[3] == 2) {
181 if (pos[4] == 0) {
182 elems->wmm_info = pos;
183 elems->wmm_info_len = elen;
184 } else if (pos[4] == 1) {
185 elems->wmm_param = pos;
186 elems->wmm_param_len = elen;
190 break;
191 case WLAN_EID_RSN:
192 elems->rsn = pos;
193 elems->rsn_len = elen;
194 break;
195 case WLAN_EID_ERP_INFO:
196 elems->erp_info = pos;
197 elems->erp_info_len = elen;
198 break;
199 case WLAN_EID_EXT_SUPP_RATES:
200 elems->ext_supp_rates = pos;
201 elems->ext_supp_rates_len = elen;
202 break;
203 default:
204 #if 0
205 printk(KERN_DEBUG "IEEE 802.11 element parse ignored "
206 "unknown element (id=%d elen=%d)\n",
207 id, elen);
208 #endif
209 unknown++;
210 break;
213 left -= elen;
214 pos += elen;
217 /* Do not trigger error if left == 1 as Apple Airport base stations
218 * send AssocResps that are one spurious byte too long. */
220 return unknown ? ParseUnknown : ParseOK;
226 static int ecw2cw(int ecw)
228 int cw = 1;
229 while (ecw > 0) {
230 cw <<= 1;
231 ecw--;
233 return cw - 1;
236 static void ieee80211_sta_wmm_params(struct net_device *dev,
237 struct ieee80211_if_sta *ifsta,
238 u8 *wmm_param, size_t wmm_param_len)
240 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
241 struct ieee80211_tx_queue_params params;
242 size_t left;
243 int count;
244 u8 *pos;
246 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
247 return;
248 count = wmm_param[6] & 0x0f;
249 if (count == ifsta->wmm_last_param_set)
250 return;
251 ifsta->wmm_last_param_set = count;
253 pos = wmm_param + 8;
254 left = wmm_param_len - 8;
256 memset(&params, 0, sizeof(params));
258 if (!local->ops->conf_tx)
259 return;
261 local->wmm_acm = 0;
262 for (; left >= 4; left -= 4, pos += 4) {
263 int aci = (pos[0] >> 5) & 0x03;
264 int acm = (pos[0] >> 4) & 0x01;
265 int queue;
267 switch (aci) {
268 case 1:
269 queue = IEEE80211_TX_QUEUE_DATA3;
270 if (acm) {
271 local->wmm_acm |= BIT(0) | BIT(3);
273 break;
274 case 2:
275 queue = IEEE80211_TX_QUEUE_DATA1;
276 if (acm) {
277 local->wmm_acm |= BIT(4) | BIT(5);
279 break;
280 case 3:
281 queue = IEEE80211_TX_QUEUE_DATA0;
282 if (acm) {
283 local->wmm_acm |= BIT(6) | BIT(7);
285 break;
286 case 0:
287 default:
288 queue = IEEE80211_TX_QUEUE_DATA2;
289 if (acm) {
290 local->wmm_acm |= BIT(1) | BIT(2);
292 break;
295 params.aifs = pos[0] & 0x0f;
296 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
297 params.cw_min = ecw2cw(pos[1] & 0x0f);
298 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
299 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
300 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
301 "cWmin=%d cWmax=%d burst=%d\n",
302 dev->name, queue, aci, acm, params.aifs, params.cw_min,
303 params.cw_max, params.burst_time);
304 /* TODO: handle ACM (block TX, fallback to next lowest allowed
305 * AC for now) */
306 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
307 printk(KERN_DEBUG "%s: failed to set TX queue "
308 "parameters for queue %d\n", dev->name, queue);
314 static void ieee80211_handle_erp_ie(struct net_device *dev, u8 erp_value)
316 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
317 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
318 int use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
319 int preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
320 u8 changes = 0;
321 DECLARE_MAC_BUF(mac);
323 if (use_protection != !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION)) {
324 if (net_ratelimit()) {
325 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
326 "%s)\n",
327 dev->name,
328 use_protection ? "enabled" : "disabled",
329 print_mac(mac, ifsta->bssid));
331 if (use_protection)
332 sdata->flags |= IEEE80211_SDATA_USE_PROTECTION;
333 else
334 sdata->flags &= ~IEEE80211_SDATA_USE_PROTECTION;
335 changes |= IEEE80211_ERP_CHANGE_PROTECTION;
338 if (preamble_mode != !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE)) {
339 if (net_ratelimit()) {
340 printk(KERN_DEBUG "%s: switched to %s barker preamble"
341 " (BSSID=%s)\n",
342 dev->name,
343 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
344 "short" : "long",
345 print_mac(mac, ifsta->bssid));
347 if (preamble_mode)
348 sdata->flags &= ~IEEE80211_SDATA_SHORT_PREAMBLE;
349 else
350 sdata->flags |= IEEE80211_SDATA_SHORT_PREAMBLE;
351 changes |= IEEE80211_ERP_CHANGE_PREAMBLE;
354 if (changes)
355 ieee80211_erp_info_change_notify(dev, changes);
359 static void ieee80211_sta_send_associnfo(struct net_device *dev,
360 struct ieee80211_if_sta *ifsta)
362 char *buf;
363 size_t len;
364 int i;
365 union iwreq_data wrqu;
367 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
368 return;
370 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
371 ifsta->assocresp_ies_len), GFP_KERNEL);
372 if (!buf)
373 return;
375 len = sprintf(buf, "ASSOCINFO(");
376 if (ifsta->assocreq_ies) {
377 len += sprintf(buf + len, "ReqIEs=");
378 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
379 len += sprintf(buf + len, "%02x",
380 ifsta->assocreq_ies[i]);
383 if (ifsta->assocresp_ies) {
384 if (ifsta->assocreq_ies)
385 len += sprintf(buf + len, " ");
386 len += sprintf(buf + len, "RespIEs=");
387 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
388 len += sprintf(buf + len, "%02x",
389 ifsta->assocresp_ies[i]);
392 len += sprintf(buf + len, ")");
394 if (len > IW_CUSTOM_MAX) {
395 len = sprintf(buf, "ASSOCRESPIE=");
396 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
397 len += sprintf(buf + len, "%02x",
398 ifsta->assocresp_ies[i]);
402 memset(&wrqu, 0, sizeof(wrqu));
403 wrqu.data.length = len;
404 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
406 kfree(buf);
410 static void ieee80211_set_associated(struct net_device *dev,
411 struct ieee80211_if_sta *ifsta,
412 bool assoc)
414 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
415 union iwreq_data wrqu;
417 if (!!(ifsta->flags & IEEE80211_STA_ASSOCIATED) == assoc)
418 return;
420 if (assoc) {
421 struct ieee80211_sub_if_data *sdata;
422 struct ieee80211_sta_bss *bss;
424 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
426 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
427 if (sdata->type != IEEE80211_IF_TYPE_STA)
428 return;
430 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
431 if (bss) {
432 if (bss->has_erp_value)
433 ieee80211_handle_erp_ie(dev, bss->erp_value);
434 ieee80211_rx_bss_put(dev, bss);
437 netif_carrier_on(dev);
438 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
439 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
440 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
441 ieee80211_sta_send_associnfo(dev, ifsta);
442 } else {
443 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
445 netif_carrier_off(dev);
446 ieee80211_reset_erp_info(dev);
447 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
449 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
450 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
451 ifsta->last_probe = jiffies;
452 ieee80211_led_assoc(local, assoc);
455 static void ieee80211_set_disassoc(struct net_device *dev,
456 struct ieee80211_if_sta *ifsta, int deauth)
458 if (deauth)
459 ifsta->auth_tries = 0;
460 ifsta->assoc_tries = 0;
461 ieee80211_set_associated(dev, ifsta, 0);
464 static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
465 int encrypt)
467 struct ieee80211_sub_if_data *sdata;
468 struct ieee80211_tx_packet_data *pkt_data;
470 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
471 skb->dev = sdata->local->mdev;
472 skb_set_mac_header(skb, 0);
473 skb_set_network_header(skb, 0);
474 skb_set_transport_header(skb, 0);
476 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
477 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
478 pkt_data->ifindex = sdata->dev->ifindex;
479 if (!encrypt)
480 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
482 dev_queue_xmit(skb);
486 static void ieee80211_send_auth(struct net_device *dev,
487 struct ieee80211_if_sta *ifsta,
488 int transaction, u8 *extra, size_t extra_len,
489 int encrypt)
491 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
492 struct sk_buff *skb;
493 struct ieee80211_mgmt *mgmt;
495 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
496 sizeof(*mgmt) + 6 + extra_len);
497 if (!skb) {
498 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
499 "frame\n", dev->name);
500 return;
502 skb_reserve(skb, local->hw.extra_tx_headroom);
504 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
505 memset(mgmt, 0, 24 + 6);
506 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
507 IEEE80211_STYPE_AUTH);
508 if (encrypt)
509 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
510 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
511 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
512 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
513 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
514 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
515 ifsta->auth_transaction = transaction + 1;
516 mgmt->u.auth.status_code = cpu_to_le16(0);
517 if (extra)
518 memcpy(skb_put(skb, extra_len), extra, extra_len);
520 ieee80211_sta_tx(dev, skb, encrypt);
524 static void ieee80211_authenticate(struct net_device *dev,
525 struct ieee80211_if_sta *ifsta)
527 DECLARE_MAC_BUF(mac);
529 ifsta->auth_tries++;
530 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
531 printk(KERN_DEBUG "%s: authentication with AP %s"
532 " timed out\n",
533 dev->name, print_mac(mac, ifsta->bssid));
534 ifsta->state = IEEE80211_DISABLED;
535 return;
538 ifsta->state = IEEE80211_AUTHENTICATE;
539 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
540 dev->name, print_mac(mac, ifsta->bssid));
542 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
544 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
548 static void ieee80211_send_assoc(struct net_device *dev,
549 struct ieee80211_if_sta *ifsta)
551 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
552 struct ieee80211_hw_mode *mode;
553 struct sk_buff *skb;
554 struct ieee80211_mgmt *mgmt;
555 u8 *pos, *ies;
556 int i, len;
557 u16 capab;
558 struct ieee80211_sta_bss *bss;
559 int wmm = 0;
561 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
562 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
563 ifsta->ssid_len);
564 if (!skb) {
565 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
566 "frame\n", dev->name);
567 return;
569 skb_reserve(skb, local->hw.extra_tx_headroom);
571 mode = local->oper_hw_mode;
572 capab = ifsta->capab;
573 if (mode->mode == MODE_IEEE80211G) {
574 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
575 WLAN_CAPABILITY_SHORT_PREAMBLE;
577 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
578 if (bss) {
579 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
580 capab |= WLAN_CAPABILITY_PRIVACY;
581 if (bss->wmm_ie) {
582 wmm = 1;
584 ieee80211_rx_bss_put(dev, bss);
587 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
588 memset(mgmt, 0, 24);
589 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
590 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
591 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
593 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
594 skb_put(skb, 10);
595 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
596 IEEE80211_STYPE_REASSOC_REQ);
597 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
598 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
599 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
600 ETH_ALEN);
601 } else {
602 skb_put(skb, 4);
603 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
604 IEEE80211_STYPE_ASSOC_REQ);
605 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
606 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
609 /* SSID */
610 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
611 *pos++ = WLAN_EID_SSID;
612 *pos++ = ifsta->ssid_len;
613 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
615 len = mode->num_rates;
616 if (len > 8)
617 len = 8;
618 pos = skb_put(skb, len + 2);
619 *pos++ = WLAN_EID_SUPP_RATES;
620 *pos++ = len;
621 for (i = 0; i < len; i++) {
622 int rate = mode->rates[i].rate;
623 *pos++ = (u8) (rate / 5);
626 if (mode->num_rates > len) {
627 pos = skb_put(skb, mode->num_rates - len + 2);
628 *pos++ = WLAN_EID_EXT_SUPP_RATES;
629 *pos++ = mode->num_rates - len;
630 for (i = len; i < mode->num_rates; i++) {
631 int rate = mode->rates[i].rate;
632 *pos++ = (u8) (rate / 5);
636 if (ifsta->extra_ie) {
637 pos = skb_put(skb, ifsta->extra_ie_len);
638 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
641 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
642 pos = skb_put(skb, 9);
643 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
644 *pos++ = 7; /* len */
645 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
646 *pos++ = 0x50;
647 *pos++ = 0xf2;
648 *pos++ = 2; /* WME */
649 *pos++ = 0; /* WME info */
650 *pos++ = 1; /* WME ver */
651 *pos++ = 0;
654 kfree(ifsta->assocreq_ies);
655 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
656 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
657 if (ifsta->assocreq_ies)
658 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
660 ieee80211_sta_tx(dev, skb, 0);
664 static void ieee80211_send_deauth(struct net_device *dev,
665 struct ieee80211_if_sta *ifsta, u16 reason)
667 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
668 struct sk_buff *skb;
669 struct ieee80211_mgmt *mgmt;
671 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
672 if (!skb) {
673 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
674 "frame\n", dev->name);
675 return;
677 skb_reserve(skb, local->hw.extra_tx_headroom);
679 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
680 memset(mgmt, 0, 24);
681 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
682 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
683 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
684 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
685 IEEE80211_STYPE_DEAUTH);
686 skb_put(skb, 2);
687 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
689 ieee80211_sta_tx(dev, skb, 0);
693 static void ieee80211_send_disassoc(struct net_device *dev,
694 struct ieee80211_if_sta *ifsta, u16 reason)
696 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
697 struct sk_buff *skb;
698 struct ieee80211_mgmt *mgmt;
700 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
701 if (!skb) {
702 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
703 "frame\n", dev->name);
704 return;
706 skb_reserve(skb, local->hw.extra_tx_headroom);
708 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
709 memset(mgmt, 0, 24);
710 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
711 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
712 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
713 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
714 IEEE80211_STYPE_DISASSOC);
715 skb_put(skb, 2);
716 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
718 ieee80211_sta_tx(dev, skb, 0);
722 static int ieee80211_privacy_mismatch(struct net_device *dev,
723 struct ieee80211_if_sta *ifsta)
725 struct ieee80211_sta_bss *bss;
726 int res = 0;
728 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL) ||
729 ifsta->key_management_enabled)
730 return 0;
732 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
733 if (!bss)
734 return 0;
736 if (ieee80211_sta_wep_configured(dev) !=
737 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
738 res = 1;
740 ieee80211_rx_bss_put(dev, bss);
742 return res;
746 static void ieee80211_associate(struct net_device *dev,
747 struct ieee80211_if_sta *ifsta)
749 DECLARE_MAC_BUF(mac);
751 ifsta->assoc_tries++;
752 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
753 printk(KERN_DEBUG "%s: association with AP %s"
754 " timed out\n",
755 dev->name, print_mac(mac, ifsta->bssid));
756 ifsta->state = IEEE80211_DISABLED;
757 return;
760 ifsta->state = IEEE80211_ASSOCIATE;
761 printk(KERN_DEBUG "%s: associate with AP %s\n",
762 dev->name, print_mac(mac, ifsta->bssid));
763 if (ieee80211_privacy_mismatch(dev, ifsta)) {
764 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
765 "mixed-cell disabled - abort association\n", dev->name);
766 ifsta->state = IEEE80211_DISABLED;
767 return;
770 ieee80211_send_assoc(dev, ifsta);
772 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
776 static void ieee80211_associated(struct net_device *dev,
777 struct ieee80211_if_sta *ifsta)
779 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
780 struct sta_info *sta;
781 int disassoc;
782 DECLARE_MAC_BUF(mac);
784 /* TODO: start monitoring current AP signal quality and number of
785 * missed beacons. Scan other channels every now and then and search
786 * for better APs. */
787 /* TODO: remove expired BSSes */
789 ifsta->state = IEEE80211_ASSOCIATED;
791 sta = sta_info_get(local, ifsta->bssid);
792 if (!sta) {
793 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
794 dev->name, print_mac(mac, ifsta->bssid));
795 disassoc = 1;
796 } else {
797 disassoc = 0;
798 if (time_after(jiffies,
799 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
800 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
801 printk(KERN_DEBUG "%s: No ProbeResp from "
802 "current AP %s - assume out of "
803 "range\n",
804 dev->name, print_mac(mac, ifsta->bssid));
805 disassoc = 1;
806 sta_info_free(sta);
807 } else
808 ieee80211_send_probe_req(dev, ifsta->bssid,
809 local->scan_ssid,
810 local->scan_ssid_len);
811 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
812 } else {
813 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
814 if (time_after(jiffies, ifsta->last_probe +
815 IEEE80211_PROBE_INTERVAL)) {
816 ifsta->last_probe = jiffies;
817 ieee80211_send_probe_req(dev, ifsta->bssid,
818 ifsta->ssid,
819 ifsta->ssid_len);
822 sta_info_put(sta);
824 if (disassoc) {
825 union iwreq_data wrqu;
826 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
827 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
828 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
829 mod_timer(&ifsta->timer, jiffies +
830 IEEE80211_MONITORING_INTERVAL + 30 * HZ);
831 } else {
832 mod_timer(&ifsta->timer, jiffies +
833 IEEE80211_MONITORING_INTERVAL);
838 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
839 u8 *ssid, size_t ssid_len)
841 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
842 struct ieee80211_hw_mode *mode;
843 struct sk_buff *skb;
844 struct ieee80211_mgmt *mgmt;
845 u8 *pos, *supp_rates, *esupp_rates = NULL;
846 int i;
848 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
849 if (!skb) {
850 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
851 "request\n", dev->name);
852 return;
854 skb_reserve(skb, local->hw.extra_tx_headroom);
856 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
857 memset(mgmt, 0, 24);
858 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
859 IEEE80211_STYPE_PROBE_REQ);
860 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
861 if (dst) {
862 memcpy(mgmt->da, dst, ETH_ALEN);
863 memcpy(mgmt->bssid, dst, ETH_ALEN);
864 } else {
865 memset(mgmt->da, 0xff, ETH_ALEN);
866 memset(mgmt->bssid, 0xff, ETH_ALEN);
868 pos = skb_put(skb, 2 + ssid_len);
869 *pos++ = WLAN_EID_SSID;
870 *pos++ = ssid_len;
871 memcpy(pos, ssid, ssid_len);
873 supp_rates = skb_put(skb, 2);
874 supp_rates[0] = WLAN_EID_SUPP_RATES;
875 supp_rates[1] = 0;
876 mode = local->oper_hw_mode;
877 for (i = 0; i < mode->num_rates; i++) {
878 struct ieee80211_rate *rate = &mode->rates[i];
879 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
880 continue;
881 if (esupp_rates) {
882 pos = skb_put(skb, 1);
883 esupp_rates[1]++;
884 } else if (supp_rates[1] == 8) {
885 esupp_rates = skb_put(skb, 3);
886 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
887 esupp_rates[1] = 1;
888 pos = &esupp_rates[2];
889 } else {
890 pos = skb_put(skb, 1);
891 supp_rates[1]++;
893 *pos = rate->rate / 5;
896 ieee80211_sta_tx(dev, skb, 0);
900 static int ieee80211_sta_wep_configured(struct net_device *dev)
902 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
903 if (!sdata || !sdata->default_key ||
904 sdata->default_key->conf.alg != ALG_WEP)
905 return 0;
906 return 1;
910 static void ieee80211_auth_completed(struct net_device *dev,
911 struct ieee80211_if_sta *ifsta)
913 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
914 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
915 ieee80211_associate(dev, ifsta);
919 static void ieee80211_auth_challenge(struct net_device *dev,
920 struct ieee80211_if_sta *ifsta,
921 struct ieee80211_mgmt *mgmt,
922 size_t len)
924 u8 *pos;
925 struct ieee802_11_elems elems;
927 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
928 pos = mgmt->u.auth.variable;
929 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
930 == ParseFailed) {
931 printk(KERN_DEBUG "%s: failed to parse Auth(challenge)\n",
932 dev->name);
933 return;
935 if (!elems.challenge) {
936 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
937 "frame\n", dev->name);
938 return;
940 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
941 elems.challenge_len + 2, 1);
945 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
946 struct ieee80211_if_sta *ifsta,
947 struct ieee80211_mgmt *mgmt,
948 size_t len)
950 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
951 u16 auth_alg, auth_transaction, status_code;
952 DECLARE_MAC_BUF(mac);
954 if (ifsta->state != IEEE80211_AUTHENTICATE &&
955 sdata->type != IEEE80211_IF_TYPE_IBSS) {
956 printk(KERN_DEBUG "%s: authentication frame received from "
957 "%s, but not in authenticate state - ignored\n",
958 dev->name, print_mac(mac, mgmt->sa));
959 return;
962 if (len < 24 + 6) {
963 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
964 "received from %s - ignored\n",
965 dev->name, len, print_mac(mac, mgmt->sa));
966 return;
969 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
970 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
971 printk(KERN_DEBUG "%s: authentication frame received from "
972 "unknown AP (SA=%s BSSID=%s) - "
973 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
974 print_mac(mac, mgmt->bssid));
975 return;
978 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
979 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
980 printk(KERN_DEBUG "%s: authentication frame received from "
981 "unknown BSSID (SA=%s BSSID=%s) - "
982 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
983 print_mac(mac, mgmt->bssid));
984 return;
987 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
988 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
989 status_code = le16_to_cpu(mgmt->u.auth.status_code);
991 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
992 "transaction=%d status=%d)\n",
993 dev->name, print_mac(mac, mgmt->sa), auth_alg,
994 auth_transaction, status_code);
996 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
997 /* IEEE 802.11 standard does not require authentication in IBSS
998 * networks and most implementations do not seem to use it.
999 * However, try to reply to authentication attempts if someone
1000 * has actually implemented this.
1001 * TODO: Could implement shared key authentication. */
1002 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1003 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1004 "frame (alg=%d transaction=%d)\n",
1005 dev->name, auth_alg, auth_transaction);
1006 return;
1008 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1011 if (auth_alg != ifsta->auth_alg ||
1012 auth_transaction != ifsta->auth_transaction) {
1013 printk(KERN_DEBUG "%s: unexpected authentication frame "
1014 "(alg=%d transaction=%d)\n",
1015 dev->name, auth_alg, auth_transaction);
1016 return;
1019 if (status_code != WLAN_STATUS_SUCCESS) {
1020 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1021 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1022 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1023 u8 algs[3];
1024 const int num_algs = ARRAY_SIZE(algs);
1025 int i, pos;
1026 algs[0] = algs[1] = algs[2] = 0xff;
1027 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1028 algs[0] = WLAN_AUTH_OPEN;
1029 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1030 algs[1] = WLAN_AUTH_SHARED_KEY;
1031 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1032 algs[2] = WLAN_AUTH_LEAP;
1033 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1034 pos = 0;
1035 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1036 pos = 1;
1037 else
1038 pos = 2;
1039 for (i = 0; i < num_algs; i++) {
1040 pos++;
1041 if (pos >= num_algs)
1042 pos = 0;
1043 if (algs[pos] == ifsta->auth_alg ||
1044 algs[pos] == 0xff)
1045 continue;
1046 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1047 !ieee80211_sta_wep_configured(dev))
1048 continue;
1049 ifsta->auth_alg = algs[pos];
1050 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1051 "next try\n",
1052 dev->name, ifsta->auth_alg);
1053 break;
1056 return;
1059 switch (ifsta->auth_alg) {
1060 case WLAN_AUTH_OPEN:
1061 case WLAN_AUTH_LEAP:
1062 ieee80211_auth_completed(dev, ifsta);
1063 break;
1064 case WLAN_AUTH_SHARED_KEY:
1065 if (ifsta->auth_transaction == 4)
1066 ieee80211_auth_completed(dev, ifsta);
1067 else
1068 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1069 break;
1074 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1075 struct ieee80211_if_sta *ifsta,
1076 struct ieee80211_mgmt *mgmt,
1077 size_t len)
1079 u16 reason_code;
1080 DECLARE_MAC_BUF(mac);
1082 if (len < 24 + 2) {
1083 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1084 "received from %s - ignored\n",
1085 dev->name, len, print_mac(mac, mgmt->sa));
1086 return;
1089 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1090 printk(KERN_DEBUG "%s: deauthentication frame received from "
1091 "unknown AP (SA=%s BSSID=%s) - "
1092 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1093 print_mac(mac, mgmt->bssid));
1094 return;
1097 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1099 printk(KERN_DEBUG "%s: RX deauthentication from %s"
1100 " (reason=%d)\n",
1101 dev->name, print_mac(mac, mgmt->sa), reason_code);
1103 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
1104 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1107 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1108 ifsta->state == IEEE80211_ASSOCIATE ||
1109 ifsta->state == IEEE80211_ASSOCIATED) {
1110 ifsta->state = IEEE80211_AUTHENTICATE;
1111 mod_timer(&ifsta->timer, jiffies +
1112 IEEE80211_RETRY_AUTH_INTERVAL);
1115 ieee80211_set_disassoc(dev, ifsta, 1);
1116 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1120 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1121 struct ieee80211_if_sta *ifsta,
1122 struct ieee80211_mgmt *mgmt,
1123 size_t len)
1125 u16 reason_code;
1126 DECLARE_MAC_BUF(mac);
1128 if (len < 24 + 2) {
1129 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1130 "received from %s - ignored\n",
1131 dev->name, len, print_mac(mac, mgmt->sa));
1132 return;
1135 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1136 printk(KERN_DEBUG "%s: disassociation frame received from "
1137 "unknown AP (SA=%s BSSID=%s) - "
1138 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1139 print_mac(mac, mgmt->bssid));
1140 return;
1143 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1145 printk(KERN_DEBUG "%s: RX disassociation from %s"
1146 " (reason=%d)\n",
1147 dev->name, print_mac(mac, mgmt->sa), reason_code);
1149 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1150 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1152 if (ifsta->state == IEEE80211_ASSOCIATED) {
1153 ifsta->state = IEEE80211_ASSOCIATE;
1154 mod_timer(&ifsta->timer, jiffies +
1155 IEEE80211_RETRY_AUTH_INTERVAL);
1158 ieee80211_set_disassoc(dev, ifsta, 0);
1162 static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev,
1163 struct ieee80211_if_sta *ifsta,
1164 struct ieee80211_mgmt *mgmt,
1165 size_t len,
1166 int reassoc)
1168 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1169 struct ieee80211_hw_mode *mode;
1170 struct sta_info *sta;
1171 u32 rates;
1172 u16 capab_info, status_code, aid;
1173 struct ieee802_11_elems elems;
1174 u8 *pos;
1175 int i, j;
1176 DECLARE_MAC_BUF(mac);
1178 /* AssocResp and ReassocResp have identical structure, so process both
1179 * of them in this function. */
1181 if (ifsta->state != IEEE80211_ASSOCIATE) {
1182 printk(KERN_DEBUG "%s: association frame received from "
1183 "%s, but not in associate state - ignored\n",
1184 dev->name, print_mac(mac, mgmt->sa));
1185 return;
1188 if (len < 24 + 6) {
1189 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1190 "received from %s - ignored\n",
1191 dev->name, len, print_mac(mac, mgmt->sa));
1192 return;
1195 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1196 printk(KERN_DEBUG "%s: association frame received from "
1197 "unknown AP (SA=%s BSSID=%s) - "
1198 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1199 print_mac(mac, mgmt->bssid));
1200 return;
1203 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1204 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1205 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1206 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1207 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1208 "set\n", dev->name, aid);
1209 aid &= ~(BIT(15) | BIT(14));
1211 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1212 "status=%d aid=%d)\n",
1213 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1214 capab_info, status_code, aid);
1216 if (status_code != WLAN_STATUS_SUCCESS) {
1217 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1218 dev->name, status_code);
1219 /* if this was a reassociation, ensure we try a "full"
1220 * association next time. This works around some broken APs
1221 * which do not correctly reject reassociation requests. */
1222 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1223 return;
1226 pos = mgmt->u.assoc_resp.variable;
1227 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
1228 == ParseFailed) {
1229 printk(KERN_DEBUG "%s: failed to parse AssocResp\n",
1230 dev->name);
1231 return;
1234 if (!elems.supp_rates) {
1235 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1236 dev->name);
1237 return;
1240 /* it probably doesn't, but if the frame includes an ERP value then
1241 * update our stored copy */
1242 if (elems.erp_info && elems.erp_info_len >= 1) {
1243 struct ieee80211_sta_bss *bss
1244 = ieee80211_rx_bss_get(dev, ifsta->bssid);
1245 if (bss) {
1246 bss->erp_value = elems.erp_info[0];
1247 bss->has_erp_value = 1;
1248 ieee80211_rx_bss_put(dev, bss);
1252 printk(KERN_DEBUG "%s: associated\n", dev->name);
1253 ifsta->aid = aid;
1254 ifsta->ap_capab = capab_info;
1256 kfree(ifsta->assocresp_ies);
1257 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1258 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1259 if (ifsta->assocresp_ies)
1260 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1262 ieee80211_set_associated(dev, ifsta, 1);
1264 /* Add STA entry for the AP */
1265 sta = sta_info_get(local, ifsta->bssid);
1266 if (!sta) {
1267 struct ieee80211_sta_bss *bss;
1268 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
1269 if (!sta) {
1270 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1271 " AP\n", dev->name);
1272 return;
1274 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
1275 if (bss) {
1276 sta->last_rssi = bss->rssi;
1277 sta->last_signal = bss->signal;
1278 sta->last_noise = bss->noise;
1279 ieee80211_rx_bss_put(dev, bss);
1283 sta->dev = dev;
1284 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP;
1286 rates = 0;
1287 mode = local->oper_hw_mode;
1288 for (i = 0; i < elems.supp_rates_len; i++) {
1289 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1290 for (j = 0; j < mode->num_rates; j++)
1291 if (mode->rates[j].rate == rate)
1292 rates |= BIT(j);
1294 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1295 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1296 for (j = 0; j < mode->num_rates; j++)
1297 if (mode->rates[j].rate == rate)
1298 rates |= BIT(j);
1300 sta->supp_rates = rates;
1302 rate_control_rate_init(sta, local);
1304 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1305 sta->flags |= WLAN_STA_WME;
1306 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1307 elems.wmm_param_len);
1311 sta_info_put(sta);
1313 ieee80211_associated(dev, ifsta);
1317 /* Caller must hold local->sta_bss_lock */
1318 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1319 struct ieee80211_sta_bss *bss)
1321 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1322 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1323 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1327 /* Caller must hold local->sta_bss_lock */
1328 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1329 struct ieee80211_sta_bss *bss)
1331 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1332 struct ieee80211_sta_bss *b, *prev = NULL;
1333 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1334 while (b) {
1335 if (b == bss) {
1336 if (!prev)
1337 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1338 bss->hnext;
1339 else
1340 prev->hnext = bss->hnext;
1341 break;
1343 prev = b;
1344 b = b->hnext;
1349 static struct ieee80211_sta_bss *
1350 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid)
1352 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1353 struct ieee80211_sta_bss *bss;
1355 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
1356 if (!bss)
1357 return NULL;
1358 atomic_inc(&bss->users);
1359 atomic_inc(&bss->users);
1360 memcpy(bss->bssid, bssid, ETH_ALEN);
1362 spin_lock_bh(&local->sta_bss_lock);
1363 /* TODO: order by RSSI? */
1364 list_add_tail(&bss->list, &local->sta_bss_list);
1365 __ieee80211_rx_bss_hash_add(dev, bss);
1366 spin_unlock_bh(&local->sta_bss_lock);
1367 return bss;
1371 static struct ieee80211_sta_bss *
1372 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid)
1374 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1375 struct ieee80211_sta_bss *bss;
1377 spin_lock_bh(&local->sta_bss_lock);
1378 bss = local->sta_bss_hash[STA_HASH(bssid)];
1379 while (bss) {
1380 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1381 atomic_inc(&bss->users);
1382 break;
1384 bss = bss->hnext;
1386 spin_unlock_bh(&local->sta_bss_lock);
1387 return bss;
1391 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1393 kfree(bss->wpa_ie);
1394 kfree(bss->rsn_ie);
1395 kfree(bss->wmm_ie);
1396 kfree(bss);
1400 static void ieee80211_rx_bss_put(struct net_device *dev,
1401 struct ieee80211_sta_bss *bss)
1403 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1404 if (!atomic_dec_and_test(&bss->users))
1405 return;
1407 spin_lock_bh(&local->sta_bss_lock);
1408 __ieee80211_rx_bss_hash_del(dev, bss);
1409 list_del(&bss->list);
1410 spin_unlock_bh(&local->sta_bss_lock);
1411 ieee80211_rx_bss_free(bss);
1415 void ieee80211_rx_bss_list_init(struct net_device *dev)
1417 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1418 spin_lock_init(&local->sta_bss_lock);
1419 INIT_LIST_HEAD(&local->sta_bss_list);
1423 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1425 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1426 struct ieee80211_sta_bss *bss, *tmp;
1428 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1429 ieee80211_rx_bss_put(dev, bss);
1433 static void ieee80211_rx_bss_info(struct net_device *dev,
1434 struct ieee80211_mgmt *mgmt,
1435 size_t len,
1436 struct ieee80211_rx_status *rx_status,
1437 int beacon)
1439 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1440 struct ieee802_11_elems elems;
1441 size_t baselen;
1442 int channel, invalid = 0, clen;
1443 struct ieee80211_sta_bss *bss;
1444 struct sta_info *sta;
1445 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1446 u64 timestamp;
1447 DECLARE_MAC_BUF(mac);
1448 DECLARE_MAC_BUF(mac2);
1450 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1451 return; /* ignore ProbeResp to foreign address */
1453 #if 0
1454 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
1455 dev->name, beacon ? "Beacon" : "Probe Response",
1456 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
1457 #endif
1459 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1460 if (baselen > len)
1461 return;
1463 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1465 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1466 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1467 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1468 static unsigned long last_tsf_debug = 0;
1469 u64 tsf;
1470 if (local->ops->get_tsf)
1471 tsf = local->ops->get_tsf(local_to_hw(local));
1472 else
1473 tsf = -1LLU;
1474 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1475 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
1476 "%s TSF=0x%llx BCN=0x%llx diff=%lld "
1477 "@%lu\n",
1478 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->bssid),
1479 (unsigned long long)tsf,
1480 (unsigned long long)timestamp,
1481 (unsigned long long)(tsf - timestamp),
1482 jiffies);
1483 last_tsf_debug = jiffies;
1485 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1488 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1489 &elems) == ParseFailed)
1490 invalid = 1;
1492 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1493 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1494 (sta = sta_info_get(local, mgmt->sa))) {
1495 struct ieee80211_hw_mode *mode;
1496 struct ieee80211_rate *rates;
1497 size_t num_rates;
1498 u32 supp_rates, prev_rates;
1499 int i, j;
1501 mode = local->sta_scanning ?
1502 local->scan_hw_mode : local->oper_hw_mode;
1503 rates = mode->rates;
1504 num_rates = mode->num_rates;
1506 supp_rates = 0;
1507 for (i = 0; i < elems.supp_rates_len +
1508 elems.ext_supp_rates_len; i++) {
1509 u8 rate = 0;
1510 int own_rate;
1511 if (i < elems.supp_rates_len)
1512 rate = elems.supp_rates[i];
1513 else if (elems.ext_supp_rates)
1514 rate = elems.ext_supp_rates
1515 [i - elems.supp_rates_len];
1516 own_rate = 5 * (rate & 0x7f);
1517 for (j = 0; j < num_rates; j++)
1518 if (rates[j].rate == own_rate)
1519 supp_rates |= BIT(j);
1522 prev_rates = sta->supp_rates;
1523 sta->supp_rates &= supp_rates;
1524 if (sta->supp_rates == 0) {
1525 /* No matching rates - this should not really happen.
1526 * Make sure that at least one rate is marked
1527 * supported to avoid issues with TX rate ctrl. */
1528 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1530 if (sta->supp_rates != prev_rates) {
1531 printk(KERN_DEBUG "%s: updated supp_rates set for "
1532 "%s based on beacon info (0x%x & 0x%x -> "
1533 "0x%x)\n",
1534 dev->name, print_mac(mac, sta->addr), prev_rates,
1535 supp_rates, sta->supp_rates);
1537 sta_info_put(sta);
1540 if (!elems.ssid)
1541 return;
1543 if (elems.ds_params && elems.ds_params_len == 1)
1544 channel = elems.ds_params[0];
1545 else
1546 channel = rx_status->channel;
1548 bss = ieee80211_rx_bss_get(dev, mgmt->bssid);
1549 if (!bss) {
1550 bss = ieee80211_rx_bss_add(dev, mgmt->bssid);
1551 if (!bss)
1552 return;
1553 } else {
1554 #if 0
1555 /* TODO: order by RSSI? */
1556 spin_lock_bh(&local->sta_bss_lock);
1557 list_move_tail(&bss->list, &local->sta_bss_list);
1558 spin_unlock_bh(&local->sta_bss_lock);
1559 #endif
1562 if (bss->probe_resp && beacon) {
1563 /* Do not allow beacon to override data from Probe Response. */
1564 ieee80211_rx_bss_put(dev, bss);
1565 return;
1568 /* save the ERP value so that it is available at association time */
1569 if (elems.erp_info && elems.erp_info_len >= 1) {
1570 bss->erp_value = elems.erp_info[0];
1571 bss->has_erp_value = 1;
1574 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1575 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1576 if (elems.ssid && elems.ssid_len <= IEEE80211_MAX_SSID_LEN) {
1577 memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1578 bss->ssid_len = elems.ssid_len;
1581 bss->supp_rates_len = 0;
1582 if (elems.supp_rates) {
1583 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1584 if (clen > elems.supp_rates_len)
1585 clen = elems.supp_rates_len;
1586 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1587 clen);
1588 bss->supp_rates_len += clen;
1590 if (elems.ext_supp_rates) {
1591 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1592 if (clen > elems.ext_supp_rates_len)
1593 clen = elems.ext_supp_rates_len;
1594 memcpy(&bss->supp_rates[bss->supp_rates_len],
1595 elems.ext_supp_rates, clen);
1596 bss->supp_rates_len += clen;
1599 if (elems.wpa &&
1600 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1601 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1602 kfree(bss->wpa_ie);
1603 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1604 if (bss->wpa_ie) {
1605 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1606 bss->wpa_ie_len = elems.wpa_len + 2;
1607 } else
1608 bss->wpa_ie_len = 0;
1609 } else if (!elems.wpa && bss->wpa_ie) {
1610 kfree(bss->wpa_ie);
1611 bss->wpa_ie = NULL;
1612 bss->wpa_ie_len = 0;
1615 if (elems.rsn &&
1616 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1617 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1618 kfree(bss->rsn_ie);
1619 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1620 if (bss->rsn_ie) {
1621 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1622 bss->rsn_ie_len = elems.rsn_len + 2;
1623 } else
1624 bss->rsn_ie_len = 0;
1625 } else if (!elems.rsn && bss->rsn_ie) {
1626 kfree(bss->rsn_ie);
1627 bss->rsn_ie = NULL;
1628 bss->rsn_ie_len = 0;
1631 if (elems.wmm_param &&
1632 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1633 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1634 kfree(bss->wmm_ie);
1635 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1636 if (bss->wmm_ie) {
1637 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1638 elems.wmm_param_len + 2);
1639 bss->wmm_ie_len = elems.wmm_param_len + 2;
1640 } else
1641 bss->wmm_ie_len = 0;
1642 } else if (!elems.wmm_param && bss->wmm_ie) {
1643 kfree(bss->wmm_ie);
1644 bss->wmm_ie = NULL;
1645 bss->wmm_ie_len = 0;
1649 bss->hw_mode = rx_status->phymode;
1650 bss->channel = channel;
1651 bss->freq = rx_status->freq;
1652 if (channel != rx_status->channel &&
1653 (bss->hw_mode == MODE_IEEE80211G ||
1654 bss->hw_mode == MODE_IEEE80211B) &&
1655 channel >= 1 && channel <= 14) {
1656 static const int freq_list[] = {
1657 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1658 2447, 2452, 2457, 2462, 2467, 2472, 2484
1660 /* IEEE 802.11g/b mode can receive packets from neighboring
1661 * channels, so map the channel into frequency. */
1662 bss->freq = freq_list[channel - 1];
1664 bss->timestamp = timestamp;
1665 bss->last_update = jiffies;
1666 bss->rssi = rx_status->ssi;
1667 bss->signal = rx_status->signal;
1668 bss->noise = rx_status->noise;
1669 if (!beacon)
1670 bss->probe_resp++;
1671 ieee80211_rx_bss_put(dev, bss);
1675 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1676 struct ieee80211_mgmt *mgmt,
1677 size_t len,
1678 struct ieee80211_rx_status *rx_status)
1680 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1684 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1685 struct ieee80211_mgmt *mgmt,
1686 size_t len,
1687 struct ieee80211_rx_status *rx_status)
1689 struct ieee80211_sub_if_data *sdata;
1690 struct ieee80211_if_sta *ifsta;
1691 size_t baselen;
1692 struct ieee802_11_elems elems;
1694 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1696 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1697 if (sdata->type != IEEE80211_IF_TYPE_STA)
1698 return;
1699 ifsta = &sdata->u.sta;
1701 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
1702 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1703 return;
1705 /* Process beacon from the current BSS */
1706 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1707 if (baselen > len)
1708 return;
1710 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1711 &elems) == ParseFailed)
1712 return;
1714 if (elems.erp_info && elems.erp_info_len >= 1)
1715 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
1717 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1718 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1719 elems.wmm_param_len);
1724 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1725 struct ieee80211_if_sta *ifsta,
1726 struct ieee80211_mgmt *mgmt,
1727 size_t len,
1728 struct ieee80211_rx_status *rx_status)
1730 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1731 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1732 int tx_last_beacon;
1733 struct sk_buff *skb;
1734 struct ieee80211_mgmt *resp;
1735 u8 *pos, *end;
1736 DECLARE_MAC_BUF(mac);
1737 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1738 DECLARE_MAC_BUF(mac2);
1739 DECLARE_MAC_BUF(mac3);
1740 #endif
1742 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1743 ifsta->state != IEEE80211_IBSS_JOINED ||
1744 len < 24 + 2 || !ifsta->probe_resp)
1745 return;
1747 if (local->ops->tx_last_beacon)
1748 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1749 else
1750 tx_last_beacon = 1;
1752 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1753 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
1754 "%s (tx_last_beacon=%d)\n",
1755 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
1756 print_mac(mac3, mgmt->bssid), tx_last_beacon);
1757 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1759 if (!tx_last_beacon)
1760 return;
1762 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1763 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1764 return;
1766 end = ((u8 *) mgmt) + len;
1767 pos = mgmt->u.probe_req.variable;
1768 if (pos[0] != WLAN_EID_SSID ||
1769 pos + 2 + pos[1] > end) {
1770 if (net_ratelimit()) {
1771 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1772 "from %s\n",
1773 dev->name, print_mac(mac, mgmt->sa));
1775 return;
1777 if (pos[1] != 0 &&
1778 (pos[1] != ifsta->ssid_len ||
1779 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1780 /* Ignore ProbeReq for foreign SSID */
1781 return;
1784 /* Reply with ProbeResp */
1785 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
1786 if (!skb)
1787 return;
1789 resp = (struct ieee80211_mgmt *) skb->data;
1790 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1791 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1792 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
1793 dev->name, print_mac(mac, resp->da));
1794 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1795 ieee80211_sta_tx(dev, skb, 0);
1799 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1800 struct ieee80211_rx_status *rx_status)
1802 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1803 struct ieee80211_sub_if_data *sdata;
1804 struct ieee80211_if_sta *ifsta;
1805 struct ieee80211_mgmt *mgmt;
1806 u16 fc;
1808 if (skb->len < 24)
1809 goto fail;
1811 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1812 ifsta = &sdata->u.sta;
1814 mgmt = (struct ieee80211_mgmt *) skb->data;
1815 fc = le16_to_cpu(mgmt->frame_control);
1817 switch (fc & IEEE80211_FCTL_STYPE) {
1818 case IEEE80211_STYPE_PROBE_REQ:
1819 case IEEE80211_STYPE_PROBE_RESP:
1820 case IEEE80211_STYPE_BEACON:
1821 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1822 case IEEE80211_STYPE_AUTH:
1823 case IEEE80211_STYPE_ASSOC_RESP:
1824 case IEEE80211_STYPE_REASSOC_RESP:
1825 case IEEE80211_STYPE_DEAUTH:
1826 case IEEE80211_STYPE_DISASSOC:
1827 skb_queue_tail(&ifsta->skb_queue, skb);
1828 queue_work(local->hw.workqueue, &ifsta->work);
1829 return;
1830 default:
1831 printk(KERN_DEBUG "%s: received unknown management frame - "
1832 "stype=%d\n", dev->name,
1833 (fc & IEEE80211_FCTL_STYPE) >> 4);
1834 break;
1837 fail:
1838 kfree_skb(skb);
1842 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1843 struct sk_buff *skb)
1845 struct ieee80211_rx_status *rx_status;
1846 struct ieee80211_sub_if_data *sdata;
1847 struct ieee80211_if_sta *ifsta;
1848 struct ieee80211_mgmt *mgmt;
1849 u16 fc;
1851 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1852 ifsta = &sdata->u.sta;
1854 rx_status = (struct ieee80211_rx_status *) skb->cb;
1855 mgmt = (struct ieee80211_mgmt *) skb->data;
1856 fc = le16_to_cpu(mgmt->frame_control);
1858 switch (fc & IEEE80211_FCTL_STYPE) {
1859 case IEEE80211_STYPE_PROBE_REQ:
1860 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1861 rx_status);
1862 break;
1863 case IEEE80211_STYPE_PROBE_RESP:
1864 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1865 break;
1866 case IEEE80211_STYPE_BEACON:
1867 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1868 break;
1869 case IEEE80211_STYPE_AUTH:
1870 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1871 break;
1872 case IEEE80211_STYPE_ASSOC_RESP:
1873 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1874 break;
1875 case IEEE80211_STYPE_REASSOC_RESP:
1876 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1877 break;
1878 case IEEE80211_STYPE_DEAUTH:
1879 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1880 break;
1881 case IEEE80211_STYPE_DISASSOC:
1882 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1883 break;
1886 kfree_skb(skb);
1890 void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1891 struct ieee80211_rx_status *rx_status)
1893 struct ieee80211_mgmt *mgmt;
1894 u16 fc;
1896 if (skb->len < 24) {
1897 dev_kfree_skb(skb);
1898 return;
1901 mgmt = (struct ieee80211_mgmt *) skb->data;
1902 fc = le16_to_cpu(mgmt->frame_control);
1904 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1905 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1906 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1907 skb->len, rx_status);
1908 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1909 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1910 rx_status);
1914 dev_kfree_skb(skb);
1918 static int ieee80211_sta_active_ibss(struct net_device *dev)
1920 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1921 int active = 0;
1922 struct sta_info *sta;
1924 read_lock_bh(&local->sta_lock);
1925 list_for_each_entry(sta, &local->sta_list, list) {
1926 if (sta->dev == dev &&
1927 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1928 jiffies)) {
1929 active++;
1930 break;
1933 read_unlock_bh(&local->sta_lock);
1935 return active;
1939 static void ieee80211_sta_expire(struct net_device *dev)
1941 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1942 struct sta_info *sta, *tmp;
1943 LIST_HEAD(tmp_list);
1944 DECLARE_MAC_BUF(mac);
1946 write_lock_bh(&local->sta_lock);
1947 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1948 if (time_after(jiffies, sta->last_rx +
1949 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1950 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
1951 dev->name, print_mac(mac, sta->addr));
1952 __sta_info_get(sta);
1953 sta_info_remove(sta);
1954 list_add(&sta->list, &tmp_list);
1956 write_unlock_bh(&local->sta_lock);
1958 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
1959 sta_info_free(sta);
1960 sta_info_put(sta);
1965 static void ieee80211_sta_merge_ibss(struct net_device *dev,
1966 struct ieee80211_if_sta *ifsta)
1968 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1970 ieee80211_sta_expire(dev);
1971 if (ieee80211_sta_active_ibss(dev))
1972 return;
1974 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1975 "IBSS networks with same SSID (merge)\n", dev->name);
1976 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1980 void ieee80211_sta_timer(unsigned long data)
1982 struct ieee80211_sub_if_data *sdata =
1983 (struct ieee80211_sub_if_data *) data;
1984 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1985 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
1987 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1988 queue_work(local->hw.workqueue, &ifsta->work);
1992 void ieee80211_sta_work(struct work_struct *work)
1994 struct ieee80211_sub_if_data *sdata =
1995 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
1996 struct net_device *dev = sdata->dev;
1997 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1998 struct ieee80211_if_sta *ifsta;
1999 struct sk_buff *skb;
2001 if (!netif_running(dev))
2002 return;
2004 if (local->sta_scanning)
2005 return;
2007 if (sdata->type != IEEE80211_IF_TYPE_STA &&
2008 sdata->type != IEEE80211_IF_TYPE_IBSS) {
2009 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
2010 "(type=%d)\n", dev->name, sdata->type);
2011 return;
2013 ifsta = &sdata->u.sta;
2015 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2016 ieee80211_sta_rx_queued_mgmt(dev, skb);
2018 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2019 ifsta->state != IEEE80211_ASSOCIATE &&
2020 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2021 ieee80211_sta_start_scan(dev, NULL, 0);
2022 return;
2025 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2026 if (ieee80211_sta_config_auth(dev, ifsta))
2027 return;
2028 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2029 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2030 return;
2032 switch (ifsta->state) {
2033 case IEEE80211_DISABLED:
2034 break;
2035 case IEEE80211_AUTHENTICATE:
2036 ieee80211_authenticate(dev, ifsta);
2037 break;
2038 case IEEE80211_ASSOCIATE:
2039 ieee80211_associate(dev, ifsta);
2040 break;
2041 case IEEE80211_ASSOCIATED:
2042 ieee80211_associated(dev, ifsta);
2043 break;
2044 case IEEE80211_IBSS_SEARCH:
2045 ieee80211_sta_find_ibss(dev, ifsta);
2046 break;
2047 case IEEE80211_IBSS_JOINED:
2048 ieee80211_sta_merge_ibss(dev, ifsta);
2049 break;
2050 default:
2051 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2052 ifsta->state);
2053 break;
2056 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2057 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2058 "mixed-cell disabled - disassociate\n", dev->name);
2060 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2061 ieee80211_set_disassoc(dev, ifsta, 0);
2066 static void ieee80211_sta_reset_auth(struct net_device *dev,
2067 struct ieee80211_if_sta *ifsta)
2069 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2071 if (local->ops->reset_tsf) {
2072 /* Reset own TSF to allow time synchronization work. */
2073 local->ops->reset_tsf(local_to_hw(local));
2076 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2079 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2080 ifsta->auth_alg = WLAN_AUTH_OPEN;
2081 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2082 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2083 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2084 ifsta->auth_alg = WLAN_AUTH_LEAP;
2085 else
2086 ifsta->auth_alg = WLAN_AUTH_OPEN;
2087 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2088 ifsta->auth_alg);
2089 ifsta->auth_transaction = -1;
2090 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2091 ifsta->auth_tries = ifsta->assoc_tries = 0;
2092 netif_carrier_off(dev);
2096 void ieee80211_sta_req_auth(struct net_device *dev,
2097 struct ieee80211_if_sta *ifsta)
2099 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2100 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2102 if (sdata->type != IEEE80211_IF_TYPE_STA)
2103 return;
2105 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2106 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2107 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2108 IEEE80211_STA_AUTO_SSID_SEL))) {
2109 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2110 queue_work(local->hw.workqueue, &ifsta->work);
2114 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2115 const char *ssid, int ssid_len)
2117 int tmp, hidden_ssid;
2119 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2120 return 1;
2122 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2123 return 0;
2125 hidden_ssid = 1;
2126 tmp = ssid_len;
2127 while (tmp--) {
2128 if (ssid[tmp] != '\0') {
2129 hidden_ssid = 0;
2130 break;
2134 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2135 return 1;
2137 if (ssid_len == 1 && ssid[0] == ' ')
2138 return 1;
2140 return 0;
2143 static int ieee80211_sta_config_auth(struct net_device *dev,
2144 struct ieee80211_if_sta *ifsta)
2146 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2147 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2148 struct ieee80211_sta_bss *bss, *selected = NULL;
2149 int top_rssi = 0, freq;
2151 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2152 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
2153 ifsta->state = IEEE80211_AUTHENTICATE;
2154 ieee80211_sta_reset_auth(dev, ifsta);
2155 return 0;
2158 spin_lock_bh(&local->sta_bss_lock);
2159 freq = local->oper_channel->freq;
2160 list_for_each_entry(bss, &local->sta_bss_list, list) {
2161 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2162 continue;
2164 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2165 !!sdata->default_key)
2166 continue;
2168 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2169 bss->freq != freq)
2170 continue;
2172 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2173 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2174 continue;
2176 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2177 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2178 continue;
2180 if (!selected || top_rssi < bss->rssi) {
2181 selected = bss;
2182 top_rssi = bss->rssi;
2185 if (selected)
2186 atomic_inc(&selected->users);
2187 spin_unlock_bh(&local->sta_bss_lock);
2189 if (selected) {
2190 ieee80211_set_channel(local, -1, selected->freq);
2191 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2192 ieee80211_sta_set_ssid(dev, selected->ssid,
2193 selected->ssid_len);
2194 ieee80211_sta_set_bssid(dev, selected->bssid);
2195 ieee80211_rx_bss_put(dev, selected);
2196 ifsta->state = IEEE80211_AUTHENTICATE;
2197 ieee80211_sta_reset_auth(dev, ifsta);
2198 return 0;
2199 } else {
2200 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2201 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2202 ieee80211_sta_start_scan(dev, NULL, 0);
2203 else
2204 ieee80211_sta_start_scan(dev, ifsta->ssid,
2205 ifsta->ssid_len);
2206 ifsta->state = IEEE80211_AUTHENTICATE;
2207 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2208 } else
2209 ifsta->state = IEEE80211_DISABLED;
2211 return -1;
2214 static int ieee80211_sta_join_ibss(struct net_device *dev,
2215 struct ieee80211_if_sta *ifsta,
2216 struct ieee80211_sta_bss *bss)
2218 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2219 int res, rates, i, j;
2220 struct sk_buff *skb;
2221 struct ieee80211_mgmt *mgmt;
2222 struct ieee80211_tx_control control;
2223 struct ieee80211_rate *rate;
2224 struct ieee80211_hw_mode *mode;
2225 struct rate_control_extra extra;
2226 u8 *pos;
2227 struct ieee80211_sub_if_data *sdata;
2229 /* Remove possible STA entries from other IBSS networks. */
2230 sta_info_flush(local, NULL);
2232 if (local->ops->reset_tsf) {
2233 /* Reset own TSF to allow time synchronization work. */
2234 local->ops->reset_tsf(local_to_hw(local));
2236 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2237 res = ieee80211_if_config(dev);
2238 if (res)
2239 return res;
2241 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2243 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2244 sdata->drop_unencrypted = bss->capability &
2245 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2247 res = ieee80211_set_channel(local, -1, bss->freq);
2249 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2250 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2251 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2252 local->hw.conf.freq);
2253 return -1;
2256 /* Set beacon template based on scan results */
2257 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2258 do {
2259 if (!skb)
2260 break;
2262 skb_reserve(skb, local->hw.extra_tx_headroom);
2264 mgmt = (struct ieee80211_mgmt *)
2265 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2266 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2267 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2268 IEEE80211_STYPE_BEACON);
2269 memset(mgmt->da, 0xff, ETH_ALEN);
2270 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2271 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2272 mgmt->u.beacon.beacon_int =
2273 cpu_to_le16(local->hw.conf.beacon_int);
2274 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2276 pos = skb_put(skb, 2 + ifsta->ssid_len);
2277 *pos++ = WLAN_EID_SSID;
2278 *pos++ = ifsta->ssid_len;
2279 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2281 rates = bss->supp_rates_len;
2282 if (rates > 8)
2283 rates = 8;
2284 pos = skb_put(skb, 2 + rates);
2285 *pos++ = WLAN_EID_SUPP_RATES;
2286 *pos++ = rates;
2287 memcpy(pos, bss->supp_rates, rates);
2289 pos = skb_put(skb, 2 + 1);
2290 *pos++ = WLAN_EID_DS_PARAMS;
2291 *pos++ = 1;
2292 *pos++ = bss->channel;
2294 pos = skb_put(skb, 2 + 2);
2295 *pos++ = WLAN_EID_IBSS_PARAMS;
2296 *pos++ = 2;
2297 /* FIX: set ATIM window based on scan results */
2298 *pos++ = 0;
2299 *pos++ = 0;
2301 if (bss->supp_rates_len > 8) {
2302 rates = bss->supp_rates_len - 8;
2303 pos = skb_put(skb, 2 + rates);
2304 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2305 *pos++ = rates;
2306 memcpy(pos, &bss->supp_rates[8], rates);
2309 memset(&control, 0, sizeof(control));
2310 memset(&extra, 0, sizeof(extra));
2311 extra.mode = local->oper_hw_mode;
2312 rate = rate_control_get_rate(local, dev, skb, &extra);
2313 if (!rate) {
2314 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2315 "for IBSS beacon\n", dev->name);
2316 break;
2318 control.tx_rate =
2319 ((sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
2320 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2321 rate->val2 : rate->val;
2322 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2323 control.power_level = local->hw.conf.power_level;
2324 control.flags |= IEEE80211_TXCTL_NO_ACK;
2325 control.retry_limit = 1;
2327 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2328 if (ifsta->probe_resp) {
2329 mgmt = (struct ieee80211_mgmt *)
2330 ifsta->probe_resp->data;
2331 mgmt->frame_control =
2332 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2333 IEEE80211_STYPE_PROBE_RESP);
2334 } else {
2335 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2336 "template for IBSS\n", dev->name);
2339 if (local->ops->beacon_update &&
2340 local->ops->beacon_update(local_to_hw(local),
2341 skb, &control) == 0) {
2342 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2343 "template based on scan results\n", dev->name);
2344 skb = NULL;
2347 rates = 0;
2348 mode = local->oper_hw_mode;
2349 for (i = 0; i < bss->supp_rates_len; i++) {
2350 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2351 for (j = 0; j < mode->num_rates; j++)
2352 if (mode->rates[j].rate == bitrate)
2353 rates |= BIT(j);
2355 ifsta->supp_rates_bits = rates;
2356 } while (0);
2358 if (skb) {
2359 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2360 "template\n", dev->name);
2361 dev_kfree_skb(skb);
2364 ifsta->state = IEEE80211_IBSS_JOINED;
2365 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2367 ieee80211_rx_bss_put(dev, bss);
2369 return res;
2373 static int ieee80211_sta_create_ibss(struct net_device *dev,
2374 struct ieee80211_if_sta *ifsta)
2376 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2377 struct ieee80211_sta_bss *bss;
2378 struct ieee80211_sub_if_data *sdata;
2379 struct ieee80211_hw_mode *mode;
2380 u8 bssid[ETH_ALEN], *pos;
2381 int i;
2382 DECLARE_MAC_BUF(mac);
2384 #if 0
2385 /* Easier testing, use fixed BSSID. */
2386 memset(bssid, 0xfe, ETH_ALEN);
2387 #else
2388 /* Generate random, not broadcast, locally administered BSSID. Mix in
2389 * own MAC address to make sure that devices that do not have proper
2390 * random number generator get different BSSID. */
2391 get_random_bytes(bssid, ETH_ALEN);
2392 for (i = 0; i < ETH_ALEN; i++)
2393 bssid[i] ^= dev->dev_addr[i];
2394 bssid[0] &= ~0x01;
2395 bssid[0] |= 0x02;
2396 #endif
2398 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2399 dev->name, print_mac(mac, bssid));
2401 bss = ieee80211_rx_bss_add(dev, bssid);
2402 if (!bss)
2403 return -ENOMEM;
2405 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2406 mode = local->oper_hw_mode;
2408 if (local->hw.conf.beacon_int == 0)
2409 local->hw.conf.beacon_int = 100;
2410 bss->beacon_int = local->hw.conf.beacon_int;
2411 bss->hw_mode = local->hw.conf.phymode;
2412 bss->channel = local->hw.conf.channel;
2413 bss->freq = local->hw.conf.freq;
2414 bss->last_update = jiffies;
2415 bss->capability = WLAN_CAPABILITY_IBSS;
2416 if (sdata->default_key) {
2417 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2418 } else
2419 sdata->drop_unencrypted = 0;
2420 bss->supp_rates_len = mode->num_rates;
2421 pos = bss->supp_rates;
2422 for (i = 0; i < mode->num_rates; i++) {
2423 int rate = mode->rates[i].rate;
2424 *pos++ = (u8) (rate / 5);
2427 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2431 static int ieee80211_sta_find_ibss(struct net_device *dev,
2432 struct ieee80211_if_sta *ifsta)
2434 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2435 struct ieee80211_sta_bss *bss;
2436 int found = 0;
2437 u8 bssid[ETH_ALEN];
2438 int active_ibss;
2439 DECLARE_MAC_BUF(mac);
2440 DECLARE_MAC_BUF(mac2);
2442 if (ifsta->ssid_len == 0)
2443 return -EINVAL;
2445 active_ibss = ieee80211_sta_active_ibss(dev);
2446 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2447 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2448 dev->name, active_ibss);
2449 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2450 spin_lock_bh(&local->sta_bss_lock);
2451 list_for_each_entry(bss, &local->sta_bss_list, list) {
2452 if (ifsta->ssid_len != bss->ssid_len ||
2453 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2454 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2455 continue;
2456 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2457 printk(KERN_DEBUG " bssid=%s found\n",
2458 print_mac(mac, bss->bssid));
2459 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2460 memcpy(bssid, bss->bssid, ETH_ALEN);
2461 found = 1;
2462 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2463 break;
2465 spin_unlock_bh(&local->sta_bss_lock);
2467 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2468 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
2469 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
2470 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2471 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2472 (bss = ieee80211_rx_bss_get(dev, bssid))) {
2473 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
2474 " based on configured SSID\n",
2475 dev->name, print_mac(mac, bssid));
2476 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2478 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2479 printk(KERN_DEBUG " did not try to join ibss\n");
2480 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2482 /* Selected IBSS not found in current scan results - try to scan */
2483 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2484 !ieee80211_sta_active_ibss(dev)) {
2485 mod_timer(&ifsta->timer, jiffies +
2486 IEEE80211_IBSS_MERGE_INTERVAL);
2487 } else if (time_after(jiffies, local->last_scan_completed +
2488 IEEE80211_SCAN_INTERVAL)) {
2489 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2490 "join\n", dev->name);
2491 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2492 ifsta->ssid_len);
2493 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2494 int interval = IEEE80211_SCAN_INTERVAL;
2496 if (time_after(jiffies, ifsta->ibss_join_req +
2497 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2498 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2499 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2500 return ieee80211_sta_create_ibss(dev, ifsta);
2501 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2502 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2503 " configured channel %d (%d MHz)\n",
2504 dev->name, local->hw.conf.channel,
2505 local->hw.conf.freq);
2508 /* No IBSS found - decrease scan interval and continue
2509 * scanning. */
2510 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2513 ifsta->state = IEEE80211_IBSS_SEARCH;
2514 mod_timer(&ifsta->timer, jiffies + interval);
2515 return 0;
2518 return 0;
2522 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2524 struct ieee80211_sub_if_data *sdata;
2525 struct ieee80211_if_sta *ifsta;
2526 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2528 if (len > IEEE80211_MAX_SSID_LEN)
2529 return -EINVAL;
2531 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2532 * not defined. */
2533 if (local->ops->conf_tx) {
2534 struct ieee80211_tx_queue_params qparam;
2535 int i;
2537 memset(&qparam, 0, sizeof(qparam));
2538 /* TODO: are these ok defaults for all hw_modes? */
2539 qparam.aifs = 2;
2540 qparam.cw_min =
2541 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2542 qparam.cw_max = 1023;
2543 qparam.burst_time = 0;
2544 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2546 local->ops->conf_tx(local_to_hw(local),
2547 i + IEEE80211_TX_QUEUE_DATA0,
2548 &qparam);
2550 /* IBSS uses different parameters for Beacon sending */
2551 qparam.cw_min++;
2552 qparam.cw_min *= 2;
2553 qparam.cw_min--;
2554 local->ops->conf_tx(local_to_hw(local),
2555 IEEE80211_TX_QUEUE_BEACON, &qparam);
2558 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2559 ifsta = &sdata->u.sta;
2561 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2562 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2563 memcpy(ifsta->ssid, ssid, len);
2564 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2565 ifsta->ssid_len = len;
2567 if (len)
2568 ifsta->flags |= IEEE80211_STA_SSID_SET;
2569 else
2570 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2571 if (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2572 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2573 ifsta->ibss_join_req = jiffies;
2574 ifsta->state = IEEE80211_IBSS_SEARCH;
2575 return ieee80211_sta_find_ibss(dev, ifsta);
2577 return 0;
2581 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2583 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2584 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2585 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2586 *len = ifsta->ssid_len;
2587 return 0;
2591 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2593 struct ieee80211_sub_if_data *sdata;
2594 struct ieee80211_if_sta *ifsta;
2595 int res;
2597 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2598 ifsta = &sdata->u.sta;
2600 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2601 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2602 res = ieee80211_if_config(dev);
2603 if (res) {
2604 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2605 "the low-level driver\n", dev->name);
2606 return res;
2610 if (is_valid_ether_addr(bssid))
2611 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2612 else
2613 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2615 return 0;
2619 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2620 struct ieee80211_sub_if_data *sdata,
2621 int powersave)
2623 struct sk_buff *skb;
2624 struct ieee80211_hdr *nullfunc;
2625 u16 fc;
2627 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2628 if (!skb) {
2629 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2630 "frame\n", sdata->dev->name);
2631 return;
2633 skb_reserve(skb, local->hw.extra_tx_headroom);
2635 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2636 memset(nullfunc, 0, 24);
2637 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2638 IEEE80211_FCTL_TODS;
2639 if (powersave)
2640 fc |= IEEE80211_FCTL_PM;
2641 nullfunc->frame_control = cpu_to_le16(fc);
2642 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2643 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2644 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2646 ieee80211_sta_tx(sdata->dev, skb, 0);
2650 void ieee80211_scan_completed(struct ieee80211_hw *hw)
2652 struct ieee80211_local *local = hw_to_local(hw);
2653 struct net_device *dev = local->scan_dev;
2654 struct ieee80211_sub_if_data *sdata;
2655 union iwreq_data wrqu;
2657 local->last_scan_completed = jiffies;
2658 wmb();
2659 local->sta_scanning = 0;
2661 if (ieee80211_hw_config(local))
2662 printk(KERN_DEBUG "%s: failed to restore operational"
2663 "channel after scan\n", dev->name);
2666 netif_tx_lock_bh(local->mdev);
2667 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
2668 local->ops->configure_filter(local_to_hw(local),
2669 FIF_BCN_PRBRESP_PROMISC,
2670 &local->filter_flags,
2671 local->mdev->mc_count,
2672 local->mdev->mc_list);
2674 netif_tx_unlock_bh(local->mdev);
2676 memset(&wrqu, 0, sizeof(wrqu));
2677 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2679 rcu_read_lock();
2680 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2682 /* No need to wake the master device. */
2683 if (sdata->dev == local->mdev)
2684 continue;
2686 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2687 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
2688 ieee80211_send_nullfunc(local, sdata, 0);
2689 ieee80211_sta_timer((unsigned long)sdata);
2692 netif_wake_queue(sdata->dev);
2694 rcu_read_unlock();
2696 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2697 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2698 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2699 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
2700 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2701 !ieee80211_sta_active_ibss(dev)))
2702 ieee80211_sta_find_ibss(dev, ifsta);
2705 EXPORT_SYMBOL(ieee80211_scan_completed);
2707 void ieee80211_sta_scan_work(struct work_struct *work)
2709 struct ieee80211_local *local =
2710 container_of(work, struct ieee80211_local, scan_work.work);
2711 struct net_device *dev = local->scan_dev;
2712 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2713 struct ieee80211_hw_mode *mode;
2714 struct ieee80211_channel *chan;
2715 int skip;
2716 unsigned long next_delay = 0;
2718 if (!local->sta_scanning)
2719 return;
2721 switch (local->scan_state) {
2722 case SCAN_SET_CHANNEL:
2723 mode = local->scan_hw_mode;
2724 if (local->scan_hw_mode->list.next == &local->modes_list &&
2725 local->scan_channel_idx >= mode->num_channels) {
2726 ieee80211_scan_completed(local_to_hw(local));
2727 return;
2729 skip = !(local->enabled_modes & (1 << mode->mode));
2730 chan = &mode->channels[local->scan_channel_idx];
2731 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2732 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2733 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2734 (local->hw_modes & local->enabled_modes &
2735 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2736 skip = 1;
2738 if (!skip) {
2739 #if 0
2740 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2741 dev->name, chan->chan, chan->freq);
2742 #endif
2744 local->scan_channel = chan;
2745 if (ieee80211_hw_config(local)) {
2746 printk(KERN_DEBUG "%s: failed to set channel "
2747 "%d (%d MHz) for scan\n", dev->name,
2748 chan->chan, chan->freq);
2749 skip = 1;
2753 local->scan_channel_idx++;
2754 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2755 if (local->scan_hw_mode->list.next != &local->modes_list) {
2756 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2757 struct ieee80211_hw_mode,
2758 list);
2759 local->scan_channel_idx = 0;
2763 if (skip)
2764 break;
2766 next_delay = IEEE80211_PROBE_DELAY +
2767 usecs_to_jiffies(local->hw.channel_change_time);
2768 local->scan_state = SCAN_SEND_PROBE;
2769 break;
2770 case SCAN_SEND_PROBE:
2771 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2772 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2773 local->scan_ssid_len);
2774 next_delay = IEEE80211_CHANNEL_TIME;
2775 } else
2776 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2777 local->scan_state = SCAN_SET_CHANNEL;
2778 break;
2781 if (local->sta_scanning)
2782 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2783 next_delay);
2787 static int ieee80211_sta_start_scan(struct net_device *dev,
2788 u8 *ssid, size_t ssid_len)
2790 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2791 struct ieee80211_sub_if_data *sdata;
2793 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2794 return -EINVAL;
2796 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2797 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2798 * BSSID: MACAddress
2799 * SSID
2800 * ScanType: ACTIVE, PASSIVE
2801 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2802 * a Probe frame during active scanning
2803 * ChannelList
2804 * MinChannelTime (>= ProbeDelay), in TU
2805 * MaxChannelTime: (>= MinChannelTime), in TU
2808 /* MLME-SCAN.confirm
2809 * BSSDescriptionSet
2810 * ResultCode: SUCCESS, INVALID_PARAMETERS
2813 if (local->sta_scanning) {
2814 if (local->scan_dev == dev)
2815 return 0;
2816 return -EBUSY;
2819 if (local->ops->hw_scan) {
2820 int rc = local->ops->hw_scan(local_to_hw(local),
2821 ssid, ssid_len);
2822 if (!rc) {
2823 local->sta_scanning = 1;
2824 local->scan_dev = dev;
2826 return rc;
2829 local->sta_scanning = 1;
2831 rcu_read_lock();
2832 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2834 /* Don't stop the master interface, otherwise we can't transmit
2835 * probes! */
2836 if (sdata->dev == local->mdev)
2837 continue;
2839 netif_stop_queue(sdata->dev);
2840 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2841 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
2842 ieee80211_send_nullfunc(local, sdata, 1);
2844 rcu_read_unlock();
2846 if (ssid) {
2847 local->scan_ssid_len = ssid_len;
2848 memcpy(local->scan_ssid, ssid, ssid_len);
2849 } else
2850 local->scan_ssid_len = 0;
2851 local->scan_state = SCAN_SET_CHANNEL;
2852 local->scan_hw_mode = list_entry(local->modes_list.next,
2853 struct ieee80211_hw_mode,
2854 list);
2855 local->scan_channel_idx = 0;
2856 local->scan_dev = dev;
2858 netif_tx_lock_bh(local->mdev);
2859 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
2860 local->ops->configure_filter(local_to_hw(local),
2861 FIF_BCN_PRBRESP_PROMISC,
2862 &local->filter_flags,
2863 local->mdev->mc_count,
2864 local->mdev->mc_list);
2865 netif_tx_unlock_bh(local->mdev);
2867 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2868 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2869 IEEE80211_CHANNEL_TIME);
2871 return 0;
2875 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2877 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2878 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2879 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2881 if (sdata->type != IEEE80211_IF_TYPE_STA)
2882 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2884 if (local->sta_scanning) {
2885 if (local->scan_dev == dev)
2886 return 0;
2887 return -EBUSY;
2890 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2891 queue_work(local->hw.workqueue, &ifsta->work);
2892 return 0;
2895 static char *
2896 ieee80211_sta_scan_result(struct net_device *dev,
2897 struct ieee80211_sta_bss *bss,
2898 char *current_ev, char *end_buf)
2900 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2901 struct iw_event iwe;
2903 if (time_after(jiffies,
2904 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2905 return current_ev;
2907 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2908 return current_ev;
2910 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2911 !bss->wpa_ie && !bss->rsn_ie)
2912 return current_ev;
2914 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2915 (local->scan_ssid_len != bss->ssid_len ||
2916 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2917 return current_ev;
2919 memset(&iwe, 0, sizeof(iwe));
2920 iwe.cmd = SIOCGIWAP;
2921 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2922 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2923 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2924 IW_EV_ADDR_LEN);
2926 memset(&iwe, 0, sizeof(iwe));
2927 iwe.cmd = SIOCGIWESSID;
2928 iwe.u.data.length = bss->ssid_len;
2929 iwe.u.data.flags = 1;
2930 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2931 bss->ssid);
2933 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2934 memset(&iwe, 0, sizeof(iwe));
2935 iwe.cmd = SIOCGIWMODE;
2936 if (bss->capability & WLAN_CAPABILITY_ESS)
2937 iwe.u.mode = IW_MODE_MASTER;
2938 else
2939 iwe.u.mode = IW_MODE_ADHOC;
2940 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2941 IW_EV_UINT_LEN);
2944 memset(&iwe, 0, sizeof(iwe));
2945 iwe.cmd = SIOCGIWFREQ;
2946 iwe.u.freq.m = bss->channel;
2947 iwe.u.freq.e = 0;
2948 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2949 IW_EV_FREQ_LEN);
2950 iwe.u.freq.m = bss->freq * 100000;
2951 iwe.u.freq.e = 1;
2952 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2953 IW_EV_FREQ_LEN);
2955 memset(&iwe, 0, sizeof(iwe));
2956 iwe.cmd = IWEVQUAL;
2957 iwe.u.qual.qual = bss->signal;
2958 iwe.u.qual.level = bss->rssi;
2959 iwe.u.qual.noise = bss->noise;
2960 iwe.u.qual.updated = local->wstats_flags;
2961 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2962 IW_EV_QUAL_LEN);
2964 memset(&iwe, 0, sizeof(iwe));
2965 iwe.cmd = SIOCGIWENCODE;
2966 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2967 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2968 else
2969 iwe.u.data.flags = IW_ENCODE_DISABLED;
2970 iwe.u.data.length = 0;
2971 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2973 if (bss && bss->wpa_ie) {
2974 memset(&iwe, 0, sizeof(iwe));
2975 iwe.cmd = IWEVGENIE;
2976 iwe.u.data.length = bss->wpa_ie_len;
2977 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2978 bss->wpa_ie);
2981 if (bss && bss->rsn_ie) {
2982 memset(&iwe, 0, sizeof(iwe));
2983 iwe.cmd = IWEVGENIE;
2984 iwe.u.data.length = bss->rsn_ie_len;
2985 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2986 bss->rsn_ie);
2989 if (bss && bss->supp_rates_len > 0) {
2990 /* display all supported rates in readable format */
2991 char *p = current_ev + IW_EV_LCP_LEN;
2992 int i;
2994 memset(&iwe, 0, sizeof(iwe));
2995 iwe.cmd = SIOCGIWRATE;
2996 /* Those two flags are ignored... */
2997 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2999 for (i = 0; i < bss->supp_rates_len; i++) {
3000 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3001 0x7f) * 500000);
3002 p = iwe_stream_add_value(current_ev, p,
3003 end_buf, &iwe, IW_EV_PARAM_LEN);
3005 current_ev = p;
3008 if (bss) {
3009 char *buf;
3010 buf = kmalloc(30, GFP_ATOMIC);
3011 if (buf) {
3012 memset(&iwe, 0, sizeof(iwe));
3013 iwe.cmd = IWEVCUSTOM;
3014 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3015 iwe.u.data.length = strlen(buf);
3016 current_ev = iwe_stream_add_point(current_ev, end_buf,
3017 &iwe, buf);
3018 kfree(buf);
3022 do {
3023 char *buf;
3025 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
3026 break;
3028 buf = kmalloc(100, GFP_ATOMIC);
3029 if (!buf)
3030 break;
3032 memset(&iwe, 0, sizeof(iwe));
3033 iwe.cmd = IWEVCUSTOM;
3034 sprintf(buf, "bcn_int=%d", bss->beacon_int);
3035 iwe.u.data.length = strlen(buf);
3036 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3037 buf);
3039 memset(&iwe, 0, sizeof(iwe));
3040 iwe.cmd = IWEVCUSTOM;
3041 sprintf(buf, "capab=0x%04x", bss->capability);
3042 iwe.u.data.length = strlen(buf);
3043 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3044 buf);
3046 kfree(buf);
3047 break;
3048 } while (0);
3050 return current_ev;
3054 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3056 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3057 char *current_ev = buf;
3058 char *end_buf = buf + len;
3059 struct ieee80211_sta_bss *bss;
3061 spin_lock_bh(&local->sta_bss_lock);
3062 list_for_each_entry(bss, &local->sta_bss_list, list) {
3063 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3064 spin_unlock_bh(&local->sta_bss_lock);
3065 return -E2BIG;
3067 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3068 end_buf);
3070 spin_unlock_bh(&local->sta_bss_lock);
3071 return current_ev - buf;
3075 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3077 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3078 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3079 kfree(ifsta->extra_ie);
3080 if (len == 0) {
3081 ifsta->extra_ie = NULL;
3082 ifsta->extra_ie_len = 0;
3083 return 0;
3085 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3086 if (!ifsta->extra_ie) {
3087 ifsta->extra_ie_len = 0;
3088 return -ENOMEM;
3090 memcpy(ifsta->extra_ie, ie, len);
3091 ifsta->extra_ie_len = len;
3092 return 0;
3096 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3097 struct sk_buff *skb, u8 *bssid,
3098 u8 *addr)
3100 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3101 struct sta_info *sta;
3102 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3103 DECLARE_MAC_BUF(mac);
3105 /* TODO: Could consider removing the least recently used entry and
3106 * allow new one to be added. */
3107 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3108 if (net_ratelimit()) {
3109 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3110 "entry %s\n", dev->name, print_mac(mac, addr));
3112 return NULL;
3115 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
3116 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
3118 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3119 if (!sta)
3120 return NULL;
3122 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3124 rate_control_rate_init(sta, local);
3126 return sta; /* caller will call sta_info_put() */
3130 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3132 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3133 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3135 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3136 dev->name, reason);
3138 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3139 sdata->type != IEEE80211_IF_TYPE_IBSS)
3140 return -EINVAL;
3142 ieee80211_send_deauth(dev, ifsta, reason);
3143 ieee80211_set_disassoc(dev, ifsta, 1);
3144 return 0;
3148 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3150 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3151 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3153 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3154 dev->name, reason);
3156 if (sdata->type != IEEE80211_IF_TYPE_STA)
3157 return -EINVAL;
3159 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
3160 return -1;
3162 ieee80211_send_disassoc(dev, ifsta, reason);
3163 ieee80211_set_disassoc(dev, ifsta, 0);
3164 return 0;