2 * IBSS mode implementation
3 * Copyright 2003-2008, Jouni Malinen <j@w1.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 * Copyright 2009, Johannes Berg <johannes@sipsolutions.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/delay.h>
16 #include <linux/if_ether.h>
17 #include <linux/skbuff.h>
18 #include <linux/if_arp.h>
19 #include <linux/etherdevice.h>
20 #include <linux/rtnetlink.h>
21 #include <net/mac80211.h>
22 #include <asm/unaligned.h>
24 #include "ieee80211_i.h"
27 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
28 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
29 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
31 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
32 #define IEEE80211_IBSS_MERGE_DELAY 0x400000
33 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
35 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
38 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
39 struct ieee80211_mgmt *mgmt,
42 u16 auth_alg, auth_transaction, status_code;
47 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
48 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
49 status_code = le16_to_cpu(mgmt->u.auth.status_code);
52 * IEEE 802.11 standard does not require authentication in IBSS
53 * networks and most implementations do not seem to use it.
54 * However, try to reply to authentication attempts if someone
55 * has actually implemented this.
57 if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1)
58 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
59 sdata->u.ibss.bssid, 0);
62 static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
63 const u8 *bssid, const int beacon_int,
64 struct ieee80211_channel *chan,
65 const size_t supp_rates_len,
67 const u16 capability, u64 tsf)
69 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
70 struct ieee80211_local *local = sdata->local;
73 struct ieee80211_mgmt *mgmt;
75 struct ieee80211_supported_band *sband;
77 if (local->ops->reset_tsf) {
78 /* Reset own TSF to allow time synchronization work. */
79 local->ops->reset_tsf(local_to_hw(local));
83 rcu_assign_pointer(ifibss->presp, NULL);
85 skb->data = skb->head;
87 skb_reset_tail_pointer(skb);
88 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
90 if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
91 sta_info_flush(sdata->local, sdata);
93 memcpy(ifibss->bssid, bssid, ETH_ALEN);
95 local->hw.conf.beacon_int = beacon_int >= 10 ? beacon_int : 10;
97 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
99 ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
101 local->oper_channel = chan;
102 local->oper_channel_type = NL80211_CHAN_NO_HT;
103 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
104 sband = local->hw.wiphy->bands[chan->band];
106 /* Build IBSS probe response */
107 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
108 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
109 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
110 IEEE80211_STYPE_PROBE_RESP);
111 memset(mgmt->da, 0xff, ETH_ALEN);
112 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
113 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
114 mgmt->u.beacon.beacon_int = cpu_to_le16(local->hw.conf.beacon_int);
115 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
116 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
118 pos = skb_put(skb, 2 + ifibss->ssid_len);
119 *pos++ = WLAN_EID_SSID;
120 *pos++ = ifibss->ssid_len;
121 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
123 rates = supp_rates_len;
126 pos = skb_put(skb, 2 + rates);
127 *pos++ = WLAN_EID_SUPP_RATES;
129 memcpy(pos, supp_rates, rates);
131 if (sband->band == IEEE80211_BAND_2GHZ) {
132 pos = skb_put(skb, 2 + 1);
133 *pos++ = WLAN_EID_DS_PARAMS;
135 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
138 pos = skb_put(skb, 2 + 2);
139 *pos++ = WLAN_EID_IBSS_PARAMS;
141 /* FIX: set ATIM window based on scan results */
145 if (supp_rates_len > 8) {
146 rates = supp_rates_len - 8;
147 pos = skb_put(skb, 2 + rates);
148 *pos++ = WLAN_EID_EXT_SUPP_RATES;
150 memcpy(pos, &supp_rates[8], rates);
154 memcpy(skb_put(skb, ifibss->ie_len),
155 ifibss->ie, ifibss->ie_len);
157 rcu_assign_pointer(ifibss->presp, skb);
159 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON |
160 IEEE80211_IFCC_BEACON_ENABLED);
163 for (i = 0; i < supp_rates_len; i++) {
164 int bitrate = (supp_rates[i] & 0x7f) * 5;
165 for (j = 0; j < sband->n_bitrates; j++)
166 if (sband->bitrates[j].bitrate == bitrate)
170 ieee80211_sta_def_wmm_params(sdata, supp_rates_len, supp_rates);
172 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
173 mod_timer(&ifibss->timer,
174 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
176 cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
177 mgmt, skb->len, 0, GFP_KERNEL);
178 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
181 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
182 struct ieee80211_bss *bss)
184 __ieee80211_sta_join_ibss(sdata, bss->cbss.bssid,
185 bss->cbss.beacon_interval,
187 bss->supp_rates_len, bss->supp_rates,
188 bss->cbss.capability,
192 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
193 struct ieee80211_mgmt *mgmt,
195 struct ieee80211_rx_status *rx_status,
196 struct ieee802_11_elems *elems,
199 struct ieee80211_local *local = sdata->local;
201 struct ieee80211_bss *bss;
202 struct sta_info *sta;
203 struct ieee80211_channel *channel;
204 u64 beacon_timestamp, rx_timestamp;
206 enum ieee80211_band band = rx_status->band;
208 if (elems->ds_params && elems->ds_params_len == 1)
209 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
211 freq = rx_status->freq;
213 channel = ieee80211_get_channel(local->hw.wiphy, freq);
215 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
218 if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
219 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
220 supp_rates = ieee80211_sta_get_rates(local, elems, band);
224 sta = sta_info_get(local, mgmt->sa);
228 prev_rates = sta->sta.supp_rates[band];
229 /* make sure mandatory rates are always added */
230 sta->sta.supp_rates[band] = supp_rates |
231 ieee80211_mandatory_rates(local, band);
233 #ifdef CONFIG_MAC80211_IBSS_DEBUG
234 if (sta->sta.supp_rates[band] != prev_rates)
235 printk(KERN_DEBUG "%s: updated supp_rates set "
236 "for %pM based on beacon info (0x%llx | "
237 "0x%llx -> 0x%llx)\n",
240 (unsigned long long) prev_rates,
241 (unsigned long long) supp_rates,
242 (unsigned long long) sta->sta.supp_rates[band]);
245 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
250 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
255 /* was just updated in ieee80211_bss_info_update */
256 beacon_timestamp = bss->cbss.tsf;
258 /* check if we need to merge IBSS */
260 /* merge only on beacons (???) */
264 /* we use a fixed BSSID */
265 if (sdata->u.ibss.bssid)
269 if (!(bss->cbss.capability & WLAN_CAPABILITY_IBSS))
272 /* different channel */
273 if (bss->cbss.channel != local->oper_channel)
277 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
278 memcmp(elems->ssid, sdata->u.ibss.ssid,
279 sdata->u.ibss.ssid_len))
283 if (memcmp(bss->cbss.bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
286 if (rx_status->flag & RX_FLAG_TSFT) {
288 * For correct IBSS merging we need mactime; since mactime is
289 * defined as the time the first data symbol of the frame hits
290 * the PHY, and the timestamp of the beacon is defined as "the
291 * time that the data symbol containing the first bit of the
292 * timestamp is transmitted to the PHY plus the transmitting
293 * STA's delays through its local PHY from the MAC-PHY
294 * interface to its interface with the WM" (802.11 11.1.2)
295 * - equals the time this bit arrives at the receiver - we have
296 * to take into account the offset between the two.
298 * E.g. at 1 MBit that means mactime is 192 usec earlier
299 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
303 if (rx_status->flag & RX_FLAG_HT)
304 rate = 65; /* TODO: HT rates */
306 rate = local->hw.wiphy->bands[band]->
307 bitrates[rx_status->rate_idx].bitrate;
309 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
310 } else if (local && local->ops && local->ops->get_tsf)
311 /* second best option: get current TSF */
312 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
314 /* can't merge without knowing the TSF */
315 rx_timestamp = -1LLU;
317 #ifdef CONFIG_MAC80211_IBSS_DEBUG
318 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
319 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
320 mgmt->sa, mgmt->bssid,
321 (unsigned long long)rx_timestamp,
322 (unsigned long long)beacon_timestamp,
323 (unsigned long long)(rx_timestamp - beacon_timestamp),
327 /* give slow hardware some time to do the TSF sync */
328 if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
331 if (beacon_timestamp > rx_timestamp) {
332 #ifdef CONFIG_MAC80211_IBSS_DEBUG
333 printk(KERN_DEBUG "%s: beacon TSF higher than "
334 "local TSF - IBSS merge with BSSID %pM\n",
335 sdata->dev->name, mgmt->bssid);
337 ieee80211_sta_join_ibss(sdata, bss);
338 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
342 ieee80211_rx_bss_put(local, bss);
346 * Add a new IBSS station, will also be called by the RX code when,
347 * in IBSS mode, receiving a frame from a yet-unknown station, hence
348 * must be callable in atomic context.
350 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
351 u8 *bssid,u8 *addr, u32 supp_rates)
353 struct ieee80211_local *local = sdata->local;
354 struct sta_info *sta;
355 int band = local->hw.conf.channel->band;
358 * XXX: Consider removing the least recently used entry and
359 * allow new one to be added.
361 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
363 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
364 sdata->dev->name, addr);
368 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
371 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
372 printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n",
373 wiphy_name(local->hw.wiphy), addr, sdata->dev->name);
376 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
380 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
382 /* make sure mandatory rates are always added */
383 sta->sta.supp_rates[band] = supp_rates |
384 ieee80211_mandatory_rates(local, band);
386 rate_control_rate_init(sta);
388 if (sta_info_insert(sta))
394 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
396 struct ieee80211_local *local = sdata->local;
398 struct sta_info *sta;
402 list_for_each_entry_rcu(sta, &local->sta_list, list) {
403 if (sta->sdata == sdata &&
404 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
417 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
419 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
421 mod_timer(&ifibss->timer,
422 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
424 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
426 if (ieee80211_sta_active_ibss(sdata))
429 if (ifibss->fixed_channel)
432 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
433 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
435 /* XXX maybe racy? */
436 if (sdata->local->scan_req)
439 memcpy(sdata->local->int_scan_req.ssids[0].ssid,
440 ifibss->ssid, IEEE80211_MAX_SSID_LEN);
441 sdata->local->int_scan_req.ssids[0].ssid_len = ifibss->ssid_len;
442 if (ieee80211_request_scan(sdata, &sdata->local->int_scan_req))
443 ieee80211_scan_failed(sdata->local);
446 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
448 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
449 struct ieee80211_local *local = sdata->local;
450 struct ieee80211_supported_band *sband;
453 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
457 if (ifibss->fixed_bssid) {
458 memcpy(bssid, ifibss->bssid, ETH_ALEN);
460 /* Generate random, not broadcast, locally administered BSSID. Mix in
461 * own MAC address to make sure that devices that do not have proper
462 * random number generator get different BSSID. */
463 get_random_bytes(bssid, ETH_ALEN);
464 for (i = 0; i < ETH_ALEN; i++)
465 bssid[i] ^= sdata->dev->dev_addr[i];
470 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
471 sdata->dev->name, bssid);
473 sband = local->hw.wiphy->bands[ifibss->channel->band];
475 if (local->hw.conf.beacon_int == 0)
476 local->hw.conf.beacon_int = 100;
478 capability = WLAN_CAPABILITY_IBSS;
480 if (sdata->default_key)
481 capability |= WLAN_CAPABILITY_PRIVACY;
483 sdata->drop_unencrypted = 0;
486 for (i = 0; i < sband->n_bitrates; i++) {
487 int rate = sband->bitrates[i].bitrate;
488 *pos++ = (u8) (rate / 5);
491 __ieee80211_sta_join_ibss(sdata, bssid, local->hw.conf.beacon_int,
492 ifibss->channel, sband->n_bitrates,
493 supp_rates, capability, 0);
496 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
498 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
499 struct ieee80211_local *local = sdata->local;
500 struct ieee80211_bss *bss;
501 struct ieee80211_channel *chan = NULL;
502 const u8 *bssid = NULL;
505 active_ibss = ieee80211_sta_active_ibss(sdata);
506 #ifdef CONFIG_MAC80211_IBSS_DEBUG
507 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
508 sdata->dev->name, active_ibss);
509 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
514 if (ifibss->fixed_bssid)
515 bssid = ifibss->bssid;
516 if (ifibss->fixed_channel)
517 chan = ifibss->channel;
518 if (!is_zero_ether_addr(ifibss->bssid))
519 bssid = ifibss->bssid;
520 bss = (void *)cfg80211_get_bss(local->hw.wiphy, chan, bssid,
521 ifibss->ssid, ifibss->ssid_len,
522 WLAN_CAPABILITY_IBSS,
523 WLAN_CAPABILITY_IBSS);
525 #ifdef CONFIG_MAC80211_IBSS_DEBUG
527 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
528 "%pM\n", bss->cbss.bssid, ifibss->bssid);
529 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
531 if (bss && memcmp(ifibss->bssid, bss->cbss.bssid, ETH_ALEN)) {
532 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
533 " based on configured SSID\n",
534 sdata->dev->name, bss->cbss.bssid);
536 ieee80211_sta_join_ibss(sdata, bss);
537 ieee80211_rx_bss_put(local, bss);
540 ieee80211_rx_bss_put(local, bss);
542 #ifdef CONFIG_MAC80211_IBSS_DEBUG
543 printk(KERN_DEBUG " did not try to join ibss\n");
544 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
546 /* Selected IBSS not found in current scan results - try to scan */
547 if (ifibss->state == IEEE80211_IBSS_MLME_JOINED &&
548 !ieee80211_sta_active_ibss(sdata)) {
549 mod_timer(&ifibss->timer,
550 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
551 } else if (time_after(jiffies, ifibss->last_scan_completed +
552 IEEE80211_SCAN_INTERVAL)) {
553 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
554 "join\n", sdata->dev->name);
556 /* XXX maybe racy? */
560 memcpy(local->int_scan_req.ssids[0].ssid,
561 ifibss->ssid, IEEE80211_MAX_SSID_LEN);
562 local->int_scan_req.ssids[0].ssid_len =
564 if (ieee80211_request_scan(sdata, &local->int_scan_req))
565 ieee80211_scan_failed(local);
566 } else if (ifibss->state != IEEE80211_IBSS_MLME_JOINED) {
567 int interval = IEEE80211_SCAN_INTERVAL;
569 if (time_after(jiffies, ifibss->ibss_join_req +
570 IEEE80211_IBSS_JOIN_TIMEOUT)) {
571 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
572 ieee80211_sta_create_ibss(sdata);
575 printk(KERN_DEBUG "%s: IBSS not allowed on"
576 " %d MHz\n", sdata->dev->name,
577 local->hw.conf.channel->center_freq);
579 /* No IBSS found - decrease scan interval and continue
581 interval = IEEE80211_SCAN_INTERVAL_SLOW;
584 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
585 mod_timer(&ifibss->timer,
586 round_jiffies(jiffies + interval));
590 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
591 struct ieee80211_mgmt *mgmt,
594 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
595 struct ieee80211_local *local = sdata->local;
598 struct ieee80211_mgmt *resp;
601 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
602 len < 24 + 2 || !ifibss->presp)
605 if (local->ops->tx_last_beacon)
606 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
610 #ifdef CONFIG_MAC80211_IBSS_DEBUG
611 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
612 " (tx_last_beacon=%d)\n",
613 sdata->dev->name, mgmt->sa, mgmt->da,
614 mgmt->bssid, tx_last_beacon);
615 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
620 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
621 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
624 end = ((u8 *) mgmt) + len;
625 pos = mgmt->u.probe_req.variable;
626 if (pos[0] != WLAN_EID_SSID ||
627 pos + 2 + pos[1] > end) {
628 #ifdef CONFIG_MAC80211_IBSS_DEBUG
629 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
631 sdata->dev->name, mgmt->sa);
636 (pos[1] != ifibss->ssid_len ||
637 !memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
638 /* Ignore ProbeReq for foreign SSID */
642 /* Reply with ProbeResp */
643 skb = skb_copy(ifibss->presp, GFP_KERNEL);
647 resp = (struct ieee80211_mgmt *) skb->data;
648 memcpy(resp->da, mgmt->sa, ETH_ALEN);
649 #ifdef CONFIG_MAC80211_IBSS_DEBUG
650 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
651 sdata->dev->name, resp->da);
652 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
653 ieee80211_tx_skb(sdata, skb, 0);
656 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
657 struct ieee80211_mgmt *mgmt,
659 struct ieee80211_rx_status *rx_status)
662 struct ieee802_11_elems elems;
664 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
665 return; /* ignore ProbeResp to foreign address */
667 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
671 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
674 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
677 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
678 struct ieee80211_mgmt *mgmt,
680 struct ieee80211_rx_status *rx_status)
683 struct ieee802_11_elems elems;
685 /* Process beacon from the current BSS */
686 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
690 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
692 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
695 static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
698 struct ieee80211_rx_status *rx_status;
699 struct ieee80211_mgmt *mgmt;
702 rx_status = (struct ieee80211_rx_status *) skb->cb;
703 mgmt = (struct ieee80211_mgmt *) skb->data;
704 fc = le16_to_cpu(mgmt->frame_control);
706 switch (fc & IEEE80211_FCTL_STYPE) {
707 case IEEE80211_STYPE_PROBE_REQ:
708 ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
710 case IEEE80211_STYPE_PROBE_RESP:
711 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
714 case IEEE80211_STYPE_BEACON:
715 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
718 case IEEE80211_STYPE_AUTH:
719 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
726 static void ieee80211_ibss_work(struct work_struct *work)
728 struct ieee80211_sub_if_data *sdata =
729 container_of(work, struct ieee80211_sub_if_data, u.ibss.work);
730 struct ieee80211_local *local = sdata->local;
731 struct ieee80211_if_ibss *ifibss;
734 if (!netif_running(sdata->dev))
737 if (local->sw_scanning || local->hw_scanning)
740 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_ADHOC))
742 ifibss = &sdata->u.ibss;
744 while ((skb = skb_dequeue(&ifibss->skb_queue)))
745 ieee80211_ibss_rx_queued_mgmt(sdata, skb);
747 if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request))
750 switch (ifibss->state) {
751 case IEEE80211_IBSS_MLME_SEARCH:
752 ieee80211_sta_find_ibss(sdata);
754 case IEEE80211_IBSS_MLME_JOINED:
755 ieee80211_sta_merge_ibss(sdata);
763 static void ieee80211_ibss_timer(unsigned long data)
765 struct ieee80211_sub_if_data *sdata =
766 (struct ieee80211_sub_if_data *) data;
767 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
768 struct ieee80211_local *local = sdata->local;
770 set_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request);
771 queue_work(local->hw.workqueue, &ifibss->work);
774 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
776 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
778 INIT_WORK(&ifibss->work, ieee80211_ibss_work);
779 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
780 (unsigned long) sdata);
781 skb_queue_head_init(&ifibss->skb_queue);
784 /* scan finished notification */
785 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
787 struct ieee80211_sub_if_data *sdata;
789 mutex_lock(&local->iflist_mtx);
790 list_for_each_entry(sdata, &local->interfaces, list) {
791 if (!netif_running(sdata->dev))
793 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
795 if (!sdata->u.ibss.ssid_len)
797 sdata->u.ibss.last_scan_completed = jiffies;
798 ieee80211_sta_find_ibss(sdata);
800 mutex_unlock(&local->iflist_mtx);
804 ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
805 struct ieee80211_rx_status *rx_status)
807 struct ieee80211_local *local = sdata->local;
808 struct ieee80211_mgmt *mgmt;
812 return RX_DROP_MONITOR;
814 mgmt = (struct ieee80211_mgmt *) skb->data;
815 fc = le16_to_cpu(mgmt->frame_control);
817 switch (fc & IEEE80211_FCTL_STYPE) {
818 case IEEE80211_STYPE_PROBE_RESP:
819 case IEEE80211_STYPE_BEACON:
820 memcpy(skb->cb, rx_status, sizeof(*rx_status));
821 case IEEE80211_STYPE_PROBE_REQ:
822 case IEEE80211_STYPE_AUTH:
823 skb_queue_tail(&sdata->u.ibss.skb_queue, skb);
824 queue_work(local->hw.workqueue, &sdata->u.ibss.work);
828 return RX_DROP_MONITOR;
831 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
832 struct cfg80211_ibss_params *params)
837 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
838 sdata->u.ibss.fixed_bssid = true;
840 sdata->u.ibss.fixed_bssid = false;
842 sdata->u.ibss.channel = params->channel;
843 sdata->u.ibss.fixed_channel = params->channel_fixed;
846 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
848 if (sdata->u.ibss.ie)
849 sdata->u.ibss.ie_len = params->ie_len;
852 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
856 4 /* IBSS params */ +
861 sdata->u.ibss.skb = skb;
862 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
863 sdata->u.ibss.ibss_join_req = jiffies;
865 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
868 * The ssid_len setting below is used to see whether
869 * we are active, and we need all other settings
870 * before that may get visible.
874 sdata->u.ibss.ssid_len = params->ssid_len;
876 set_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
877 queue_work(sdata->local->hw.workqueue, &sdata->u.ibss.work);
882 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
886 del_timer_sync(&sdata->u.ibss.timer);
887 clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
888 cancel_work_sync(&sdata->u.ibss.work);
889 clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
891 sta_info_flush(sdata->local, sdata);
894 kfree(sdata->u.ibss.ie);
895 skb = sdata->u.ibss.presp;
896 rcu_assign_pointer(sdata->u.ibss.presp, NULL);
897 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON_ENABLED);
901 skb_queue_purge(&sdata->u.ibss.skb_queue);
902 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);