ibss.c 50 KB

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