ibss.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  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. * Copyright 2013-2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/rtnetlink.h>
  22. #include <net/mac80211.h>
  23. #include "ieee80211_i.h"
  24. #include "driver-ops.h"
  25. #include "rate.h"
  26. #define IEEE80211_SCAN_INTERVAL (2 * HZ)
  27. #define IEEE80211_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_RSN_INACTIVITY_LIMIT (10 * HZ)
  31. #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
  32. static struct beacon_data *
  33. ieee80211_ibss_build_presp(struct ieee80211_sub_if_data *sdata,
  34. const int beacon_int, const u32 basic_rates,
  35. const u16 capability, u64 tsf,
  36. struct cfg80211_chan_def *chandef,
  37. bool *have_higher_than_11mbit,
  38. struct cfg80211_csa_settings *csa_settings)
  39. {
  40. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  41. struct ieee80211_local *local = sdata->local;
  42. int rates_n = 0, i, ri;
  43. struct ieee80211_mgmt *mgmt;
  44. u8 *pos;
  45. struct ieee80211_supported_band *sband;
  46. u32 rate_flags, rates = 0, rates_added = 0;
  47. struct beacon_data *presp;
  48. int frame_len;
  49. int shift;
  50. /* Build IBSS probe response */
  51. frame_len = sizeof(struct ieee80211_hdr_3addr) +
  52. 12 /* struct ieee80211_mgmt.u.beacon */ +
  53. 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
  54. 2 + 8 /* max Supported Rates */ +
  55. 3 /* max DS params */ +
  56. 4 /* IBSS params */ +
  57. 5 /* Channel Switch Announcement */ +
  58. 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  59. 2 + sizeof(struct ieee80211_ht_cap) +
  60. 2 + sizeof(struct ieee80211_ht_operation) +
  61. ifibss->ie_len;
  62. presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
  63. if (!presp)
  64. return NULL;
  65. presp->head = (void *)(presp + 1);
  66. mgmt = (void *) presp->head;
  67. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  68. IEEE80211_STYPE_PROBE_RESP);
  69. eth_broadcast_addr(mgmt->da);
  70. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  71. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  72. mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
  73. mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
  74. mgmt->u.beacon.capab_info = cpu_to_le16(capability);
  75. pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
  76. *pos++ = WLAN_EID_SSID;
  77. *pos++ = ifibss->ssid_len;
  78. memcpy(pos, ifibss->ssid, ifibss->ssid_len);
  79. pos += ifibss->ssid_len;
  80. sband = local->hw.wiphy->bands[chandef->chan->band];
  81. rate_flags = ieee80211_chandef_rate_flags(chandef);
  82. shift = ieee80211_chandef_get_shift(chandef);
  83. rates_n = 0;
  84. if (have_higher_than_11mbit)
  85. *have_higher_than_11mbit = false;
  86. for (i = 0; i < sband->n_bitrates; i++) {
  87. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  88. continue;
  89. if (sband->bitrates[i].bitrate > 110 &&
  90. have_higher_than_11mbit)
  91. *have_higher_than_11mbit = true;
  92. rates |= BIT(i);
  93. rates_n++;
  94. }
  95. *pos++ = WLAN_EID_SUPP_RATES;
  96. *pos++ = min_t(int, 8, rates_n);
  97. for (ri = 0; ri < sband->n_bitrates; ri++) {
  98. int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
  99. 5 * (1 << shift));
  100. u8 basic = 0;
  101. if (!(rates & BIT(ri)))
  102. continue;
  103. if (basic_rates & BIT(ri))
  104. basic = 0x80;
  105. *pos++ = basic | (u8) rate;
  106. if (++rates_added == 8) {
  107. ri++; /* continue at next rate for EXT_SUPP_RATES */
  108. break;
  109. }
  110. }
  111. if (sband->band == IEEE80211_BAND_2GHZ) {
  112. *pos++ = WLAN_EID_DS_PARAMS;
  113. *pos++ = 1;
  114. *pos++ = ieee80211_frequency_to_channel(
  115. chandef->chan->center_freq);
  116. }
  117. *pos++ = WLAN_EID_IBSS_PARAMS;
  118. *pos++ = 2;
  119. /* FIX: set ATIM window based on scan results */
  120. *pos++ = 0;
  121. *pos++ = 0;
  122. if (csa_settings) {
  123. *pos++ = WLAN_EID_CHANNEL_SWITCH;
  124. *pos++ = 3;
  125. *pos++ = csa_settings->block_tx ? 1 : 0;
  126. *pos++ = ieee80211_frequency_to_channel(
  127. csa_settings->chandef.chan->center_freq);
  128. presp->csa_counter_offsets[0] = (pos - presp->head);
  129. *pos++ = csa_settings->count;
  130. }
  131. /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
  132. if (rates_n > 8) {
  133. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  134. *pos++ = rates_n - 8;
  135. for (; ri < sband->n_bitrates; ri++) {
  136. int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
  137. 5 * (1 << shift));
  138. u8 basic = 0;
  139. if (!(rates & BIT(ri)))
  140. continue;
  141. if (basic_rates & BIT(ri))
  142. basic = 0x80;
  143. *pos++ = basic | (u8) rate;
  144. }
  145. }
  146. if (ifibss->ie_len) {
  147. memcpy(pos, ifibss->ie, ifibss->ie_len);
  148. pos += ifibss->ie_len;
  149. }
  150. /* add HT capability and information IEs */
  151. if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  152. chandef->width != NL80211_CHAN_WIDTH_5 &&
  153. chandef->width != NL80211_CHAN_WIDTH_10 &&
  154. sband->ht_cap.ht_supported) {
  155. struct ieee80211_sta_ht_cap ht_cap;
  156. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  157. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  158. pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  159. /*
  160. * Note: According to 802.11n-2009 9.13.3.1, HT Protection
  161. * field and RIFS Mode are reserved in IBSS mode, therefore
  162. * keep them at 0
  163. */
  164. pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
  165. chandef, 0);
  166. }
  167. if (local->hw.queues >= IEEE80211_NUM_ACS)
  168. pos = ieee80211_add_wmm_info_ie(pos, 0); /* U-APSD not in use */
  169. presp->head_len = pos - presp->head;
  170. if (WARN_ON(presp->head_len > frame_len))
  171. goto error;
  172. return presp;
  173. error:
  174. kfree(presp);
  175. return NULL;
  176. }
  177. static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  178. const u8 *bssid, const int beacon_int,
  179. struct cfg80211_chan_def *req_chandef,
  180. const u32 basic_rates,
  181. const u16 capability, u64 tsf,
  182. bool creator)
  183. {
  184. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  185. struct ieee80211_local *local = sdata->local;
  186. struct ieee80211_mgmt *mgmt;
  187. struct cfg80211_bss *bss;
  188. u32 bss_change;
  189. struct cfg80211_chan_def chandef;
  190. struct ieee80211_channel *chan;
  191. struct beacon_data *presp;
  192. enum nl80211_bss_scan_width scan_width;
  193. bool have_higher_than_11mbit;
  194. bool radar_required;
  195. int err;
  196. sdata_assert_lock(sdata);
  197. /* Reset own TSF to allow time synchronization work. */
  198. drv_reset_tsf(local, sdata);
  199. if (!ether_addr_equal(ifibss->bssid, bssid))
  200. sta_info_flush(sdata);
  201. /* if merging, indicate to driver that we leave the old IBSS */
  202. if (sdata->vif.bss_conf.ibss_joined) {
  203. sdata->vif.bss_conf.ibss_joined = false;
  204. sdata->vif.bss_conf.ibss_creator = false;
  205. sdata->vif.bss_conf.enable_beacon = false;
  206. netif_carrier_off(sdata->dev);
  207. ieee80211_bss_info_change_notify(sdata,
  208. BSS_CHANGED_IBSS |
  209. BSS_CHANGED_BEACON_ENABLED);
  210. drv_leave_ibss(local, sdata);
  211. }
  212. presp = rcu_dereference_protected(ifibss->presp,
  213. lockdep_is_held(&sdata->wdev.mtx));
  214. RCU_INIT_POINTER(ifibss->presp, NULL);
  215. if (presp)
  216. kfree_rcu(presp, rcu_head);
  217. sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
  218. /* make a copy of the chandef, it could be modified below. */
  219. chandef = *req_chandef;
  220. chan = chandef.chan;
  221. if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
  222. NL80211_IFTYPE_ADHOC)) {
  223. if (chandef.width == NL80211_CHAN_WIDTH_5 ||
  224. chandef.width == NL80211_CHAN_WIDTH_10 ||
  225. chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
  226. chandef.width == NL80211_CHAN_WIDTH_20) {
  227. sdata_info(sdata,
  228. "Failed to join IBSS, beacons forbidden\n");
  229. return;
  230. }
  231. chandef.width = NL80211_CHAN_WIDTH_20;
  232. chandef.center_freq1 = chan->center_freq;
  233. /* check again for downgraded chandef */
  234. if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
  235. NL80211_IFTYPE_ADHOC)) {
  236. sdata_info(sdata,
  237. "Failed to join IBSS, beacons forbidden\n");
  238. return;
  239. }
  240. }
  241. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  242. &chandef, NL80211_IFTYPE_ADHOC);
  243. if (err < 0) {
  244. sdata_info(sdata,
  245. "Failed to join IBSS, invalid chandef\n");
  246. return;
  247. }
  248. if (err > 0 && !ifibss->userspace_handles_dfs) {
  249. sdata_info(sdata,
  250. "Failed to join IBSS, DFS channel without control program\n");
  251. return;
  252. }
  253. radar_required = err;
  254. mutex_lock(&local->mtx);
  255. if (ieee80211_vif_use_channel(sdata, &chandef,
  256. ifibss->fixed_channel ?
  257. IEEE80211_CHANCTX_SHARED :
  258. IEEE80211_CHANCTX_EXCLUSIVE)) {
  259. sdata_info(sdata, "Failed to join IBSS, no channel context\n");
  260. mutex_unlock(&local->mtx);
  261. return;
  262. }
  263. sdata->radar_required = radar_required;
  264. mutex_unlock(&local->mtx);
  265. memcpy(ifibss->bssid, bssid, ETH_ALEN);
  266. presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates,
  267. capability, tsf, &chandef,
  268. &have_higher_than_11mbit, NULL);
  269. if (!presp)
  270. return;
  271. rcu_assign_pointer(ifibss->presp, presp);
  272. mgmt = (void *)presp->head;
  273. sdata->vif.bss_conf.enable_beacon = true;
  274. sdata->vif.bss_conf.beacon_int = beacon_int;
  275. sdata->vif.bss_conf.basic_rates = basic_rates;
  276. sdata->vif.bss_conf.ssid_len = ifibss->ssid_len;
  277. memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len);
  278. bss_change = BSS_CHANGED_BEACON_INT;
  279. bss_change |= ieee80211_reset_erp_info(sdata);
  280. bss_change |= BSS_CHANGED_BSSID;
  281. bss_change |= BSS_CHANGED_BEACON;
  282. bss_change |= BSS_CHANGED_BEACON_ENABLED;
  283. bss_change |= BSS_CHANGED_BASIC_RATES;
  284. bss_change |= BSS_CHANGED_HT;
  285. bss_change |= BSS_CHANGED_IBSS;
  286. bss_change |= BSS_CHANGED_SSID;
  287. /*
  288. * In 5 GHz/802.11a, we can always use short slot time.
  289. * (IEEE 802.11-2012 18.3.8.7)
  290. *
  291. * In 2.4GHz, we must always use long slots in IBSS for compatibility
  292. * reasons.
  293. * (IEEE 802.11-2012 19.4.5)
  294. *
  295. * HT follows these specifications (IEEE 802.11-2012 20.3.18)
  296. */
  297. sdata->vif.bss_conf.use_short_slot = chan->band == IEEE80211_BAND_5GHZ;
  298. bss_change |= BSS_CHANGED_ERP_SLOT;
  299. /* cf. IEEE 802.11 9.2.12 */
  300. if (chan->band == IEEE80211_BAND_2GHZ && have_higher_than_11mbit)
  301. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  302. else
  303. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  304. ieee80211_set_wmm_default(sdata, true);
  305. sdata->vif.bss_conf.ibss_joined = true;
  306. sdata->vif.bss_conf.ibss_creator = creator;
  307. err = drv_join_ibss(local, sdata);
  308. if (err) {
  309. sdata->vif.bss_conf.ibss_joined = false;
  310. sdata->vif.bss_conf.ibss_creator = false;
  311. sdata->vif.bss_conf.enable_beacon = false;
  312. sdata->vif.bss_conf.ssid_len = 0;
  313. RCU_INIT_POINTER(ifibss->presp, NULL);
  314. kfree_rcu(presp, rcu_head);
  315. mutex_lock(&local->mtx);
  316. ieee80211_vif_release_channel(sdata);
  317. mutex_unlock(&local->mtx);
  318. sdata_info(sdata, "Failed to join IBSS, driver failure: %d\n",
  319. err);
  320. return;
  321. }
  322. ieee80211_bss_info_change_notify(sdata, bss_change);
  323. ifibss->state = IEEE80211_IBSS_MLME_JOINED;
  324. mod_timer(&ifibss->timer,
  325. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  326. scan_width = cfg80211_chandef_to_scan_width(&chandef);
  327. bss = cfg80211_inform_bss_width_frame(local->hw.wiphy, chan,
  328. scan_width, mgmt,
  329. presp->head_len, 0, GFP_KERNEL);
  330. cfg80211_put_bss(local->hw.wiphy, bss);
  331. netif_carrier_on(sdata->dev);
  332. cfg80211_ibss_joined(sdata->dev, ifibss->bssid, chan, GFP_KERNEL);
  333. }
  334. static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  335. struct ieee80211_bss *bss)
  336. {
  337. struct cfg80211_bss *cbss =
  338. container_of((void *)bss, struct cfg80211_bss, priv);
  339. struct ieee80211_supported_band *sband;
  340. struct cfg80211_chan_def chandef;
  341. u32 basic_rates;
  342. int i, j;
  343. u16 beacon_int = cbss->beacon_interval;
  344. const struct cfg80211_bss_ies *ies;
  345. enum nl80211_channel_type chan_type;
  346. u64 tsf;
  347. u32 rate_flags;
  348. int shift;
  349. sdata_assert_lock(sdata);
  350. if (beacon_int < 10)
  351. beacon_int = 10;
  352. switch (sdata->u.ibss.chandef.width) {
  353. case NL80211_CHAN_WIDTH_20_NOHT:
  354. case NL80211_CHAN_WIDTH_20:
  355. case NL80211_CHAN_WIDTH_40:
  356. chan_type = cfg80211_get_chandef_type(&sdata->u.ibss.chandef);
  357. cfg80211_chandef_create(&chandef, cbss->channel, chan_type);
  358. break;
  359. case NL80211_CHAN_WIDTH_5:
  360. case NL80211_CHAN_WIDTH_10:
  361. cfg80211_chandef_create(&chandef, cbss->channel,
  362. NL80211_CHAN_WIDTH_20_NOHT);
  363. chandef.width = sdata->u.ibss.chandef.width;
  364. break;
  365. default:
  366. /* fall back to 20 MHz for unsupported modes */
  367. cfg80211_chandef_create(&chandef, cbss->channel,
  368. NL80211_CHAN_WIDTH_20_NOHT);
  369. break;
  370. }
  371. sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
  372. rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
  373. shift = ieee80211_vif_get_shift(&sdata->vif);
  374. basic_rates = 0;
  375. for (i = 0; i < bss->supp_rates_len; i++) {
  376. int rate = bss->supp_rates[i] & 0x7f;
  377. bool is_basic = !!(bss->supp_rates[i] & 0x80);
  378. for (j = 0; j < sband->n_bitrates; j++) {
  379. int brate;
  380. if ((rate_flags & sband->bitrates[j].flags)
  381. != rate_flags)
  382. continue;
  383. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  384. 5 * (1 << shift));
  385. if (brate == rate) {
  386. if (is_basic)
  387. basic_rates |= BIT(j);
  388. break;
  389. }
  390. }
  391. }
  392. rcu_read_lock();
  393. ies = rcu_dereference(cbss->ies);
  394. tsf = ies->tsf;
  395. rcu_read_unlock();
  396. __ieee80211_sta_join_ibss(sdata, cbss->bssid,
  397. beacon_int,
  398. &chandef,
  399. basic_rates,
  400. cbss->capability,
  401. tsf, false);
  402. }
  403. int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
  404. struct cfg80211_csa_settings *csa_settings)
  405. {
  406. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  407. struct beacon_data *presp, *old_presp;
  408. struct cfg80211_bss *cbss;
  409. const struct cfg80211_bss_ies *ies;
  410. u16 capability;
  411. u64 tsf;
  412. int ret = 0;
  413. sdata_assert_lock(sdata);
  414. capability = WLAN_CAPABILITY_IBSS;
  415. if (ifibss->privacy)
  416. capability |= WLAN_CAPABILITY_PRIVACY;
  417. cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
  418. ifibss->bssid, ifibss->ssid,
  419. ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
  420. WLAN_CAPABILITY_PRIVACY,
  421. capability);
  422. if (WARN_ON(!cbss)) {
  423. ret = -EINVAL;
  424. goto out;
  425. }
  426. rcu_read_lock();
  427. ies = rcu_dereference(cbss->ies);
  428. tsf = ies->tsf;
  429. rcu_read_unlock();
  430. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  431. old_presp = rcu_dereference_protected(ifibss->presp,
  432. lockdep_is_held(&sdata->wdev.mtx));
  433. presp = ieee80211_ibss_build_presp(sdata,
  434. sdata->vif.bss_conf.beacon_int,
  435. sdata->vif.bss_conf.basic_rates,
  436. capability, tsf, &ifibss->chandef,
  437. NULL, csa_settings);
  438. if (!presp) {
  439. ret = -ENOMEM;
  440. goto out;
  441. }
  442. rcu_assign_pointer(ifibss->presp, presp);
  443. if (old_presp)
  444. kfree_rcu(old_presp, rcu_head);
  445. return BSS_CHANGED_BEACON;
  446. out:
  447. return ret;
  448. }
  449. int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata)
  450. {
  451. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  452. struct cfg80211_bss *cbss;
  453. int err, changed = 0;
  454. u16 capability;
  455. sdata_assert_lock(sdata);
  456. /* update cfg80211 bss information with the new channel */
  457. if (!is_zero_ether_addr(ifibss->bssid)) {
  458. capability = WLAN_CAPABILITY_IBSS;
  459. if (ifibss->privacy)
  460. capability |= WLAN_CAPABILITY_PRIVACY;
  461. cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
  462. ifibss->chandef.chan,
  463. ifibss->bssid, ifibss->ssid,
  464. ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
  465. WLAN_CAPABILITY_PRIVACY,
  466. capability);
  467. /* XXX: should not really modify cfg80211 data */
  468. if (cbss) {
  469. cbss->channel = sdata->csa_chandef.chan;
  470. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  471. }
  472. }
  473. ifibss->chandef = sdata->csa_chandef;
  474. /* generate the beacon */
  475. err = ieee80211_ibss_csa_beacon(sdata, NULL);
  476. if (err < 0)
  477. return err;
  478. changed |= err;
  479. return changed;
  480. }
  481. void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
  482. {
  483. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  484. cancel_work_sync(&ifibss->csa_connection_drop_work);
  485. }
  486. static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
  487. __acquires(RCU)
  488. {
  489. struct ieee80211_sub_if_data *sdata = sta->sdata;
  490. u8 addr[ETH_ALEN];
  491. memcpy(addr, sta->sta.addr, ETH_ALEN);
  492. ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
  493. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  494. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  495. /* authorize the station only if the network is not RSN protected. If
  496. * not wait for the userspace to authorize it */
  497. if (!sta->sdata->u.ibss.control_port)
  498. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  499. rate_control_rate_init(sta);
  500. /* If it fails, maybe we raced another insertion? */
  501. if (sta_info_insert_rcu(sta))
  502. return sta_info_get(sdata, addr);
  503. return sta;
  504. }
  505. static struct sta_info *
  506. ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
  507. const u8 *addr, u32 supp_rates)
  508. __acquires(RCU)
  509. {
  510. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  511. struct ieee80211_local *local = sdata->local;
  512. struct sta_info *sta;
  513. struct ieee80211_chanctx_conf *chanctx_conf;
  514. struct ieee80211_supported_band *sband;
  515. enum nl80211_bss_scan_width scan_width;
  516. int band;
  517. /*
  518. * XXX: Consider removing the least recently used entry and
  519. * allow new one to be added.
  520. */
  521. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  522. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  523. sdata->name, addr);
  524. rcu_read_lock();
  525. return NULL;
  526. }
  527. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
  528. rcu_read_lock();
  529. return NULL;
  530. }
  531. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
  532. rcu_read_lock();
  533. return NULL;
  534. }
  535. rcu_read_lock();
  536. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  537. if (WARN_ON_ONCE(!chanctx_conf))
  538. return NULL;
  539. band = chanctx_conf->def.chan->band;
  540. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  541. rcu_read_unlock();
  542. sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
  543. if (!sta) {
  544. rcu_read_lock();
  545. return NULL;
  546. }
  547. sta->last_rx = jiffies;
  548. /* make sure mandatory rates are always added */
  549. sband = local->hw.wiphy->bands[band];
  550. sta->sta.supp_rates[band] = supp_rates |
  551. ieee80211_mandatory_rates(sband, scan_width);
  552. return ieee80211_ibss_finish_sta(sta);
  553. }
  554. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  555. {
  556. struct ieee80211_local *local = sdata->local;
  557. int active = 0;
  558. struct sta_info *sta;
  559. sdata_assert_lock(sdata);
  560. rcu_read_lock();
  561. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  562. if (sta->sdata == sdata &&
  563. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  564. jiffies)) {
  565. active++;
  566. break;
  567. }
  568. }
  569. rcu_read_unlock();
  570. return active;
  571. }
  572. static void ieee80211_ibss_disconnect(struct ieee80211_sub_if_data *sdata)
  573. {
  574. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  575. struct ieee80211_local *local = sdata->local;
  576. struct cfg80211_bss *cbss;
  577. struct beacon_data *presp;
  578. struct sta_info *sta;
  579. u16 capability;
  580. if (!is_zero_ether_addr(ifibss->bssid)) {
  581. capability = WLAN_CAPABILITY_IBSS;
  582. if (ifibss->privacy)
  583. capability |= WLAN_CAPABILITY_PRIVACY;
  584. cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
  585. ifibss->bssid, ifibss->ssid,
  586. ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
  587. WLAN_CAPABILITY_PRIVACY,
  588. capability);
  589. if (cbss) {
  590. cfg80211_unlink_bss(local->hw.wiphy, cbss);
  591. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  592. }
  593. }
  594. ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
  595. sta_info_flush(sdata);
  596. spin_lock_bh(&ifibss->incomplete_lock);
  597. while (!list_empty(&ifibss->incomplete_stations)) {
  598. sta = list_first_entry(&ifibss->incomplete_stations,
  599. struct sta_info, list);
  600. list_del(&sta->list);
  601. spin_unlock_bh(&ifibss->incomplete_lock);
  602. sta_info_free(local, sta);
  603. spin_lock_bh(&ifibss->incomplete_lock);
  604. }
  605. spin_unlock_bh(&ifibss->incomplete_lock);
  606. netif_carrier_off(sdata->dev);
  607. sdata->vif.bss_conf.ibss_joined = false;
  608. sdata->vif.bss_conf.ibss_creator = false;
  609. sdata->vif.bss_conf.enable_beacon = false;
  610. sdata->vif.bss_conf.ssid_len = 0;
  611. /* remove beacon */
  612. presp = rcu_dereference_protected(ifibss->presp,
  613. lockdep_is_held(&sdata->wdev.mtx));
  614. RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
  615. if (presp)
  616. kfree_rcu(presp, rcu_head);
  617. clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
  618. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
  619. BSS_CHANGED_IBSS);
  620. drv_leave_ibss(local, sdata);
  621. mutex_lock(&local->mtx);
  622. ieee80211_vif_release_channel(sdata);
  623. mutex_unlock(&local->mtx);
  624. }
  625. static void ieee80211_csa_connection_drop_work(struct work_struct *work)
  626. {
  627. struct ieee80211_sub_if_data *sdata =
  628. container_of(work, struct ieee80211_sub_if_data,
  629. u.ibss.csa_connection_drop_work);
  630. sdata_lock(sdata);
  631. ieee80211_ibss_disconnect(sdata);
  632. synchronize_rcu();
  633. skb_queue_purge(&sdata->skb_queue);
  634. /* trigger a scan to find another IBSS network to join */
  635. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  636. sdata_unlock(sdata);
  637. }
  638. static void ieee80211_ibss_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
  639. {
  640. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  641. int err;
  642. /* if the current channel is a DFS channel, mark the channel as
  643. * unavailable.
  644. */
  645. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  646. &ifibss->chandef,
  647. NL80211_IFTYPE_ADHOC);
  648. if (err > 0)
  649. cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
  650. GFP_ATOMIC);
  651. }
  652. static bool
  653. ieee80211_ibss_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  654. struct ieee802_11_elems *elems,
  655. bool beacon)
  656. {
  657. struct cfg80211_csa_settings params;
  658. struct ieee80211_csa_ie csa_ie;
  659. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  660. enum nl80211_channel_type ch_type;
  661. int err;
  662. u32 sta_flags;
  663. sdata_assert_lock(sdata);
  664. sta_flags = IEEE80211_STA_DISABLE_VHT;
  665. switch (ifibss->chandef.width) {
  666. case NL80211_CHAN_WIDTH_5:
  667. case NL80211_CHAN_WIDTH_10:
  668. case NL80211_CHAN_WIDTH_20_NOHT:
  669. sta_flags |= IEEE80211_STA_DISABLE_HT;
  670. /* fall through */
  671. case NL80211_CHAN_WIDTH_20:
  672. sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
  673. break;
  674. default:
  675. break;
  676. }
  677. memset(&params, 0, sizeof(params));
  678. memset(&csa_ie, 0, sizeof(csa_ie));
  679. err = ieee80211_parse_ch_switch_ie(sdata, elems,
  680. ifibss->chandef.chan->band,
  681. sta_flags, ifibss->bssid, &csa_ie);
  682. /* can't switch to destination channel, fail */
  683. if (err < 0)
  684. goto disconnect;
  685. /* did not contain a CSA */
  686. if (err)
  687. return false;
  688. /* channel switch is not supported, disconnect */
  689. if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
  690. goto disconnect;
  691. params.count = csa_ie.count;
  692. params.chandef = csa_ie.chandef;
  693. switch (ifibss->chandef.width) {
  694. case NL80211_CHAN_WIDTH_20_NOHT:
  695. case NL80211_CHAN_WIDTH_20:
  696. case NL80211_CHAN_WIDTH_40:
  697. /* keep our current HT mode (HT20/HT40+/HT40-), even if
  698. * another mode has been announced. The mode is not adopted
  699. * within the beacon while doing CSA and we should therefore
  700. * keep the mode which we announce.
  701. */
  702. ch_type = cfg80211_get_chandef_type(&ifibss->chandef);
  703. cfg80211_chandef_create(&params.chandef, params.chandef.chan,
  704. ch_type);
  705. break;
  706. case NL80211_CHAN_WIDTH_5:
  707. case NL80211_CHAN_WIDTH_10:
  708. if (params.chandef.width != ifibss->chandef.width) {
  709. sdata_info(sdata,
  710. "IBSS %pM received channel switch from incompatible channel width (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
  711. ifibss->bssid,
  712. params.chandef.chan->center_freq,
  713. params.chandef.width,
  714. params.chandef.center_freq1,
  715. params.chandef.center_freq2);
  716. goto disconnect;
  717. }
  718. break;
  719. default:
  720. /* should not happen, sta_flags should prevent VHT modes. */
  721. WARN_ON(1);
  722. goto disconnect;
  723. }
  724. if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
  725. NL80211_IFTYPE_ADHOC)) {
  726. sdata_info(sdata,
  727. "IBSS %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
  728. ifibss->bssid,
  729. params.chandef.chan->center_freq,
  730. params.chandef.width,
  731. params.chandef.center_freq1,
  732. params.chandef.center_freq2);
  733. goto disconnect;
  734. }
  735. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  736. &params.chandef,
  737. NL80211_IFTYPE_ADHOC);
  738. if (err < 0)
  739. goto disconnect;
  740. if (err > 0 && !ifibss->userspace_handles_dfs) {
  741. /* IBSS-DFS only allowed with a control program */
  742. goto disconnect;
  743. }
  744. params.radar_required = err;
  745. if (cfg80211_chandef_identical(&params.chandef,
  746. &sdata->vif.bss_conf.chandef)) {
  747. ibss_dbg(sdata,
  748. "received csa with an identical chandef, ignoring\n");
  749. return true;
  750. }
  751. /* all checks done, now perform the channel switch. */
  752. ibss_dbg(sdata,
  753. "received channel switch announcement to go to channel %d MHz\n",
  754. params.chandef.chan->center_freq);
  755. params.block_tx = !!csa_ie.mode;
  756. if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
  757. &params))
  758. goto disconnect;
  759. ieee80211_ibss_csa_mark_radar(sdata);
  760. return true;
  761. disconnect:
  762. ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
  763. ieee80211_queue_work(&sdata->local->hw,
  764. &ifibss->csa_connection_drop_work);
  765. ieee80211_ibss_csa_mark_radar(sdata);
  766. return true;
  767. }
  768. static void
  769. ieee80211_rx_mgmt_spectrum_mgmt(struct ieee80211_sub_if_data *sdata,
  770. struct ieee80211_mgmt *mgmt, size_t len,
  771. struct ieee80211_rx_status *rx_status,
  772. struct ieee802_11_elems *elems)
  773. {
  774. int required_len;
  775. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  776. return;
  777. /* CSA is the only action we handle for now */
  778. if (mgmt->u.action.u.measurement.action_code !=
  779. WLAN_ACTION_SPCT_CHL_SWITCH)
  780. return;
  781. required_len = IEEE80211_MIN_ACTION_SIZE +
  782. sizeof(mgmt->u.action.u.chan_switch);
  783. if (len < required_len)
  784. return;
  785. if (!sdata->vif.csa_active)
  786. ieee80211_ibss_process_chanswitch(sdata, elems, false);
  787. }
  788. static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
  789. struct ieee80211_mgmt *mgmt,
  790. size_t len)
  791. {
  792. u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
  793. if (len < IEEE80211_DEAUTH_FRAME_LEN)
  794. return;
  795. ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM BSSID=%pM (reason: %d)\n",
  796. mgmt->sa, mgmt->da, mgmt->bssid, reason);
  797. sta_info_destroy_addr(sdata, mgmt->sa);
  798. }
  799. static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
  800. struct ieee80211_mgmt *mgmt,
  801. size_t len)
  802. {
  803. u16 auth_alg, auth_transaction;
  804. sdata_assert_lock(sdata);
  805. if (len < 24 + 6)
  806. return;
  807. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  808. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  809. ibss_dbg(sdata,
  810. "RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
  811. mgmt->sa, mgmt->da, mgmt->bssid, auth_transaction);
  812. if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
  813. return;
  814. /*
  815. * IEEE 802.11 standard does not require authentication in IBSS
  816. * networks and most implementations do not seem to use it.
  817. * However, try to reply to authentication attempts if someone
  818. * has actually implemented this.
  819. */
  820. ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
  821. mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
  822. }
  823. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  824. struct ieee80211_mgmt *mgmt, size_t len,
  825. struct ieee80211_rx_status *rx_status,
  826. struct ieee802_11_elems *elems)
  827. {
  828. struct ieee80211_local *local = sdata->local;
  829. struct cfg80211_bss *cbss;
  830. struct ieee80211_bss *bss;
  831. struct sta_info *sta;
  832. struct ieee80211_channel *channel;
  833. u64 beacon_timestamp, rx_timestamp;
  834. u32 supp_rates = 0;
  835. enum ieee80211_band band = rx_status->band;
  836. enum nl80211_bss_scan_width scan_width;
  837. struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
  838. bool rates_updated = false;
  839. channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
  840. if (!channel)
  841. return;
  842. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  843. ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid)) {
  844. rcu_read_lock();
  845. sta = sta_info_get(sdata, mgmt->sa);
  846. if (elems->supp_rates) {
  847. supp_rates = ieee80211_sta_get_rates(sdata, elems,
  848. band, NULL);
  849. if (sta) {
  850. u32 prev_rates;
  851. prev_rates = sta->sta.supp_rates[band];
  852. /* make sure mandatory rates are always added */
  853. scan_width = NL80211_BSS_CHAN_WIDTH_20;
  854. if (rx_status->flag & RX_FLAG_5MHZ)
  855. scan_width = NL80211_BSS_CHAN_WIDTH_5;
  856. if (rx_status->flag & RX_FLAG_10MHZ)
  857. scan_width = NL80211_BSS_CHAN_WIDTH_10;
  858. sta->sta.supp_rates[band] = supp_rates |
  859. ieee80211_mandatory_rates(sband,
  860. scan_width);
  861. if (sta->sta.supp_rates[band] != prev_rates) {
  862. ibss_dbg(sdata,
  863. "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
  864. sta->sta.addr, prev_rates,
  865. sta->sta.supp_rates[band]);
  866. rates_updated = true;
  867. }
  868. } else {
  869. rcu_read_unlock();
  870. sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
  871. mgmt->sa, supp_rates);
  872. }
  873. }
  874. if (sta && elems->wmm_info)
  875. sta->sta.wme = true;
  876. if (sta && elems->ht_operation && elems->ht_cap_elem &&
  877. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
  878. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
  879. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
  880. /* we both use HT */
  881. struct ieee80211_ht_cap htcap_ie;
  882. struct cfg80211_chan_def chandef;
  883. ieee80211_ht_oper_to_chandef(channel,
  884. elems->ht_operation,
  885. &chandef);
  886. memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
  887. /*
  888. * fall back to HT20 if we don't use or use
  889. * the other extension channel
  890. */
  891. if (chandef.center_freq1 !=
  892. sdata->u.ibss.chandef.center_freq1)
  893. htcap_ie.cap_info &=
  894. cpu_to_le16(~IEEE80211_HT_CAP_SUP_WIDTH_20_40);
  895. rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(
  896. sdata, sband, &htcap_ie, sta);
  897. }
  898. if (sta && rates_updated) {
  899. u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
  900. u8 rx_nss = sta->sta.rx_nss;
  901. /* Force rx_nss recalculation */
  902. sta->sta.rx_nss = 0;
  903. rate_control_rate_init(sta);
  904. if (sta->sta.rx_nss != rx_nss)
  905. changed |= IEEE80211_RC_NSS_CHANGED;
  906. drv_sta_rc_update(local, sdata, &sta->sta, changed);
  907. }
  908. rcu_read_unlock();
  909. }
  910. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  911. channel);
  912. if (!bss)
  913. return;
  914. cbss = container_of((void *)bss, struct cfg80211_bss, priv);
  915. /* same for beacon and probe response */
  916. beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
  917. /* check if we need to merge IBSS */
  918. /* not an IBSS */
  919. if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
  920. goto put_bss;
  921. /* different channel */
  922. if (sdata->u.ibss.fixed_channel &&
  923. sdata->u.ibss.chandef.chan != cbss->channel)
  924. goto put_bss;
  925. /* different SSID */
  926. if (elems->ssid_len != sdata->u.ibss.ssid_len ||
  927. memcmp(elems->ssid, sdata->u.ibss.ssid,
  928. sdata->u.ibss.ssid_len))
  929. goto put_bss;
  930. /* process channel switch */
  931. if (sdata->vif.csa_active ||
  932. ieee80211_ibss_process_chanswitch(sdata, elems, true))
  933. goto put_bss;
  934. /* same BSSID */
  935. if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
  936. goto put_bss;
  937. /* we use a fixed BSSID */
  938. if (sdata->u.ibss.fixed_bssid)
  939. goto put_bss;
  940. if (ieee80211_have_rx_timestamp(rx_status)) {
  941. /* time when timestamp field was received */
  942. rx_timestamp =
  943. ieee80211_calculate_rx_timestamp(local, rx_status,
  944. len + FCS_LEN, 24);
  945. } else {
  946. /*
  947. * second best option: get current TSF
  948. * (will return -1 if not supported)
  949. */
  950. rx_timestamp = drv_get_tsf(local, sdata);
  951. }
  952. ibss_dbg(sdata,
  953. "RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  954. mgmt->sa, mgmt->bssid,
  955. (unsigned long long)rx_timestamp,
  956. (unsigned long long)beacon_timestamp,
  957. (unsigned long long)(rx_timestamp - beacon_timestamp),
  958. jiffies);
  959. if (beacon_timestamp > rx_timestamp) {
  960. ibss_dbg(sdata,
  961. "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
  962. mgmt->bssid);
  963. ieee80211_sta_join_ibss(sdata, bss);
  964. supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
  965. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
  966. supp_rates);
  967. rcu_read_unlock();
  968. }
  969. put_bss:
  970. ieee80211_rx_bss_put(local, bss);
  971. }
  972. void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
  973. const u8 *bssid, const u8 *addr,
  974. u32 supp_rates)
  975. {
  976. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  977. struct ieee80211_local *local = sdata->local;
  978. struct sta_info *sta;
  979. struct ieee80211_chanctx_conf *chanctx_conf;
  980. struct ieee80211_supported_band *sband;
  981. enum nl80211_bss_scan_width scan_width;
  982. int band;
  983. /*
  984. * XXX: Consider removing the least recently used entry and
  985. * allow new one to be added.
  986. */
  987. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  988. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  989. sdata->name, addr);
  990. return;
  991. }
  992. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
  993. return;
  994. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
  995. return;
  996. rcu_read_lock();
  997. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  998. if (WARN_ON_ONCE(!chanctx_conf)) {
  999. rcu_read_unlock();
  1000. return;
  1001. }
  1002. band = chanctx_conf->def.chan->band;
  1003. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  1004. rcu_read_unlock();
  1005. sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
  1006. if (!sta)
  1007. return;
  1008. sta->last_rx = jiffies;
  1009. /* make sure mandatory rates are always added */
  1010. sband = local->hw.wiphy->bands[band];
  1011. sta->sta.supp_rates[band] = supp_rates |
  1012. ieee80211_mandatory_rates(sband, scan_width);
  1013. spin_lock(&ifibss->incomplete_lock);
  1014. list_add(&sta->list, &ifibss->incomplete_stations);
  1015. spin_unlock(&ifibss->incomplete_lock);
  1016. ieee80211_queue_work(&local->hw, &sdata->work);
  1017. }
  1018. static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
  1019. {
  1020. struct ieee80211_local *local = sdata->local;
  1021. struct sta_info *sta, *tmp;
  1022. unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
  1023. unsigned long exp_rsn_time = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
  1024. mutex_lock(&local->sta_mtx);
  1025. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  1026. if (sdata != sta->sdata)
  1027. continue;
  1028. if (time_after(jiffies, sta->last_rx + exp_time) ||
  1029. (time_after(jiffies, sta->last_rx + exp_rsn_time) &&
  1030. sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
  1031. sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
  1032. sta->sta_state != IEEE80211_STA_AUTHORIZED ?
  1033. "not authorized " : "", sta->sta.addr);
  1034. WARN_ON(__sta_info_destroy(sta));
  1035. }
  1036. }
  1037. mutex_unlock(&local->sta_mtx);
  1038. }
  1039. /*
  1040. * This function is called with state == IEEE80211_IBSS_MLME_JOINED
  1041. */
  1042. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
  1043. {
  1044. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1045. enum nl80211_bss_scan_width scan_width;
  1046. sdata_assert_lock(sdata);
  1047. mod_timer(&ifibss->timer,
  1048. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  1049. ieee80211_ibss_sta_expire(sdata);
  1050. if (time_before(jiffies, ifibss->last_scan_completed +
  1051. IEEE80211_IBSS_MERGE_INTERVAL))
  1052. return;
  1053. if (ieee80211_sta_active_ibss(sdata))
  1054. return;
  1055. if (ifibss->fixed_channel)
  1056. return;
  1057. sdata_info(sdata,
  1058. "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
  1059. scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
  1060. ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
  1061. NULL, scan_width);
  1062. }
  1063. static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
  1064. {
  1065. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1066. u8 bssid[ETH_ALEN];
  1067. u16 capability;
  1068. int i;
  1069. sdata_assert_lock(sdata);
  1070. if (ifibss->fixed_bssid) {
  1071. memcpy(bssid, ifibss->bssid, ETH_ALEN);
  1072. } else {
  1073. /* Generate random, not broadcast, locally administered BSSID. Mix in
  1074. * own MAC address to make sure that devices that do not have proper
  1075. * random number generator get different BSSID. */
  1076. get_random_bytes(bssid, ETH_ALEN);
  1077. for (i = 0; i < ETH_ALEN; i++)
  1078. bssid[i] ^= sdata->vif.addr[i];
  1079. bssid[0] &= ~0x01;
  1080. bssid[0] |= 0x02;
  1081. }
  1082. sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
  1083. capability = WLAN_CAPABILITY_IBSS;
  1084. if (ifibss->privacy)
  1085. capability |= WLAN_CAPABILITY_PRIVACY;
  1086. else
  1087. sdata->drop_unencrypted = 0;
  1088. __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
  1089. &ifibss->chandef, ifibss->basic_rates,
  1090. capability, 0, true);
  1091. }
  1092. /*
  1093. * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
  1094. */
  1095. static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
  1096. {
  1097. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1098. struct ieee80211_local *local = sdata->local;
  1099. struct cfg80211_bss *cbss;
  1100. struct ieee80211_channel *chan = NULL;
  1101. const u8 *bssid = NULL;
  1102. enum nl80211_bss_scan_width scan_width;
  1103. int active_ibss;
  1104. u16 capability;
  1105. sdata_assert_lock(sdata);
  1106. active_ibss = ieee80211_sta_active_ibss(sdata);
  1107. ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
  1108. if (active_ibss)
  1109. return;
  1110. capability = WLAN_CAPABILITY_IBSS;
  1111. if (ifibss->privacy)
  1112. capability |= WLAN_CAPABILITY_PRIVACY;
  1113. if (ifibss->fixed_bssid)
  1114. bssid = ifibss->bssid;
  1115. if (ifibss->fixed_channel)
  1116. chan = ifibss->chandef.chan;
  1117. if (!is_zero_ether_addr(ifibss->bssid))
  1118. bssid = ifibss->bssid;
  1119. cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
  1120. ifibss->ssid, ifibss->ssid_len,
  1121. WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
  1122. capability);
  1123. if (cbss) {
  1124. struct ieee80211_bss *bss;
  1125. bss = (void *)cbss->priv;
  1126. ibss_dbg(sdata,
  1127. "sta_find_ibss: selected %pM current %pM\n",
  1128. cbss->bssid, ifibss->bssid);
  1129. sdata_info(sdata,
  1130. "Selected IBSS BSSID %pM based on configured SSID\n",
  1131. cbss->bssid);
  1132. ieee80211_sta_join_ibss(sdata, bss);
  1133. ieee80211_rx_bss_put(local, bss);
  1134. return;
  1135. }
  1136. /* if a fixed bssid and a fixed freq have been provided create the IBSS
  1137. * directly and do not waste time scanning
  1138. */
  1139. if (ifibss->fixed_bssid && ifibss->fixed_channel) {
  1140. sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
  1141. bssid);
  1142. ieee80211_sta_create_ibss(sdata);
  1143. return;
  1144. }
  1145. ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
  1146. /* Selected IBSS not found in current scan results - try to scan */
  1147. if (time_after(jiffies, ifibss->last_scan_completed +
  1148. IEEE80211_SCAN_INTERVAL)) {
  1149. sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
  1150. scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
  1151. ieee80211_request_ibss_scan(sdata, ifibss->ssid,
  1152. ifibss->ssid_len, chan,
  1153. scan_width);
  1154. } else {
  1155. int interval = IEEE80211_SCAN_INTERVAL;
  1156. if (time_after(jiffies, ifibss->ibss_join_req +
  1157. IEEE80211_IBSS_JOIN_TIMEOUT))
  1158. ieee80211_sta_create_ibss(sdata);
  1159. mod_timer(&ifibss->timer,
  1160. round_jiffies(jiffies + interval));
  1161. }
  1162. }
  1163. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  1164. struct sk_buff *req)
  1165. {
  1166. struct ieee80211_mgmt *mgmt = (void *)req->data;
  1167. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1168. struct ieee80211_local *local = sdata->local;
  1169. int tx_last_beacon, len = req->len;
  1170. struct sk_buff *skb;
  1171. struct beacon_data *presp;
  1172. u8 *pos, *end;
  1173. sdata_assert_lock(sdata);
  1174. presp = rcu_dereference_protected(ifibss->presp,
  1175. lockdep_is_held(&sdata->wdev.mtx));
  1176. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  1177. len < 24 + 2 || !presp)
  1178. return;
  1179. tx_last_beacon = drv_tx_last_beacon(local);
  1180. ibss_dbg(sdata,
  1181. "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
  1182. mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
  1183. if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
  1184. return;
  1185. if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
  1186. !is_broadcast_ether_addr(mgmt->bssid))
  1187. return;
  1188. end = ((u8 *) mgmt) + len;
  1189. pos = mgmt->u.probe_req.variable;
  1190. if (pos[0] != WLAN_EID_SSID ||
  1191. pos + 2 + pos[1] > end) {
  1192. ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
  1193. mgmt->sa);
  1194. return;
  1195. }
  1196. if (pos[1] != 0 &&
  1197. (pos[1] != ifibss->ssid_len ||
  1198. memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
  1199. /* Ignore ProbeReq for foreign SSID */
  1200. return;
  1201. }
  1202. /* Reply with ProbeResp */
  1203. skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
  1204. if (!skb)
  1205. return;
  1206. skb_reserve(skb, local->tx_headroom);
  1207. memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
  1208. memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
  1209. ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
  1210. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1211. /* avoid excessive retries for probe request to wildcard SSIDs */
  1212. if (pos[1] == 0)
  1213. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
  1214. ieee80211_tx_skb(sdata, skb);
  1215. }
  1216. static
  1217. void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
  1218. struct ieee80211_mgmt *mgmt, size_t len,
  1219. struct ieee80211_rx_status *rx_status)
  1220. {
  1221. size_t baselen;
  1222. struct ieee802_11_elems elems;
  1223. BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
  1224. offsetof(typeof(mgmt->u.beacon), variable));
  1225. /*
  1226. * either beacon or probe_resp but the variable field is at the
  1227. * same offset
  1228. */
  1229. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1230. if (baselen > len)
  1231. return;
  1232. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1233. false, &elems);
  1234. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  1235. }
  1236. void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1237. struct sk_buff *skb)
  1238. {
  1239. struct ieee80211_rx_status *rx_status;
  1240. struct ieee80211_mgmt *mgmt;
  1241. u16 fc;
  1242. struct ieee802_11_elems elems;
  1243. int ies_len;
  1244. rx_status = IEEE80211_SKB_RXCB(skb);
  1245. mgmt = (struct ieee80211_mgmt *) skb->data;
  1246. fc = le16_to_cpu(mgmt->frame_control);
  1247. sdata_lock(sdata);
  1248. if (!sdata->u.ibss.ssid_len)
  1249. goto mgmt_out; /* not ready to merge yet */
  1250. switch (fc & IEEE80211_FCTL_STYPE) {
  1251. case IEEE80211_STYPE_PROBE_REQ:
  1252. ieee80211_rx_mgmt_probe_req(sdata, skb);
  1253. break;
  1254. case IEEE80211_STYPE_PROBE_RESP:
  1255. case IEEE80211_STYPE_BEACON:
  1256. ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
  1257. rx_status);
  1258. break;
  1259. case IEEE80211_STYPE_AUTH:
  1260. ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
  1261. break;
  1262. case IEEE80211_STYPE_DEAUTH:
  1263. ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
  1264. break;
  1265. case IEEE80211_STYPE_ACTION:
  1266. switch (mgmt->u.action.category) {
  1267. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1268. ies_len = skb->len -
  1269. offsetof(struct ieee80211_mgmt,
  1270. u.action.u.chan_switch.variable);
  1271. if (ies_len < 0)
  1272. break;
  1273. ieee802_11_parse_elems(
  1274. mgmt->u.action.u.chan_switch.variable,
  1275. ies_len, true, &elems);
  1276. if (elems.parse_error)
  1277. break;
  1278. ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt, skb->len,
  1279. rx_status, &elems);
  1280. break;
  1281. }
  1282. }
  1283. mgmt_out:
  1284. sdata_unlock(sdata);
  1285. }
  1286. void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
  1287. {
  1288. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1289. struct sta_info *sta;
  1290. sdata_lock(sdata);
  1291. /*
  1292. * Work could be scheduled after scan or similar
  1293. * when we aren't even joined (or trying) with a
  1294. * network.
  1295. */
  1296. if (!ifibss->ssid_len)
  1297. goto out;
  1298. spin_lock_bh(&ifibss->incomplete_lock);
  1299. while (!list_empty(&ifibss->incomplete_stations)) {
  1300. sta = list_first_entry(&ifibss->incomplete_stations,
  1301. struct sta_info, list);
  1302. list_del(&sta->list);
  1303. spin_unlock_bh(&ifibss->incomplete_lock);
  1304. ieee80211_ibss_finish_sta(sta);
  1305. rcu_read_unlock();
  1306. spin_lock_bh(&ifibss->incomplete_lock);
  1307. }
  1308. spin_unlock_bh(&ifibss->incomplete_lock);
  1309. switch (ifibss->state) {
  1310. case IEEE80211_IBSS_MLME_SEARCH:
  1311. ieee80211_sta_find_ibss(sdata);
  1312. break;
  1313. case IEEE80211_IBSS_MLME_JOINED:
  1314. ieee80211_sta_merge_ibss(sdata);
  1315. break;
  1316. default:
  1317. WARN_ON(1);
  1318. break;
  1319. }
  1320. out:
  1321. sdata_unlock(sdata);
  1322. }
  1323. static void ieee80211_ibss_timer(unsigned long data)
  1324. {
  1325. struct ieee80211_sub_if_data *sdata =
  1326. (struct ieee80211_sub_if_data *) data;
  1327. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1328. }
  1329. void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1330. {
  1331. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1332. setup_timer(&ifibss->timer, ieee80211_ibss_timer,
  1333. (unsigned long) sdata);
  1334. INIT_LIST_HEAD(&ifibss->incomplete_stations);
  1335. spin_lock_init(&ifibss->incomplete_lock);
  1336. INIT_WORK(&ifibss->csa_connection_drop_work,
  1337. ieee80211_csa_connection_drop_work);
  1338. }
  1339. /* scan finished notification */
  1340. void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
  1341. {
  1342. struct ieee80211_sub_if_data *sdata;
  1343. mutex_lock(&local->iflist_mtx);
  1344. list_for_each_entry(sdata, &local->interfaces, list) {
  1345. if (!ieee80211_sdata_running(sdata))
  1346. continue;
  1347. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  1348. continue;
  1349. sdata->u.ibss.last_scan_completed = jiffies;
  1350. ieee80211_queue_work(&local->hw, &sdata->work);
  1351. }
  1352. mutex_unlock(&local->iflist_mtx);
  1353. }
  1354. int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
  1355. struct cfg80211_ibss_params *params)
  1356. {
  1357. u32 changed = 0;
  1358. u32 rate_flags;
  1359. struct ieee80211_supported_band *sband;
  1360. enum ieee80211_chanctx_mode chanmode;
  1361. struct ieee80211_local *local = sdata->local;
  1362. int radar_detect_width = 0;
  1363. int i;
  1364. int ret;
  1365. ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
  1366. &params->chandef,
  1367. sdata->wdev.iftype);
  1368. if (ret < 0)
  1369. return ret;
  1370. if (ret > 0) {
  1371. if (!params->userspace_handles_dfs)
  1372. return -EINVAL;
  1373. radar_detect_width = BIT(params->chandef.width);
  1374. }
  1375. chanmode = (params->channel_fixed && !ret) ?
  1376. IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
  1377. mutex_lock(&local->chanctx_mtx);
  1378. ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
  1379. radar_detect_width);
  1380. mutex_unlock(&local->chanctx_mtx);
  1381. if (ret < 0)
  1382. return ret;
  1383. if (params->bssid) {
  1384. memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
  1385. sdata->u.ibss.fixed_bssid = true;
  1386. } else
  1387. sdata->u.ibss.fixed_bssid = false;
  1388. sdata->u.ibss.privacy = params->privacy;
  1389. sdata->u.ibss.control_port = params->control_port;
  1390. sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
  1391. sdata->u.ibss.basic_rates = params->basic_rates;
  1392. sdata->u.ibss.last_scan_completed = jiffies;
  1393. /* fix basic_rates if channel does not support these rates */
  1394. rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
  1395. sband = local->hw.wiphy->bands[params->chandef.chan->band];
  1396. for (i = 0; i < sband->n_bitrates; i++) {
  1397. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1398. sdata->u.ibss.basic_rates &= ~BIT(i);
  1399. }
  1400. memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
  1401. sizeof(params->mcast_rate));
  1402. sdata->vif.bss_conf.beacon_int = params->beacon_interval;
  1403. sdata->u.ibss.chandef = params->chandef;
  1404. sdata->u.ibss.fixed_channel = params->channel_fixed;
  1405. if (params->ie) {
  1406. sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
  1407. GFP_KERNEL);
  1408. if (sdata->u.ibss.ie)
  1409. sdata->u.ibss.ie_len = params->ie_len;
  1410. }
  1411. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  1412. sdata->u.ibss.ibss_join_req = jiffies;
  1413. memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
  1414. sdata->u.ibss.ssid_len = params->ssid_len;
  1415. memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
  1416. sizeof(sdata->u.ibss.ht_capa));
  1417. memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
  1418. sizeof(sdata->u.ibss.ht_capa_mask));
  1419. /*
  1420. * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
  1421. * reserved, but an HT STA shall protect HT transmissions as though
  1422. * the HT Protection field were set to non-HT mixed mode.
  1423. *
  1424. * In an IBSS, the RIFS Mode field of the HT Operation element is
  1425. * also reserved, but an HT STA shall operate as though this field
  1426. * were set to 1.
  1427. */
  1428. sdata->vif.bss_conf.ht_operation_mode |=
  1429. IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
  1430. | IEEE80211_HT_PARAM_RIFS_MODE;
  1431. changed |= BSS_CHANGED_HT;
  1432. ieee80211_bss_info_change_notify(sdata, changed);
  1433. sdata->smps_mode = IEEE80211_SMPS_OFF;
  1434. sdata->needed_rx_chains = local->rx_chains;
  1435. ieee80211_queue_work(&local->hw, &sdata->work);
  1436. return 0;
  1437. }
  1438. int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
  1439. {
  1440. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1441. ieee80211_ibss_disconnect(sdata);
  1442. ifibss->ssid_len = 0;
  1443. memset(ifibss->bssid, 0, ETH_ALEN);
  1444. /* remove beacon */
  1445. kfree(sdata->u.ibss.ie);
  1446. /* on the next join, re-program HT parameters */
  1447. memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
  1448. memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
  1449. synchronize_rcu();
  1450. skb_queue_purge(&sdata->skb_queue);
  1451. del_timer_sync(&sdata->u.ibss.timer);
  1452. return 0;
  1453. }