mac80211: tell driver about IBSS merge
[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/if_ether.h>
17 #include <linux/skbuff.h>
18 #include <linux/if_arp.h>
19 #include <linux/etherdevice.h>
20 #include <linux/rtnetlink.h>
21 #include <net/mac80211.h>
22 #include <asm/unaligned.h>
23
24 #include "ieee80211_i.h"
25 #include "driver-ops.h"
26 #include "rate.h"
27
28 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
29 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
30 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
31
32 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
33 #define IEEE80211_IBSS_MERGE_DELAY 0x400000
34 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
35
36 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
37
38
39 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
40                                         struct ieee80211_mgmt *mgmt,
41                                         size_t len)
42 {
43         u16 auth_alg, auth_transaction, status_code;
44
45         if (len < 24 + 6)
46                 return;
47
48         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
49         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
50         status_code = le16_to_cpu(mgmt->u.auth.status_code);
51
52         /*
53          * IEEE 802.11 standard does not require authentication in IBSS
54          * networks and most implementations do not seem to use it.
55          * However, try to reply to authentication attempts if someone
56          * has actually implemented this.
57          */
58         if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1)
59                 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
60                                     sdata->u.ibss.bssid, NULL, 0, 0);
61 }
62
63 static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
64                                       const u8 *bssid, const int beacon_int,
65                                       struct ieee80211_channel *chan,
66                                       const u32 basic_rates,
67                                       const u16 capability, u64 tsf)
68 {
69         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
70         struct ieee80211_local *local = sdata->local;
71         int rates, i;
72         struct sk_buff *skb;
73         struct ieee80211_mgmt *mgmt;
74         u8 *pos;
75         struct ieee80211_supported_band *sband;
76         struct cfg80211_bss *bss;
77         u32 bss_change;
78         u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
79
80         /* Reset own TSF to allow time synchronization work. */
81         drv_reset_tsf(local);
82
83         skb = ifibss->skb;
84         rcu_assign_pointer(ifibss->presp, NULL);
85         synchronize_rcu();
86         skb->data = skb->head;
87         skb->len = 0;
88         skb_reset_tail_pointer(skb);
89         skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
90
91         if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
92                 sta_info_flush(sdata->local, sdata);
93
94         /* if merging, indicate to driver that we leave the old IBSS */
95         if (sdata->vif.bss_conf.ibss_joined) {
96                 sdata->vif.bss_conf.ibss_joined = false;
97                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
98         }
99
100         memcpy(ifibss->bssid, bssid, ETH_ALEN);
101
102         sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
103
104         local->oper_channel = chan;
105         local->oper_channel_type = NL80211_CHAN_NO_HT;
106         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
107
108         sband = local->hw.wiphy->bands[chan->band];
109
110         /* build supported rates array */
111         pos = supp_rates;
112         for (i = 0; i < sband->n_bitrates; i++) {
113                 int rate = sband->bitrates[i].bitrate;
114                 u8 basic = 0;
115                 if (basic_rates & BIT(i))
116                         basic = 0x80;
117                 *pos++ = basic | (u8) (rate / 5);
118         }
119
120         /* Build IBSS probe response */
121         mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
122         memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
123         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
124                                           IEEE80211_STYPE_PROBE_RESP);
125         memset(mgmt->da, 0xff, ETH_ALEN);
126         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
127         memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
128         mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
129         mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
130         mgmt->u.beacon.capab_info = cpu_to_le16(capability);
131
132         pos = skb_put(skb, 2 + ifibss->ssid_len);
133         *pos++ = WLAN_EID_SSID;
134         *pos++ = ifibss->ssid_len;
135         memcpy(pos, ifibss->ssid, ifibss->ssid_len);
136
137         rates = sband->n_bitrates;
138         if (rates > 8)
139                 rates = 8;
140         pos = skb_put(skb, 2 + rates);
141         *pos++ = WLAN_EID_SUPP_RATES;
142         *pos++ = rates;
143         memcpy(pos, supp_rates, rates);
144
145         if (sband->band == IEEE80211_BAND_2GHZ) {
146                 pos = skb_put(skb, 2 + 1);
147                 *pos++ = WLAN_EID_DS_PARAMS;
148                 *pos++ = 1;
149                 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
150         }
151
152         pos = skb_put(skb, 2 + 2);
153         *pos++ = WLAN_EID_IBSS_PARAMS;
154         *pos++ = 2;
155         /* FIX: set ATIM window based on scan results */
156         *pos++ = 0;
157         *pos++ = 0;
158
159         if (sband->n_bitrates > 8) {
160                 rates = sband->n_bitrates - 8;
161                 pos = skb_put(skb, 2 + rates);
162                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
163                 *pos++ = rates;
164                 memcpy(pos, &supp_rates[8], rates);
165         }
166
167         if (ifibss->ie_len)
168                 memcpy(skb_put(skb, ifibss->ie_len),
169                        ifibss->ie, ifibss->ie_len);
170
171         rcu_assign_pointer(ifibss->presp, skb);
172
173         sdata->vif.bss_conf.beacon_int = beacon_int;
174         bss_change = BSS_CHANGED_BEACON_INT;
175         bss_change |= ieee80211_reset_erp_info(sdata);
176         bss_change |= BSS_CHANGED_BSSID;
177         bss_change |= BSS_CHANGED_BEACON;
178         bss_change |= BSS_CHANGED_BEACON_ENABLED;
179         bss_change |= BSS_CHANGED_IBSS;
180         sdata->vif.bss_conf.ibss_joined = true;
181         ieee80211_bss_info_change_notify(sdata, bss_change);
182
183         ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
184
185         ifibss->state = IEEE80211_IBSS_MLME_JOINED;
186         mod_timer(&ifibss->timer,
187                   round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
188
189         bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
190                                         mgmt, skb->len, 0, GFP_KERNEL);
191         cfg80211_put_bss(bss);
192         cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
193 }
194
195 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
196                                     struct ieee80211_bss *bss)
197 {
198         struct cfg80211_bss *cbss =
199                 container_of((void *)bss, struct cfg80211_bss, priv);
200         struct ieee80211_supported_band *sband;
201         u32 basic_rates;
202         int i, j;
203         u16 beacon_int = cbss->beacon_interval;
204
205         if (beacon_int < 10)
206                 beacon_int = 10;
207
208         sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
209
210         basic_rates = 0;
211
212         for (i = 0; i < bss->supp_rates_len; i++) {
213                 int rate = (bss->supp_rates[i] & 0x7f) * 5;
214                 bool is_basic = !!(bss->supp_rates[i] & 0x80);
215
216                 for (j = 0; j < sband->n_bitrates; j++) {
217                         if (sband->bitrates[j].bitrate == rate) {
218                                 if (is_basic)
219                                         basic_rates |= BIT(j);
220                                 break;
221                         }
222                 }
223         }
224
225         __ieee80211_sta_join_ibss(sdata, cbss->bssid,
226                                   beacon_int,
227                                   cbss->channel,
228                                   basic_rates,
229                                   cbss->capability,
230                                   cbss->tsf);
231 }
232
233 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
234                                   struct ieee80211_mgmt *mgmt,
235                                   size_t len,
236                                   struct ieee80211_rx_status *rx_status,
237                                   struct ieee802_11_elems *elems,
238                                   bool beacon)
239 {
240         struct ieee80211_local *local = sdata->local;
241         int freq;
242         struct cfg80211_bss *cbss;
243         struct ieee80211_bss *bss;
244         struct sta_info *sta;
245         struct ieee80211_channel *channel;
246         u64 beacon_timestamp, rx_timestamp;
247         u32 supp_rates = 0;
248         enum ieee80211_band band = rx_status->band;
249
250         if (elems->ds_params && elems->ds_params_len == 1)
251                 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
252         else
253                 freq = rx_status->freq;
254
255         channel = ieee80211_get_channel(local->hw.wiphy, freq);
256
257         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
258                 return;
259
260         if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
261             memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
262                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
263
264                 rcu_read_lock();
265
266                 sta = sta_info_get(sdata, mgmt->sa);
267                 if (sta) {
268                         u32 prev_rates;
269
270                         prev_rates = sta->sta.supp_rates[band];
271                         /* make sure mandatory rates are always added */
272                         sta->sta.supp_rates[band] = supp_rates |
273                                 ieee80211_mandatory_rates(local, band);
274
275                         if (sta->sta.supp_rates[band] != prev_rates) {
276 #ifdef CONFIG_MAC80211_IBSS_DEBUG
277                                 printk(KERN_DEBUG "%s: updated supp_rates set "
278                                     "for %pM based on beacon/probe_response "
279                                     "(0x%x -> 0x%x)\n",
280                                     sdata->name, sta->sta.addr,
281                                     prev_rates, sta->sta.supp_rates[band]);
282 #endif
283                                 rate_control_rate_init(sta);
284                         }
285                         rcu_read_unlock();
286                 } else {
287                         rcu_read_unlock();
288                         ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
289                                                supp_rates, GFP_KERNEL);
290                 }
291         }
292
293         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
294                                         channel, beacon);
295         if (!bss)
296                 return;
297
298         cbss = container_of((void *)bss, struct cfg80211_bss, priv);
299
300         /* was just updated in ieee80211_bss_info_update */
301         beacon_timestamp = cbss->tsf;
302
303         /* check if we need to merge IBSS */
304
305         /* we use a fixed BSSID */
306         if (sdata->u.ibss.fixed_bssid)
307                 goto put_bss;
308
309         /* not an IBSS */
310         if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
311                 goto put_bss;
312
313         /* different channel */
314         if (cbss->channel != local->oper_channel)
315                 goto put_bss;
316
317         /* different SSID */
318         if (elems->ssid_len != sdata->u.ibss.ssid_len ||
319             memcmp(elems->ssid, sdata->u.ibss.ssid,
320                                 sdata->u.ibss.ssid_len))
321                 goto put_bss;
322
323         /* same BSSID */
324         if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
325                 goto put_bss;
326
327         if (rx_status->flag & RX_FLAG_TSFT) {
328                 /*
329                  * For correct IBSS merging we need mactime; since mactime is
330                  * defined as the time the first data symbol of the frame hits
331                  * the PHY, and the timestamp of the beacon is defined as "the
332                  * time that the data symbol containing the first bit of the
333                  * timestamp is transmitted to the PHY plus the transmitting
334                  * STA's delays through its local PHY from the MAC-PHY
335                  * interface to its interface with the WM" (802.11 11.1.2)
336                  * - equals the time this bit arrives at the receiver - we have
337                  * to take into account the offset between the two.
338                  *
339                  * E.g. at 1 MBit that means mactime is 192 usec earlier
340                  * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
341                  */
342                 int rate;
343
344                 if (rx_status->flag & RX_FLAG_HT)
345                         rate = 65; /* TODO: HT rates */
346                 else
347                         rate = local->hw.wiphy->bands[band]->
348                                 bitrates[rx_status->rate_idx].bitrate;
349
350                 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
351         } else {
352                 /*
353                  * second best option: get current TSF
354                  * (will return -1 if not supported)
355                  */
356                 rx_timestamp = drv_get_tsf(local);
357         }
358
359 #ifdef CONFIG_MAC80211_IBSS_DEBUG
360         printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
361                "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
362                mgmt->sa, mgmt->bssid,
363                (unsigned long long)rx_timestamp,
364                (unsigned long long)beacon_timestamp,
365                (unsigned long long)(rx_timestamp - beacon_timestamp),
366                jiffies);
367 #endif
368
369         /* give slow hardware some time to do the TSF sync */
370         if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
371                 goto put_bss;
372
373         if (beacon_timestamp > rx_timestamp) {
374 #ifdef CONFIG_MAC80211_IBSS_DEBUG
375                 printk(KERN_DEBUG "%s: beacon TSF higher than "
376                        "local TSF - IBSS merge with BSSID %pM\n",
377                        sdata->name, mgmt->bssid);
378 #endif
379                 ieee80211_sta_join_ibss(sdata, bss);
380                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
381                 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
382                                        supp_rates, GFP_KERNEL);
383         }
384
385  put_bss:
386         ieee80211_rx_bss_put(local, bss);
387 }
388
389 /*
390  * Add a new IBSS station, will also be called by the RX code when,
391  * in IBSS mode, receiving a frame from a yet-unknown station, hence
392  * must be callable in atomic context.
393  */
394 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
395                                         u8 *bssid,u8 *addr, u32 supp_rates,
396                                         gfp_t gfp)
397 {
398         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
399         struct ieee80211_local *local = sdata->local;
400         struct sta_info *sta;
401         int band = local->hw.conf.channel->band;
402
403         /*
404          * XXX: Consider removing the least recently used entry and
405          *      allow new one to be added.
406          */
407         if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
408                 if (net_ratelimit())
409                         printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
410                                sdata->name, addr);
411                 return NULL;
412         }
413
414         if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
415                 return NULL;
416
417         if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
418                 return NULL;
419
420 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
421         printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n",
422                wiphy_name(local->hw.wiphy), addr, sdata->name);
423 #endif
424
425         sta = sta_info_alloc(sdata, addr, gfp);
426         if (!sta)
427                 return NULL;
428
429         set_sta_flags(sta, WLAN_STA_AUTHORIZED);
430
431         /* make sure mandatory rates are always added */
432         sta->sta.supp_rates[band] = supp_rates |
433                         ieee80211_mandatory_rates(local, band);
434
435         rate_control_rate_init(sta);
436
437         /* If it fails, maybe we raced another insertion? */
438         if (sta_info_insert(sta))
439                 return sta_info_get(sdata, addr);
440         return sta;
441 }
442
443 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
444 {
445         struct ieee80211_local *local = sdata->local;
446         int active = 0;
447         struct sta_info *sta;
448
449         rcu_read_lock();
450
451         list_for_each_entry_rcu(sta, &local->sta_list, list) {
452                 if (sta->sdata == sdata &&
453                     time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
454                                jiffies)) {
455                         active++;
456                         break;
457                 }
458         }
459
460         rcu_read_unlock();
461
462         return active;
463 }
464
465 /*
466  * This function is called with state == IEEE80211_IBSS_MLME_JOINED
467  */
468
469 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
470 {
471         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
472
473         mod_timer(&ifibss->timer,
474                   round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
475
476         ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
477
478         if (time_before(jiffies, ifibss->last_scan_completed +
479                        IEEE80211_IBSS_MERGE_INTERVAL))
480                 return;
481
482         if (ieee80211_sta_active_ibss(sdata))
483                 return;
484
485         if (ifibss->fixed_channel)
486                 return;
487
488         printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
489                "IBSS networks with same SSID (merge)\n", sdata->name);
490
491         ieee80211_request_internal_scan(sdata, ifibss->ssid, ifibss->ssid_len);
492 }
493
494 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
495 {
496         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
497         struct ieee80211_local *local = sdata->local;
498         struct ieee80211_supported_band *sband;
499         u8 bssid[ETH_ALEN];
500         u16 capability;
501         int i;
502
503         if (ifibss->fixed_bssid) {
504                 memcpy(bssid, ifibss->bssid, ETH_ALEN);
505         } else {
506                 /* Generate random, not broadcast, locally administered BSSID. Mix in
507                  * own MAC address to make sure that devices that do not have proper
508                  * random number generator get different BSSID. */
509                 get_random_bytes(bssid, ETH_ALEN);
510                 for (i = 0; i < ETH_ALEN; i++)
511                         bssid[i] ^= sdata->vif.addr[i];
512                 bssid[0] &= ~0x01;
513                 bssid[0] |= 0x02;
514         }
515
516         printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
517                sdata->name, bssid);
518
519         sband = local->hw.wiphy->bands[ifibss->channel->band];
520
521         capability = WLAN_CAPABILITY_IBSS;
522
523         if (ifibss->privacy)
524                 capability |= WLAN_CAPABILITY_PRIVACY;
525         else
526                 sdata->drop_unencrypted = 0;
527
528         __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
529                                   ifibss->channel, 3, /* first two are basic */
530                                   capability, 0);
531 }
532
533 /*
534  * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
535  */
536
537 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
538 {
539         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
540         struct ieee80211_local *local = sdata->local;
541         struct cfg80211_bss *cbss;
542         struct ieee80211_channel *chan = NULL;
543         const u8 *bssid = NULL;
544         int active_ibss;
545         u16 capability;
546
547         active_ibss = ieee80211_sta_active_ibss(sdata);
548 #ifdef CONFIG_MAC80211_IBSS_DEBUG
549         printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
550                sdata->name, active_ibss);
551 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
552
553         if (active_ibss)
554                 return;
555
556         capability = WLAN_CAPABILITY_IBSS;
557         if (ifibss->privacy)
558                 capability |= WLAN_CAPABILITY_PRIVACY;
559         if (ifibss->fixed_bssid)
560                 bssid = ifibss->bssid;
561         if (ifibss->fixed_channel)
562                 chan = ifibss->channel;
563         if (!is_zero_ether_addr(ifibss->bssid))
564                 bssid = ifibss->bssid;
565         cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
566                                 ifibss->ssid, ifibss->ssid_len,
567                                 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
568                                 capability);
569
570         if (cbss) {
571                 struct ieee80211_bss *bss;
572
573                 bss = (void *)cbss->priv;
574 #ifdef CONFIG_MAC80211_IBSS_DEBUG
575                 printk(KERN_DEBUG "   sta_find_ibss: selected %pM current "
576                        "%pM\n", cbss->bssid, ifibss->bssid);
577 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
578
579                 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
580                        " based on configured SSID\n",
581                        sdata->name, cbss->bssid);
582
583                 ieee80211_sta_join_ibss(sdata, bss);
584                 ieee80211_rx_bss_put(local, bss);
585                 return;
586         }
587
588 #ifdef CONFIG_MAC80211_IBSS_DEBUG
589         printk(KERN_DEBUG "   did not try to join ibss\n");
590 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
591
592         /* Selected IBSS not found in current scan results - try to scan */
593         if (time_after(jiffies, ifibss->last_scan_completed +
594                                         IEEE80211_SCAN_INTERVAL)) {
595                 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
596                        "join\n", sdata->name);
597
598                 ieee80211_request_internal_scan(sdata, ifibss->ssid,
599                                                 ifibss->ssid_len);
600         } else {
601                 int interval = IEEE80211_SCAN_INTERVAL;
602
603                 if (time_after(jiffies, ifibss->ibss_join_req +
604                                IEEE80211_IBSS_JOIN_TIMEOUT)) {
605                         if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
606                                 ieee80211_sta_create_ibss(sdata);
607                                 return;
608                         }
609                         printk(KERN_DEBUG "%s: IBSS not allowed on"
610                                " %d MHz\n", sdata->name,
611                                local->hw.conf.channel->center_freq);
612
613                         /* No IBSS found - decrease scan interval and continue
614                          * scanning. */
615                         interval = IEEE80211_SCAN_INTERVAL_SLOW;
616                 }
617
618                 mod_timer(&ifibss->timer,
619                           round_jiffies(jiffies + interval));
620         }
621 }
622
623 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
624                                         struct ieee80211_mgmt *mgmt,
625                                         size_t len)
626 {
627         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
628         struct ieee80211_local *local = sdata->local;
629         int tx_last_beacon;
630         struct sk_buff *skb;
631         struct ieee80211_mgmt *resp;
632         u8 *pos, *end;
633
634         if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
635             len < 24 + 2 || !ifibss->presp)
636                 return;
637
638         tx_last_beacon = drv_tx_last_beacon(local);
639
640 #ifdef CONFIG_MAC80211_IBSS_DEBUG
641         printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
642                " (tx_last_beacon=%d)\n",
643                sdata->name, mgmt->sa, mgmt->da,
644                mgmt->bssid, tx_last_beacon);
645 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
646
647         if (!tx_last_beacon)
648                 return;
649
650         if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
651             memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
652                 return;
653
654         end = ((u8 *) mgmt) + len;
655         pos = mgmt->u.probe_req.variable;
656         if (pos[0] != WLAN_EID_SSID ||
657             pos + 2 + pos[1] > end) {
658 #ifdef CONFIG_MAC80211_IBSS_DEBUG
659                 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
660                        "from %pM\n",
661                        sdata->name, mgmt->sa);
662 #endif
663                 return;
664         }
665         if (pos[1] != 0 &&
666             (pos[1] != ifibss->ssid_len ||
667              memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
668                 /* Ignore ProbeReq for foreign SSID */
669                 return;
670         }
671
672         /* Reply with ProbeResp */
673         skb = skb_copy(ifibss->presp, GFP_KERNEL);
674         if (!skb)
675                 return;
676
677         resp = (struct ieee80211_mgmt *) skb->data;
678         memcpy(resp->da, mgmt->sa, ETH_ALEN);
679 #ifdef CONFIG_MAC80211_IBSS_DEBUG
680         printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
681                sdata->name, resp->da);
682 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
683         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
684         ieee80211_tx_skb(sdata, skb);
685 }
686
687 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
688                                          struct ieee80211_mgmt *mgmt,
689                                          size_t len,
690                                          struct ieee80211_rx_status *rx_status)
691 {
692         size_t baselen;
693         struct ieee802_11_elems elems;
694
695         if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
696                 return; /* ignore ProbeResp to foreign address */
697
698         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
699         if (baselen > len)
700                 return;
701
702         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
703                                 &elems);
704
705         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
706 }
707
708 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
709                                      struct ieee80211_mgmt *mgmt,
710                                      size_t len,
711                                      struct ieee80211_rx_status *rx_status)
712 {
713         size_t baselen;
714         struct ieee802_11_elems elems;
715
716         /* Process beacon from the current BSS */
717         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
718         if (baselen > len)
719                 return;
720
721         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
722
723         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
724 }
725
726 static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
727                                           struct sk_buff *skb)
728 {
729         struct ieee80211_rx_status *rx_status;
730         struct ieee80211_mgmt *mgmt;
731         u16 fc;
732
733         rx_status = IEEE80211_SKB_RXCB(skb);
734         mgmt = (struct ieee80211_mgmt *) skb->data;
735         fc = le16_to_cpu(mgmt->frame_control);
736
737         switch (fc & IEEE80211_FCTL_STYPE) {
738         case IEEE80211_STYPE_PROBE_REQ:
739                 ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
740                 break;
741         case IEEE80211_STYPE_PROBE_RESP:
742                 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
743                                              rx_status);
744                 break;
745         case IEEE80211_STYPE_BEACON:
746                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
747                                          rx_status);
748                 break;
749         case IEEE80211_STYPE_AUTH:
750                 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
751                 break;
752         }
753
754         kfree_skb(skb);
755 }
756
757 static void ieee80211_ibss_work(struct work_struct *work)
758 {
759         struct ieee80211_sub_if_data *sdata =
760                 container_of(work, struct ieee80211_sub_if_data, u.ibss.work);
761         struct ieee80211_local *local = sdata->local;
762         struct ieee80211_if_ibss *ifibss;
763         struct sk_buff *skb;
764
765         if (WARN_ON(local->suspended))
766                 return;
767
768         if (!ieee80211_sdata_running(sdata))
769                 return;
770
771         if (local->scanning)
772                 return;
773
774         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_ADHOC))
775                 return;
776         ifibss = &sdata->u.ibss;
777
778         while ((skb = skb_dequeue(&ifibss->skb_queue)))
779                 ieee80211_ibss_rx_queued_mgmt(sdata, skb);
780
781         if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request))
782                 return;
783
784         switch (ifibss->state) {
785         case IEEE80211_IBSS_MLME_SEARCH:
786                 ieee80211_sta_find_ibss(sdata);
787                 break;
788         case IEEE80211_IBSS_MLME_JOINED:
789                 ieee80211_sta_merge_ibss(sdata);
790                 break;
791         default:
792                 WARN_ON(1);
793                 break;
794         }
795 }
796
797 static void ieee80211_ibss_timer(unsigned long data)
798 {
799         struct ieee80211_sub_if_data *sdata =
800                 (struct ieee80211_sub_if_data *) data;
801         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
802         struct ieee80211_local *local = sdata->local;
803
804         if (local->quiescing) {
805                 ifibss->timer_running = true;
806                 return;
807         }
808
809         set_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request);
810         ieee80211_queue_work(&local->hw, &ifibss->work);
811 }
812
813 #ifdef CONFIG_PM
814 void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
815 {
816         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
817
818         cancel_work_sync(&ifibss->work);
819         if (del_timer_sync(&ifibss->timer))
820                 ifibss->timer_running = true;
821 }
822
823 void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
824 {
825         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
826
827         if (ifibss->timer_running) {
828                 add_timer(&ifibss->timer);
829                 ifibss->timer_running = false;
830         }
831 }
832 #endif
833
834 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
835 {
836         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
837
838         INIT_WORK(&ifibss->work, ieee80211_ibss_work);
839         setup_timer(&ifibss->timer, ieee80211_ibss_timer,
840                     (unsigned long) sdata);
841         skb_queue_head_init(&ifibss->skb_queue);
842 }
843
844 /* scan finished notification */
845 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
846 {
847         struct ieee80211_sub_if_data *sdata;
848
849         mutex_lock(&local->iflist_mtx);
850         list_for_each_entry(sdata, &local->interfaces, list) {
851                 if (!ieee80211_sdata_running(sdata))
852                         continue;
853                 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
854                         continue;
855                 if (!sdata->u.ibss.ssid_len)
856                         continue;
857                 sdata->u.ibss.last_scan_completed = jiffies;
858                 mod_timer(&sdata->u.ibss.timer, 0);
859         }
860         mutex_unlock(&local->iflist_mtx);
861 }
862
863 ieee80211_rx_result
864 ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
865 {
866         struct ieee80211_local *local = sdata->local;
867         struct ieee80211_mgmt *mgmt;
868         u16 fc;
869
870         if (skb->len < 24)
871                 return RX_DROP_MONITOR;
872
873         mgmt = (struct ieee80211_mgmt *) skb->data;
874         fc = le16_to_cpu(mgmt->frame_control);
875
876         switch (fc & IEEE80211_FCTL_STYPE) {
877         case IEEE80211_STYPE_PROBE_RESP:
878         case IEEE80211_STYPE_BEACON:
879         case IEEE80211_STYPE_PROBE_REQ:
880         case IEEE80211_STYPE_AUTH:
881                 skb_queue_tail(&sdata->u.ibss.skb_queue, skb);
882                 ieee80211_queue_work(&local->hw, &sdata->u.ibss.work);
883                 return RX_QUEUED;
884         }
885
886         return RX_DROP_MONITOR;
887 }
888
889 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
890                         struct cfg80211_ibss_params *params)
891 {
892         struct sk_buff *skb;
893
894         if (params->bssid) {
895                 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
896                 sdata->u.ibss.fixed_bssid = true;
897         } else
898                 sdata->u.ibss.fixed_bssid = false;
899
900         sdata->u.ibss.privacy = params->privacy;
901
902         sdata->vif.bss_conf.beacon_int = params->beacon_interval;
903
904         sdata->u.ibss.channel = params->channel;
905         sdata->u.ibss.fixed_channel = params->channel_fixed;
906
907         if (params->ie) {
908                 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
909                                            GFP_KERNEL);
910                 if (sdata->u.ibss.ie)
911                         sdata->u.ibss.ie_len = params->ie_len;
912         }
913
914         skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
915                             36 /* bitrates */ +
916                             34 /* SSID */ +
917                             3  /* DS params */ +
918                             4  /* IBSS params */ +
919                             params->ie_len);
920         if (!skb)
921                 return -ENOMEM;
922
923         sdata->u.ibss.skb = skb;
924         sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
925         sdata->u.ibss.ibss_join_req = jiffies;
926
927         memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
928
929         /*
930          * The ssid_len setting below is used to see whether
931          * we are active, and we need all other settings
932          * before that may get visible.
933          */
934         mb();
935
936         sdata->u.ibss.ssid_len = params->ssid_len;
937
938         ieee80211_recalc_idle(sdata->local);
939
940         set_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
941         ieee80211_queue_work(&sdata->local->hw, &sdata->u.ibss.work);
942
943         return 0;
944 }
945
946 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
947 {
948         struct sk_buff *skb;
949
950         del_timer_sync(&sdata->u.ibss.timer);
951         clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
952         cancel_work_sync(&sdata->u.ibss.work);
953         clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
954
955         sta_info_flush(sdata->local, sdata);
956
957         /* remove beacon */
958         kfree(sdata->u.ibss.ie);
959         skb = sdata->u.ibss.presp;
960         rcu_assign_pointer(sdata->u.ibss.presp, NULL);
961         sdata->vif.bss_conf.ibss_joined = false;
962         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
963                                                 BSS_CHANGED_IBSS);
964         synchronize_rcu();
965         kfree_skb(skb);
966
967         skb_queue_purge(&sdata->u.ibss.skb_queue);
968         memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
969         sdata->u.ibss.ssid_len = 0;
970
971         ieee80211_recalc_idle(sdata->local);
972
973         return 0;
974 }