ibss.c 32 KB

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