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_INACTIVITY_LIMIT (60 * HZ)
35 
36 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
37 
38 
ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)39 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
40 					struct ieee80211_mgmt *mgmt,
41 					size_t len)
42 {
43 	u16 auth_alg, auth_transaction;
44 
45 	lockdep_assert_held(&sdata->u.ibss.mtx);
46 
47 	if (len < 24 + 6)
48 		return;
49 
50 	auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
51 	auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
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 
__ieee80211_sta_join_ibss(struct ieee80211_sub_if_data * sdata,const u8 * bssid,const int beacon_int,struct ieee80211_channel * chan,const u32 basic_rates,const u16 capability,u64 tsf)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 	enum nl80211_channel_type channel_type;
81 
82 	lockdep_assert_held(&ifibss->mtx);
83 
84 	/* Reset own TSF to allow time synchronization work. */
85 	drv_reset_tsf(local, sdata);
86 
87 	skb = ifibss->skb;
88 	RCU_INIT_POINTER(ifibss->presp, NULL);
89 	synchronize_rcu();
90 	skb->data = skb->head;
91 	skb->len = 0;
92 	skb_reset_tail_pointer(skb);
93 	skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
94 
95 	if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
96 		sta_info_flush(sdata->local, sdata);
97 
98 	/* if merging, indicate to driver that we leave the old IBSS */
99 	if (sdata->vif.bss_conf.ibss_joined) {
100 		sdata->vif.bss_conf.ibss_joined = false;
101 		netif_carrier_off(sdata->dev);
102 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
103 	}
104 
105 	memcpy(ifibss->bssid, bssid, ETH_ALEN);
106 
107 	sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
108 
109 	local->oper_channel = chan;
110 	channel_type = ifibss->channel_type;
111 	if (channel_type > NL80211_CHAN_HT20 &&
112 	    !cfg80211_can_beacon_sec_chan(local->hw.wiphy, chan, channel_type))
113 		channel_type = NL80211_CHAN_HT20;
114 	if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
115 		/* can only fail due to HT40+/- mismatch */
116 		channel_type = NL80211_CHAN_HT20;
117 		WARN_ON(!ieee80211_set_channel_type(local, sdata,
118 						    NL80211_CHAN_HT20));
119 	}
120 	ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
121 
122 	sband = local->hw.wiphy->bands[chan->band];
123 
124 	/* build supported rates array */
125 	pos = supp_rates;
126 	for (i = 0; i < sband->n_bitrates; i++) {
127 		int rate = sband->bitrates[i].bitrate;
128 		u8 basic = 0;
129 		if (basic_rates & BIT(i))
130 			basic = 0x80;
131 		*pos++ = basic | (u8) (rate / 5);
132 	}
133 
134 	/* Build IBSS probe response */
135 	mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
136 	memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
137 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
138 					  IEEE80211_STYPE_PROBE_RESP);
139 	memset(mgmt->da, 0xff, ETH_ALEN);
140 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
141 	memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
142 	mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
143 	mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
144 	mgmt->u.beacon.capab_info = cpu_to_le16(capability);
145 
146 	pos = skb_put(skb, 2 + ifibss->ssid_len);
147 	*pos++ = WLAN_EID_SSID;
148 	*pos++ = ifibss->ssid_len;
149 	memcpy(pos, ifibss->ssid, ifibss->ssid_len);
150 
151 	rates = sband->n_bitrates;
152 	if (rates > 8)
153 		rates = 8;
154 	pos = skb_put(skb, 2 + rates);
155 	*pos++ = WLAN_EID_SUPP_RATES;
156 	*pos++ = rates;
157 	memcpy(pos, supp_rates, rates);
158 
159 	if (sband->band == IEEE80211_BAND_2GHZ) {
160 		pos = skb_put(skb, 2 + 1);
161 		*pos++ = WLAN_EID_DS_PARAMS;
162 		*pos++ = 1;
163 		*pos++ = ieee80211_frequency_to_channel(chan->center_freq);
164 	}
165 
166 	pos = skb_put(skb, 2 + 2);
167 	*pos++ = WLAN_EID_IBSS_PARAMS;
168 	*pos++ = 2;
169 	/* FIX: set ATIM window based on scan results */
170 	*pos++ = 0;
171 	*pos++ = 0;
172 
173 	if (sband->n_bitrates > 8) {
174 		rates = sband->n_bitrates - 8;
175 		pos = skb_put(skb, 2 + rates);
176 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
177 		*pos++ = rates;
178 		memcpy(pos, &supp_rates[8], rates);
179 	}
180 
181 	if (ifibss->ie_len)
182 		memcpy(skb_put(skb, ifibss->ie_len),
183 		       ifibss->ie, ifibss->ie_len);
184 
185 	/* add HT capability and information IEs */
186 	if (channel_type && sband->ht_cap.ht_supported) {
187 		pos = skb_put(skb, 4 +
188 				   sizeof(struct ieee80211_ht_cap) +
189 				   sizeof(struct ieee80211_ht_info));
190 		pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
191 						sband->ht_cap.cap);
192 		pos = ieee80211_ie_build_ht_info(pos,
193 						 &sband->ht_cap,
194 						 chan,
195 						 channel_type);
196 	}
197 
198 	if (local->hw.queues >= 4) {
199 		pos = skb_put(skb, 9);
200 		*pos++ = WLAN_EID_VENDOR_SPECIFIC;
201 		*pos++ = 7; /* len */
202 		*pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
203 		*pos++ = 0x50;
204 		*pos++ = 0xf2;
205 		*pos++ = 2; /* WME */
206 		*pos++ = 0; /* WME info */
207 		*pos++ = 1; /* WME ver */
208 		*pos++ = 0; /* U-APSD no in use */
209 	}
210 
211 	rcu_assign_pointer(ifibss->presp, skb);
212 
213 	sdata->vif.bss_conf.beacon_int = beacon_int;
214 	sdata->vif.bss_conf.basic_rates = basic_rates;
215 	bss_change = BSS_CHANGED_BEACON_INT;
216 	bss_change |= ieee80211_reset_erp_info(sdata);
217 	bss_change |= BSS_CHANGED_BSSID;
218 	bss_change |= BSS_CHANGED_BEACON;
219 	bss_change |= BSS_CHANGED_BEACON_ENABLED;
220 	bss_change |= BSS_CHANGED_BASIC_RATES;
221 	bss_change |= BSS_CHANGED_HT;
222 	bss_change |= BSS_CHANGED_IBSS;
223 	sdata->vif.bss_conf.ibss_joined = true;
224 	ieee80211_bss_info_change_notify(sdata, bss_change);
225 
226 	ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
227 
228 	ifibss->state = IEEE80211_IBSS_MLME_JOINED;
229 	mod_timer(&ifibss->timer,
230 		  round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
231 
232 	bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
233 					mgmt, skb->len, 0, GFP_KERNEL);
234 	cfg80211_put_bss(bss);
235 	netif_carrier_on(sdata->dev);
236 	cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
237 }
238 
ieee80211_sta_join_ibss(struct ieee80211_sub_if_data * sdata,struct ieee80211_bss * bss)239 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
240 				    struct ieee80211_bss *bss)
241 {
242 	struct cfg80211_bss *cbss =
243 		container_of((void *)bss, struct cfg80211_bss, priv);
244 	struct ieee80211_supported_band *sband;
245 	u32 basic_rates;
246 	int i, j;
247 	u16 beacon_int = cbss->beacon_interval;
248 
249 	lockdep_assert_held(&sdata->u.ibss.mtx);
250 
251 	if (beacon_int < 10)
252 		beacon_int = 10;
253 
254 	sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
255 
256 	basic_rates = 0;
257 
258 	for (i = 0; i < bss->supp_rates_len; i++) {
259 		int rate = (bss->supp_rates[i] & 0x7f) * 5;
260 		bool is_basic = !!(bss->supp_rates[i] & 0x80);
261 
262 		for (j = 0; j < sband->n_bitrates; j++) {
263 			if (sband->bitrates[j].bitrate == rate) {
264 				if (is_basic)
265 					basic_rates |= BIT(j);
266 				break;
267 			}
268 		}
269 	}
270 
271 	__ieee80211_sta_join_ibss(sdata, cbss->bssid,
272 				  beacon_int,
273 				  cbss->channel,
274 				  basic_rates,
275 				  cbss->capability,
276 				  cbss->tsf);
277 }
278 
ieee80211_ibss_finish_sta(struct sta_info * sta)279 static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
280 	__acquires(RCU)
281 {
282 	struct ieee80211_sub_if_data *sdata = sta->sdata;
283 	u8 addr[ETH_ALEN];
284 
285 	memcpy(addr, sta->sta.addr, ETH_ALEN);
286 
287 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
288 	wiphy_debug(sdata->local->hw.wiphy,
289 		    "Adding new IBSS station %pM (dev=%s)\n",
290 		    addr, sdata->name);
291 #endif
292 
293 	sta_info_move_state(sta, IEEE80211_STA_AUTH);
294 	sta_info_move_state(sta, IEEE80211_STA_ASSOC);
295 	sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
296 
297 	rate_control_rate_init(sta);
298 
299 	/* If it fails, maybe we raced another insertion? */
300 	if (sta_info_insert_rcu(sta))
301 		return sta_info_get(sdata, addr);
302 	return sta;
303 }
304 
305 static struct sta_info *
ieee80211_ibss_add_sta(struct ieee80211_sub_if_data * sdata,const u8 * bssid,const u8 * addr,u32 supp_rates)306 ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
307 		       const u8 *bssid, const u8 *addr,
308 		       u32 supp_rates)
309 	__acquires(RCU)
310 {
311 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
312 	struct ieee80211_local *local = sdata->local;
313 	struct sta_info *sta;
314 	int band = local->hw.conf.channel->band;
315 
316 	/*
317 	 * XXX: Consider removing the least recently used entry and
318 	 * 	allow new one to be added.
319 	 */
320 	if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
321 		if (net_ratelimit())
322 			printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
323 			       sdata->name, addr);
324 		rcu_read_lock();
325 		return NULL;
326 	}
327 
328 	if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
329 		rcu_read_lock();
330 		return NULL;
331 	}
332 
333 	if (compare_ether_addr(bssid, sdata->u.ibss.bssid)) {
334 		rcu_read_lock();
335 		return NULL;
336 	}
337 
338 	sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
339 	if (!sta) {
340 		rcu_read_lock();
341 		return NULL;
342 	}
343 
344 	sta->last_rx = jiffies;
345 
346 	/* make sure mandatory rates are always added */
347 	sta->sta.supp_rates[band] = supp_rates |
348 			ieee80211_mandatory_rates(local, band);
349 
350 	return ieee80211_ibss_finish_sta(sta);
351 }
352 
ieee80211_rx_bss_info(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len,struct ieee80211_rx_status * rx_status,struct ieee802_11_elems * elems,bool beacon)353 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
354 				  struct ieee80211_mgmt *mgmt,
355 				  size_t len,
356 				  struct ieee80211_rx_status *rx_status,
357 				  struct ieee802_11_elems *elems,
358 				  bool beacon)
359 {
360 	struct ieee80211_local *local = sdata->local;
361 	int freq;
362 	struct cfg80211_bss *cbss;
363 	struct ieee80211_bss *bss;
364 	struct sta_info *sta;
365 	struct ieee80211_channel *channel;
366 	u64 beacon_timestamp, rx_timestamp;
367 	u32 supp_rates = 0;
368 	enum ieee80211_band band = rx_status->band;
369 	struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
370 	bool rates_updated = false;
371 
372 	if (elems->ds_params && elems->ds_params_len == 1)
373 		freq = ieee80211_channel_to_frequency(elems->ds_params[0],
374 						      band);
375 	else
376 		freq = rx_status->freq;
377 
378 	channel = ieee80211_get_channel(local->hw.wiphy, freq);
379 
380 	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
381 		return;
382 
383 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
384 	    memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
385 
386 		rcu_read_lock();
387 		sta = sta_info_get(sdata, mgmt->sa);
388 
389 		if (elems->supp_rates) {
390 			supp_rates = ieee80211_sta_get_rates(local, elems,
391 							     band);
392 			if (sta) {
393 				u32 prev_rates;
394 
395 				prev_rates = sta->sta.supp_rates[band];
396 				/* make sure mandatory rates are always added */
397 				sta->sta.supp_rates[band] = supp_rates |
398 					ieee80211_mandatory_rates(local, band);
399 
400 				if (sta->sta.supp_rates[band] != prev_rates) {
401 #ifdef CONFIG_MAC80211_IBSS_DEBUG
402 					printk(KERN_DEBUG
403 						"%s: updated supp_rates set "
404 						"for %pM based on beacon"
405 						"/probe_resp (0x%x -> 0x%x)\n",
406 						sdata->name, sta->sta.addr,
407 						prev_rates,
408 						sta->sta.supp_rates[band]);
409 #endif
410 					rates_updated = true;
411 				}
412 			} else {
413 				rcu_read_unlock();
414 				sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
415 						mgmt->sa, supp_rates);
416 			}
417 		}
418 
419 		if (sta && elems->wmm_info)
420 			set_sta_flag(sta, WLAN_STA_WME);
421 
422 		if (sta && elems->ht_info_elem && elems->ht_cap_elem &&
423 		    sdata->u.ibss.channel_type != NL80211_CHAN_NO_HT) {
424 			/* we both use HT */
425 			struct ieee80211_sta_ht_cap sta_ht_cap_new;
426 			enum nl80211_channel_type channel_type =
427 				ieee80211_ht_info_to_channel_type(
428 							elems->ht_info_elem);
429 
430 			ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
431 							  elems->ht_cap_elem,
432 							  &sta_ht_cap_new);
433 
434 			/*
435 			 * fall back to HT20 if we don't use or use
436 			 * the other extension channel
437 			 */
438 			if ((channel_type == NL80211_CHAN_HT40MINUS ||
439 			     channel_type == NL80211_CHAN_HT40PLUS) &&
440 			    channel_type != sdata->u.ibss.channel_type)
441 				sta_ht_cap_new.cap &=
442 					~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
443 
444 			if (memcmp(&sta->sta.ht_cap, &sta_ht_cap_new,
445 				   sizeof(sta_ht_cap_new))) {
446 				memcpy(&sta->sta.ht_cap, &sta_ht_cap_new,
447 				       sizeof(sta_ht_cap_new));
448 				rates_updated = true;
449 			}
450 		}
451 
452 		if (sta && rates_updated)
453 			rate_control_rate_init(sta);
454 
455 		rcu_read_unlock();
456 	}
457 
458 	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
459 					channel, beacon);
460 	if (!bss)
461 		return;
462 
463 	cbss = container_of((void *)bss, struct cfg80211_bss, priv);
464 
465 	/* was just updated in ieee80211_bss_info_update */
466 	beacon_timestamp = cbss->tsf;
467 
468 	/* check if we need to merge IBSS */
469 
470 	/* we use a fixed BSSID */
471 	if (sdata->u.ibss.fixed_bssid)
472 		goto put_bss;
473 
474 	/* not an IBSS */
475 	if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
476 		goto put_bss;
477 
478 	/* different channel */
479 	if (cbss->channel != local->oper_channel)
480 		goto put_bss;
481 
482 	/* different SSID */
483 	if (elems->ssid_len != sdata->u.ibss.ssid_len ||
484 	    memcmp(elems->ssid, sdata->u.ibss.ssid,
485 				sdata->u.ibss.ssid_len))
486 		goto put_bss;
487 
488 	/* same BSSID */
489 	if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
490 		goto put_bss;
491 
492 	if (rx_status->flag & RX_FLAG_MACTIME_MPDU) {
493 		/*
494 		 * For correct IBSS merging we need mactime; since mactime is
495 		 * defined as the time the first data symbol of the frame hits
496 		 * the PHY, and the timestamp of the beacon is defined as "the
497 		 * time that the data symbol containing the first bit of the
498 		 * timestamp is transmitted to the PHY plus the transmitting
499 		 * STA's delays through its local PHY from the MAC-PHY
500 		 * interface to its interface with the WM" (802.11 11.1.2)
501 		 * - equals the time this bit arrives at the receiver - we have
502 		 * to take into account the offset between the two.
503 		 *
504 		 * E.g. at 1 MBit that means mactime is 192 usec earlier
505 		 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
506 		 */
507 		int rate;
508 
509 		if (rx_status->flag & RX_FLAG_HT)
510 			rate = 65; /* TODO: HT rates */
511 		else
512 			rate = local->hw.wiphy->bands[band]->
513 				bitrates[rx_status->rate_idx].bitrate;
514 
515 		rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
516 	} else {
517 		/*
518 		 * second best option: get current TSF
519 		 * (will return -1 if not supported)
520 		 */
521 		rx_timestamp = drv_get_tsf(local, sdata);
522 	}
523 
524 #ifdef CONFIG_MAC80211_IBSS_DEBUG
525 	printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
526 	       "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
527 	       mgmt->sa, mgmt->bssid,
528 	       (unsigned long long)rx_timestamp,
529 	       (unsigned long long)beacon_timestamp,
530 	       (unsigned long long)(rx_timestamp - beacon_timestamp),
531 	       jiffies);
532 #endif
533 
534 	if (beacon_timestamp > rx_timestamp) {
535 #ifdef CONFIG_MAC80211_IBSS_DEBUG
536 		printk(KERN_DEBUG "%s: beacon TSF higher than "
537 		       "local TSF - IBSS merge with BSSID %pM\n",
538 		       sdata->name, mgmt->bssid);
539 #endif
540 		ieee80211_sta_join_ibss(sdata, bss);
541 		supp_rates = ieee80211_sta_get_rates(local, elems, band);
542 		ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
543 				       supp_rates);
544 		rcu_read_unlock();
545 	}
546 
547  put_bss:
548 	ieee80211_rx_bss_put(local, bss);
549 }
550 
ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data * sdata,const u8 * bssid,const u8 * addr,u32 supp_rates)551 void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
552 			      const u8 *bssid, const u8 *addr,
553 			      u32 supp_rates)
554 {
555 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
556 	struct ieee80211_local *local = sdata->local;
557 	struct sta_info *sta;
558 	int band = local->hw.conf.channel->band;
559 
560 	/*
561 	 * XXX: Consider removing the least recently used entry and
562 	 * 	allow new one to be added.
563 	 */
564 	if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
565 		if (net_ratelimit())
566 			printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
567 			       sdata->name, addr);
568 		return;
569 	}
570 
571 	if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
572 		return;
573 
574 	if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
575 		return;
576 
577 	sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
578 	if (!sta)
579 		return;
580 
581 	sta->last_rx = jiffies;
582 
583 	/* make sure mandatory rates are always added */
584 	sta->sta.supp_rates[band] = supp_rates |
585 			ieee80211_mandatory_rates(local, band);
586 
587 	spin_lock(&ifibss->incomplete_lock);
588 	list_add(&sta->list, &ifibss->incomplete_stations);
589 	spin_unlock(&ifibss->incomplete_lock);
590 	ieee80211_queue_work(&local->hw, &sdata->work);
591 }
592 
ieee80211_sta_active_ibss(struct ieee80211_sub_if_data * sdata)593 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
594 {
595 	struct ieee80211_local *local = sdata->local;
596 	int active = 0;
597 	struct sta_info *sta;
598 
599 	lockdep_assert_held(&sdata->u.ibss.mtx);
600 
601 	rcu_read_lock();
602 
603 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
604 		if (sta->sdata == sdata &&
605 		    time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
606 			       jiffies)) {
607 			active++;
608 			break;
609 		}
610 	}
611 
612 	rcu_read_unlock();
613 
614 	return active;
615 }
616 
617 /*
618  * This function is called with state == IEEE80211_IBSS_MLME_JOINED
619  */
620 
ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data * sdata)621 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
622 {
623 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
624 
625 	lockdep_assert_held(&ifibss->mtx);
626 
627 	mod_timer(&ifibss->timer,
628 		  round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
629 
630 	ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
631 
632 	if (time_before(jiffies, ifibss->last_scan_completed +
633 		       IEEE80211_IBSS_MERGE_INTERVAL))
634 		return;
635 
636 	if (ieee80211_sta_active_ibss(sdata))
637 		return;
638 
639 	if (ifibss->fixed_channel)
640 		return;
641 
642 	printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
643 	       "IBSS networks with same SSID (merge)\n", sdata->name);
644 
645 	ieee80211_request_internal_scan(sdata,
646 			ifibss->ssid, ifibss->ssid_len,
647 			ifibss->fixed_channel ? ifibss->channel : NULL);
648 }
649 
ieee80211_sta_create_ibss(struct ieee80211_sub_if_data * sdata)650 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
651 {
652 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
653 	u8 bssid[ETH_ALEN];
654 	u16 capability;
655 	int i;
656 
657 	lockdep_assert_held(&ifibss->mtx);
658 
659 	if (ifibss->fixed_bssid) {
660 		memcpy(bssid, ifibss->bssid, ETH_ALEN);
661 	} else {
662 		/* Generate random, not broadcast, locally administered BSSID. Mix in
663 		 * own MAC address to make sure that devices that do not have proper
664 		 * random number generator get different BSSID. */
665 		get_random_bytes(bssid, ETH_ALEN);
666 		for (i = 0; i < ETH_ALEN; i++)
667 			bssid[i] ^= sdata->vif.addr[i];
668 		bssid[0] &= ~0x01;
669 		bssid[0] |= 0x02;
670 	}
671 
672 	printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
673 	       sdata->name, bssid);
674 
675 	capability = WLAN_CAPABILITY_IBSS;
676 
677 	if (ifibss->privacy)
678 		capability |= WLAN_CAPABILITY_PRIVACY;
679 	else
680 		sdata->drop_unencrypted = 0;
681 
682 	__ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
683 				  ifibss->channel, ifibss->basic_rates,
684 				  capability, 0);
685 }
686 
687 /*
688  * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
689  */
690 
ieee80211_sta_find_ibss(struct ieee80211_sub_if_data * sdata)691 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
692 {
693 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
694 	struct ieee80211_local *local = sdata->local;
695 	struct cfg80211_bss *cbss;
696 	struct ieee80211_channel *chan = NULL;
697 	const u8 *bssid = NULL;
698 	int active_ibss;
699 	u16 capability;
700 
701 	lockdep_assert_held(&ifibss->mtx);
702 
703 	active_ibss = ieee80211_sta_active_ibss(sdata);
704 #ifdef CONFIG_MAC80211_IBSS_DEBUG
705 	printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
706 	       sdata->name, active_ibss);
707 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
708 
709 	if (active_ibss)
710 		return;
711 
712 	capability = WLAN_CAPABILITY_IBSS;
713 	if (ifibss->privacy)
714 		capability |= WLAN_CAPABILITY_PRIVACY;
715 	if (ifibss->fixed_bssid)
716 		bssid = ifibss->bssid;
717 	if (ifibss->fixed_channel)
718 		chan = ifibss->channel;
719 	if (!is_zero_ether_addr(ifibss->bssid))
720 		bssid = ifibss->bssid;
721 	cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
722 				ifibss->ssid, ifibss->ssid_len,
723 				WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
724 				capability);
725 
726 	if (cbss) {
727 		struct ieee80211_bss *bss;
728 
729 		bss = (void *)cbss->priv;
730 #ifdef CONFIG_MAC80211_IBSS_DEBUG
731 		printk(KERN_DEBUG "   sta_find_ibss: selected %pM current "
732 		       "%pM\n", cbss->bssid, ifibss->bssid);
733 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
734 
735 		printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
736 		       " based on configured SSID\n",
737 		       sdata->name, cbss->bssid);
738 
739 		ieee80211_sta_join_ibss(sdata, bss);
740 		ieee80211_rx_bss_put(local, bss);
741 		return;
742 	}
743 
744 #ifdef CONFIG_MAC80211_IBSS_DEBUG
745 	printk(KERN_DEBUG "   did not try to join ibss\n");
746 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
747 
748 	/* Selected IBSS not found in current scan results - try to scan */
749 	if (time_after(jiffies, ifibss->last_scan_completed +
750 					IEEE80211_SCAN_INTERVAL)) {
751 		printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
752 		       "join\n", sdata->name);
753 
754 		ieee80211_request_internal_scan(sdata,
755 				ifibss->ssid, ifibss->ssid_len,
756 				ifibss->fixed_channel ? ifibss->channel : NULL);
757 	} else {
758 		int interval = IEEE80211_SCAN_INTERVAL;
759 
760 		if (time_after(jiffies, ifibss->ibss_join_req +
761 			       IEEE80211_IBSS_JOIN_TIMEOUT)) {
762 			if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
763 				ieee80211_sta_create_ibss(sdata);
764 				return;
765 			}
766 			printk(KERN_DEBUG "%s: IBSS not allowed on"
767 			       " %d MHz\n", sdata->name,
768 			       local->hw.conf.channel->center_freq);
769 
770 			/* No IBSS found - decrease scan interval and continue
771 			 * scanning. */
772 			interval = IEEE80211_SCAN_INTERVAL_SLOW;
773 		}
774 
775 		mod_timer(&ifibss->timer,
776 			  round_jiffies(jiffies + interval));
777 	}
778 }
779 
ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data * sdata,struct sk_buff * req)780 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
781 					struct sk_buff *req)
782 {
783 	struct ieee80211_mgmt *mgmt = (void *)req->data;
784 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
785 	struct ieee80211_local *local = sdata->local;
786 	int tx_last_beacon, len = req->len;
787 	struct sk_buff *skb;
788 	struct ieee80211_mgmt *resp;
789 	struct sk_buff *presp;
790 	u8 *pos, *end;
791 
792 	lockdep_assert_held(&ifibss->mtx);
793 
794 	presp = rcu_dereference_protected(ifibss->presp,
795 					  lockdep_is_held(&ifibss->mtx));
796 
797 	if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
798 	    len < 24 + 2 || !presp)
799 		return;
800 
801 	tx_last_beacon = drv_tx_last_beacon(local);
802 
803 #ifdef CONFIG_MAC80211_IBSS_DEBUG
804 	printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
805 	       " (tx_last_beacon=%d)\n",
806 	       sdata->name, mgmt->sa, mgmt->da,
807 	       mgmt->bssid, tx_last_beacon);
808 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
809 
810 	if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
811 		return;
812 
813 	if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
814 	    memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
815 		return;
816 
817 	end = ((u8 *) mgmt) + len;
818 	pos = mgmt->u.probe_req.variable;
819 	if (pos[0] != WLAN_EID_SSID ||
820 	    pos + 2 + pos[1] > end) {
821 #ifdef CONFIG_MAC80211_IBSS_DEBUG
822 		printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
823 		       "from %pM\n",
824 		       sdata->name, mgmt->sa);
825 #endif
826 		return;
827 	}
828 	if (pos[1] != 0 &&
829 	    (pos[1] != ifibss->ssid_len ||
830 	     memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
831 		/* Ignore ProbeReq for foreign SSID */
832 		return;
833 	}
834 
835 	/* Reply with ProbeResp */
836 	skb = skb_copy(presp, GFP_KERNEL);
837 	if (!skb)
838 		return;
839 
840 	resp = (struct ieee80211_mgmt *) skb->data;
841 	memcpy(resp->da, mgmt->sa, ETH_ALEN);
842 #ifdef CONFIG_MAC80211_IBSS_DEBUG
843 	printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
844 	       sdata->name, resp->da);
845 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
846 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
847 	ieee80211_tx_skb(sdata, skb);
848 }
849 
ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len,struct ieee80211_rx_status * rx_status)850 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
851 					 struct ieee80211_mgmt *mgmt,
852 					 size_t len,
853 					 struct ieee80211_rx_status *rx_status)
854 {
855 	size_t baselen;
856 	struct ieee802_11_elems elems;
857 
858 	if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
859 		return; /* ignore ProbeResp to foreign address */
860 
861 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
862 	if (baselen > len)
863 		return;
864 
865 	ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
866 				&elems);
867 
868 	ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
869 }
870 
ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len,struct ieee80211_rx_status * rx_status)871 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
872 				     struct ieee80211_mgmt *mgmt,
873 				     size_t len,
874 				     struct ieee80211_rx_status *rx_status)
875 {
876 	size_t baselen;
877 	struct ieee802_11_elems elems;
878 
879 	/* Process beacon from the current BSS */
880 	baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
881 	if (baselen > len)
882 		return;
883 
884 	ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
885 
886 	ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
887 }
888 
ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)889 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
890 				   struct sk_buff *skb)
891 {
892 	struct ieee80211_rx_status *rx_status;
893 	struct ieee80211_mgmt *mgmt;
894 	u16 fc;
895 
896 	rx_status = IEEE80211_SKB_RXCB(skb);
897 	mgmt = (struct ieee80211_mgmt *) skb->data;
898 	fc = le16_to_cpu(mgmt->frame_control);
899 
900 	mutex_lock(&sdata->u.ibss.mtx);
901 
902 	if (!sdata->u.ibss.ssid_len)
903 		goto mgmt_out; /* not ready to merge yet */
904 
905 	switch (fc & IEEE80211_FCTL_STYPE) {
906 	case IEEE80211_STYPE_PROBE_REQ:
907 		ieee80211_rx_mgmt_probe_req(sdata, skb);
908 		break;
909 	case IEEE80211_STYPE_PROBE_RESP:
910 		ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
911 					     rx_status);
912 		break;
913 	case IEEE80211_STYPE_BEACON:
914 		ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
915 					 rx_status);
916 		break;
917 	case IEEE80211_STYPE_AUTH:
918 		ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
919 		break;
920 	}
921 
922  mgmt_out:
923 	mutex_unlock(&sdata->u.ibss.mtx);
924 }
925 
ieee80211_ibss_work(struct ieee80211_sub_if_data * sdata)926 void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
927 {
928 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
929 	struct sta_info *sta;
930 
931 	mutex_lock(&ifibss->mtx);
932 
933 	/*
934 	 * Work could be scheduled after scan or similar
935 	 * when we aren't even joined (or trying) with a
936 	 * network.
937 	 */
938 	if (!ifibss->ssid_len)
939 		goto out;
940 
941 	spin_lock_bh(&ifibss->incomplete_lock);
942 	while (!list_empty(&ifibss->incomplete_stations)) {
943 		sta = list_first_entry(&ifibss->incomplete_stations,
944 				       struct sta_info, list);
945 		list_del(&sta->list);
946 		spin_unlock_bh(&ifibss->incomplete_lock);
947 
948 		ieee80211_ibss_finish_sta(sta);
949 		rcu_read_unlock();
950 		spin_lock_bh(&ifibss->incomplete_lock);
951 	}
952 	spin_unlock_bh(&ifibss->incomplete_lock);
953 
954 	switch (ifibss->state) {
955 	case IEEE80211_IBSS_MLME_SEARCH:
956 		ieee80211_sta_find_ibss(sdata);
957 		break;
958 	case IEEE80211_IBSS_MLME_JOINED:
959 		ieee80211_sta_merge_ibss(sdata);
960 		break;
961 	default:
962 		WARN_ON(1);
963 		break;
964 	}
965 
966  out:
967 	mutex_unlock(&ifibss->mtx);
968 }
969 
ieee80211_ibss_timer(unsigned long data)970 static void ieee80211_ibss_timer(unsigned long data)
971 {
972 	struct ieee80211_sub_if_data *sdata =
973 		(struct ieee80211_sub_if_data *) data;
974 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
975 	struct ieee80211_local *local = sdata->local;
976 
977 	if (local->quiescing) {
978 		ifibss->timer_running = true;
979 		return;
980 	}
981 
982 	ieee80211_queue_work(&local->hw, &sdata->work);
983 }
984 
985 #ifdef CONFIG_PM
ieee80211_ibss_quiesce(struct ieee80211_sub_if_data * sdata)986 void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
987 {
988 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
989 
990 	if (del_timer_sync(&ifibss->timer))
991 		ifibss->timer_running = true;
992 }
993 
ieee80211_ibss_restart(struct ieee80211_sub_if_data * sdata)994 void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
995 {
996 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
997 
998 	if (ifibss->timer_running) {
999 		add_timer(&ifibss->timer);
1000 		ifibss->timer_running = false;
1001 	}
1002 }
1003 #endif
1004 
ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data * sdata)1005 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1006 {
1007 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1008 
1009 	setup_timer(&ifibss->timer, ieee80211_ibss_timer,
1010 		    (unsigned long) sdata);
1011 	mutex_init(&ifibss->mtx);
1012 	INIT_LIST_HEAD(&ifibss->incomplete_stations);
1013 	spin_lock_init(&ifibss->incomplete_lock);
1014 }
1015 
1016 /* scan finished notification */
ieee80211_ibss_notify_scan_completed(struct ieee80211_local * local)1017 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1018 {
1019 	struct ieee80211_sub_if_data *sdata;
1020 
1021 	mutex_lock(&local->iflist_mtx);
1022 	list_for_each_entry(sdata, &local->interfaces, list) {
1023 		if (!ieee80211_sdata_running(sdata))
1024 			continue;
1025 		if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1026 			continue;
1027 		sdata->u.ibss.last_scan_completed = jiffies;
1028 		ieee80211_queue_work(&local->hw, &sdata->work);
1029 	}
1030 	mutex_unlock(&local->iflist_mtx);
1031 }
1032 
ieee80211_ibss_join(struct ieee80211_sub_if_data * sdata,struct cfg80211_ibss_params * params)1033 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1034 			struct cfg80211_ibss_params *params)
1035 {
1036 	struct sk_buff *skb;
1037 	u32 changed = 0;
1038 
1039 	skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
1040 			    sizeof(struct ieee80211_hdr_3addr) +
1041 			    12 /* struct ieee80211_mgmt.u.beacon */ +
1042 			    2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
1043 			    2 + 8 /* max Supported Rates */ +
1044 			    3 /* max DS params */ +
1045 			    4 /* IBSS params */ +
1046 			    2 + (IEEE80211_MAX_SUPP_RATES - 8) +
1047 			    2 + sizeof(struct ieee80211_ht_cap) +
1048 			    2 + sizeof(struct ieee80211_ht_info) +
1049 			    params->ie_len);
1050 	if (!skb)
1051 		return -ENOMEM;
1052 
1053 	mutex_lock(&sdata->u.ibss.mtx);
1054 
1055 	if (params->bssid) {
1056 		memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1057 		sdata->u.ibss.fixed_bssid = true;
1058 	} else
1059 		sdata->u.ibss.fixed_bssid = false;
1060 
1061 	sdata->u.ibss.privacy = params->privacy;
1062 	sdata->u.ibss.basic_rates = params->basic_rates;
1063 	memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1064 	       sizeof(params->mcast_rate));
1065 
1066 	sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1067 
1068 	sdata->u.ibss.channel = params->channel;
1069 	sdata->u.ibss.channel_type = params->channel_type;
1070 	sdata->u.ibss.fixed_channel = params->channel_fixed;
1071 
1072 	/* fix ourselves to that channel now already */
1073 	if (params->channel_fixed) {
1074 		sdata->local->oper_channel = params->channel;
1075 		if (!ieee80211_set_channel_type(sdata->local, sdata,
1076 					       params->channel_type)) {
1077 			mutex_unlock(&sdata->u.ibss.mtx);
1078 			kfree_skb(skb);
1079 			return -EINVAL;
1080 		}
1081 	}
1082 
1083 	if (params->ie) {
1084 		sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1085 					   GFP_KERNEL);
1086 		if (sdata->u.ibss.ie)
1087 			sdata->u.ibss.ie_len = params->ie_len;
1088 	}
1089 
1090 	sdata->u.ibss.skb = skb;
1091 	sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1092 	sdata->u.ibss.ibss_join_req = jiffies;
1093 
1094 	memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
1095 	sdata->u.ibss.ssid_len = params->ssid_len;
1096 
1097 	mutex_unlock(&sdata->u.ibss.mtx);
1098 
1099 	mutex_lock(&sdata->local->mtx);
1100 	ieee80211_recalc_idle(sdata->local);
1101 	mutex_unlock(&sdata->local->mtx);
1102 
1103 	/*
1104 	 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1105 	 * reserved, but an HT STA shall protect HT transmissions as though
1106 	 * the HT Protection field were set to non-HT mixed mode.
1107 	 *
1108 	 * In an IBSS, the RIFS Mode field of the HT Operation element is
1109 	 * also reserved, but an HT STA shall operate as though this field
1110 	 * were set to 1.
1111 	 */
1112 
1113 	sdata->vif.bss_conf.ht_operation_mode |=
1114 		  IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1115 		| IEEE80211_HT_PARAM_RIFS_MODE;
1116 
1117 	changed |= BSS_CHANGED_HT;
1118 	ieee80211_bss_info_change_notify(sdata, changed);
1119 
1120 	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1121 
1122 	return 0;
1123 }
1124 
ieee80211_ibss_leave(struct ieee80211_sub_if_data * sdata)1125 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1126 {
1127 	struct sk_buff *skb;
1128 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1129 	struct ieee80211_local *local = sdata->local;
1130 	struct cfg80211_bss *cbss;
1131 	u16 capability;
1132 	int active_ibss;
1133 	struct sta_info *sta;
1134 
1135 	mutex_lock(&sdata->u.ibss.mtx);
1136 
1137 	sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1138 	memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
1139 	sdata->u.ibss.ssid_len = 0;
1140 
1141 	active_ibss = ieee80211_sta_active_ibss(sdata);
1142 
1143 	if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
1144 		capability = WLAN_CAPABILITY_IBSS;
1145 
1146 		if (ifibss->privacy)
1147 			capability |= WLAN_CAPABILITY_PRIVACY;
1148 
1149 		cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
1150 					ifibss->bssid, ifibss->ssid,
1151 					ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
1152 					WLAN_CAPABILITY_PRIVACY,
1153 					capability);
1154 
1155 		if (cbss) {
1156 			cfg80211_unlink_bss(local->hw.wiphy, cbss);
1157 			cfg80211_put_bss(cbss);
1158 		}
1159 	}
1160 
1161 	sta_info_flush(sdata->local, sdata);
1162 
1163 	spin_lock_bh(&ifibss->incomplete_lock);
1164 	while (!list_empty(&ifibss->incomplete_stations)) {
1165 		sta = list_first_entry(&ifibss->incomplete_stations,
1166 				       struct sta_info, list);
1167 		list_del(&sta->list);
1168 		spin_unlock_bh(&ifibss->incomplete_lock);
1169 
1170 		sta_info_free(local, sta);
1171 		spin_lock_bh(&ifibss->incomplete_lock);
1172 	}
1173 	spin_unlock_bh(&ifibss->incomplete_lock);
1174 
1175 	netif_carrier_off(sdata->dev);
1176 
1177 	/* remove beacon */
1178 	kfree(sdata->u.ibss.ie);
1179 	skb = rcu_dereference_protected(sdata->u.ibss.presp,
1180 					lockdep_is_held(&sdata->u.ibss.mtx));
1181 	RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
1182 	sdata->vif.bss_conf.ibss_joined = false;
1183 	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1184 						BSS_CHANGED_IBSS);
1185 	synchronize_rcu();
1186 	kfree_skb(skb);
1187 
1188 	skb_queue_purge(&sdata->skb_queue);
1189 
1190 	del_timer_sync(&sdata->u.ibss.timer);
1191 
1192 	mutex_unlock(&sdata->u.ibss.mtx);
1193 
1194 	mutex_lock(&local->mtx);
1195 	ieee80211_recalc_idle(sdata->local);
1196 	mutex_unlock(&local->mtx);
1197 
1198 	return 0;
1199 }
1200