ibss.c 48 KB

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