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_INACTIVITY_LIMIT (60 * HZ)
34 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
37 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
38 struct ieee80211_mgmt *mgmt,
41 u16 auth_alg, auth_transaction, status_code;
46 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
47 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
48 status_code = le16_to_cpu(mgmt->u.auth.status_code);
51 * IEEE 802.11 standard does not require authentication in IBSS
52 * networks and most implementations do not seem to use it.
53 * However, try to reply to authentication attempts if someone
54 * has actually implemented this.
56 if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1)
57 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
58 sdata->u.ibss.bssid, 0);
61 static int __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
62 const u8 *bssid, const int beacon_int,
64 const size_t supp_rates_len,
68 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
69 struct ieee80211_local *local = sdata->local;
70 int res = 0, rates, i, j;
72 struct ieee80211_mgmt *mgmt;
74 struct ieee80211_supported_band *sband;
75 union iwreq_data wrqu;
77 if (local->ops->reset_tsf) {
78 /* Reset own TSF to allow time synchronization work. */
79 local->ops->reset_tsf(local_to_hw(local));
82 if ((ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET) &&
83 memcmp(ifibss->bssid, bssid, ETH_ALEN) == 0)
86 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
88 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
89 "response\n", sdata->dev->name);
93 if (!(ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET)) {
94 /* Remove possible STA entries from other IBSS networks. */
95 sta_info_flush_delayed(sdata);
98 memcpy(ifibss->bssid, bssid, ETH_ALEN);
99 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
103 local->hw.conf.beacon_int = beacon_int >= 10 ? beacon_int : 10;
105 sdata->drop_unencrypted = capability &
106 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
108 res = ieee80211_set_freq(sdata, freq);
113 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
115 /* Build IBSS probe response */
117 skb_reserve(skb, local->hw.extra_tx_headroom);
119 mgmt = (struct ieee80211_mgmt *)
120 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
121 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
122 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
123 IEEE80211_STYPE_PROBE_RESP);
124 memset(mgmt->da, 0xff, ETH_ALEN);
125 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
126 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
127 mgmt->u.beacon.beacon_int =
128 cpu_to_le16(local->hw.conf.beacon_int);
129 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
131 pos = skb_put(skb, 2 + ifibss->ssid_len);
132 *pos++ = WLAN_EID_SSID;
133 *pos++ = ifibss->ssid_len;
134 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
136 rates = supp_rates_len;
139 pos = skb_put(skb, 2 + rates);
140 *pos++ = WLAN_EID_SUPP_RATES;
142 memcpy(pos, supp_rates, rates);
144 if (sband->band == IEEE80211_BAND_2GHZ) {
145 pos = skb_put(skb, 2 + 1);
146 *pos++ = WLAN_EID_DS_PARAMS;
148 *pos++ = ieee80211_frequency_to_channel(freq);
151 pos = skb_put(skb, 2 + 2);
152 *pos++ = WLAN_EID_IBSS_PARAMS;
154 /* FIX: set ATIM window based on scan results */
158 if (supp_rates_len > 8) {
159 rates = supp_rates_len - 8;
160 pos = skb_put(skb, 2 + rates);
161 *pos++ = WLAN_EID_EXT_SUPP_RATES;
163 memcpy(pos, &supp_rates[8], rates);
166 ifibss->probe_resp = skb;
168 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON |
169 IEEE80211_IFCC_BEACON_ENABLED);
173 for (i = 0; i < supp_rates_len; i++) {
174 int bitrate = (supp_rates[i] & 0x7f) * 5;
175 for (j = 0; j < sband->n_bitrates; j++)
176 if (sband->bitrates[j].bitrate == bitrate)
180 ieee80211_sta_def_wmm_params(sdata, supp_rates_len, supp_rates);
182 ifibss->flags |= IEEE80211_IBSS_PREV_BSSID_SET;
183 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
184 mod_timer(&ifibss->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
186 memset(&wrqu, 0, sizeof(wrqu));
187 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
188 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
193 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
194 struct ieee80211_bss *bss)
196 return __ieee80211_sta_join_ibss(sdata,
198 bss->cbss.beacon_interval,
199 bss->cbss.channel->center_freq,
200 bss->supp_rates_len, bss->supp_rates,
201 bss->cbss.capability);
204 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
205 struct ieee80211_mgmt *mgmt,
207 struct ieee80211_rx_status *rx_status,
208 struct ieee802_11_elems *elems,
211 struct ieee80211_local *local = sdata->local;
213 struct ieee80211_bss *bss;
214 struct sta_info *sta;
215 struct ieee80211_channel *channel;
216 u64 beacon_timestamp, rx_timestamp;
218 enum ieee80211_band band = rx_status->band;
220 if (elems->ds_params && elems->ds_params_len == 1)
221 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
223 freq = rx_status->freq;
225 channel = ieee80211_get_channel(local->hw.wiphy, freq);
227 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
230 if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
231 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
232 supp_rates = ieee80211_sta_get_rates(local, elems, band);
236 sta = sta_info_get(local, mgmt->sa);
240 prev_rates = sta->sta.supp_rates[band];
241 /* make sure mandatory rates are always added */
242 sta->sta.supp_rates[band] = supp_rates |
243 ieee80211_mandatory_rates(local, band);
245 #ifdef CONFIG_MAC80211_IBSS_DEBUG
246 if (sta->sta.supp_rates[band] != prev_rates)
247 printk(KERN_DEBUG "%s: updated supp_rates set "
248 "for %pM based on beacon info (0x%llx | "
249 "0x%llx -> 0x%llx)\n",
252 (unsigned long long) prev_rates,
253 (unsigned long long) supp_rates,
254 (unsigned long long) sta->sta.supp_rates[band]);
257 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
262 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
267 /* was just updated in ieee80211_bss_info_update */
268 beacon_timestamp = bss->cbss.tsf;
270 /* check if we need to merge IBSS */
272 /* merge only on beacons (???) */
276 /* we use a fixed BSSID */
277 if (sdata->u.ibss.flags & IEEE80211_IBSS_BSSID_SET)
281 if (!(bss->cbss.capability & WLAN_CAPABILITY_IBSS))
284 /* different channel */
285 if (bss->cbss.channel != local->oper_channel)
289 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
290 memcmp(elems->ssid, sdata->u.ibss.ssid,
291 sdata->u.ibss.ssid_len))
294 if (rx_status->flag & RX_FLAG_TSFT) {
296 * For correct IBSS merging we need mactime; since mactime is
297 * defined as the time the first data symbol of the frame hits
298 * the PHY, and the timestamp of the beacon is defined as "the
299 * time that the data symbol containing the first bit of the
300 * timestamp is transmitted to the PHY plus the transmitting
301 * STA's delays through its local PHY from the MAC-PHY
302 * interface to its interface with the WM" (802.11 11.1.2)
303 * - equals the time this bit arrives at the receiver - we have
304 * to take into account the offset between the two.
306 * E.g. at 1 MBit that means mactime is 192 usec earlier
307 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
311 if (rx_status->flag & RX_FLAG_HT)
312 rate = 65; /* TODO: HT rates */
314 rate = local->hw.wiphy->bands[band]->
315 bitrates[rx_status->rate_idx].bitrate;
317 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
318 } else if (local && local->ops && local->ops->get_tsf)
319 /* second best option: get current TSF */
320 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
322 /* can't merge without knowing the TSF */
323 rx_timestamp = -1LLU;
325 #ifdef CONFIG_MAC80211_IBSS_DEBUG
326 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
327 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
328 mgmt->sa, mgmt->bssid,
329 (unsigned long long)rx_timestamp,
330 (unsigned long long)beacon_timestamp,
331 (unsigned long long)(rx_timestamp - beacon_timestamp),
335 if (beacon_timestamp > rx_timestamp) {
336 #ifdef CONFIG_MAC80211_IBSS_DEBUG
337 printk(KERN_DEBUG "%s: beacon TSF higher than "
338 "local TSF - IBSS merge with BSSID %pM\n",
339 sdata->dev->name, mgmt->bssid);
341 ieee80211_sta_join_ibss(sdata, bss);
342 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
346 ieee80211_rx_bss_put(local, bss);
350 * Add a new IBSS station, will also be called by the RX code when,
351 * in IBSS mode, receiving a frame from a yet-unknown station, hence
352 * must be callable in atomic context.
354 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
355 u8 *bssid,u8 *addr, u32 supp_rates)
357 struct ieee80211_local *local = sdata->local;
358 struct sta_info *sta;
359 int band = local->hw.conf.channel->band;
361 /* TODO: Could consider removing the least recently used entry and
362 * allow new one to be added. */
363 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
364 if (net_ratelimit()) {
365 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
366 "entry %pM\n", sdata->dev->name, addr);
371 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
374 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
375 printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n",
376 wiphy_name(local->hw.wiphy), addr, sdata->dev->name);
379 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
383 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
385 /* make sure mandatory rates are always added */
386 sta->sta.supp_rates[band] = supp_rates |
387 ieee80211_mandatory_rates(local, band);
389 rate_control_rate_init(sta);
391 if (sta_info_insert(sta))
397 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
399 struct ieee80211_local *local = sdata->local;
401 struct sta_info *sta;
405 list_for_each_entry_rcu(sta, &local->sta_list, list) {
406 if (sta->sdata == sdata &&
407 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
420 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
422 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
424 mod_timer(&ifibss->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
426 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
427 if (ieee80211_sta_active_ibss(sdata))
430 if ((ifibss->flags & IEEE80211_IBSS_BSSID_SET) &&
431 (!(ifibss->flags & IEEE80211_IBSS_AUTO_CHANNEL_SEL)))
434 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
435 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
437 /* XXX maybe racy? */
438 if (sdata->local->scan_req)
441 memcpy(sdata->local->int_scan_req.ssids[0].ssid,
442 ifibss->ssid, IEEE80211_MAX_SSID_LEN);
443 sdata->local->int_scan_req.ssids[0].ssid_len = ifibss->ssid_len;
444 ieee80211_request_scan(sdata, &sdata->local->int_scan_req);
447 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
449 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
450 struct ieee80211_local *local = sdata->local;
451 struct ieee80211_supported_band *sband;
454 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
458 if (ifibss->flags & IEEE80211_IBSS_BSSID_SET) {
459 memcpy(bssid, ifibss->bssid, ETH_ALEN);
461 /* Generate random, not broadcast, locally administered BSSID. Mix in
462 * own MAC address to make sure that devices that do not have proper
463 * random number generator get different BSSID. */
464 get_random_bytes(bssid, ETH_ALEN);
465 for (i = 0; i < ETH_ALEN; i++)
466 bssid[i] ^= sdata->dev->dev_addr[i];
471 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
472 sdata->dev->name, bssid);
474 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
476 if (local->hw.conf.beacon_int == 0)
477 local->hw.conf.beacon_int = 100;
479 capability = WLAN_CAPABILITY_IBSS;
481 if (sdata->default_key)
482 capability |= WLAN_CAPABILITY_PRIVACY;
484 sdata->drop_unencrypted = 0;
487 for (i = 0; i < sband->n_bitrates; i++) {
488 int rate = sband->bitrates[i].bitrate;
489 *pos++ = (u8) (rate / 5);
492 return __ieee80211_sta_join_ibss(sdata,
493 bssid, local->hw.conf.beacon_int,
494 local->hw.conf.channel->center_freq,
495 sband->n_bitrates, supp_rates,
499 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
501 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
502 struct ieee80211_local *local = sdata->local;
503 struct ieee80211_bss *bss;
504 const u8 *bssid = NULL;
507 if (ifibss->ssid_len == 0)
510 active_ibss = ieee80211_sta_active_ibss(sdata);
511 #ifdef CONFIG_MAC80211_IBSS_DEBUG
512 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
513 sdata->dev->name, active_ibss);
514 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
519 if (ifibss->flags & IEEE80211_IBSS_BSSID_SET)
520 bssid = ifibss->bssid;
521 bss = (void *)cfg80211_get_bss(local->hw.wiphy, NULL, bssid,
522 ifibss->ssid, ifibss->ssid_len,
523 WLAN_CAPABILITY_IBSS,
524 WLAN_CAPABILITY_IBSS);
526 #ifdef CONFIG_MAC80211_IBSS_DEBUG
528 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
529 "%pM\n", bss->cbss.bssid, ifibss->bssid);
530 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
533 (!(ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET) ||
534 memcmp(ifibss->bssid, bss->cbss.bssid, ETH_ALEN))) {
537 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
538 " based on configured SSID\n",
539 sdata->dev->name, bss->cbss.bssid);
541 ret = ieee80211_sta_join_ibss(sdata, bss);
542 ieee80211_rx_bss_put(local, bss);
545 ieee80211_rx_bss_put(local, bss);
547 #ifdef CONFIG_MAC80211_IBSS_DEBUG
548 printk(KERN_DEBUG " did not try to join ibss\n");
549 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
551 /* Selected IBSS not found in current scan results - try to scan */
552 if (ifibss->state == IEEE80211_IBSS_MLME_JOINED &&
553 !ieee80211_sta_active_ibss(sdata)) {
554 mod_timer(&ifibss->timer, jiffies +
555 IEEE80211_IBSS_MERGE_INTERVAL);
556 } else if (time_after(jiffies, local->last_scan_completed +
557 IEEE80211_SCAN_INTERVAL)) {
558 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
559 "join\n", sdata->dev->name);
561 /* XXX maybe racy? */
565 memcpy(local->int_scan_req.ssids[0].ssid,
566 ifibss->ssid, IEEE80211_MAX_SSID_LEN);
567 local->int_scan_req.ssids[0].ssid_len = ifibss->ssid_len;
568 return ieee80211_request_scan(sdata, &local->int_scan_req);
569 } else if (ifibss->state != IEEE80211_IBSS_MLME_JOINED) {
570 int interval = IEEE80211_SCAN_INTERVAL;
572 if (time_after(jiffies, ifibss->ibss_join_req +
573 IEEE80211_IBSS_JOIN_TIMEOUT)) {
574 if (!(local->oper_channel->flags &
575 IEEE80211_CHAN_NO_IBSS))
576 return ieee80211_sta_create_ibss(sdata);
577 printk(KERN_DEBUG "%s: IBSS not allowed on"
578 " %d MHz\n", sdata->dev->name,
579 local->hw.conf.channel->center_freq);
581 /* No IBSS found - decrease scan interval and continue
583 interval = IEEE80211_SCAN_INTERVAL_SLOW;
586 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
587 mod_timer(&ifibss->timer, jiffies + interval);
594 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
595 struct ieee80211_mgmt *mgmt,
598 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
599 struct ieee80211_local *local = sdata->local;
602 struct ieee80211_mgmt *resp;
605 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
606 len < 24 + 2 || !ifibss->probe_resp)
609 if (local->ops->tx_last_beacon)
610 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
614 #ifdef CONFIG_MAC80211_IBSS_DEBUG
615 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
616 " (tx_last_beacon=%d)\n",
617 sdata->dev->name, mgmt->sa, mgmt->da,
618 mgmt->bssid, tx_last_beacon);
619 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
624 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
625 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
628 end = ((u8 *) mgmt) + len;
629 pos = mgmt->u.probe_req.variable;
630 if (pos[0] != WLAN_EID_SSID ||
631 pos + 2 + pos[1] > end) {
632 #ifdef CONFIG_MAC80211_IBSS_DEBUG
633 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
635 sdata->dev->name, mgmt->sa);
640 (pos[1] != ifibss->ssid_len ||
641 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len) != 0)) {
642 /* Ignore ProbeReq for foreign SSID */
646 /* Reply with ProbeResp */
647 skb = skb_copy(ifibss->probe_resp, GFP_KERNEL);
651 resp = (struct ieee80211_mgmt *) skb->data;
652 memcpy(resp->da, mgmt->sa, ETH_ALEN);
653 #ifdef CONFIG_MAC80211_IBSS_DEBUG
654 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
655 sdata->dev->name, resp->da);
656 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
657 ieee80211_tx_skb(sdata, skb, 0);
660 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
661 struct ieee80211_mgmt *mgmt,
663 struct ieee80211_rx_status *rx_status)
666 struct ieee802_11_elems elems;
668 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
669 return; /* ignore ProbeResp to foreign address */
671 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
675 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
678 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
681 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
682 struct ieee80211_mgmt *mgmt,
684 struct ieee80211_rx_status *rx_status)
687 struct ieee802_11_elems elems;
689 /* Process beacon from the current BSS */
690 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
694 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
696 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
699 static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
702 struct ieee80211_rx_status *rx_status;
703 struct ieee80211_mgmt *mgmt;
706 rx_status = (struct ieee80211_rx_status *) skb->cb;
707 mgmt = (struct ieee80211_mgmt *) skb->data;
708 fc = le16_to_cpu(mgmt->frame_control);
710 switch (fc & IEEE80211_FCTL_STYPE) {
711 case IEEE80211_STYPE_PROBE_REQ:
712 ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
714 case IEEE80211_STYPE_PROBE_RESP:
715 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
718 case IEEE80211_STYPE_BEACON:
719 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
722 case IEEE80211_STYPE_AUTH:
723 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
730 static void ieee80211_ibss_work(struct work_struct *work)
732 struct ieee80211_sub_if_data *sdata =
733 container_of(work, struct ieee80211_sub_if_data, u.ibss.work);
734 struct ieee80211_local *local = sdata->local;
735 struct ieee80211_if_ibss *ifibss;
738 if (!netif_running(sdata->dev))
741 if (local->sw_scanning || local->hw_scanning)
744 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_ADHOC))
746 ifibss = &sdata->u.ibss;
748 while ((skb = skb_dequeue(&ifibss->skb_queue)))
749 ieee80211_ibss_rx_queued_mgmt(sdata, skb);
751 if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request))
754 switch (ifibss->state) {
755 case IEEE80211_IBSS_MLME_SEARCH:
756 ieee80211_sta_find_ibss(sdata);
758 case IEEE80211_IBSS_MLME_JOINED:
759 ieee80211_sta_merge_ibss(sdata);
767 static void ieee80211_ibss_timer(unsigned long data)
769 struct ieee80211_sub_if_data *sdata =
770 (struct ieee80211_sub_if_data *) data;
771 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
772 struct ieee80211_local *local = sdata->local;
774 set_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request);
775 queue_work(local->hw.workqueue, &ifibss->work);
778 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
780 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
782 INIT_WORK(&ifibss->work, ieee80211_ibss_work);
783 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
784 (unsigned long) sdata);
785 skb_queue_head_init(&ifibss->skb_queue);
787 ifibss->flags |= IEEE80211_IBSS_AUTO_BSSID_SEL |
788 IEEE80211_IBSS_AUTO_CHANNEL_SEL;
791 int ieee80211_ibss_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
793 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
795 if (len > IEEE80211_MAX_SSID_LEN)
798 if (ifibss->ssid_len != len || memcmp(ifibss->ssid, ssid, len) != 0) {
799 memset(ifibss->ssid, 0, sizeof(ifibss->ssid));
800 memcpy(ifibss->ssid, ssid, len);
801 ifibss->ssid_len = len;
804 ifibss->flags &= ~IEEE80211_IBSS_PREV_BSSID_SET;
807 ifibss->flags |= IEEE80211_IBSS_SSID_SET;
809 ifibss->flags &= ~IEEE80211_IBSS_SSID_SET;
811 ifibss->ibss_join_req = jiffies;
812 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
813 return ieee80211_sta_find_ibss(sdata);
816 int ieee80211_ibss_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
818 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
820 memcpy(ssid, ifibss->ssid, ifibss->ssid_len);
821 *len = ifibss->ssid_len;
826 int ieee80211_ibss_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
828 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
830 if (is_valid_ether_addr(bssid)) {
831 memcpy(ifibss->bssid, bssid, ETH_ALEN);
832 ifibss->flags |= IEEE80211_IBSS_BSSID_SET;
834 memset(ifibss->bssid, 0, ETH_ALEN);
835 ifibss->flags &= ~IEEE80211_IBSS_BSSID_SET;
838 if (netif_running(sdata->dev)) {
839 if (ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID)) {
840 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
841 "the low-level driver\n", sdata->dev->name);
845 return ieee80211_ibss_set_ssid(sdata, ifibss->ssid, ifibss->ssid_len);
848 /* scan finished notification */
849 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
851 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
852 struct ieee80211_if_ibss *ifibss;
854 if (sdata && sdata->vif.type == NL80211_IFTYPE_ADHOC) {
855 ifibss = &sdata->u.ibss;
856 if ((!(ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET)) ||
857 !ieee80211_sta_active_ibss(sdata))
858 ieee80211_sta_find_ibss(sdata);
863 ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
864 struct ieee80211_rx_status *rx_status)
866 struct ieee80211_local *local = sdata->local;
867 struct ieee80211_mgmt *mgmt;
871 return RX_DROP_MONITOR;
873 mgmt = (struct ieee80211_mgmt *) skb->data;
874 fc = le16_to_cpu(mgmt->frame_control);
876 switch (fc & IEEE80211_FCTL_STYPE) {
877 case IEEE80211_STYPE_PROBE_RESP:
878 case IEEE80211_STYPE_BEACON:
879 memcpy(skb->cb, rx_status, sizeof(*rx_status));
880 case IEEE80211_STYPE_PROBE_REQ:
881 case IEEE80211_STYPE_AUTH:
882 skb_queue_tail(&sdata->u.ibss.skb_queue, skb);
883 queue_work(local->hw.workqueue, &sdata->u.ibss.work);
887 return RX_DROP_MONITOR;