ibss.c 50 KB

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