ibss.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  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 == NL80211_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 == NL80211_BAND_5GHZ;
  310. bss_change |= BSS_CHANGED_ERP_SLOT;
  311. /* cf. IEEE 802.11 9.2.12 */
  312. if (chan->band == NL80211_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_NO_HT);
  375. chandef.width = sdata->u.ibss.chandef.width;
  376. break;
  377. case NL80211_CHAN_WIDTH_80:
  378. case NL80211_CHAN_WIDTH_80P80:
  379. case NL80211_CHAN_WIDTH_160:
  380. chandef = sdata->u.ibss.chandef;
  381. chandef.chan = cbss->channel;
  382. break;
  383. default:
  384. /* fall back to 20 MHz for unsupported modes */
  385. cfg80211_chandef_create(&chandef, cbss->channel,
  386. NL80211_CHAN_NO_HT);
  387. break;
  388. }
  389. sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
  390. rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
  391. shift = ieee80211_vif_get_shift(&sdata->vif);
  392. basic_rates = 0;
  393. for (i = 0; i < bss->supp_rates_len; i++) {
  394. int rate = bss->supp_rates[i] & 0x7f;
  395. bool is_basic = !!(bss->supp_rates[i] & 0x80);
  396. for (j = 0; j < sband->n_bitrates; j++) {
  397. int brate;
  398. if ((rate_flags & sband->bitrates[j].flags)
  399. != rate_flags)
  400. continue;
  401. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  402. 5 * (1 << shift));
  403. if (brate == rate) {
  404. if (is_basic)
  405. basic_rates |= BIT(j);
  406. break;
  407. }
  408. }
  409. }
  410. rcu_read_lock();
  411. ies = rcu_dereference(cbss->ies);
  412. tsf = ies->tsf;
  413. rcu_read_unlock();
  414. __ieee80211_sta_join_ibss(sdata, cbss->bssid,
  415. beacon_int,
  416. &chandef,
  417. basic_rates,
  418. cbss->capability,
  419. tsf, false);
  420. }
  421. int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
  422. struct cfg80211_csa_settings *csa_settings)
  423. {
  424. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  425. struct beacon_data *presp, *old_presp;
  426. struct cfg80211_bss *cbss;
  427. const struct cfg80211_bss_ies *ies;
  428. u16 capability = WLAN_CAPABILITY_IBSS;
  429. u64 tsf;
  430. int ret = 0;
  431. sdata_assert_lock(sdata);
  432. if (ifibss->privacy)
  433. capability |= WLAN_CAPABILITY_PRIVACY;
  434. cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
  435. ifibss->bssid, ifibss->ssid,
  436. ifibss->ssid_len, IEEE80211_BSS_TYPE_IBSS,
  437. IEEE80211_PRIVACY(ifibss->privacy));
  438. if (WARN_ON(!cbss)) {
  439. ret = -EINVAL;
  440. goto out;
  441. }
  442. rcu_read_lock();
  443. ies = rcu_dereference(cbss->ies);
  444. tsf = ies->tsf;
  445. rcu_read_unlock();
  446. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  447. old_presp = rcu_dereference_protected(ifibss->presp,
  448. lockdep_is_held(&sdata->wdev.mtx));
  449. presp = ieee80211_ibss_build_presp(sdata,
  450. sdata->vif.bss_conf.beacon_int,
  451. sdata->vif.bss_conf.basic_rates,
  452. capability, tsf, &ifibss->chandef,
  453. NULL, csa_settings);
  454. if (!presp) {
  455. ret = -ENOMEM;
  456. goto out;
  457. }
  458. rcu_assign_pointer(ifibss->presp, presp);
  459. if (old_presp)
  460. kfree_rcu(old_presp, rcu_head);
  461. return BSS_CHANGED_BEACON;
  462. out:
  463. return ret;
  464. }
  465. int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata)
  466. {
  467. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  468. struct cfg80211_bss *cbss;
  469. int err, changed = 0;
  470. sdata_assert_lock(sdata);
  471. /* update cfg80211 bss information with the new channel */
  472. if (!is_zero_ether_addr(ifibss->bssid)) {
  473. cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
  474. ifibss->chandef.chan,
  475. ifibss->bssid, ifibss->ssid,
  476. ifibss->ssid_len,
  477. IEEE80211_BSS_TYPE_IBSS,
  478. IEEE80211_PRIVACY(ifibss->privacy));
  479. /* XXX: should not really modify cfg80211 data */
  480. if (cbss) {
  481. cbss->channel = sdata->csa_chandef.chan;
  482. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  483. }
  484. }
  485. ifibss->chandef = sdata->csa_chandef;
  486. /* generate the beacon */
  487. err = ieee80211_ibss_csa_beacon(sdata, NULL);
  488. if (err < 0)
  489. return err;
  490. changed |= err;
  491. return changed;
  492. }
  493. void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
  494. {
  495. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  496. cancel_work_sync(&ifibss->csa_connection_drop_work);
  497. }
  498. static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
  499. __acquires(RCU)
  500. {
  501. struct ieee80211_sub_if_data *sdata = sta->sdata;
  502. u8 addr[ETH_ALEN];
  503. memcpy(addr, sta->sta.addr, ETH_ALEN);
  504. ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
  505. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  506. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  507. /* authorize the station only if the network is not RSN protected. If
  508. * not wait for the userspace to authorize it */
  509. if (!sta->sdata->u.ibss.control_port)
  510. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  511. rate_control_rate_init(sta);
  512. /* If it fails, maybe we raced another insertion? */
  513. if (sta_info_insert_rcu(sta))
  514. return sta_info_get(sdata, addr);
  515. return sta;
  516. }
  517. static struct sta_info *
  518. ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
  519. const u8 *addr, u32 supp_rates)
  520. __acquires(RCU)
  521. {
  522. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  523. struct ieee80211_local *local = sdata->local;
  524. struct sta_info *sta;
  525. struct ieee80211_chanctx_conf *chanctx_conf;
  526. struct ieee80211_supported_band *sband;
  527. enum nl80211_bss_scan_width scan_width;
  528. int band;
  529. /*
  530. * XXX: Consider removing the least recently used entry and
  531. * allow new one to be added.
  532. */
  533. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  534. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  535. sdata->name, addr);
  536. rcu_read_lock();
  537. return NULL;
  538. }
  539. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
  540. rcu_read_lock();
  541. return NULL;
  542. }
  543. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
  544. rcu_read_lock();
  545. return NULL;
  546. }
  547. rcu_read_lock();
  548. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  549. if (WARN_ON_ONCE(!chanctx_conf))
  550. return NULL;
  551. band = chanctx_conf->def.chan->band;
  552. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  553. rcu_read_unlock();
  554. sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
  555. if (!sta) {
  556. rcu_read_lock();
  557. return NULL;
  558. }
  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. unsigned long last_active = ieee80211_sta_last_active(sta);
  574. if (sta->sdata == sdata &&
  575. time_is_after_jiffies(last_active +
  576. IEEE80211_IBSS_MERGE_INTERVAL)) {
  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 BSSID=%pM (reason: %d)\n",
  804. mgmt->sa, mgmt->da, 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,
  818. "RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
  819. mgmt->sa, mgmt->da, 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 nl80211_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;
  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. sband = local->hw.wiphy->bands[band];
  849. if (WARN_ON(!sband))
  850. return;
  851. rcu_read_lock();
  852. sta = sta_info_get(sdata, mgmt->sa);
  853. if (elems->supp_rates) {
  854. supp_rates = ieee80211_sta_get_rates(sdata, elems,
  855. band, NULL);
  856. if (sta) {
  857. u32 prev_rates;
  858. prev_rates = sta->sta.supp_rates[band];
  859. /* make sure mandatory rates are always added */
  860. scan_width = NL80211_BSS_CHAN_WIDTH_20;
  861. if (rx_status->flag & RX_FLAG_5MHZ)
  862. scan_width = NL80211_BSS_CHAN_WIDTH_5;
  863. if (rx_status->flag & RX_FLAG_10MHZ)
  864. scan_width = NL80211_BSS_CHAN_WIDTH_10;
  865. sta->sta.supp_rates[band] = supp_rates |
  866. ieee80211_mandatory_rates(sband, scan_width);
  867. if (sta->sta.supp_rates[band] != prev_rates) {
  868. ibss_dbg(sdata,
  869. "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
  870. sta->sta.addr, prev_rates,
  871. sta->sta.supp_rates[band]);
  872. rates_updated = true;
  873. }
  874. } else {
  875. rcu_read_unlock();
  876. sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
  877. mgmt->sa, supp_rates);
  878. }
  879. }
  880. if (sta && !sta->sta.wme &&
  881. elems->wmm_info && local->hw.queues >= IEEE80211_NUM_ACS) {
  882. sta->sta.wme = true;
  883. ieee80211_check_fast_xmit(sta);
  884. }
  885. if (sta && elems->ht_operation && elems->ht_cap_elem &&
  886. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
  887. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
  888. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
  889. /* we both use HT */
  890. struct ieee80211_ht_cap htcap_ie;
  891. struct cfg80211_chan_def chandef;
  892. enum ieee80211_sta_rx_bandwidth bw = sta->sta.bandwidth;
  893. cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
  894. ieee80211_chandef_ht_oper(elems->ht_operation, &chandef);
  895. memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
  896. rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  897. &htcap_ie,
  898. sta);
  899. if (elems->vht_operation && elems->vht_cap_elem &&
  900. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20 &&
  901. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_40) {
  902. /* we both use VHT */
  903. struct ieee80211_vht_cap cap_ie;
  904. struct ieee80211_sta_vht_cap cap = sta->sta.vht_cap;
  905. ieee80211_chandef_vht_oper(elems->vht_operation,
  906. &chandef);
  907. memcpy(&cap_ie, elems->vht_cap_elem, sizeof(cap_ie));
  908. ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
  909. &cap_ie, sta);
  910. if (memcmp(&cap, &sta->sta.vht_cap, sizeof(cap)))
  911. rates_updated |= true;
  912. }
  913. if (bw != sta->sta.bandwidth)
  914. rates_updated |= true;
  915. if (!cfg80211_chandef_compatible(&sdata->u.ibss.chandef,
  916. &chandef))
  917. WARN_ON_ONCE(1);
  918. }
  919. if (sta && rates_updated) {
  920. u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
  921. u8 rx_nss = sta->sta.rx_nss;
  922. /* Force rx_nss recalculation */
  923. sta->sta.rx_nss = 0;
  924. rate_control_rate_init(sta);
  925. if (sta->sta.rx_nss != rx_nss)
  926. changed |= IEEE80211_RC_NSS_CHANGED;
  927. drv_sta_rc_update(local, sdata, &sta->sta, changed);
  928. }
  929. rcu_read_unlock();
  930. }
  931. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  932. struct ieee80211_mgmt *mgmt, size_t len,
  933. struct ieee80211_rx_status *rx_status,
  934. struct ieee802_11_elems *elems)
  935. {
  936. struct ieee80211_local *local = sdata->local;
  937. struct cfg80211_bss *cbss;
  938. struct ieee80211_bss *bss;
  939. struct ieee80211_channel *channel;
  940. u64 beacon_timestamp, rx_timestamp;
  941. u32 supp_rates = 0;
  942. enum nl80211_band band = rx_status->band;
  943. channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
  944. if (!channel)
  945. return;
  946. ieee80211_update_sta_info(sdata, mgmt, len, rx_status, elems, channel);
  947. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  948. channel);
  949. if (!bss)
  950. return;
  951. cbss = container_of((void *)bss, struct cfg80211_bss, priv);
  952. /* same for beacon and probe response */
  953. beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
  954. /* check if we need to merge IBSS */
  955. /* not an IBSS */
  956. if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
  957. goto put_bss;
  958. /* different channel */
  959. if (sdata->u.ibss.fixed_channel &&
  960. sdata->u.ibss.chandef.chan != cbss->channel)
  961. goto put_bss;
  962. /* different SSID */
  963. if (elems->ssid_len != sdata->u.ibss.ssid_len ||
  964. memcmp(elems->ssid, sdata->u.ibss.ssid,
  965. sdata->u.ibss.ssid_len))
  966. goto put_bss;
  967. /* process channel switch */
  968. if (sdata->vif.csa_active ||
  969. ieee80211_ibss_process_chanswitch(sdata, elems, true))
  970. goto put_bss;
  971. /* same BSSID */
  972. if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
  973. goto put_bss;
  974. /* we use a fixed BSSID */
  975. if (sdata->u.ibss.fixed_bssid)
  976. goto put_bss;
  977. if (ieee80211_have_rx_timestamp(rx_status)) {
  978. /* time when timestamp field was received */
  979. rx_timestamp =
  980. ieee80211_calculate_rx_timestamp(local, rx_status,
  981. len + FCS_LEN, 24);
  982. } else {
  983. /*
  984. * second best option: get current TSF
  985. * (will return -1 if not supported)
  986. */
  987. rx_timestamp = drv_get_tsf(local, sdata);
  988. }
  989. ibss_dbg(sdata,
  990. "RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  991. mgmt->sa, mgmt->bssid,
  992. (unsigned long long)rx_timestamp,
  993. (unsigned long long)beacon_timestamp,
  994. (unsigned long long)(rx_timestamp - beacon_timestamp),
  995. jiffies);
  996. if (beacon_timestamp > rx_timestamp) {
  997. ibss_dbg(sdata,
  998. "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
  999. mgmt->bssid);
  1000. ieee80211_sta_join_ibss(sdata, bss);
  1001. supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
  1002. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
  1003. supp_rates);
  1004. rcu_read_unlock();
  1005. }
  1006. put_bss:
  1007. ieee80211_rx_bss_put(local, bss);
  1008. }
  1009. void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
  1010. const u8 *bssid, const u8 *addr,
  1011. u32 supp_rates)
  1012. {
  1013. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1014. struct ieee80211_local *local = sdata->local;
  1015. struct sta_info *sta;
  1016. struct ieee80211_chanctx_conf *chanctx_conf;
  1017. struct ieee80211_supported_band *sband;
  1018. enum nl80211_bss_scan_width scan_width;
  1019. int band;
  1020. /*
  1021. * XXX: Consider removing the least recently used entry and
  1022. * allow new one to be added.
  1023. */
  1024. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  1025. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  1026. sdata->name, addr);
  1027. return;
  1028. }
  1029. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
  1030. return;
  1031. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
  1032. return;
  1033. rcu_read_lock();
  1034. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1035. if (WARN_ON_ONCE(!chanctx_conf)) {
  1036. rcu_read_unlock();
  1037. return;
  1038. }
  1039. band = chanctx_conf->def.chan->band;
  1040. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  1041. rcu_read_unlock();
  1042. sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
  1043. if (!sta)
  1044. return;
  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. unsigned long last_active = ieee80211_sta_last_active(sta);
  1063. if (sdata != sta->sdata)
  1064. continue;
  1065. if (time_is_before_jiffies(last_active + exp_time) ||
  1066. (time_is_before_jiffies(last_active + exp_rsn) &&
  1067. sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
  1068. sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
  1069. sta->sta_state != IEEE80211_STA_AUTHORIZED ?
  1070. "not authorized " : "", sta->sta.addr);
  1071. WARN_ON(__sta_info_destroy(sta));
  1072. }
  1073. }
  1074. mutex_unlock(&local->sta_mtx);
  1075. }
  1076. /*
  1077. * This function is called with state == IEEE80211_IBSS_MLME_JOINED
  1078. */
  1079. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
  1080. {
  1081. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1082. enum nl80211_bss_scan_width scan_width;
  1083. sdata_assert_lock(sdata);
  1084. mod_timer(&ifibss->timer,
  1085. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  1086. ieee80211_ibss_sta_expire(sdata);
  1087. if (time_before(jiffies, ifibss->last_scan_completed +
  1088. IEEE80211_IBSS_MERGE_INTERVAL))
  1089. return;
  1090. if (ieee80211_sta_active_ibss(sdata))
  1091. return;
  1092. if (ifibss->fixed_channel)
  1093. return;
  1094. sdata_info(sdata,
  1095. "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
  1096. scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
  1097. ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
  1098. NULL, 0, scan_width);
  1099. }
  1100. static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
  1101. {
  1102. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1103. u8 bssid[ETH_ALEN];
  1104. u16 capability;
  1105. int i;
  1106. sdata_assert_lock(sdata);
  1107. if (ifibss->fixed_bssid) {
  1108. memcpy(bssid, ifibss->bssid, ETH_ALEN);
  1109. } else {
  1110. /* Generate random, not broadcast, locally administered BSSID. Mix in
  1111. * own MAC address to make sure that devices that do not have proper
  1112. * random number generator get different BSSID. */
  1113. get_random_bytes(bssid, ETH_ALEN);
  1114. for (i = 0; i < ETH_ALEN; i++)
  1115. bssid[i] ^= sdata->vif.addr[i];
  1116. bssid[0] &= ~0x01;
  1117. bssid[0] |= 0x02;
  1118. }
  1119. sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
  1120. capability = WLAN_CAPABILITY_IBSS;
  1121. if (ifibss->privacy)
  1122. capability |= WLAN_CAPABILITY_PRIVACY;
  1123. __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
  1124. &ifibss->chandef, ifibss->basic_rates,
  1125. capability, 0, true);
  1126. }
  1127. static unsigned ibss_setup_channels(struct wiphy *wiphy,
  1128. struct ieee80211_channel **channels,
  1129. unsigned int channels_max,
  1130. u32 center_freq, u32 width)
  1131. {
  1132. struct ieee80211_channel *chan = NULL;
  1133. unsigned int n_chan = 0;
  1134. u32 start_freq, end_freq, freq;
  1135. if (width <= 20) {
  1136. start_freq = center_freq;
  1137. end_freq = center_freq;
  1138. } else {
  1139. start_freq = center_freq - width / 2 + 10;
  1140. end_freq = center_freq + width / 2 - 10;
  1141. }
  1142. for (freq = start_freq; freq <= end_freq; freq += 20) {
  1143. chan = ieee80211_get_channel(wiphy, freq);
  1144. if (!chan)
  1145. continue;
  1146. if (n_chan >= channels_max)
  1147. return n_chan;
  1148. channels[n_chan] = chan;
  1149. n_chan++;
  1150. }
  1151. return n_chan;
  1152. }
  1153. static unsigned int
  1154. ieee80211_ibss_setup_scan_channels(struct wiphy *wiphy,
  1155. const struct cfg80211_chan_def *chandef,
  1156. struct ieee80211_channel **channels,
  1157. unsigned int channels_max)
  1158. {
  1159. unsigned int n_chan = 0;
  1160. u32 width, cf1, cf2 = 0;
  1161. switch (chandef->width) {
  1162. case NL80211_CHAN_WIDTH_40:
  1163. width = 40;
  1164. break;
  1165. case NL80211_CHAN_WIDTH_80P80:
  1166. cf2 = chandef->center_freq2;
  1167. /* fall through */
  1168. case NL80211_CHAN_WIDTH_80:
  1169. width = 80;
  1170. break;
  1171. case NL80211_CHAN_WIDTH_160:
  1172. width = 160;
  1173. break;
  1174. default:
  1175. width = 20;
  1176. break;
  1177. }
  1178. cf1 = chandef->center_freq1;
  1179. n_chan = ibss_setup_channels(wiphy, channels, channels_max, cf1, width);
  1180. if (cf2)
  1181. n_chan += ibss_setup_channels(wiphy, &channels[n_chan],
  1182. channels_max - n_chan, cf2,
  1183. width);
  1184. return n_chan;
  1185. }
  1186. /*
  1187. * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
  1188. */
  1189. static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
  1190. {
  1191. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1192. struct ieee80211_local *local = sdata->local;
  1193. struct cfg80211_bss *cbss;
  1194. struct ieee80211_channel *chan = NULL;
  1195. const u8 *bssid = NULL;
  1196. enum nl80211_bss_scan_width scan_width;
  1197. int active_ibss;
  1198. sdata_assert_lock(sdata);
  1199. active_ibss = ieee80211_sta_active_ibss(sdata);
  1200. ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
  1201. if (active_ibss)
  1202. return;
  1203. if (ifibss->fixed_bssid)
  1204. bssid = ifibss->bssid;
  1205. if (ifibss->fixed_channel)
  1206. chan = ifibss->chandef.chan;
  1207. if (!is_zero_ether_addr(ifibss->bssid))
  1208. bssid = ifibss->bssid;
  1209. cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
  1210. ifibss->ssid, ifibss->ssid_len,
  1211. IEEE80211_BSS_TYPE_IBSS,
  1212. IEEE80211_PRIVACY(ifibss->privacy));
  1213. if (cbss) {
  1214. struct ieee80211_bss *bss;
  1215. bss = (void *)cbss->priv;
  1216. ibss_dbg(sdata,
  1217. "sta_find_ibss: selected %pM current %pM\n",
  1218. cbss->bssid, ifibss->bssid);
  1219. sdata_info(sdata,
  1220. "Selected IBSS BSSID %pM based on configured SSID\n",
  1221. cbss->bssid);
  1222. ieee80211_sta_join_ibss(sdata, bss);
  1223. ieee80211_rx_bss_put(local, bss);
  1224. return;
  1225. }
  1226. /* if a fixed bssid and a fixed freq have been provided create the IBSS
  1227. * directly and do not waste time scanning
  1228. */
  1229. if (ifibss->fixed_bssid && ifibss->fixed_channel) {
  1230. sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
  1231. bssid);
  1232. ieee80211_sta_create_ibss(sdata);
  1233. return;
  1234. }
  1235. ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
  1236. /* Selected IBSS not found in current scan results - try to scan */
  1237. if (time_after(jiffies, ifibss->last_scan_completed +
  1238. IEEE80211_SCAN_INTERVAL)) {
  1239. struct ieee80211_channel *channels[8];
  1240. unsigned int num;
  1241. sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
  1242. scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
  1243. if (ifibss->fixed_channel) {
  1244. num = ieee80211_ibss_setup_scan_channels(local->hw.wiphy,
  1245. &ifibss->chandef,
  1246. channels,
  1247. ARRAY_SIZE(channels));
  1248. ieee80211_request_ibss_scan(sdata, ifibss->ssid,
  1249. ifibss->ssid_len, channels,
  1250. num, scan_width);
  1251. } else {
  1252. ieee80211_request_ibss_scan(sdata, ifibss->ssid,
  1253. ifibss->ssid_len, NULL,
  1254. 0, scan_width);
  1255. }
  1256. } else {
  1257. int interval = IEEE80211_SCAN_INTERVAL;
  1258. if (time_after(jiffies, ifibss->ibss_join_req +
  1259. IEEE80211_IBSS_JOIN_TIMEOUT))
  1260. ieee80211_sta_create_ibss(sdata);
  1261. mod_timer(&ifibss->timer,
  1262. round_jiffies(jiffies + interval));
  1263. }
  1264. }
  1265. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  1266. struct sk_buff *req)
  1267. {
  1268. struct ieee80211_mgmt *mgmt = (void *)req->data;
  1269. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1270. struct ieee80211_local *local = sdata->local;
  1271. int tx_last_beacon, len = req->len;
  1272. struct sk_buff *skb;
  1273. struct beacon_data *presp;
  1274. u8 *pos, *end;
  1275. sdata_assert_lock(sdata);
  1276. presp = rcu_dereference_protected(ifibss->presp,
  1277. lockdep_is_held(&sdata->wdev.mtx));
  1278. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  1279. len < 24 + 2 || !presp)
  1280. return;
  1281. tx_last_beacon = drv_tx_last_beacon(local);
  1282. ibss_dbg(sdata,
  1283. "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
  1284. mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
  1285. if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
  1286. return;
  1287. if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
  1288. !is_broadcast_ether_addr(mgmt->bssid))
  1289. return;
  1290. end = ((u8 *) mgmt) + len;
  1291. pos = mgmt->u.probe_req.variable;
  1292. if (pos[0] != WLAN_EID_SSID ||
  1293. pos + 2 + pos[1] > end) {
  1294. ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
  1295. mgmt->sa);
  1296. return;
  1297. }
  1298. if (pos[1] != 0 &&
  1299. (pos[1] != ifibss->ssid_len ||
  1300. memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
  1301. /* Ignore ProbeReq for foreign SSID */
  1302. return;
  1303. }
  1304. /* Reply with ProbeResp */
  1305. skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
  1306. if (!skb)
  1307. return;
  1308. skb_reserve(skb, local->tx_headroom);
  1309. memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
  1310. memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
  1311. ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
  1312. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1313. /* avoid excessive retries for probe request to wildcard SSIDs */
  1314. if (pos[1] == 0)
  1315. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
  1316. ieee80211_tx_skb(sdata, skb);
  1317. }
  1318. static
  1319. void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
  1320. struct ieee80211_mgmt *mgmt, size_t len,
  1321. struct ieee80211_rx_status *rx_status)
  1322. {
  1323. size_t baselen;
  1324. struct ieee802_11_elems elems;
  1325. BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
  1326. offsetof(typeof(mgmt->u.beacon), variable));
  1327. /*
  1328. * either beacon or probe_resp but the variable field is at the
  1329. * same offset
  1330. */
  1331. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1332. if (baselen > len)
  1333. return;
  1334. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1335. false, &elems);
  1336. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  1337. }
  1338. void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1339. struct sk_buff *skb)
  1340. {
  1341. struct ieee80211_rx_status *rx_status;
  1342. struct ieee80211_mgmt *mgmt;
  1343. u16 fc;
  1344. struct ieee802_11_elems elems;
  1345. int ies_len;
  1346. rx_status = IEEE80211_SKB_RXCB(skb);
  1347. mgmt = (struct ieee80211_mgmt *) skb->data;
  1348. fc = le16_to_cpu(mgmt->frame_control);
  1349. sdata_lock(sdata);
  1350. if (!sdata->u.ibss.ssid_len)
  1351. goto mgmt_out; /* not ready to merge yet */
  1352. switch (fc & IEEE80211_FCTL_STYPE) {
  1353. case IEEE80211_STYPE_PROBE_REQ:
  1354. ieee80211_rx_mgmt_probe_req(sdata, skb);
  1355. break;
  1356. case IEEE80211_STYPE_PROBE_RESP:
  1357. case IEEE80211_STYPE_BEACON:
  1358. ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
  1359. rx_status);
  1360. break;
  1361. case IEEE80211_STYPE_AUTH:
  1362. ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
  1363. break;
  1364. case IEEE80211_STYPE_DEAUTH:
  1365. ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
  1366. break;
  1367. case IEEE80211_STYPE_ACTION:
  1368. switch (mgmt->u.action.category) {
  1369. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1370. ies_len = skb->len -
  1371. offsetof(struct ieee80211_mgmt,
  1372. u.action.u.chan_switch.variable);
  1373. if (ies_len < 0)
  1374. break;
  1375. ieee802_11_parse_elems(
  1376. mgmt->u.action.u.chan_switch.variable,
  1377. ies_len, true, &elems);
  1378. if (elems.parse_error)
  1379. break;
  1380. ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt, skb->len,
  1381. rx_status, &elems);
  1382. break;
  1383. }
  1384. }
  1385. mgmt_out:
  1386. sdata_unlock(sdata);
  1387. }
  1388. void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
  1389. {
  1390. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1391. struct sta_info *sta;
  1392. sdata_lock(sdata);
  1393. /*
  1394. * Work could be scheduled after scan or similar
  1395. * when we aren't even joined (or trying) with a
  1396. * network.
  1397. */
  1398. if (!ifibss->ssid_len)
  1399. goto out;
  1400. spin_lock_bh(&ifibss->incomplete_lock);
  1401. while (!list_empty(&ifibss->incomplete_stations)) {
  1402. sta = list_first_entry(&ifibss->incomplete_stations,
  1403. struct sta_info, list);
  1404. list_del(&sta->list);
  1405. spin_unlock_bh(&ifibss->incomplete_lock);
  1406. ieee80211_ibss_finish_sta(sta);
  1407. rcu_read_unlock();
  1408. spin_lock_bh(&ifibss->incomplete_lock);
  1409. }
  1410. spin_unlock_bh(&ifibss->incomplete_lock);
  1411. switch (ifibss->state) {
  1412. case IEEE80211_IBSS_MLME_SEARCH:
  1413. ieee80211_sta_find_ibss(sdata);
  1414. break;
  1415. case IEEE80211_IBSS_MLME_JOINED:
  1416. ieee80211_sta_merge_ibss(sdata);
  1417. break;
  1418. default:
  1419. WARN_ON(1);
  1420. break;
  1421. }
  1422. out:
  1423. sdata_unlock(sdata);
  1424. }
  1425. static void ieee80211_ibss_timer(unsigned long data)
  1426. {
  1427. struct ieee80211_sub_if_data *sdata =
  1428. (struct ieee80211_sub_if_data *) data;
  1429. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1430. }
  1431. void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1432. {
  1433. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1434. setup_timer(&ifibss->timer, ieee80211_ibss_timer,
  1435. (unsigned long) sdata);
  1436. INIT_LIST_HEAD(&ifibss->incomplete_stations);
  1437. spin_lock_init(&ifibss->incomplete_lock);
  1438. INIT_WORK(&ifibss->csa_connection_drop_work,
  1439. ieee80211_csa_connection_drop_work);
  1440. }
  1441. /* scan finished notification */
  1442. void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
  1443. {
  1444. struct ieee80211_sub_if_data *sdata;
  1445. mutex_lock(&local->iflist_mtx);
  1446. list_for_each_entry(sdata, &local->interfaces, list) {
  1447. if (!ieee80211_sdata_running(sdata))
  1448. continue;
  1449. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  1450. continue;
  1451. sdata->u.ibss.last_scan_completed = jiffies;
  1452. }
  1453. mutex_unlock(&local->iflist_mtx);
  1454. }
  1455. int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
  1456. struct cfg80211_ibss_params *params)
  1457. {
  1458. u32 changed = 0;
  1459. u32 rate_flags;
  1460. struct ieee80211_supported_band *sband;
  1461. enum ieee80211_chanctx_mode chanmode;
  1462. struct ieee80211_local *local = sdata->local;
  1463. int radar_detect_width = 0;
  1464. int i;
  1465. int ret;
  1466. ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
  1467. &params->chandef,
  1468. sdata->wdev.iftype);
  1469. if (ret < 0)
  1470. return ret;
  1471. if (ret > 0) {
  1472. if (!params->userspace_handles_dfs)
  1473. return -EINVAL;
  1474. radar_detect_width = BIT(params->chandef.width);
  1475. }
  1476. chanmode = (params->channel_fixed && !ret) ?
  1477. IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
  1478. mutex_lock(&local->chanctx_mtx);
  1479. ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
  1480. radar_detect_width);
  1481. mutex_unlock(&local->chanctx_mtx);
  1482. if (ret < 0)
  1483. return ret;
  1484. if (params->bssid) {
  1485. memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
  1486. sdata->u.ibss.fixed_bssid = true;
  1487. } else
  1488. sdata->u.ibss.fixed_bssid = false;
  1489. sdata->u.ibss.privacy = params->privacy;
  1490. sdata->u.ibss.control_port = params->control_port;
  1491. sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
  1492. sdata->u.ibss.basic_rates = params->basic_rates;
  1493. sdata->u.ibss.last_scan_completed = jiffies;
  1494. /* fix basic_rates if channel does not support these rates */
  1495. rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
  1496. sband = local->hw.wiphy->bands[params->chandef.chan->band];
  1497. for (i = 0; i < sband->n_bitrates; i++) {
  1498. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1499. sdata->u.ibss.basic_rates &= ~BIT(i);
  1500. }
  1501. memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
  1502. sizeof(params->mcast_rate));
  1503. sdata->vif.bss_conf.beacon_int = params->beacon_interval;
  1504. sdata->u.ibss.chandef = params->chandef;
  1505. sdata->u.ibss.fixed_channel = params->channel_fixed;
  1506. if (params->ie) {
  1507. sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
  1508. GFP_KERNEL);
  1509. if (sdata->u.ibss.ie)
  1510. sdata->u.ibss.ie_len = params->ie_len;
  1511. }
  1512. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  1513. sdata->u.ibss.ibss_join_req = jiffies;
  1514. memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
  1515. sdata->u.ibss.ssid_len = params->ssid_len;
  1516. memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
  1517. sizeof(sdata->u.ibss.ht_capa));
  1518. memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
  1519. sizeof(sdata->u.ibss.ht_capa_mask));
  1520. /*
  1521. * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
  1522. * reserved, but an HT STA shall protect HT transmissions as though
  1523. * the HT Protection field were set to non-HT mixed mode.
  1524. *
  1525. * In an IBSS, the RIFS Mode field of the HT Operation element is
  1526. * also reserved, but an HT STA shall operate as though this field
  1527. * were set to 1.
  1528. */
  1529. sdata->vif.bss_conf.ht_operation_mode |=
  1530. IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
  1531. | IEEE80211_HT_PARAM_RIFS_MODE;
  1532. changed |= BSS_CHANGED_HT;
  1533. ieee80211_bss_info_change_notify(sdata, changed);
  1534. sdata->smps_mode = IEEE80211_SMPS_OFF;
  1535. sdata->needed_rx_chains = local->rx_chains;
  1536. ieee80211_queue_work(&local->hw, &sdata->work);
  1537. return 0;
  1538. }
  1539. int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
  1540. {
  1541. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1542. ieee80211_ibss_disconnect(sdata);
  1543. ifibss->ssid_len = 0;
  1544. eth_zero_addr(ifibss->bssid);
  1545. /* remove beacon */
  1546. kfree(sdata->u.ibss.ie);
  1547. /* on the next join, re-program HT parameters */
  1548. memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
  1549. memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
  1550. synchronize_rcu();
  1551. skb_queue_purge(&sdata->skb_queue);
  1552. del_timer_sync(&sdata->u.ibss.timer);
  1553. return 0;
  1554. }