ibss.c 52 KB

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