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