ibss.c 32 KB

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