ibss.c 48 KB

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