ibss.c 48 KB

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