ibss.c 48 KB

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