ibss.c 47 KB

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