net: mac80211: Add and use ibss_vdbg debugging macro
[linux-2.6/libata-dev.git] / net / mac80211 / ibss.c
blob0bc47a8256925ce2876eb98b617e5c26863a1837
1 /*
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/slab.h>
17 #include <linux/if_ether.h>
18 #include <linux/skbuff.h>
19 #include <linux/if_arp.h>
20 #include <linux/etherdevice.h>
21 #include <linux/rtnetlink.h>
22 #include <net/mac80211.h>
24 #include "ieee80211_i.h"
25 #include "driver-ops.h"
26 #include "rate.h"
28 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
29 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
30 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
32 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
33 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
35 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
38 static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
39 const u8 *bssid, const int beacon_int,
40 struct ieee80211_channel *chan,
41 const u32 basic_rates,
42 const u16 capability, u64 tsf)
44 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
45 struct ieee80211_local *local = sdata->local;
46 int rates, i;
47 struct sk_buff *skb;
48 struct ieee80211_mgmt *mgmt;
49 u8 *pos;
50 struct ieee80211_supported_band *sband;
51 struct cfg80211_bss *bss;
52 u32 bss_change;
53 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
54 enum nl80211_channel_type channel_type;
56 lockdep_assert_held(&ifibss->mtx);
58 /* Reset own TSF to allow time synchronization work. */
59 drv_reset_tsf(local, sdata);
61 skb = ifibss->skb;
62 RCU_INIT_POINTER(ifibss->presp, NULL);
63 synchronize_rcu();
64 skb->data = skb->head;
65 skb->len = 0;
66 skb_reset_tail_pointer(skb);
67 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
69 if (!ether_addr_equal(ifibss->bssid, bssid))
70 sta_info_flush(sdata->local, sdata);
72 /* if merging, indicate to driver that we leave the old IBSS */
73 if (sdata->vif.bss_conf.ibss_joined) {
74 sdata->vif.bss_conf.ibss_joined = false;
75 netif_carrier_off(sdata->dev);
76 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
79 memcpy(ifibss->bssid, bssid, ETH_ALEN);
81 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
83 local->oper_channel = chan;
84 channel_type = ifibss->channel_type;
85 if (channel_type > NL80211_CHAN_HT20 &&
86 !cfg80211_can_beacon_sec_chan(local->hw.wiphy, chan, channel_type))
87 channel_type = NL80211_CHAN_HT20;
88 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
89 /* can only fail due to HT40+/- mismatch */
90 channel_type = NL80211_CHAN_HT20;
91 WARN_ON(!ieee80211_set_channel_type(local, sdata,
92 NL80211_CHAN_HT20));
94 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
96 sband = local->hw.wiphy->bands[chan->band];
98 /* build supported rates array */
99 pos = supp_rates;
100 for (i = 0; i < sband->n_bitrates; i++) {
101 int rate = sband->bitrates[i].bitrate;
102 u8 basic = 0;
103 if (basic_rates & BIT(i))
104 basic = 0x80;
105 *pos++ = basic | (u8) (rate / 5);
108 /* Build IBSS probe response */
109 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
110 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
111 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
112 IEEE80211_STYPE_PROBE_RESP);
113 memset(mgmt->da, 0xff, ETH_ALEN);
114 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
115 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
116 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
117 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
118 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
120 pos = skb_put(skb, 2 + ifibss->ssid_len);
121 *pos++ = WLAN_EID_SSID;
122 *pos++ = ifibss->ssid_len;
123 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
125 rates = sband->n_bitrates;
126 if (rates > 8)
127 rates = 8;
128 pos = skb_put(skb, 2 + rates);
129 *pos++ = WLAN_EID_SUPP_RATES;
130 *pos++ = rates;
131 memcpy(pos, supp_rates, rates);
133 if (sband->band == IEEE80211_BAND_2GHZ) {
134 pos = skb_put(skb, 2 + 1);
135 *pos++ = WLAN_EID_DS_PARAMS;
136 *pos++ = 1;
137 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
140 pos = skb_put(skb, 2 + 2);
141 *pos++ = WLAN_EID_IBSS_PARAMS;
142 *pos++ = 2;
143 /* FIX: set ATIM window based on scan results */
144 *pos++ = 0;
145 *pos++ = 0;
147 if (sband->n_bitrates > 8) {
148 rates = sband->n_bitrates - 8;
149 pos = skb_put(skb, 2 + rates);
150 *pos++ = WLAN_EID_EXT_SUPP_RATES;
151 *pos++ = rates;
152 memcpy(pos, &supp_rates[8], rates);
155 if (ifibss->ie_len)
156 memcpy(skb_put(skb, ifibss->ie_len),
157 ifibss->ie, ifibss->ie_len);
159 /* add HT capability and information IEs */
160 if (channel_type && sband->ht_cap.ht_supported) {
161 pos = skb_put(skb, 4 +
162 sizeof(struct ieee80211_ht_cap) +
163 sizeof(struct ieee80211_ht_operation));
164 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
165 sband->ht_cap.cap);
167 * Note: According to 802.11n-2009 9.13.3.1, HT Protection
168 * field and RIFS Mode are reserved in IBSS mode, therefore
169 * keep them at 0
171 pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
172 chan, channel_type, 0);
175 if (local->hw.queues >= IEEE80211_NUM_ACS) {
176 pos = skb_put(skb, 9);
177 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
178 *pos++ = 7; /* len */
179 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
180 *pos++ = 0x50;
181 *pos++ = 0xf2;
182 *pos++ = 2; /* WME */
183 *pos++ = 0; /* WME info */
184 *pos++ = 1; /* WME ver */
185 *pos++ = 0; /* U-APSD no in use */
188 rcu_assign_pointer(ifibss->presp, skb);
190 sdata->vif.bss_conf.beacon_int = beacon_int;
191 sdata->vif.bss_conf.basic_rates = basic_rates;
192 bss_change = BSS_CHANGED_BEACON_INT;
193 bss_change |= ieee80211_reset_erp_info(sdata);
194 bss_change |= BSS_CHANGED_BSSID;
195 bss_change |= BSS_CHANGED_BEACON;
196 bss_change |= BSS_CHANGED_BEACON_ENABLED;
197 bss_change |= BSS_CHANGED_BASIC_RATES;
198 bss_change |= BSS_CHANGED_HT;
199 bss_change |= BSS_CHANGED_IBSS;
200 sdata->vif.bss_conf.ibss_joined = true;
201 ieee80211_bss_info_change_notify(sdata, bss_change);
203 ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
205 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
206 mod_timer(&ifibss->timer,
207 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
209 bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
210 mgmt, skb->len, 0, GFP_KERNEL);
211 cfg80211_put_bss(bss);
212 netif_carrier_on(sdata->dev);
213 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
216 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
217 struct ieee80211_bss *bss)
219 struct cfg80211_bss *cbss =
220 container_of((void *)bss, struct cfg80211_bss, priv);
221 struct ieee80211_supported_band *sband;
222 u32 basic_rates;
223 int i, j;
224 u16 beacon_int = cbss->beacon_interval;
226 lockdep_assert_held(&sdata->u.ibss.mtx);
228 if (beacon_int < 10)
229 beacon_int = 10;
231 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
233 basic_rates = 0;
235 for (i = 0; i < bss->supp_rates_len; i++) {
236 int rate = (bss->supp_rates[i] & 0x7f) * 5;
237 bool is_basic = !!(bss->supp_rates[i] & 0x80);
239 for (j = 0; j < sband->n_bitrates; j++) {
240 if (sband->bitrates[j].bitrate == rate) {
241 if (is_basic)
242 basic_rates |= BIT(j);
243 break;
248 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
249 beacon_int,
250 cbss->channel,
251 basic_rates,
252 cbss->capability,
253 cbss->tsf);
256 static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta,
257 bool auth)
258 __acquires(RCU)
260 struct ieee80211_sub_if_data *sdata = sta->sdata;
261 u8 addr[ETH_ALEN];
263 memcpy(addr, sta->sta.addr, ETH_ALEN);
265 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
266 wiphy_debug(sdata->local->hw.wiphy,
267 "Adding new IBSS station %pM (dev=%s)\n",
268 addr, sdata->name);
269 #endif
271 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
272 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
273 /* authorize the station only if the network is not RSN protected. If
274 * not wait for the userspace to authorize it */
275 if (!sta->sdata->u.ibss.control_port)
276 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
278 rate_control_rate_init(sta);
280 /* If it fails, maybe we raced another insertion? */
281 if (sta_info_insert_rcu(sta))
282 return sta_info_get(sdata, addr);
283 if (auth) {
284 ibss_vdbg("TX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=1)\n",
285 sdata->vif.addr, sdata->u.ibss.bssid, addr);
286 ieee80211_send_auth(sdata, 1, WLAN_AUTH_OPEN, NULL, 0,
287 addr, sdata->u.ibss.bssid, NULL, 0, 0);
289 return sta;
292 static struct sta_info *
293 ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
294 const u8 *bssid, const u8 *addr,
295 u32 supp_rates, bool auth)
296 __acquires(RCU)
298 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
299 struct ieee80211_local *local = sdata->local;
300 struct sta_info *sta;
301 int band = local->hw.conf.channel->band;
304 * XXX: Consider removing the least recently used entry and
305 * allow new one to be added.
307 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
308 net_dbg_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
309 sdata->name, addr);
310 rcu_read_lock();
311 return NULL;
314 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
315 rcu_read_lock();
316 return NULL;
319 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
320 rcu_read_lock();
321 return NULL;
324 sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
325 if (!sta) {
326 rcu_read_lock();
327 return NULL;
330 sta->last_rx = jiffies;
332 /* make sure mandatory rates are always added */
333 sta->sta.supp_rates[band] = supp_rates |
334 ieee80211_mandatory_rates(local, band);
336 return ieee80211_ibss_finish_sta(sta, auth);
339 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
340 struct ieee80211_mgmt *mgmt,
341 size_t len)
343 u16 auth_alg, auth_transaction;
345 lockdep_assert_held(&sdata->u.ibss.mtx);
347 if (len < 24 + 6)
348 return;
350 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
351 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
353 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
354 return;
355 ibss_vdbg("%s: RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
356 sdata->name, mgmt->sa, mgmt->da, mgmt->bssid,
357 auth_transaction);
358 sta_info_destroy_addr(sdata, mgmt->sa);
359 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, 0, false);
360 rcu_read_unlock();
363 * IEEE 802.11 standard does not require authentication in IBSS
364 * networks and most implementations do not seem to use it.
365 * However, try to reply to authentication attempts if someone
366 * has actually implemented this.
368 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
369 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0);
372 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
373 struct ieee80211_mgmt *mgmt,
374 size_t len,
375 struct ieee80211_rx_status *rx_status,
376 struct ieee802_11_elems *elems,
377 bool beacon)
379 struct ieee80211_local *local = sdata->local;
380 int freq;
381 struct cfg80211_bss *cbss;
382 struct ieee80211_bss *bss;
383 struct sta_info *sta;
384 struct ieee80211_channel *channel;
385 u64 beacon_timestamp, rx_timestamp;
386 u32 supp_rates = 0;
387 enum ieee80211_band band = rx_status->band;
388 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
389 bool rates_updated = false;
391 if (elems->ds_params && elems->ds_params_len == 1)
392 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
393 band);
394 else
395 freq = rx_status->freq;
397 channel = ieee80211_get_channel(local->hw.wiphy, freq);
399 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
400 return;
402 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
403 ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid)) {
405 rcu_read_lock();
406 sta = sta_info_get(sdata, mgmt->sa);
408 if (elems->supp_rates) {
409 supp_rates = ieee80211_sta_get_rates(local, elems,
410 band, NULL);
411 if (sta) {
412 u32 prev_rates;
414 prev_rates = sta->sta.supp_rates[band];
415 /* make sure mandatory rates are always added */
416 sta->sta.supp_rates[band] = supp_rates |
417 ieee80211_mandatory_rates(local, band);
419 if (sta->sta.supp_rates[band] != prev_rates) {
420 ibss_vdbg("%s: updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
421 sdata->name, sta->sta.addr,
422 prev_rates,
423 sta->sta.supp_rates[band]);
424 rates_updated = true;
426 } else {
427 rcu_read_unlock();
428 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
429 mgmt->sa, supp_rates, true);
433 if (sta && elems->wmm_info)
434 set_sta_flag(sta, WLAN_STA_WME);
436 if (sta && elems->ht_operation && elems->ht_cap_elem &&
437 sdata->u.ibss.channel_type != NL80211_CHAN_NO_HT) {
438 /* we both use HT */
439 struct ieee80211_sta_ht_cap sta_ht_cap_new;
440 enum nl80211_channel_type channel_type =
441 ieee80211_ht_oper_to_channel_type(
442 elems->ht_operation);
444 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
445 elems->ht_cap_elem,
446 &sta_ht_cap_new);
449 * fall back to HT20 if we don't use or use
450 * the other extension channel
452 if (!(channel_type == NL80211_CHAN_HT40MINUS ||
453 channel_type == NL80211_CHAN_HT40PLUS) ||
454 channel_type != sdata->u.ibss.channel_type)
455 sta_ht_cap_new.cap &=
456 ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
458 if (memcmp(&sta->sta.ht_cap, &sta_ht_cap_new,
459 sizeof(sta_ht_cap_new))) {
460 memcpy(&sta->sta.ht_cap, &sta_ht_cap_new,
461 sizeof(sta_ht_cap_new));
462 rates_updated = true;
466 if (sta && rates_updated)
467 rate_control_rate_init(sta);
469 rcu_read_unlock();
472 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
473 channel, beacon);
474 if (!bss)
475 return;
477 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
479 /* was just updated in ieee80211_bss_info_update */
480 beacon_timestamp = cbss->tsf;
482 /* check if we need to merge IBSS */
484 /* we use a fixed BSSID */
485 if (sdata->u.ibss.fixed_bssid)
486 goto put_bss;
488 /* not an IBSS */
489 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
490 goto put_bss;
492 /* different channel */
493 if (cbss->channel != local->oper_channel)
494 goto put_bss;
496 /* different SSID */
497 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
498 memcmp(elems->ssid, sdata->u.ibss.ssid,
499 sdata->u.ibss.ssid_len))
500 goto put_bss;
502 /* same BSSID */
503 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
504 goto put_bss;
506 if (rx_status->flag & RX_FLAG_MACTIME_MPDU) {
508 * For correct IBSS merging we need mactime; since mactime is
509 * defined as the time the first data symbol of the frame hits
510 * the PHY, and the timestamp of the beacon is defined as "the
511 * time that the data symbol containing the first bit of the
512 * timestamp is transmitted to the PHY plus the transmitting
513 * STA's delays through its local PHY from the MAC-PHY
514 * interface to its interface with the WM" (802.11 11.1.2)
515 * - equals the time this bit arrives at the receiver - we have
516 * to take into account the offset between the two.
518 * E.g. at 1 MBit that means mactime is 192 usec earlier
519 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
521 int rate;
523 if (rx_status->flag & RX_FLAG_HT)
524 rate = 65; /* TODO: HT rates */
525 else
526 rate = local->hw.wiphy->bands[band]->
527 bitrates[rx_status->rate_idx].bitrate;
529 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
530 } else {
532 * second best option: get current TSF
533 * (will return -1 if not supported)
535 rx_timestamp = drv_get_tsf(local, sdata);
538 ibss_vdbg("RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
539 mgmt->sa, mgmt->bssid,
540 (unsigned long long)rx_timestamp,
541 (unsigned long long)beacon_timestamp,
542 (unsigned long long)(rx_timestamp - beacon_timestamp),
543 jiffies);
545 if (beacon_timestamp > rx_timestamp) {
546 ibss_vdbg("%s: beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
547 sdata->name, mgmt->bssid);
548 ieee80211_sta_join_ibss(sdata, bss);
549 supp_rates = ieee80211_sta_get_rates(local, elems, band, NULL);
550 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
551 supp_rates, true);
552 rcu_read_unlock();
555 put_bss:
556 ieee80211_rx_bss_put(local, bss);
559 void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
560 const u8 *bssid, const u8 *addr,
561 u32 supp_rates)
563 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
564 struct ieee80211_local *local = sdata->local;
565 struct sta_info *sta;
566 int band = local->hw.conf.channel->band;
569 * XXX: Consider removing the least recently used entry and
570 * allow new one to be added.
572 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
573 net_dbg_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
574 sdata->name, addr);
575 return;
578 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
579 return;
581 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
582 return;
584 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
585 if (!sta)
586 return;
588 sta->last_rx = jiffies;
590 /* make sure mandatory rates are always added */
591 sta->sta.supp_rates[band] = supp_rates |
592 ieee80211_mandatory_rates(local, band);
594 spin_lock(&ifibss->incomplete_lock);
595 list_add(&sta->list, &ifibss->incomplete_stations);
596 spin_unlock(&ifibss->incomplete_lock);
597 ieee80211_queue_work(&local->hw, &sdata->work);
600 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
602 struct ieee80211_local *local = sdata->local;
603 int active = 0;
604 struct sta_info *sta;
606 lockdep_assert_held(&sdata->u.ibss.mtx);
608 rcu_read_lock();
610 list_for_each_entry_rcu(sta, &local->sta_list, list) {
611 if (sta->sdata == sdata &&
612 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
613 jiffies)) {
614 active++;
615 break;
619 rcu_read_unlock();
621 return active;
625 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
628 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
630 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
632 lockdep_assert_held(&ifibss->mtx);
634 mod_timer(&ifibss->timer,
635 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
637 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
639 if (time_before(jiffies, ifibss->last_scan_completed +
640 IEEE80211_IBSS_MERGE_INTERVAL))
641 return;
643 if (ieee80211_sta_active_ibss(sdata))
644 return;
646 if (ifibss->fixed_channel)
647 return;
649 pr_debug("%s: No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n",
650 sdata->name);
652 ieee80211_request_internal_scan(sdata,
653 ifibss->ssid, ifibss->ssid_len, NULL);
656 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
658 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
659 u8 bssid[ETH_ALEN];
660 u16 capability;
661 int i;
663 lockdep_assert_held(&ifibss->mtx);
665 if (ifibss->fixed_bssid) {
666 memcpy(bssid, ifibss->bssid, ETH_ALEN);
667 } else {
668 /* Generate random, not broadcast, locally administered BSSID. Mix in
669 * own MAC address to make sure that devices that do not have proper
670 * random number generator get different BSSID. */
671 get_random_bytes(bssid, ETH_ALEN);
672 for (i = 0; i < ETH_ALEN; i++)
673 bssid[i] ^= sdata->vif.addr[i];
674 bssid[0] &= ~0x01;
675 bssid[0] |= 0x02;
678 pr_debug("%s: Creating new IBSS network, BSSID %pM\n",
679 sdata->name, bssid);
681 capability = WLAN_CAPABILITY_IBSS;
683 if (ifibss->privacy)
684 capability |= WLAN_CAPABILITY_PRIVACY;
685 else
686 sdata->drop_unencrypted = 0;
688 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
689 ifibss->channel, ifibss->basic_rates,
690 capability, 0);
694 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
697 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
699 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
700 struct ieee80211_local *local = sdata->local;
701 struct cfg80211_bss *cbss;
702 struct ieee80211_channel *chan = NULL;
703 const u8 *bssid = NULL;
704 int active_ibss;
705 u16 capability;
707 lockdep_assert_held(&ifibss->mtx);
709 active_ibss = ieee80211_sta_active_ibss(sdata);
710 ibss_vdbg("%s: sta_find_ibss (active_ibss=%d)\n",
711 sdata->name, active_ibss);
713 if (active_ibss)
714 return;
716 capability = WLAN_CAPABILITY_IBSS;
717 if (ifibss->privacy)
718 capability |= WLAN_CAPABILITY_PRIVACY;
719 if (ifibss->fixed_bssid)
720 bssid = ifibss->bssid;
721 if (ifibss->fixed_channel)
722 chan = ifibss->channel;
723 if (!is_zero_ether_addr(ifibss->bssid))
724 bssid = ifibss->bssid;
725 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
726 ifibss->ssid, ifibss->ssid_len,
727 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
728 capability);
730 if (cbss) {
731 struct ieee80211_bss *bss;
733 bss = (void *)cbss->priv;
734 ibss_vdbg(" sta_find_ibss: selected %pM current %pM\n",
735 cbss->bssid, ifibss->bssid);
736 pr_debug("%s: Selected IBSS BSSID %pM based on configured SSID\n",
737 sdata->name, cbss->bssid);
739 ieee80211_sta_join_ibss(sdata, bss);
740 ieee80211_rx_bss_put(local, bss);
741 return;
744 ibss_vdbg(" did not try to join ibss\n");
746 /* Selected IBSS not found in current scan results - try to scan */
747 if (time_after(jiffies, ifibss->last_scan_completed +
748 IEEE80211_SCAN_INTERVAL)) {
749 pr_debug("%s: Trigger new scan to find an IBSS to join\n",
750 sdata->name);
752 ieee80211_request_internal_scan(sdata,
753 ifibss->ssid, ifibss->ssid_len,
754 ifibss->fixed_channel ? ifibss->channel : NULL);
755 } else {
756 int interval = IEEE80211_SCAN_INTERVAL;
758 if (time_after(jiffies, ifibss->ibss_join_req +
759 IEEE80211_IBSS_JOIN_TIMEOUT)) {
760 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
761 ieee80211_sta_create_ibss(sdata);
762 return;
764 pr_debug("%s: IBSS not allowed on %d MHz\n",
765 sdata->name,
766 local->hw.conf.channel->center_freq);
768 /* No IBSS found - decrease scan interval and continue
769 * scanning. */
770 interval = IEEE80211_SCAN_INTERVAL_SLOW;
773 mod_timer(&ifibss->timer,
774 round_jiffies(jiffies + interval));
778 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
779 struct sk_buff *req)
781 struct ieee80211_mgmt *mgmt = (void *)req->data;
782 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
783 struct ieee80211_local *local = sdata->local;
784 int tx_last_beacon, len = req->len;
785 struct sk_buff *skb;
786 struct ieee80211_mgmt *resp;
787 struct sk_buff *presp;
788 u8 *pos, *end;
790 lockdep_assert_held(&ifibss->mtx);
792 presp = rcu_dereference_protected(ifibss->presp,
793 lockdep_is_held(&ifibss->mtx));
795 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
796 len < 24 + 2 || !presp)
797 return;
799 tx_last_beacon = drv_tx_last_beacon(local);
801 ibss_vdbg("%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
802 sdata->name, mgmt->sa, mgmt->da,
803 mgmt->bssid, tx_last_beacon);
805 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
806 return;
808 if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
809 !is_broadcast_ether_addr(mgmt->bssid))
810 return;
812 end = ((u8 *) mgmt) + len;
813 pos = mgmt->u.probe_req.variable;
814 if (pos[0] != WLAN_EID_SSID ||
815 pos + 2 + pos[1] > end) {
816 ibss_vdbg("%s: Invalid SSID IE in ProbeReq from %pM\n",
817 sdata->name, mgmt->sa);
818 return;
820 if (pos[1] != 0 &&
821 (pos[1] != ifibss->ssid_len ||
822 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
823 /* Ignore ProbeReq for foreign SSID */
824 return;
827 /* Reply with ProbeResp */
828 skb = skb_copy(presp, GFP_KERNEL);
829 if (!skb)
830 return;
832 resp = (struct ieee80211_mgmt *) skb->data;
833 memcpy(resp->da, mgmt->sa, ETH_ALEN);
834 ibss_vdbg("%s: Sending ProbeResp to %pM\n", sdata->name, resp->da);
835 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
836 ieee80211_tx_skb(sdata, skb);
839 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
840 struct ieee80211_mgmt *mgmt,
841 size_t len,
842 struct ieee80211_rx_status *rx_status)
844 size_t baselen;
845 struct ieee802_11_elems elems;
847 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
848 if (baselen > len)
849 return;
851 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
852 &elems);
854 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
857 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
858 struct ieee80211_mgmt *mgmt,
859 size_t len,
860 struct ieee80211_rx_status *rx_status)
862 size_t baselen;
863 struct ieee802_11_elems elems;
865 /* Process beacon from the current BSS */
866 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
867 if (baselen > len)
868 return;
870 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
872 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
875 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
876 struct sk_buff *skb)
878 struct ieee80211_rx_status *rx_status;
879 struct ieee80211_mgmt *mgmt;
880 u16 fc;
882 rx_status = IEEE80211_SKB_RXCB(skb);
883 mgmt = (struct ieee80211_mgmt *) skb->data;
884 fc = le16_to_cpu(mgmt->frame_control);
886 mutex_lock(&sdata->u.ibss.mtx);
888 if (!sdata->u.ibss.ssid_len)
889 goto mgmt_out; /* not ready to merge yet */
891 switch (fc & IEEE80211_FCTL_STYPE) {
892 case IEEE80211_STYPE_PROBE_REQ:
893 ieee80211_rx_mgmt_probe_req(sdata, skb);
894 break;
895 case IEEE80211_STYPE_PROBE_RESP:
896 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
897 rx_status);
898 break;
899 case IEEE80211_STYPE_BEACON:
900 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
901 rx_status);
902 break;
903 case IEEE80211_STYPE_AUTH:
904 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
905 break;
908 mgmt_out:
909 mutex_unlock(&sdata->u.ibss.mtx);
912 void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
914 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
915 struct sta_info *sta;
917 mutex_lock(&ifibss->mtx);
920 * Work could be scheduled after scan or similar
921 * when we aren't even joined (or trying) with a
922 * network.
924 if (!ifibss->ssid_len)
925 goto out;
927 spin_lock_bh(&ifibss->incomplete_lock);
928 while (!list_empty(&ifibss->incomplete_stations)) {
929 sta = list_first_entry(&ifibss->incomplete_stations,
930 struct sta_info, list);
931 list_del(&sta->list);
932 spin_unlock_bh(&ifibss->incomplete_lock);
934 ieee80211_ibss_finish_sta(sta, true);
935 rcu_read_unlock();
936 spin_lock_bh(&ifibss->incomplete_lock);
938 spin_unlock_bh(&ifibss->incomplete_lock);
940 switch (ifibss->state) {
941 case IEEE80211_IBSS_MLME_SEARCH:
942 ieee80211_sta_find_ibss(sdata);
943 break;
944 case IEEE80211_IBSS_MLME_JOINED:
945 ieee80211_sta_merge_ibss(sdata);
946 break;
947 default:
948 WARN_ON(1);
949 break;
952 out:
953 mutex_unlock(&ifibss->mtx);
956 static void ieee80211_ibss_timer(unsigned long data)
958 struct ieee80211_sub_if_data *sdata =
959 (struct ieee80211_sub_if_data *) data;
960 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
961 struct ieee80211_local *local = sdata->local;
963 if (local->quiescing) {
964 ifibss->timer_running = true;
965 return;
968 ieee80211_queue_work(&local->hw, &sdata->work);
971 #ifdef CONFIG_PM
972 void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
974 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
976 if (del_timer_sync(&ifibss->timer))
977 ifibss->timer_running = true;
980 void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
982 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
984 if (ifibss->timer_running) {
985 add_timer(&ifibss->timer);
986 ifibss->timer_running = false;
989 #endif
991 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
993 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
995 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
996 (unsigned long) sdata);
997 mutex_init(&ifibss->mtx);
998 INIT_LIST_HEAD(&ifibss->incomplete_stations);
999 spin_lock_init(&ifibss->incomplete_lock);
1002 /* scan finished notification */
1003 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1005 struct ieee80211_sub_if_data *sdata;
1007 mutex_lock(&local->iflist_mtx);
1008 list_for_each_entry(sdata, &local->interfaces, list) {
1009 if (!ieee80211_sdata_running(sdata))
1010 continue;
1011 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1012 continue;
1013 sdata->u.ibss.last_scan_completed = jiffies;
1014 ieee80211_queue_work(&local->hw, &sdata->work);
1016 mutex_unlock(&local->iflist_mtx);
1019 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1020 struct cfg80211_ibss_params *params)
1022 struct sk_buff *skb;
1023 u32 changed = 0;
1025 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
1026 sizeof(struct ieee80211_hdr_3addr) +
1027 12 /* struct ieee80211_mgmt.u.beacon */ +
1028 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
1029 2 + 8 /* max Supported Rates */ +
1030 3 /* max DS params */ +
1031 4 /* IBSS params */ +
1032 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
1033 2 + sizeof(struct ieee80211_ht_cap) +
1034 2 + sizeof(struct ieee80211_ht_operation) +
1035 params->ie_len);
1036 if (!skb)
1037 return -ENOMEM;
1039 mutex_lock(&sdata->u.ibss.mtx);
1041 if (params->bssid) {
1042 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1043 sdata->u.ibss.fixed_bssid = true;
1044 } else
1045 sdata->u.ibss.fixed_bssid = false;
1047 sdata->u.ibss.privacy = params->privacy;
1048 sdata->u.ibss.control_port = params->control_port;
1049 sdata->u.ibss.basic_rates = params->basic_rates;
1050 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1051 sizeof(params->mcast_rate));
1053 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1055 sdata->u.ibss.channel = params->channel;
1056 sdata->u.ibss.channel_type = params->channel_type;
1057 sdata->u.ibss.fixed_channel = params->channel_fixed;
1059 /* fix ourselves to that channel now already */
1060 if (params->channel_fixed) {
1061 sdata->local->oper_channel = params->channel;
1062 if (!ieee80211_set_channel_type(sdata->local, sdata,
1063 params->channel_type)) {
1064 mutex_unlock(&sdata->u.ibss.mtx);
1065 kfree_skb(skb);
1066 return -EINVAL;
1070 if (params->ie) {
1071 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1072 GFP_KERNEL);
1073 if (sdata->u.ibss.ie)
1074 sdata->u.ibss.ie_len = params->ie_len;
1077 sdata->u.ibss.skb = skb;
1078 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1079 sdata->u.ibss.ibss_join_req = jiffies;
1081 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
1082 sdata->u.ibss.ssid_len = params->ssid_len;
1084 mutex_unlock(&sdata->u.ibss.mtx);
1086 mutex_lock(&sdata->local->mtx);
1087 ieee80211_recalc_idle(sdata->local);
1088 mutex_unlock(&sdata->local->mtx);
1091 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1092 * reserved, but an HT STA shall protect HT transmissions as though
1093 * the HT Protection field were set to non-HT mixed mode.
1095 * In an IBSS, the RIFS Mode field of the HT Operation element is
1096 * also reserved, but an HT STA shall operate as though this field
1097 * were set to 1.
1100 sdata->vif.bss_conf.ht_operation_mode |=
1101 IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1102 | IEEE80211_HT_PARAM_RIFS_MODE;
1104 changed |= BSS_CHANGED_HT;
1105 ieee80211_bss_info_change_notify(sdata, changed);
1107 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1109 return 0;
1112 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1114 struct sk_buff *skb;
1115 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1116 struct ieee80211_local *local = sdata->local;
1117 struct cfg80211_bss *cbss;
1118 u16 capability;
1119 int active_ibss;
1120 struct sta_info *sta;
1122 mutex_lock(&sdata->u.ibss.mtx);
1124 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1125 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
1126 sdata->u.ibss.ssid_len = 0;
1128 active_ibss = ieee80211_sta_active_ibss(sdata);
1130 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
1131 capability = WLAN_CAPABILITY_IBSS;
1133 if (ifibss->privacy)
1134 capability |= WLAN_CAPABILITY_PRIVACY;
1136 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
1137 ifibss->bssid, ifibss->ssid,
1138 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
1139 WLAN_CAPABILITY_PRIVACY,
1140 capability);
1142 if (cbss) {
1143 cfg80211_unlink_bss(local->hw.wiphy, cbss);
1144 cfg80211_put_bss(cbss);
1148 sta_info_flush(sdata->local, sdata);
1150 spin_lock_bh(&ifibss->incomplete_lock);
1151 while (!list_empty(&ifibss->incomplete_stations)) {
1152 sta = list_first_entry(&ifibss->incomplete_stations,
1153 struct sta_info, list);
1154 list_del(&sta->list);
1155 spin_unlock_bh(&ifibss->incomplete_lock);
1157 sta_info_free(local, sta);
1158 spin_lock_bh(&ifibss->incomplete_lock);
1160 spin_unlock_bh(&ifibss->incomplete_lock);
1162 netif_carrier_off(sdata->dev);
1164 /* remove beacon */
1165 kfree(sdata->u.ibss.ie);
1166 skb = rcu_dereference_protected(sdata->u.ibss.presp,
1167 lockdep_is_held(&sdata->u.ibss.mtx));
1168 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
1169 sdata->vif.bss_conf.ibss_joined = false;
1170 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1171 BSS_CHANGED_IBSS);
1172 synchronize_rcu();
1173 kfree_skb(skb);
1175 skb_queue_purge(&sdata->skb_queue);
1177 del_timer_sync(&sdata->u.ibss.timer);
1179 mutex_unlock(&sdata->u.ibss.mtx);
1181 mutex_lock(&local->mtx);
1182 ieee80211_recalc_idle(sdata->local);
1183 mutex_unlock(&local->mtx);
1185 return 0;