ibss.c 51 KB

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