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