a42aa61269eafd2d292628a3fe7de44940e13fdc
[pandora-kernel.git] / net / mac80211 / ibss.c
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>
9  *
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.
13  */
14
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>
24
25 #include "ieee80211_i.h"
26 #include "driver-ops.h"
27 #include "rate.h"
28
29 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
30 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
31 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
32
33 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
34 #define IEEE80211_IBSS_MERGE_DELAY 0x400000
35 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
36
37 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
38
39
40 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
41                                         struct ieee80211_mgmt *mgmt,
42                                         size_t len)
43 {
44         u16 auth_alg, auth_transaction, status_code;
45
46         lockdep_assert_held(&sdata->u.ibss.mtx);
47
48         if (len < 24 + 6)
49                 return;
50
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);
54
55         /*
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.
60          */
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);
64 }
65
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)
71 {
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];
82
83         lockdep_assert_held(&ifibss->mtx);
84
85         /* Reset own TSF to allow time synchronization work. */
86         drv_reset_tsf(local);
87
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);
95
96         if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
97                 sta_info_flush(sdata->local, sdata);
98
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);
103         }
104
105         memcpy(ifibss->bssid, bssid, ETH_ALEN);
106
107         sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
108
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);
112
113         sband = local->hw.wiphy->bands[chan->band];
114
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);
123         }
124
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);
136
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);
141
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);
149
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);
155         }
156
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;
163
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);
170         }
171
172         if (ifibss->ie_len)
173                 memcpy(skb_put(skb, ifibss->ie_len),
174                        ifibss->ie, ifibss->ie_len);
175
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 */
187         }
188
189         rcu_assign_pointer(ifibss->presp, skb);
190
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);
202
203         ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
204
205         ifibss->state = IEEE80211_IBSS_MLME_JOINED;
206         mod_timer(&ifibss->timer,
207                   round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
208
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);
213 }
214
215 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
216                                     struct ieee80211_bss *bss)
217 {
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;
224
225         lockdep_assert_held(&sdata->u.ibss.mtx);
226
227         if (beacon_int < 10)
228                 beacon_int = 10;
229
230         sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
231
232         basic_rates = 0;
233
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);
237
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;
243                         }
244                 }
245         }
246
247         __ieee80211_sta_join_ibss(sdata, cbss->bssid,
248                                   beacon_int,
249                                   cbss->channel,
250                                   basic_rates,
251                                   cbss->capability,
252                                   cbss->tsf);
253 }
254
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)
261 {
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;
271
272         if (elems->ds_params && elems->ds_params_len == 1)
273                 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
274                                                       band);
275         else
276                 freq = rx_status->freq;
277
278         channel = ieee80211_get_channel(local->hw.wiphy, freq);
279
280         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
281                 return;
282
283         if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
284             memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
285
286                 rcu_read_lock();
287                 sta = sta_info_get(sdata, mgmt->sa);
288
289                 if (elems->supp_rates) {
290                         supp_rates = ieee80211_sta_get_rates(local, elems,
291                                                              band);
292                         if (sta) {
293                                 u32 prev_rates;
294
295                                 prev_rates = sta->sta.supp_rates[band];
296                                 /* make sure mandatory rates are always added */
297                                 sta->sta.supp_rates[band] = supp_rates |
298                                         ieee80211_mandatory_rates(local, band);
299
300                                 if (sta->sta.supp_rates[band] != prev_rates) {
301 #ifdef CONFIG_MAC80211_IBSS_DEBUG
302                                         printk(KERN_DEBUG
303                                                 "%s: updated supp_rates set "
304                                                 "for %pM based on beacon"
305                                                 "/probe_resp (0x%x -> 0x%x)\n",
306                                                 sdata->name, sta->sta.addr,
307                                                 prev_rates,
308                                                 sta->sta.supp_rates[band]);
309 #endif
310                                         rate_control_rate_init(sta);
311                                 }
312                         } else
313                                 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
314                                                 mgmt->sa, supp_rates,
315                                                 GFP_ATOMIC);
316                 }
317
318                 if (sta && elems->wmm_info)
319                         set_sta_flags(sta, WLAN_STA_WME);
320
321                 rcu_read_unlock();
322         }
323
324         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
325                                         channel, beacon);
326         if (!bss)
327                 return;
328
329         cbss = container_of((void *)bss, struct cfg80211_bss, priv);
330
331         /* was just updated in ieee80211_bss_info_update */
332         beacon_timestamp = cbss->tsf;
333
334         /* check if we need to merge IBSS */
335
336         /* we use a fixed BSSID */
337         if (sdata->u.ibss.fixed_bssid)
338                 goto put_bss;
339
340         /* not an IBSS */
341         if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
342                 goto put_bss;
343
344         /* different channel */
345         if (cbss->channel != local->oper_channel)
346                 goto put_bss;
347
348         /* different SSID */
349         if (elems->ssid_len != sdata->u.ibss.ssid_len ||
350             memcmp(elems->ssid, sdata->u.ibss.ssid,
351                                 sdata->u.ibss.ssid_len))
352                 goto put_bss;
353
354         /* same BSSID */
355         if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
356                 goto put_bss;
357
358         if (rx_status->flag & RX_FLAG_TSFT) {
359                 /*
360                  * For correct IBSS merging we need mactime; since mactime is
361                  * defined as the time the first data symbol of the frame hits
362                  * the PHY, and the timestamp of the beacon is defined as "the
363                  * time that the data symbol containing the first bit of the
364                  * timestamp is transmitted to the PHY plus the transmitting
365                  * STA's delays through its local PHY from the MAC-PHY
366                  * interface to its interface with the WM" (802.11 11.1.2)
367                  * - equals the time this bit arrives at the receiver - we have
368                  * to take into account the offset between the two.
369                  *
370                  * E.g. at 1 MBit that means mactime is 192 usec earlier
371                  * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
372                  */
373                 int rate;
374
375                 if (rx_status->flag & RX_FLAG_HT)
376                         rate = 65; /* TODO: HT rates */
377                 else
378                         rate = local->hw.wiphy->bands[band]->
379                                 bitrates[rx_status->rate_idx].bitrate;
380
381                 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
382         } else {
383                 /*
384                  * second best option: get current TSF
385                  * (will return -1 if not supported)
386                  */
387                 rx_timestamp = drv_get_tsf(local);
388         }
389
390 #ifdef CONFIG_MAC80211_IBSS_DEBUG
391         printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
392                "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
393                mgmt->sa, mgmt->bssid,
394                (unsigned long long)rx_timestamp,
395                (unsigned long long)beacon_timestamp,
396                (unsigned long long)(rx_timestamp - beacon_timestamp),
397                jiffies);
398 #endif
399
400         /* give slow hardware some time to do the TSF sync */
401         if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
402                 goto put_bss;
403
404         if (beacon_timestamp > rx_timestamp) {
405 #ifdef CONFIG_MAC80211_IBSS_DEBUG
406                 printk(KERN_DEBUG "%s: beacon TSF higher than "
407                        "local TSF - IBSS merge with BSSID %pM\n",
408                        sdata->name, mgmt->bssid);
409 #endif
410                 ieee80211_sta_join_ibss(sdata, bss);
411                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
412                 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
413                                        supp_rates, GFP_KERNEL);
414         }
415
416  put_bss:
417         ieee80211_rx_bss_put(local, bss);
418 }
419
420 /*
421  * Add a new IBSS station, will also be called by the RX code when,
422  * in IBSS mode, receiving a frame from a yet-unknown station, hence
423  * must be callable in atomic context.
424  */
425 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
426                                         u8 *bssid,u8 *addr, u32 supp_rates,
427                                         gfp_t gfp)
428 {
429         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
430         struct ieee80211_local *local = sdata->local;
431         struct sta_info *sta;
432         int band = local->hw.conf.channel->band;
433
434         /*
435          * XXX: Consider removing the least recently used entry and
436          *      allow new one to be added.
437          */
438         if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
439                 if (net_ratelimit())
440                         printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
441                                sdata->name, addr);
442                 return NULL;
443         }
444
445         if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
446                 return NULL;
447
448         if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
449                 return NULL;
450
451 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
452         wiphy_debug(local->hw.wiphy, "Adding new IBSS station %pM (dev=%s)\n",
453                     addr, sdata->name);
454 #endif
455
456         sta = sta_info_alloc(sdata, addr, gfp);
457         if (!sta)
458                 return NULL;
459
460         sta->last_rx = jiffies;
461         set_sta_flags(sta, WLAN_STA_AUTHORIZED);
462
463         /* make sure mandatory rates are always added */
464         sta->sta.supp_rates[band] = supp_rates |
465                         ieee80211_mandatory_rates(local, band);
466
467         rate_control_rate_init(sta);
468
469         /* If it fails, maybe we raced another insertion? */
470         if (sta_info_insert(sta))
471                 return sta_info_get(sdata, addr);
472         return sta;
473 }
474
475 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
476 {
477         struct ieee80211_local *local = sdata->local;
478         int active = 0;
479         struct sta_info *sta;
480
481         lockdep_assert_held(&sdata->u.ibss.mtx);
482
483         rcu_read_lock();
484
485         list_for_each_entry_rcu(sta, &local->sta_list, list) {
486                 if (sta->sdata == sdata &&
487                     time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
488                                jiffies)) {
489                         active++;
490                         break;
491                 }
492         }
493
494         rcu_read_unlock();
495
496         return active;
497 }
498
499 /*
500  * This function is called with state == IEEE80211_IBSS_MLME_JOINED
501  */
502
503 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
504 {
505         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
506
507         lockdep_assert_held(&ifibss->mtx);
508
509         mod_timer(&ifibss->timer,
510                   round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
511
512         ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
513
514         if (time_before(jiffies, ifibss->last_scan_completed +
515                        IEEE80211_IBSS_MERGE_INTERVAL))
516                 return;
517
518         if (ieee80211_sta_active_ibss(sdata))
519                 return;
520
521         if (ifibss->fixed_channel)
522                 return;
523
524         printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
525                "IBSS networks with same SSID (merge)\n", sdata->name);
526
527         ieee80211_request_internal_scan(sdata,
528                         ifibss->ssid, ifibss->ssid_len,
529                         ifibss->fixed_channel ? ifibss->channel : NULL);
530 }
531
532 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
533 {
534         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
535         struct ieee80211_local *local = sdata->local;
536         struct ieee80211_supported_band *sband;
537         u8 bssid[ETH_ALEN];
538         u16 capability;
539         int i;
540
541         lockdep_assert_held(&ifibss->mtx);
542
543         if (ifibss->fixed_bssid) {
544                 memcpy(bssid, ifibss->bssid, ETH_ALEN);
545         } else {
546                 /* Generate random, not broadcast, locally administered BSSID. Mix in
547                  * own MAC address to make sure that devices that do not have proper
548                  * random number generator get different BSSID. */
549                 get_random_bytes(bssid, ETH_ALEN);
550                 for (i = 0; i < ETH_ALEN; i++)
551                         bssid[i] ^= sdata->vif.addr[i];
552                 bssid[0] &= ~0x01;
553                 bssid[0] |= 0x02;
554         }
555
556         printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
557                sdata->name, bssid);
558
559         sband = local->hw.wiphy->bands[ifibss->channel->band];
560
561         capability = WLAN_CAPABILITY_IBSS;
562
563         if (ifibss->privacy)
564                 capability |= WLAN_CAPABILITY_PRIVACY;
565         else
566                 sdata->drop_unencrypted = 0;
567
568         __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
569                                   ifibss->channel, ifibss->basic_rates,
570                                   capability, 0);
571 }
572
573 /*
574  * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
575  */
576
577 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
578 {
579         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
580         struct ieee80211_local *local = sdata->local;
581         struct cfg80211_bss *cbss;
582         struct ieee80211_channel *chan = NULL;
583         const u8 *bssid = NULL;
584         int active_ibss;
585         u16 capability;
586
587         lockdep_assert_held(&ifibss->mtx);
588
589         active_ibss = ieee80211_sta_active_ibss(sdata);
590 #ifdef CONFIG_MAC80211_IBSS_DEBUG
591         printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
592                sdata->name, active_ibss);
593 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
594
595         if (active_ibss)
596                 return;
597
598         capability = WLAN_CAPABILITY_IBSS;
599         if (ifibss->privacy)
600                 capability |= WLAN_CAPABILITY_PRIVACY;
601         if (ifibss->fixed_bssid)
602                 bssid = ifibss->bssid;
603         if (ifibss->fixed_channel)
604                 chan = ifibss->channel;
605         if (!is_zero_ether_addr(ifibss->bssid))
606                 bssid = ifibss->bssid;
607         cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
608                                 ifibss->ssid, ifibss->ssid_len,
609                                 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
610                                 capability);
611
612         if (cbss) {
613                 struct ieee80211_bss *bss;
614
615                 bss = (void *)cbss->priv;
616 #ifdef CONFIG_MAC80211_IBSS_DEBUG
617                 printk(KERN_DEBUG "   sta_find_ibss: selected %pM current "
618                        "%pM\n", cbss->bssid, ifibss->bssid);
619 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
620
621                 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
622                        " based on configured SSID\n",
623                        sdata->name, cbss->bssid);
624
625                 ieee80211_sta_join_ibss(sdata, bss);
626                 ieee80211_rx_bss_put(local, bss);
627                 return;
628         }
629
630 #ifdef CONFIG_MAC80211_IBSS_DEBUG
631         printk(KERN_DEBUG "   did not try to join ibss\n");
632 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
633
634         /* Selected IBSS not found in current scan results - try to scan */
635         if (time_after(jiffies, ifibss->last_scan_completed +
636                                         IEEE80211_SCAN_INTERVAL)) {
637                 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
638                        "join\n", sdata->name);
639
640                 ieee80211_request_internal_scan(sdata,
641                                 ifibss->ssid, ifibss->ssid_len,
642                                 ifibss->fixed_channel ? ifibss->channel : NULL);
643         } else {
644                 int interval = IEEE80211_SCAN_INTERVAL;
645
646                 if (time_after(jiffies, ifibss->ibss_join_req +
647                                IEEE80211_IBSS_JOIN_TIMEOUT)) {
648                         if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
649                                 ieee80211_sta_create_ibss(sdata);
650                                 return;
651                         }
652                         printk(KERN_DEBUG "%s: IBSS not allowed on"
653                                " %d MHz\n", sdata->name,
654                                local->hw.conf.channel->center_freq);
655
656                         /* No IBSS found - decrease scan interval and continue
657                          * scanning. */
658                         interval = IEEE80211_SCAN_INTERVAL_SLOW;
659                 }
660
661                 mod_timer(&ifibss->timer,
662                           round_jiffies(jiffies + interval));
663         }
664 }
665
666 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
667                                         struct sk_buff *req)
668 {
669         struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(req);
670         struct ieee80211_mgmt *mgmt = (void *)req->data;
671         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
672         struct ieee80211_local *local = sdata->local;
673         int tx_last_beacon, len = req->len;
674         struct sk_buff *skb;
675         struct ieee80211_mgmt *resp;
676         u8 *pos, *end;
677
678         lockdep_assert_held(&ifibss->mtx);
679
680         if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
681             len < 24 + 2 || !ifibss->presp)
682                 return;
683
684         tx_last_beacon = drv_tx_last_beacon(local);
685
686 #ifdef CONFIG_MAC80211_IBSS_DEBUG
687         printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
688                " (tx_last_beacon=%d)\n",
689                sdata->name, mgmt->sa, mgmt->da,
690                mgmt->bssid, tx_last_beacon);
691 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
692
693         if (!tx_last_beacon && !(rx_status->rx_flags & IEEE80211_RX_RA_MATCH))
694                 return;
695
696         if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
697             memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
698                 return;
699
700         end = ((u8 *) mgmt) + len;
701         pos = mgmt->u.probe_req.variable;
702         if (pos[0] != WLAN_EID_SSID ||
703             pos + 2 + pos[1] > end) {
704 #ifdef CONFIG_MAC80211_IBSS_DEBUG
705                 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
706                        "from %pM\n",
707                        sdata->name, mgmt->sa);
708 #endif
709                 return;
710         }
711         if (pos[1] != 0 &&
712             (pos[1] != ifibss->ssid_len ||
713              memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
714                 /* Ignore ProbeReq for foreign SSID */
715                 return;
716         }
717
718         /* Reply with ProbeResp */
719         skb = skb_copy(ifibss->presp, GFP_KERNEL);
720         if (!skb)
721                 return;
722
723         resp = (struct ieee80211_mgmt *) skb->data;
724         memcpy(resp->da, mgmt->sa, ETH_ALEN);
725 #ifdef CONFIG_MAC80211_IBSS_DEBUG
726         printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
727                sdata->name, resp->da);
728 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
729         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
730         ieee80211_tx_skb(sdata, skb);
731 }
732
733 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
734                                          struct ieee80211_mgmt *mgmt,
735                                          size_t len,
736                                          struct ieee80211_rx_status *rx_status)
737 {
738         size_t baselen;
739         struct ieee802_11_elems elems;
740
741         if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
742                 return; /* ignore ProbeResp to foreign address */
743
744         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
745         if (baselen > len)
746                 return;
747
748         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
749                                 &elems);
750
751         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
752 }
753
754 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
755                                      struct ieee80211_mgmt *mgmt,
756                                      size_t len,
757                                      struct ieee80211_rx_status *rx_status)
758 {
759         size_t baselen;
760         struct ieee802_11_elems elems;
761
762         /* Process beacon from the current BSS */
763         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
764         if (baselen > len)
765                 return;
766
767         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
768
769         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
770 }
771
772 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
773                                    struct sk_buff *skb)
774 {
775         struct ieee80211_rx_status *rx_status;
776         struct ieee80211_mgmt *mgmt;
777         u16 fc;
778
779         rx_status = IEEE80211_SKB_RXCB(skb);
780         mgmt = (struct ieee80211_mgmt *) skb->data;
781         fc = le16_to_cpu(mgmt->frame_control);
782
783         mutex_lock(&sdata->u.ibss.mtx);
784
785         if (!sdata->u.ibss.ssid_len)
786                 goto mgmt_out; /* not ready to merge yet */
787
788         switch (fc & IEEE80211_FCTL_STYPE) {
789         case IEEE80211_STYPE_PROBE_REQ:
790                 ieee80211_rx_mgmt_probe_req(sdata, skb);
791                 break;
792         case IEEE80211_STYPE_PROBE_RESP:
793                 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
794                                              rx_status);
795                 break;
796         case IEEE80211_STYPE_BEACON:
797                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
798                                          rx_status);
799                 break;
800         case IEEE80211_STYPE_AUTH:
801                 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
802                 break;
803         }
804
805  mgmt_out:
806         mutex_unlock(&sdata->u.ibss.mtx);
807 }
808
809 void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
810 {
811         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
812
813         mutex_lock(&ifibss->mtx);
814
815         /*
816          * Work could be scheduled after scan or similar
817          * when we aren't even joined (or trying) with a
818          * network.
819          */
820         if (!ifibss->ssid_len)
821                 goto out;
822
823         switch (ifibss->state) {
824         case IEEE80211_IBSS_MLME_SEARCH:
825                 ieee80211_sta_find_ibss(sdata);
826                 break;
827         case IEEE80211_IBSS_MLME_JOINED:
828                 ieee80211_sta_merge_ibss(sdata);
829                 break;
830         default:
831                 WARN_ON(1);
832                 break;
833         }
834
835  out:
836         mutex_unlock(&ifibss->mtx);
837 }
838
839 static void ieee80211_ibss_timer(unsigned long data)
840 {
841         struct ieee80211_sub_if_data *sdata =
842                 (struct ieee80211_sub_if_data *) data;
843         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
844         struct ieee80211_local *local = sdata->local;
845
846         if (local->quiescing) {
847                 ifibss->timer_running = true;
848                 return;
849         }
850
851         ieee80211_queue_work(&local->hw, &sdata->work);
852 }
853
854 #ifdef CONFIG_PM
855 void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
856 {
857         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
858
859         if (del_timer_sync(&ifibss->timer))
860                 ifibss->timer_running = true;
861 }
862
863 void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
864 {
865         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
866
867         if (ifibss->timer_running) {
868                 add_timer(&ifibss->timer);
869                 ifibss->timer_running = false;
870         }
871 }
872 #endif
873
874 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
875 {
876         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
877
878         setup_timer(&ifibss->timer, ieee80211_ibss_timer,
879                     (unsigned long) sdata);
880         mutex_init(&ifibss->mtx);
881 }
882
883 /* scan finished notification */
884 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
885 {
886         struct ieee80211_sub_if_data *sdata;
887
888         mutex_lock(&local->iflist_mtx);
889         list_for_each_entry(sdata, &local->interfaces, list) {
890                 if (!ieee80211_sdata_running(sdata))
891                         continue;
892                 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
893                         continue;
894                 sdata->u.ibss.last_scan_completed = jiffies;
895                 ieee80211_queue_work(&local->hw, &sdata->work);
896         }
897         mutex_unlock(&local->iflist_mtx);
898 }
899
900 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
901                         struct cfg80211_ibss_params *params)
902 {
903         struct sk_buff *skb;
904
905         skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
906                             36 /* bitrates */ +
907                             34 /* SSID */ +
908                             3  /* DS params */ +
909                             4  /* IBSS params */ +
910                             params->ie_len);
911         if (!skb)
912                 return -ENOMEM;
913
914         mutex_lock(&sdata->u.ibss.mtx);
915
916         if (params->bssid) {
917                 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
918                 sdata->u.ibss.fixed_bssid = true;
919         } else
920                 sdata->u.ibss.fixed_bssid = false;
921
922         sdata->u.ibss.privacy = params->privacy;
923         sdata->u.ibss.basic_rates = params->basic_rates;
924         memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
925                sizeof(params->mcast_rate));
926
927         sdata->vif.bss_conf.beacon_int = params->beacon_interval;
928
929         sdata->u.ibss.channel = params->channel;
930         sdata->u.ibss.fixed_channel = params->channel_fixed;
931
932         /* fix ourselves to that channel now already */
933         if (params->channel_fixed) {
934                 sdata->local->oper_channel = params->channel;
935                 WARN_ON(!ieee80211_set_channel_type(sdata->local, sdata,
936                                                     NL80211_CHAN_NO_HT));
937         }
938
939         if (params->ie) {
940                 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
941                                            GFP_KERNEL);
942                 if (sdata->u.ibss.ie)
943                         sdata->u.ibss.ie_len = params->ie_len;
944         }
945
946         sdata->u.ibss.skb = skb;
947         sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
948         sdata->u.ibss.ibss_join_req = jiffies;
949
950         memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
951         sdata->u.ibss.ssid_len = params->ssid_len;
952
953         mutex_unlock(&sdata->u.ibss.mtx);
954
955         mutex_lock(&sdata->local->mtx);
956         ieee80211_recalc_idle(sdata->local);
957         mutex_unlock(&sdata->local->mtx);
958
959         ieee80211_queue_work(&sdata->local->hw, &sdata->work);
960
961         return 0;
962 }
963
964 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
965 {
966         struct sk_buff *skb;
967         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
968         struct ieee80211_local *local = sdata->local;
969         struct cfg80211_bss *cbss;
970         u16 capability;
971         int active_ibss;
972
973         mutex_lock(&sdata->u.ibss.mtx);
974
975         active_ibss = ieee80211_sta_active_ibss(sdata);
976
977         if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
978                 capability = WLAN_CAPABILITY_IBSS;
979
980                 if (ifibss->privacy)
981                         capability |= WLAN_CAPABILITY_PRIVACY;
982
983                 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
984                                         ifibss->bssid, ifibss->ssid,
985                                         ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
986                                         WLAN_CAPABILITY_PRIVACY,
987                                         capability);
988
989                 if (cbss) {
990                         cfg80211_unlink_bss(local->hw.wiphy, cbss);
991                         cfg80211_put_bss(cbss);
992                 }
993         }
994
995         sta_info_flush(sdata->local, sdata);
996
997         /* remove beacon */
998         kfree(sdata->u.ibss.ie);
999         skb = sdata->u.ibss.presp;
1000         rcu_assign_pointer(sdata->u.ibss.presp, NULL);
1001         sdata->vif.bss_conf.ibss_joined = false;
1002         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1003                                                 BSS_CHANGED_IBSS);
1004         synchronize_rcu();
1005         kfree_skb(skb);
1006
1007         skb_queue_purge(&sdata->skb_queue);
1008         memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
1009         sdata->u.ibss.ssid_len = 0;
1010
1011         del_timer_sync(&sdata->u.ibss.timer);
1012
1013         mutex_unlock(&sdata->u.ibss.mtx);
1014
1015         mutex_lock(&local->mtx);
1016         ieee80211_recalc_idle(sdata->local);
1017         mutex_unlock(&local->mtx);
1018
1019         return 0;
1020 }