ibss.c 33 KB

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