ibss.c 51 KB

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