ibss.c 34 KB

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