ocfs2: Make the left masklogs compat.
[taoma-kernel.git] / net / mac80211 / ibss.c
blob53c7077ffd4f2ff36e5ddcbdcc3f33bd378cb1c7
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>
23 #include <asm/unaligned.h>
25 #include "ieee80211_i.h"
26 #include "driver-ops.h"
27 #include "rate.h"
29 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
30 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
31 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
33 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
34 #define IEEE80211_IBSS_MERGE_DELAY 0x400000
35 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
37 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
40 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
41 struct ieee80211_mgmt *mgmt,
42 size_t len)
44 u16 auth_alg, auth_transaction, status_code;
46 lockdep_assert_held(&sdata->u.ibss.mtx);
48 if (len < 24 + 6)
49 return;
51 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
52 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
53 status_code = le16_to_cpu(mgmt->u.auth.status_code);
56 * IEEE 802.11 standard does not require authentication in IBSS
57 * networks and most implementations do not seem to use it.
58 * However, try to reply to authentication attempts if someone
59 * has actually implemented this.
61 if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1)
62 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
63 sdata->u.ibss.bssid, NULL, 0, 0);
66 static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
67 const u8 *bssid, const int beacon_int,
68 struct ieee80211_channel *chan,
69 const u32 basic_rates,
70 const u16 capability, u64 tsf)
72 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
73 struct ieee80211_local *local = sdata->local;
74 int rates, i;
75 struct sk_buff *skb;
76 struct ieee80211_mgmt *mgmt;
77 u8 *pos;
78 struct ieee80211_supported_band *sband;
79 struct cfg80211_bss *bss;
80 u32 bss_change;
81 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
83 lockdep_assert_held(&ifibss->mtx);
85 /* Reset own TSF to allow time synchronization work. */
86 drv_reset_tsf(local);
88 skb = ifibss->skb;
89 rcu_assign_pointer(ifibss->presp, NULL);
90 synchronize_rcu();
91 skb->data = skb->head;
92 skb->len = 0;
93 skb_reset_tail_pointer(skb);
94 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
96 if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
97 sta_info_flush(sdata->local, sdata);
99 /* if merging, indicate to driver that we leave the old IBSS */
100 if (sdata->vif.bss_conf.ibss_joined) {
101 sdata->vif.bss_conf.ibss_joined = false;
102 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
105 memcpy(ifibss->bssid, bssid, ETH_ALEN);
107 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
109 local->oper_channel = chan;
110 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
111 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
113 sband = local->hw.wiphy->bands[chan->band];
115 /* build supported rates array */
116 pos = supp_rates;
117 for (i = 0; i < sband->n_bitrates; i++) {
118 int rate = sband->bitrates[i].bitrate;
119 u8 basic = 0;
120 if (basic_rates & BIT(i))
121 basic = 0x80;
122 *pos++ = basic | (u8) (rate / 5);
125 /* Build IBSS probe response */
126 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
127 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
128 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
129 IEEE80211_STYPE_PROBE_RESP);
130 memset(mgmt->da, 0xff, ETH_ALEN);
131 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
132 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
133 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
134 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
135 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
137 pos = skb_put(skb, 2 + ifibss->ssid_len);
138 *pos++ = WLAN_EID_SSID;
139 *pos++ = ifibss->ssid_len;
140 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
142 rates = sband->n_bitrates;
143 if (rates > 8)
144 rates = 8;
145 pos = skb_put(skb, 2 + rates);
146 *pos++ = WLAN_EID_SUPP_RATES;
147 *pos++ = rates;
148 memcpy(pos, supp_rates, rates);
150 if (sband->band == IEEE80211_BAND_2GHZ) {
151 pos = skb_put(skb, 2 + 1);
152 *pos++ = WLAN_EID_DS_PARAMS;
153 *pos++ = 1;
154 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
157 pos = skb_put(skb, 2 + 2);
158 *pos++ = WLAN_EID_IBSS_PARAMS;
159 *pos++ = 2;
160 /* FIX: set ATIM window based on scan results */
161 *pos++ = 0;
162 *pos++ = 0;
164 if (sband->n_bitrates > 8) {
165 rates = sband->n_bitrates - 8;
166 pos = skb_put(skb, 2 + rates);
167 *pos++ = WLAN_EID_EXT_SUPP_RATES;
168 *pos++ = rates;
169 memcpy(pos, &supp_rates[8], rates);
172 if (ifibss->ie_len)
173 memcpy(skb_put(skb, ifibss->ie_len),
174 ifibss->ie, ifibss->ie_len);
176 if (local->hw.queues >= 4) {
177 pos = skb_put(skb, 9);
178 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
179 *pos++ = 7; /* len */
180 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
181 *pos++ = 0x50;
182 *pos++ = 0xf2;
183 *pos++ = 2; /* WME */
184 *pos++ = 0; /* WME info */
185 *pos++ = 1; /* WME ver */
186 *pos++ = 0; /* U-APSD no in use */
189 rcu_assign_pointer(ifibss->presp, skb);
191 sdata->vif.bss_conf.beacon_int = beacon_int;
192 sdata->vif.bss_conf.basic_rates = basic_rates;
193 bss_change = BSS_CHANGED_BEACON_INT;
194 bss_change |= ieee80211_reset_erp_info(sdata);
195 bss_change |= BSS_CHANGED_BSSID;
196 bss_change |= BSS_CHANGED_BEACON;
197 bss_change |= BSS_CHANGED_BEACON_ENABLED;
198 bss_change |= BSS_CHANGED_BASIC_RATES;
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 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
215 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
216 struct ieee80211_bss *bss)
218 struct cfg80211_bss *cbss =
219 container_of((void *)bss, struct cfg80211_bss, priv);
220 struct ieee80211_supported_band *sband;
221 u32 basic_rates;
222 int i, j;
223 u16 beacon_int = cbss->beacon_interval;
225 lockdep_assert_held(&sdata->u.ibss.mtx);
227 if (beacon_int < 10)
228 beacon_int = 10;
230 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
232 basic_rates = 0;
234 for (i = 0; i < bss->supp_rates_len; i++) {
235 int rate = (bss->supp_rates[i] & 0x7f) * 5;
236 bool is_basic = !!(bss->supp_rates[i] & 0x80);
238 for (j = 0; j < sband->n_bitrates; j++) {
239 if (sband->bitrates[j].bitrate == rate) {
240 if (is_basic)
241 basic_rates |= BIT(j);
242 break;
247 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
248 beacon_int,
249 cbss->channel,
250 basic_rates,
251 cbss->capability,
252 cbss->tsf);
255 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
256 struct ieee80211_mgmt *mgmt,
257 size_t len,
258 struct ieee80211_rx_status *rx_status,
259 struct ieee802_11_elems *elems,
260 bool beacon)
262 struct ieee80211_local *local = sdata->local;
263 int freq;
264 struct cfg80211_bss *cbss;
265 struct ieee80211_bss *bss;
266 struct sta_info *sta;
267 struct ieee80211_channel *channel;
268 u64 beacon_timestamp, rx_timestamp;
269 u32 supp_rates = 0;
270 enum ieee80211_band band = rx_status->band;
272 if (elems->ds_params && elems->ds_params_len == 1)
273 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
274 else
275 freq = rx_status->freq;
277 channel = ieee80211_get_channel(local->hw.wiphy, freq);
279 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
280 return;
282 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
283 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
285 rcu_read_lock();
286 sta = sta_info_get(sdata, mgmt->sa);
288 if (elems->supp_rates) {
289 supp_rates = ieee80211_sta_get_rates(local, elems,
290 band);
291 if (sta) {
292 u32 prev_rates;
294 prev_rates = sta->sta.supp_rates[band];
295 /* make sure mandatory rates are always added */
296 sta->sta.supp_rates[band] = supp_rates |
297 ieee80211_mandatory_rates(local, band);
299 if (sta->sta.supp_rates[band] != prev_rates) {
300 #ifdef CONFIG_MAC80211_IBSS_DEBUG
301 printk(KERN_DEBUG
302 "%s: updated supp_rates set "
303 "for %pM based on beacon"
304 "/probe_resp (0x%x -> 0x%x)\n",
305 sdata->name, sta->sta.addr,
306 prev_rates,
307 sta->sta.supp_rates[band]);
308 #endif
309 rate_control_rate_init(sta);
311 } else
312 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
313 mgmt->sa, supp_rates,
314 GFP_ATOMIC);
317 if (sta && elems->wmm_info)
318 set_sta_flags(sta, WLAN_STA_WME);
320 rcu_read_unlock();
323 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
324 channel, beacon);
325 if (!bss)
326 return;
328 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
330 /* was just updated in ieee80211_bss_info_update */
331 beacon_timestamp = cbss->tsf;
333 /* check if we need to merge IBSS */
335 /* we use a fixed BSSID */
336 if (sdata->u.ibss.fixed_bssid)
337 goto put_bss;
339 /* not an IBSS */
340 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
341 goto put_bss;
343 /* different channel */
344 if (cbss->channel != local->oper_channel)
345 goto put_bss;
347 /* different SSID */
348 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
349 memcmp(elems->ssid, sdata->u.ibss.ssid,
350 sdata->u.ibss.ssid_len))
351 goto put_bss;
353 /* same BSSID */
354 if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
355 goto put_bss;
357 if (rx_status->flag & RX_FLAG_TSFT) {
359 * For correct IBSS merging we need mactime; since mactime is
360 * defined as the time the first data symbol of the frame hits
361 * the PHY, and the timestamp of the beacon is defined as "the
362 * time that the data symbol containing the first bit of the
363 * timestamp is transmitted to the PHY plus the transmitting
364 * STA's delays through its local PHY from the MAC-PHY
365 * interface to its interface with the WM" (802.11 11.1.2)
366 * - equals the time this bit arrives at the receiver - we have
367 * to take into account the offset between the two.
369 * E.g. at 1 MBit that means mactime is 192 usec earlier
370 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
372 int rate;
374 if (rx_status->flag & RX_FLAG_HT)
375 rate = 65; /* TODO: HT rates */
376 else
377 rate = local->hw.wiphy->bands[band]->
378 bitrates[rx_status->rate_idx].bitrate;
380 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
381 } else {
383 * second best option: get current TSF
384 * (will return -1 if not supported)
386 rx_timestamp = drv_get_tsf(local);
389 #ifdef CONFIG_MAC80211_IBSS_DEBUG
390 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
391 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
392 mgmt->sa, mgmt->bssid,
393 (unsigned long long)rx_timestamp,
394 (unsigned long long)beacon_timestamp,
395 (unsigned long long)(rx_timestamp - beacon_timestamp),
396 jiffies);
397 #endif
399 /* give slow hardware some time to do the TSF sync */
400 if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
401 goto put_bss;
403 if (beacon_timestamp > rx_timestamp) {
404 #ifdef CONFIG_MAC80211_IBSS_DEBUG
405 printk(KERN_DEBUG "%s: beacon TSF higher than "
406 "local TSF - IBSS merge with BSSID %pM\n",
407 sdata->name, mgmt->bssid);
408 #endif
409 ieee80211_sta_join_ibss(sdata, bss);
410 supp_rates = ieee80211_sta_get_rates(local, elems, band);
411 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
412 supp_rates, GFP_KERNEL);
415 put_bss:
416 ieee80211_rx_bss_put(local, bss);
420 * Add a new IBSS station, will also be called by the RX code when,
421 * in IBSS mode, receiving a frame from a yet-unknown station, hence
422 * must be callable in atomic context.
424 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
425 u8 *bssid,u8 *addr, u32 supp_rates,
426 gfp_t gfp)
428 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
429 struct ieee80211_local *local = sdata->local;
430 struct sta_info *sta;
431 int band = local->hw.conf.channel->band;
434 * XXX: Consider removing the least recently used entry and
435 * allow new one to be added.
437 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
438 if (net_ratelimit())
439 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
440 sdata->name, addr);
441 return NULL;
444 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
445 return NULL;
447 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
448 return NULL;
450 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
451 wiphy_debug(local->hw.wiphy, "Adding new IBSS station %pM (dev=%s)\n",
452 addr, sdata->name);
453 #endif
455 sta = sta_info_alloc(sdata, addr, gfp);
456 if (!sta)
457 return NULL;
459 sta->last_rx = jiffies;
460 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
462 /* make sure mandatory rates are always added */
463 sta->sta.supp_rates[band] = supp_rates |
464 ieee80211_mandatory_rates(local, band);
466 rate_control_rate_init(sta);
468 /* If it fails, maybe we raced another insertion? */
469 if (sta_info_insert(sta))
470 return sta_info_get(sdata, addr);
471 return sta;
474 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
476 struct ieee80211_local *local = sdata->local;
477 int active = 0;
478 struct sta_info *sta;
480 lockdep_assert_held(&sdata->u.ibss.mtx);
482 rcu_read_lock();
484 list_for_each_entry_rcu(sta, &local->sta_list, list) {
485 if (sta->sdata == sdata &&
486 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
487 jiffies)) {
488 active++;
489 break;
493 rcu_read_unlock();
495 return active;
499 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
502 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
504 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
506 lockdep_assert_held(&ifibss->mtx);
508 mod_timer(&ifibss->timer,
509 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
511 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
513 if (time_before(jiffies, ifibss->last_scan_completed +
514 IEEE80211_IBSS_MERGE_INTERVAL))
515 return;
517 if (ieee80211_sta_active_ibss(sdata))
518 return;
520 if (ifibss->fixed_channel)
521 return;
523 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
524 "IBSS networks with same SSID (merge)\n", sdata->name);
526 ieee80211_request_internal_scan(sdata,
527 ifibss->ssid, ifibss->ssid_len,
528 ifibss->fixed_channel ? ifibss->channel : NULL);
531 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
533 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
534 struct ieee80211_local *local = sdata->local;
535 struct ieee80211_supported_band *sband;
536 u8 bssid[ETH_ALEN];
537 u16 capability;
538 int i;
540 lockdep_assert_held(&ifibss->mtx);
542 if (ifibss->fixed_bssid) {
543 memcpy(bssid, ifibss->bssid, ETH_ALEN);
544 } else {
545 /* Generate random, not broadcast, locally administered BSSID. Mix in
546 * own MAC address to make sure that devices that do not have proper
547 * random number generator get different BSSID. */
548 get_random_bytes(bssid, ETH_ALEN);
549 for (i = 0; i < ETH_ALEN; i++)
550 bssid[i] ^= sdata->vif.addr[i];
551 bssid[0] &= ~0x01;
552 bssid[0] |= 0x02;
555 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
556 sdata->name, bssid);
558 sband = local->hw.wiphy->bands[ifibss->channel->band];
560 capability = WLAN_CAPABILITY_IBSS;
562 if (ifibss->privacy)
563 capability |= WLAN_CAPABILITY_PRIVACY;
564 else
565 sdata->drop_unencrypted = 0;
567 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
568 ifibss->channel, ifibss->basic_rates,
569 capability, 0);
573 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
576 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
578 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
579 struct ieee80211_local *local = sdata->local;
580 struct cfg80211_bss *cbss;
581 struct ieee80211_channel *chan = NULL;
582 const u8 *bssid = NULL;
583 int active_ibss;
584 u16 capability;
586 lockdep_assert_held(&ifibss->mtx);
588 active_ibss = ieee80211_sta_active_ibss(sdata);
589 #ifdef CONFIG_MAC80211_IBSS_DEBUG
590 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
591 sdata->name, active_ibss);
592 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
594 if (active_ibss)
595 return;
597 capability = WLAN_CAPABILITY_IBSS;
598 if (ifibss->privacy)
599 capability |= WLAN_CAPABILITY_PRIVACY;
600 if (ifibss->fixed_bssid)
601 bssid = ifibss->bssid;
602 if (ifibss->fixed_channel)
603 chan = ifibss->channel;
604 if (!is_zero_ether_addr(ifibss->bssid))
605 bssid = ifibss->bssid;
606 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
607 ifibss->ssid, ifibss->ssid_len,
608 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
609 capability);
611 if (cbss) {
612 struct ieee80211_bss *bss;
614 bss = (void *)cbss->priv;
615 #ifdef CONFIG_MAC80211_IBSS_DEBUG
616 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
617 "%pM\n", cbss->bssid, ifibss->bssid);
618 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
620 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
621 " based on configured SSID\n",
622 sdata->name, cbss->bssid);
624 ieee80211_sta_join_ibss(sdata, bss);
625 ieee80211_rx_bss_put(local, bss);
626 return;
629 #ifdef CONFIG_MAC80211_IBSS_DEBUG
630 printk(KERN_DEBUG " did not try to join ibss\n");
631 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
633 /* Selected IBSS not found in current scan results - try to scan */
634 if (time_after(jiffies, ifibss->last_scan_completed +
635 IEEE80211_SCAN_INTERVAL)) {
636 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
637 "join\n", sdata->name);
639 ieee80211_request_internal_scan(sdata,
640 ifibss->ssid, ifibss->ssid_len,
641 ifibss->fixed_channel ? ifibss->channel : NULL);
642 } else {
643 int interval = IEEE80211_SCAN_INTERVAL;
645 if (time_after(jiffies, ifibss->ibss_join_req +
646 IEEE80211_IBSS_JOIN_TIMEOUT)) {
647 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
648 ieee80211_sta_create_ibss(sdata);
649 return;
651 printk(KERN_DEBUG "%s: IBSS not allowed on"
652 " %d MHz\n", sdata->name,
653 local->hw.conf.channel->center_freq);
655 /* No IBSS found - decrease scan interval and continue
656 * scanning. */
657 interval = IEEE80211_SCAN_INTERVAL_SLOW;
660 mod_timer(&ifibss->timer,
661 round_jiffies(jiffies + interval));
665 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
666 struct ieee80211_mgmt *mgmt,
667 size_t len)
669 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
670 struct ieee80211_local *local = sdata->local;
671 int tx_last_beacon;
672 struct sk_buff *skb;
673 struct ieee80211_mgmt *resp;
674 u8 *pos, *end;
676 lockdep_assert_held(&ifibss->mtx);
678 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
679 len < 24 + 2 || !ifibss->presp)
680 return;
682 tx_last_beacon = drv_tx_last_beacon(local);
684 #ifdef CONFIG_MAC80211_IBSS_DEBUG
685 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
686 " (tx_last_beacon=%d)\n",
687 sdata->name, mgmt->sa, mgmt->da,
688 mgmt->bssid, tx_last_beacon);
689 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
691 if (!tx_last_beacon)
692 return;
694 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
695 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
696 return;
698 end = ((u8 *) mgmt) + len;
699 pos = mgmt->u.probe_req.variable;
700 if (pos[0] != WLAN_EID_SSID ||
701 pos + 2 + pos[1] > end) {
702 #ifdef CONFIG_MAC80211_IBSS_DEBUG
703 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
704 "from %pM\n",
705 sdata->name, mgmt->sa);
706 #endif
707 return;
709 if (pos[1] != 0 &&
710 (pos[1] != ifibss->ssid_len ||
711 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
712 /* Ignore ProbeReq for foreign SSID */
713 return;
716 /* Reply with ProbeResp */
717 skb = skb_copy(ifibss->presp, GFP_KERNEL);
718 if (!skb)
719 return;
721 resp = (struct ieee80211_mgmt *) skb->data;
722 memcpy(resp->da, mgmt->sa, ETH_ALEN);
723 #ifdef CONFIG_MAC80211_IBSS_DEBUG
724 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
725 sdata->name, resp->da);
726 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
727 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
728 ieee80211_tx_skb(sdata, skb);
731 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
732 struct ieee80211_mgmt *mgmt,
733 size_t len,
734 struct ieee80211_rx_status *rx_status)
736 size_t baselen;
737 struct ieee802_11_elems elems;
739 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
740 return; /* ignore ProbeResp to foreign address */
742 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
743 if (baselen > len)
744 return;
746 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
747 &elems);
749 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
752 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
753 struct ieee80211_mgmt *mgmt,
754 size_t len,
755 struct ieee80211_rx_status *rx_status)
757 size_t baselen;
758 struct ieee802_11_elems elems;
760 /* Process beacon from the current BSS */
761 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
762 if (baselen > len)
763 return;
765 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
767 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
770 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
771 struct sk_buff *skb)
773 struct ieee80211_rx_status *rx_status;
774 struct ieee80211_mgmt *mgmt;
775 u16 fc;
777 rx_status = IEEE80211_SKB_RXCB(skb);
778 mgmt = (struct ieee80211_mgmt *) skb->data;
779 fc = le16_to_cpu(mgmt->frame_control);
781 mutex_lock(&sdata->u.ibss.mtx);
783 if (!sdata->u.ibss.ssid_len)
784 goto mgmt_out; /* not ready to merge yet */
786 switch (fc & IEEE80211_FCTL_STYPE) {
787 case IEEE80211_STYPE_PROBE_REQ:
788 ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
789 break;
790 case IEEE80211_STYPE_PROBE_RESP:
791 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
792 rx_status);
793 break;
794 case IEEE80211_STYPE_BEACON:
795 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
796 rx_status);
797 break;
798 case IEEE80211_STYPE_AUTH:
799 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
800 break;
803 mgmt_out:
804 mutex_unlock(&sdata->u.ibss.mtx);
807 void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
809 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
811 mutex_lock(&ifibss->mtx);
814 * Work could be scheduled after scan or similar
815 * when we aren't even joined (or trying) with a
816 * network.
818 if (!ifibss->ssid_len)
819 goto out;
821 switch (ifibss->state) {
822 case IEEE80211_IBSS_MLME_SEARCH:
823 ieee80211_sta_find_ibss(sdata);
824 break;
825 case IEEE80211_IBSS_MLME_JOINED:
826 ieee80211_sta_merge_ibss(sdata);
827 break;
828 default:
829 WARN_ON(1);
830 break;
833 out:
834 mutex_unlock(&ifibss->mtx);
837 static void ieee80211_ibss_timer(unsigned long data)
839 struct ieee80211_sub_if_data *sdata =
840 (struct ieee80211_sub_if_data *) data;
841 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
842 struct ieee80211_local *local = sdata->local;
844 if (local->quiescing) {
845 ifibss->timer_running = true;
846 return;
849 ieee80211_queue_work(&local->hw, &sdata->work);
852 #ifdef CONFIG_PM
853 void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
855 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
857 if (del_timer_sync(&ifibss->timer))
858 ifibss->timer_running = true;
861 void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
863 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
865 if (ifibss->timer_running) {
866 add_timer(&ifibss->timer);
867 ifibss->timer_running = false;
870 #endif
872 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
874 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
876 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
877 (unsigned long) sdata);
878 mutex_init(&ifibss->mtx);
881 /* scan finished notification */
882 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
884 struct ieee80211_sub_if_data *sdata;
886 mutex_lock(&local->iflist_mtx);
887 list_for_each_entry(sdata, &local->interfaces, list) {
888 if (!ieee80211_sdata_running(sdata))
889 continue;
890 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
891 continue;
892 sdata->u.ibss.last_scan_completed = jiffies;
893 ieee80211_queue_work(&local->hw, &sdata->work);
895 mutex_unlock(&local->iflist_mtx);
898 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
899 struct cfg80211_ibss_params *params)
901 struct sk_buff *skb;
903 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
904 36 /* bitrates */ +
905 34 /* SSID */ +
906 3 /* DS params */ +
907 4 /* IBSS params */ +
908 params->ie_len);
909 if (!skb)
910 return -ENOMEM;
912 mutex_lock(&sdata->u.ibss.mtx);
914 if (params->bssid) {
915 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
916 sdata->u.ibss.fixed_bssid = true;
917 } else
918 sdata->u.ibss.fixed_bssid = false;
920 sdata->u.ibss.privacy = params->privacy;
921 sdata->u.ibss.basic_rates = params->basic_rates;
922 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
923 sizeof(params->mcast_rate));
925 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
927 sdata->u.ibss.channel = params->channel;
928 sdata->u.ibss.fixed_channel = params->channel_fixed;
930 /* fix ourselves to that channel now already */
931 if (params->channel_fixed) {
932 sdata->local->oper_channel = params->channel;
933 WARN_ON(!ieee80211_set_channel_type(sdata->local, sdata,
934 NL80211_CHAN_NO_HT));
937 if (params->ie) {
938 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
939 GFP_KERNEL);
940 if (sdata->u.ibss.ie)
941 sdata->u.ibss.ie_len = params->ie_len;
944 sdata->u.ibss.skb = skb;
945 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
946 sdata->u.ibss.ibss_join_req = jiffies;
948 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
949 sdata->u.ibss.ssid_len = params->ssid_len;
951 mutex_unlock(&sdata->u.ibss.mtx);
953 mutex_lock(&sdata->local->mtx);
954 ieee80211_recalc_idle(sdata->local);
955 mutex_unlock(&sdata->local->mtx);
957 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
959 return 0;
962 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
964 struct sk_buff *skb;
965 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
966 struct ieee80211_local *local = sdata->local;
967 struct cfg80211_bss *cbss;
968 u16 capability;
969 int active_ibss;
971 mutex_lock(&sdata->u.ibss.mtx);
973 active_ibss = ieee80211_sta_active_ibss(sdata);
975 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
976 capability = WLAN_CAPABILITY_IBSS;
978 if (ifibss->privacy)
979 capability |= WLAN_CAPABILITY_PRIVACY;
981 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
982 ifibss->bssid, ifibss->ssid,
983 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
984 WLAN_CAPABILITY_PRIVACY,
985 capability);
987 if (cbss) {
988 cfg80211_unlink_bss(local->hw.wiphy, cbss);
989 cfg80211_put_bss(cbss);
993 sta_info_flush(sdata->local, sdata);
995 /* remove beacon */
996 kfree(sdata->u.ibss.ie);
997 skb = sdata->u.ibss.presp;
998 rcu_assign_pointer(sdata->u.ibss.presp, NULL);
999 sdata->vif.bss_conf.ibss_joined = false;
1000 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1001 BSS_CHANGED_IBSS);
1002 synchronize_rcu();
1003 kfree_skb(skb);
1005 skb_queue_purge(&sdata->skb_queue);
1006 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
1007 sdata->u.ibss.ssid_len = 0;
1009 del_timer_sync(&sdata->u.ibss.timer);
1011 mutex_unlock(&sdata->u.ibss.mtx);
1013 mutex_lock(&local->mtx);
1014 ieee80211_recalc_idle(sdata->local);
1015 mutex_unlock(&local->mtx);
1017 return 0;