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 int __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
63 const u8 *bssid, const int beacon_int,
65 const size_t supp_rates_len,
69 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
70 struct ieee80211_local *local = sdata->local;
71 int res = 0, rates, i, j;
73 struct ieee80211_mgmt *mgmt;
75 struct ieee80211_supported_band *sband;
76 union iwreq_data wrqu;
78 if (local->ops->reset_tsf) {
79 /* Reset own TSF to allow time synchronization work. */
80 local->ops->reset_tsf(local_to_hw(local));
83 if ((ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET) &&
84 memcmp(ifibss->bssid, bssid, ETH_ALEN) == 0)
87 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
89 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
90 "response\n", sdata->dev->name);
94 if (!(ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET)) {
95 /* Remove possible STA entries from other IBSS networks. */
96 sta_info_flush_delayed(sdata);
99 memcpy(ifibss->bssid, bssid, ETH_ALEN);
100 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
104 local->hw.conf.beacon_int = beacon_int >= 10 ? beacon_int : 10;
106 sdata->drop_unencrypted = capability &
107 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
109 res = ieee80211_set_freq(sdata, freq);
114 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
116 /* Build IBSS probe response */
118 skb_reserve(skb, local->hw.extra_tx_headroom);
120 mgmt = (struct ieee80211_mgmt *)
121 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
122 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
123 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
124 IEEE80211_STYPE_PROBE_RESP);
125 memset(mgmt->da, 0xff, ETH_ALEN);
126 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
127 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
128 mgmt->u.beacon.beacon_int =
129 cpu_to_le16(local->hw.conf.beacon_int);
130 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
132 pos = skb_put(skb, 2 + ifibss->ssid_len);
133 *pos++ = WLAN_EID_SSID;
134 *pos++ = ifibss->ssid_len;
135 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
137 rates = supp_rates_len;
140 pos = skb_put(skb, 2 + rates);
141 *pos++ = WLAN_EID_SUPP_RATES;
143 memcpy(pos, supp_rates, rates);
145 if (sband->band == IEEE80211_BAND_2GHZ) {
146 pos = skb_put(skb, 2 + 1);
147 *pos++ = WLAN_EID_DS_PARAMS;
149 *pos++ = ieee80211_frequency_to_channel(freq);
152 pos = skb_put(skb, 2 + 2);
153 *pos++ = WLAN_EID_IBSS_PARAMS;
155 /* FIX: set ATIM window based on scan results */
159 if (supp_rates_len > 8) {
160 rates = supp_rates_len - 8;
161 pos = skb_put(skb, 2 + rates);
162 *pos++ = WLAN_EID_EXT_SUPP_RATES;
164 memcpy(pos, &supp_rates[8], rates);
167 ifibss->probe_resp = skb;
169 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON |
170 IEEE80211_IFCC_BEACON_ENABLED);
174 for (i = 0; i < supp_rates_len; i++) {
175 int bitrate = (supp_rates[i] & 0x7f) * 5;
176 for (j = 0; j < sband->n_bitrates; j++)
177 if (sband->bitrates[j].bitrate == bitrate)
181 ieee80211_sta_def_wmm_params(sdata, supp_rates_len, supp_rates);
183 ifibss->flags |= IEEE80211_IBSS_PREV_BSSID_SET;
184 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
185 mod_timer(&ifibss->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
187 memset(&wrqu, 0, sizeof(wrqu));
188 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
189 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
194 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
195 struct ieee80211_bss *bss)
197 return __ieee80211_sta_join_ibss(sdata,
199 bss->cbss.beacon_interval,
200 bss->cbss.channel->center_freq,
201 bss->supp_rates_len, bss->supp_rates,
202 bss->cbss.capability);
205 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
206 struct ieee80211_mgmt *mgmt,
208 struct ieee80211_rx_status *rx_status,
209 struct ieee802_11_elems *elems,
212 struct ieee80211_local *local = sdata->local;
214 struct ieee80211_bss *bss;
215 struct sta_info *sta;
216 struct ieee80211_channel *channel;
217 u64 beacon_timestamp, rx_timestamp;
219 enum ieee80211_band band = rx_status->band;
221 if (elems->ds_params && elems->ds_params_len == 1)
222 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
224 freq = rx_status->freq;
226 channel = ieee80211_get_channel(local->hw.wiphy, freq);
228 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
231 if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
232 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
233 supp_rates = ieee80211_sta_get_rates(local, elems, band);
237 sta = sta_info_get(local, mgmt->sa);
241 prev_rates = sta->sta.supp_rates[band];
242 /* make sure mandatory rates are always added */
243 sta->sta.supp_rates[band] = supp_rates |
244 ieee80211_mandatory_rates(local, band);
246 #ifdef CONFIG_MAC80211_IBSS_DEBUG
247 if (sta->sta.supp_rates[band] != prev_rates)
248 printk(KERN_DEBUG "%s: updated supp_rates set "
249 "for %pM based on beacon info (0x%llx | "
250 "0x%llx -> 0x%llx)\n",
253 (unsigned long long) prev_rates,
254 (unsigned long long) supp_rates,
255 (unsigned long long) sta->sta.supp_rates[band]);
258 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
263 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
268 /* was just updated in ieee80211_bss_info_update */
269 beacon_timestamp = bss->cbss.tsf;
271 /* check if we need to merge IBSS */
273 /* merge only on beacons (???) */
277 /* we use a fixed BSSID */
278 if (sdata->u.ibss.flags & IEEE80211_IBSS_BSSID_SET)
282 if (!(bss->cbss.capability & WLAN_CAPABILITY_IBSS))
285 /* different channel */
286 if (bss->cbss.channel != local->oper_channel)
290 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
291 memcmp(elems->ssid, sdata->u.ibss.ssid,
292 sdata->u.ibss.ssid_len))
296 if (memcmp(bss->cbss.bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
299 if (rx_status->flag & RX_FLAG_TSFT) {
301 * For correct IBSS merging we need mactime; since mactime is
302 * defined as the time the first data symbol of the frame hits
303 * the PHY, and the timestamp of the beacon is defined as "the
304 * time that the data symbol containing the first bit of the
305 * timestamp is transmitted to the PHY plus the transmitting
306 * STA's delays through its local PHY from the MAC-PHY
307 * interface to its interface with the WM" (802.11 11.1.2)
308 * - equals the time this bit arrives at the receiver - we have
309 * to take into account the offset between the two.
311 * E.g. at 1 MBit that means mactime is 192 usec earlier
312 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
316 if (rx_status->flag & RX_FLAG_HT)
317 rate = 65; /* TODO: HT rates */
319 rate = local->hw.wiphy->bands[band]->
320 bitrates[rx_status->rate_idx].bitrate;
322 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
323 } else if (local && local->ops && local->ops->get_tsf)
324 /* second best option: get current TSF */
325 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
327 /* can't merge without knowing the TSF */
328 rx_timestamp = -1LLU;
330 #ifdef CONFIG_MAC80211_IBSS_DEBUG
331 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
332 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
333 mgmt->sa, mgmt->bssid,
334 (unsigned long long)rx_timestamp,
335 (unsigned long long)beacon_timestamp,
336 (unsigned long long)(rx_timestamp - beacon_timestamp),
340 /* give slow hardware some time to do the TSF sync */
341 if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
344 if (beacon_timestamp > rx_timestamp) {
345 #ifdef CONFIG_MAC80211_IBSS_DEBUG
346 printk(KERN_DEBUG "%s: beacon TSF higher than "
347 "local TSF - IBSS merge with BSSID %pM\n",
348 sdata->dev->name, mgmt->bssid);
350 ieee80211_sta_join_ibss(sdata, bss);
351 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
355 ieee80211_rx_bss_put(local, bss);
359 * Add a new IBSS station, will also be called by the RX code when,
360 * in IBSS mode, receiving a frame from a yet-unknown station, hence
361 * must be callable in atomic context.
363 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
364 u8 *bssid,u8 *addr, u32 supp_rates)
366 struct ieee80211_local *local = sdata->local;
367 struct sta_info *sta;
368 int band = local->hw.conf.channel->band;
370 /* TODO: Could consider removing the least recently used entry and
371 * allow new one to be added. */
372 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
373 if (net_ratelimit()) {
374 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
375 "entry %pM\n", sdata->dev->name, addr);
380 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
383 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
384 printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n",
385 wiphy_name(local->hw.wiphy), addr, sdata->dev->name);
388 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
392 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
394 /* make sure mandatory rates are always added */
395 sta->sta.supp_rates[band] = supp_rates |
396 ieee80211_mandatory_rates(local, band);
398 rate_control_rate_init(sta);
400 if (sta_info_insert(sta))
406 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
408 struct ieee80211_local *local = sdata->local;
410 struct sta_info *sta;
414 list_for_each_entry_rcu(sta, &local->sta_list, list) {
415 if (sta->sdata == sdata &&
416 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
429 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
431 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
433 mod_timer(&ifibss->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
435 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
436 if (ieee80211_sta_active_ibss(sdata))
439 if ((ifibss->flags & IEEE80211_IBSS_BSSID_SET) &&
440 (!(ifibss->flags & IEEE80211_IBSS_AUTO_CHANNEL_SEL)))
443 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
444 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
446 /* XXX maybe racy? */
447 if (sdata->local->scan_req)
450 memcpy(sdata->local->int_scan_req.ssids[0].ssid,
451 ifibss->ssid, IEEE80211_MAX_SSID_LEN);
452 sdata->local->int_scan_req.ssids[0].ssid_len = ifibss->ssid_len;
453 ieee80211_request_scan(sdata, &sdata->local->int_scan_req);
456 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
458 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
459 struct ieee80211_local *local = sdata->local;
460 struct ieee80211_supported_band *sband;
463 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
467 if (ifibss->flags & IEEE80211_IBSS_BSSID_SET) {
468 memcpy(bssid, ifibss->bssid, ETH_ALEN);
470 /* Generate random, not broadcast, locally administered BSSID. Mix in
471 * own MAC address to make sure that devices that do not have proper
472 * random number generator get different BSSID. */
473 get_random_bytes(bssid, ETH_ALEN);
474 for (i = 0; i < ETH_ALEN; i++)
475 bssid[i] ^= sdata->dev->dev_addr[i];
480 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
481 sdata->dev->name, bssid);
483 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
485 if (local->hw.conf.beacon_int == 0)
486 local->hw.conf.beacon_int = 100;
488 capability = WLAN_CAPABILITY_IBSS;
490 if (sdata->default_key)
491 capability |= WLAN_CAPABILITY_PRIVACY;
493 sdata->drop_unencrypted = 0;
496 for (i = 0; i < sband->n_bitrates; i++) {
497 int rate = sband->bitrates[i].bitrate;
498 *pos++ = (u8) (rate / 5);
501 return __ieee80211_sta_join_ibss(sdata,
502 bssid, local->hw.conf.beacon_int,
503 local->hw.conf.channel->center_freq,
504 sband->n_bitrates, supp_rates,
508 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
510 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
511 struct ieee80211_local *local = sdata->local;
512 struct ieee80211_bss *bss;
513 const u8 *bssid = NULL;
516 if (ifibss->ssid_len == 0)
519 active_ibss = ieee80211_sta_active_ibss(sdata);
520 #ifdef CONFIG_MAC80211_IBSS_DEBUG
521 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
522 sdata->dev->name, active_ibss);
523 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
528 if (ifibss->flags & IEEE80211_IBSS_BSSID_SET)
529 bssid = ifibss->bssid;
530 bss = (void *)cfg80211_get_bss(local->hw.wiphy, NULL, bssid,
531 ifibss->ssid, ifibss->ssid_len,
532 WLAN_CAPABILITY_IBSS,
533 WLAN_CAPABILITY_IBSS);
535 #ifdef CONFIG_MAC80211_IBSS_DEBUG
537 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
538 "%pM\n", bss->cbss.bssid, ifibss->bssid);
539 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
542 (!(ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET) ||
543 memcmp(ifibss->bssid, bss->cbss.bssid, ETH_ALEN))) {
546 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
547 " based on configured SSID\n",
548 sdata->dev->name, bss->cbss.bssid);
550 ret = ieee80211_sta_join_ibss(sdata, bss);
551 ieee80211_rx_bss_put(local, bss);
554 ieee80211_rx_bss_put(local, bss);
556 #ifdef CONFIG_MAC80211_IBSS_DEBUG
557 printk(KERN_DEBUG " did not try to join ibss\n");
558 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
560 /* Selected IBSS not found in current scan results - try to scan */
561 if (ifibss->state == IEEE80211_IBSS_MLME_JOINED &&
562 !ieee80211_sta_active_ibss(sdata)) {
563 mod_timer(&ifibss->timer, jiffies +
564 IEEE80211_IBSS_MERGE_INTERVAL);
565 } else if (time_after(jiffies, local->last_scan_completed +
566 IEEE80211_SCAN_INTERVAL)) {
567 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
568 "join\n", sdata->dev->name);
570 /* XXX maybe racy? */
574 memcpy(local->int_scan_req.ssids[0].ssid,
575 ifibss->ssid, IEEE80211_MAX_SSID_LEN);
576 local->int_scan_req.ssids[0].ssid_len = ifibss->ssid_len;
577 return ieee80211_request_scan(sdata, &local->int_scan_req);
578 } else if (ifibss->state != IEEE80211_IBSS_MLME_JOINED) {
579 int interval = IEEE80211_SCAN_INTERVAL;
581 if (time_after(jiffies, ifibss->ibss_join_req +
582 IEEE80211_IBSS_JOIN_TIMEOUT)) {
583 if (!(local->oper_channel->flags &
584 IEEE80211_CHAN_NO_IBSS))
585 return ieee80211_sta_create_ibss(sdata);
586 printk(KERN_DEBUG "%s: IBSS not allowed on"
587 " %d MHz\n", sdata->dev->name,
588 local->hw.conf.channel->center_freq);
590 /* No IBSS found - decrease scan interval and continue
592 interval = IEEE80211_SCAN_INTERVAL_SLOW;
595 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
596 mod_timer(&ifibss->timer, jiffies + interval);
603 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
604 struct ieee80211_mgmt *mgmt,
607 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
608 struct ieee80211_local *local = sdata->local;
611 struct ieee80211_mgmt *resp;
614 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
615 len < 24 + 2 || !ifibss->probe_resp)
618 if (local->ops->tx_last_beacon)
619 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
623 #ifdef CONFIG_MAC80211_IBSS_DEBUG
624 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
625 " (tx_last_beacon=%d)\n",
626 sdata->dev->name, mgmt->sa, mgmt->da,
627 mgmt->bssid, tx_last_beacon);
628 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
633 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
634 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
637 end = ((u8 *) mgmt) + len;
638 pos = mgmt->u.probe_req.variable;
639 if (pos[0] != WLAN_EID_SSID ||
640 pos + 2 + pos[1] > end) {
641 #ifdef CONFIG_MAC80211_IBSS_DEBUG
642 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
644 sdata->dev->name, mgmt->sa);
649 (pos[1] != ifibss->ssid_len ||
650 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len) != 0)) {
651 /* Ignore ProbeReq for foreign SSID */
655 /* Reply with ProbeResp */
656 skb = skb_copy(ifibss->probe_resp, GFP_KERNEL);
660 resp = (struct ieee80211_mgmt *) skb->data;
661 memcpy(resp->da, mgmt->sa, ETH_ALEN);
662 #ifdef CONFIG_MAC80211_IBSS_DEBUG
663 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
664 sdata->dev->name, resp->da);
665 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
666 ieee80211_tx_skb(sdata, skb, 0);
669 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
670 struct ieee80211_mgmt *mgmt,
672 struct ieee80211_rx_status *rx_status)
675 struct ieee802_11_elems elems;
677 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
678 return; /* ignore ProbeResp to foreign address */
680 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
684 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
687 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
690 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
691 struct ieee80211_mgmt *mgmt,
693 struct ieee80211_rx_status *rx_status)
696 struct ieee802_11_elems elems;
698 /* Process beacon from the current BSS */
699 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
703 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
705 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
708 static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
711 struct ieee80211_rx_status *rx_status;
712 struct ieee80211_mgmt *mgmt;
715 rx_status = (struct ieee80211_rx_status *) skb->cb;
716 mgmt = (struct ieee80211_mgmt *) skb->data;
717 fc = le16_to_cpu(mgmt->frame_control);
719 switch (fc & IEEE80211_FCTL_STYPE) {
720 case IEEE80211_STYPE_PROBE_REQ:
721 ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
723 case IEEE80211_STYPE_PROBE_RESP:
724 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
727 case IEEE80211_STYPE_BEACON:
728 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
731 case IEEE80211_STYPE_AUTH:
732 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
739 static void ieee80211_ibss_work(struct work_struct *work)
741 struct ieee80211_sub_if_data *sdata =
742 container_of(work, struct ieee80211_sub_if_data, u.ibss.work);
743 struct ieee80211_local *local = sdata->local;
744 struct ieee80211_if_ibss *ifibss;
747 if (!netif_running(sdata->dev))
750 if (local->sw_scanning || local->hw_scanning)
753 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_ADHOC))
755 ifibss = &sdata->u.ibss;
757 while ((skb = skb_dequeue(&ifibss->skb_queue)))
758 ieee80211_ibss_rx_queued_mgmt(sdata, skb);
760 if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request))
763 switch (ifibss->state) {
764 case IEEE80211_IBSS_MLME_SEARCH:
765 ieee80211_sta_find_ibss(sdata);
767 case IEEE80211_IBSS_MLME_JOINED:
768 ieee80211_sta_merge_ibss(sdata);
776 static void ieee80211_ibss_timer(unsigned long data)
778 struct ieee80211_sub_if_data *sdata =
779 (struct ieee80211_sub_if_data *) data;
780 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
781 struct ieee80211_local *local = sdata->local;
783 set_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request);
784 queue_work(local->hw.workqueue, &ifibss->work);
787 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
789 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
791 INIT_WORK(&ifibss->work, ieee80211_ibss_work);
792 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
793 (unsigned long) sdata);
794 skb_queue_head_init(&ifibss->skb_queue);
796 ifibss->flags |= IEEE80211_IBSS_AUTO_BSSID_SEL |
797 IEEE80211_IBSS_AUTO_CHANNEL_SEL;
800 int ieee80211_ibss_commit(struct ieee80211_sub_if_data *sdata)
802 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
804 ifibss->flags &= ~IEEE80211_IBSS_PREV_BSSID_SET;
806 if (ifibss->ssid_len)
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;
814 return ieee80211_sta_find_ibss(sdata);
817 int ieee80211_ibss_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
819 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
821 if (len > IEEE80211_MAX_SSID_LEN)
824 if (ifibss->ssid_len != len || memcmp(ifibss->ssid, ssid, len) != 0) {
825 memset(ifibss->ssid, 0, sizeof(ifibss->ssid));
826 memcpy(ifibss->ssid, ssid, len);
827 ifibss->ssid_len = len;
830 return ieee80211_ibss_commit(sdata);
833 int ieee80211_ibss_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
835 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
837 memcpy(ssid, ifibss->ssid, ifibss->ssid_len);
838 *len = ifibss->ssid_len;
843 int ieee80211_ibss_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
845 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
847 if (is_valid_ether_addr(bssid)) {
848 memcpy(ifibss->bssid, bssid, ETH_ALEN);
849 ifibss->flags |= IEEE80211_IBSS_BSSID_SET;
851 memset(ifibss->bssid, 0, ETH_ALEN);
852 ifibss->flags &= ~IEEE80211_IBSS_BSSID_SET;
855 if (netif_running(sdata->dev)) {
856 if (ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID)) {
857 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
858 "the low-level driver\n", sdata->dev->name);
862 return ieee80211_ibss_commit(sdata);
865 /* scan finished notification */
866 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
868 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
869 struct ieee80211_if_ibss *ifibss;
871 if (sdata && sdata->vif.type == NL80211_IFTYPE_ADHOC) {
872 ifibss = &sdata->u.ibss;
873 if ((!(ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET)) ||
874 !ieee80211_sta_active_ibss(sdata))
875 ieee80211_sta_find_ibss(sdata);
880 ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
881 struct ieee80211_rx_status *rx_status)
883 struct ieee80211_local *local = sdata->local;
884 struct ieee80211_mgmt *mgmt;
888 return RX_DROP_MONITOR;
890 mgmt = (struct ieee80211_mgmt *) skb->data;
891 fc = le16_to_cpu(mgmt->frame_control);
893 switch (fc & IEEE80211_FCTL_STYPE) {
894 case IEEE80211_STYPE_PROBE_RESP:
895 case IEEE80211_STYPE_BEACON:
896 memcpy(skb->cb, rx_status, sizeof(*rx_status));
897 case IEEE80211_STYPE_PROBE_REQ:
898 case IEEE80211_STYPE_AUTH:
899 skb_queue_tail(&sdata->u.ibss.skb_queue, skb);
900 queue_work(local->hw.workqueue, &sdata->u.ibss.work);
904 return RX_DROP_MONITOR;