ibss.c 33 KB

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