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