ibss.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  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. * Copyright 2013-2014 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/slab.h>
  18. #include <linux/if_ether.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/rtnetlink.h>
  23. #include <net/mac80211.h>
  24. #include "ieee80211_i.h"
  25. #include "driver-ops.h"
  26. #include "rate.h"
  27. #define IEEE80211_SCAN_INTERVAL (2 * HZ)
  28. #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
  29. #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
  30. #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
  31. #define IEEE80211_IBSS_RSN_INACTIVITY_LIMIT (10 * HZ)
  32. #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
  33. static struct beacon_data *
  34. ieee80211_ibss_build_presp(struct ieee80211_sub_if_data *sdata,
  35. const int beacon_int, const u32 basic_rates,
  36. const u16 capability, u64 tsf,
  37. struct cfg80211_chan_def *chandef,
  38. bool *have_higher_than_11mbit,
  39. struct cfg80211_csa_settings *csa_settings)
  40. {
  41. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  42. struct ieee80211_local *local = sdata->local;
  43. int rates_n = 0, i, ri;
  44. struct ieee80211_mgmt *mgmt;
  45. u8 *pos;
  46. struct ieee80211_supported_band *sband;
  47. u32 rate_flags, rates = 0, rates_added = 0;
  48. struct beacon_data *presp;
  49. int frame_len;
  50. int shift;
  51. /* Build IBSS probe response */
  52. frame_len = sizeof(struct ieee80211_hdr_3addr) +
  53. 12 /* struct ieee80211_mgmt.u.beacon */ +
  54. 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
  55. 2 + 8 /* max Supported Rates */ +
  56. 3 /* max DS params */ +
  57. 4 /* IBSS params */ +
  58. 5 /* Channel Switch Announcement */ +
  59. 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  60. 2 + sizeof(struct ieee80211_ht_cap) +
  61. 2 + sizeof(struct ieee80211_ht_operation) +
  62. 2 + sizeof(struct ieee80211_vht_cap) +
  63. 2 + sizeof(struct ieee80211_vht_operation) +
  64. ifibss->ie_len;
  65. presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
  66. if (!presp)
  67. return NULL;
  68. presp->head = (void *)(presp + 1);
  69. mgmt = (void *) presp->head;
  70. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  71. IEEE80211_STYPE_PROBE_RESP);
  72. eth_broadcast_addr(mgmt->da);
  73. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  74. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  75. mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
  76. mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
  77. mgmt->u.beacon.capab_info = cpu_to_le16(capability);
  78. pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
  79. *pos++ = WLAN_EID_SSID;
  80. *pos++ = ifibss->ssid_len;
  81. memcpy(pos, ifibss->ssid, ifibss->ssid_len);
  82. pos += ifibss->ssid_len;
  83. sband = local->hw.wiphy->bands[chandef->chan->band];
  84. rate_flags = ieee80211_chandef_rate_flags(chandef);
  85. shift = ieee80211_chandef_get_shift(chandef);
  86. rates_n = 0;
  87. if (have_higher_than_11mbit)
  88. *have_higher_than_11mbit = false;
  89. for (i = 0; i < sband->n_bitrates; i++) {
  90. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  91. continue;
  92. if (sband->bitrates[i].bitrate > 110 &&
  93. have_higher_than_11mbit)
  94. *have_higher_than_11mbit = true;
  95. rates |= BIT(i);
  96. rates_n++;
  97. }
  98. *pos++ = WLAN_EID_SUPP_RATES;
  99. *pos++ = min_t(int, 8, rates_n);
  100. for (ri = 0; ri < sband->n_bitrates; ri++) {
  101. int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
  102. 5 * (1 << shift));
  103. u8 basic = 0;
  104. if (!(rates & BIT(ri)))
  105. continue;
  106. if (basic_rates & BIT(ri))
  107. basic = 0x80;
  108. *pos++ = basic | (u8) rate;
  109. if (++rates_added == 8) {
  110. ri++; /* continue at next rate for EXT_SUPP_RATES */
  111. break;
  112. }
  113. }
  114. if (sband->band == NL80211_BAND_2GHZ) {
  115. *pos++ = WLAN_EID_DS_PARAMS;
  116. *pos++ = 1;
  117. *pos++ = ieee80211_frequency_to_channel(
  118. chandef->chan->center_freq);
  119. }
  120. *pos++ = WLAN_EID_IBSS_PARAMS;
  121. *pos++ = 2;
  122. /* FIX: set ATIM window based on scan results */
  123. *pos++ = 0;
  124. *pos++ = 0;
  125. if (csa_settings) {
  126. *pos++ = WLAN_EID_CHANNEL_SWITCH;
  127. *pos++ = 3;
  128. *pos++ = csa_settings->block_tx ? 1 : 0;
  129. *pos++ = ieee80211_frequency_to_channel(
  130. csa_settings->chandef.chan->center_freq);
  131. presp->csa_counter_offsets[0] = (pos - presp->head);
  132. *pos++ = csa_settings->count;
  133. presp->csa_current_counter = csa_settings->count;
  134. }
  135. /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
  136. if (rates_n > 8) {
  137. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  138. *pos++ = rates_n - 8;
  139. for (; ri < sband->n_bitrates; ri++) {
  140. int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
  141. 5 * (1 << shift));
  142. u8 basic = 0;
  143. if (!(rates & BIT(ri)))
  144. continue;
  145. if (basic_rates & BIT(ri))
  146. basic = 0x80;
  147. *pos++ = basic | (u8) rate;
  148. }
  149. }
  150. if (ifibss->ie_len) {
  151. memcpy(pos, ifibss->ie, ifibss->ie_len);
  152. pos += ifibss->ie_len;
  153. }
  154. /* add HT capability and information IEs */
  155. if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  156. chandef->width != NL80211_CHAN_WIDTH_5 &&
  157. chandef->width != NL80211_CHAN_WIDTH_10 &&
  158. sband->ht_cap.ht_supported) {
  159. struct ieee80211_sta_ht_cap ht_cap;
  160. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  161. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  162. pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  163. /*
  164. * Note: According to 802.11n-2009 9.13.3.1, HT Protection
  165. * field and RIFS Mode are reserved in IBSS mode, therefore
  166. * keep them at 0
  167. */
  168. pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
  169. chandef, 0, false);
  170. /* add VHT capability and information IEs */
  171. if (chandef->width != NL80211_CHAN_WIDTH_20 &&
  172. chandef->width != NL80211_CHAN_WIDTH_40 &&
  173. sband->vht_cap.vht_supported) {
  174. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  175. sband->vht_cap.cap);
  176. pos = ieee80211_ie_build_vht_oper(pos, &sband->vht_cap,
  177. chandef);
  178. }
  179. }
  180. if (local->hw.queues >= IEEE80211_NUM_ACS)
  181. pos = ieee80211_add_wmm_info_ie(pos, 0); /* U-APSD not in use */
  182. presp->head_len = pos - presp->head;
  183. if (WARN_ON(presp->head_len > frame_len))
  184. goto error;
  185. return presp;
  186. error:
  187. kfree(presp);
  188. return NULL;
  189. }
  190. static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  191. const u8 *bssid, const int beacon_int,
  192. struct cfg80211_chan_def *req_chandef,
  193. const u32 basic_rates,
  194. const u16 capability, u64 tsf,
  195. bool creator)
  196. {
  197. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  198. struct ieee80211_local *local = sdata->local;
  199. struct ieee80211_mgmt *mgmt;
  200. struct cfg80211_bss *bss;
  201. u32 bss_change;
  202. struct cfg80211_chan_def chandef;
  203. struct ieee80211_channel *chan;
  204. struct beacon_data *presp;
  205. struct cfg80211_inform_bss bss_meta = {};
  206. bool have_higher_than_11mbit;
  207. bool radar_required;
  208. int err;
  209. sdata_assert_lock(sdata);
  210. /* Reset own TSF to allow time synchronization work. */
  211. drv_reset_tsf(local, sdata);
  212. if (!ether_addr_equal(ifibss->bssid, bssid))
  213. sta_info_flush(sdata);
  214. /* if merging, indicate to driver that we leave the old IBSS */
  215. if (sdata->vif.bss_conf.ibss_joined) {
  216. sdata->vif.bss_conf.ibss_joined = false;
  217. sdata->vif.bss_conf.ibss_creator = false;
  218. sdata->vif.bss_conf.enable_beacon = false;
  219. netif_carrier_off(sdata->dev);
  220. ieee80211_bss_info_change_notify(sdata,
  221. BSS_CHANGED_IBSS |
  222. BSS_CHANGED_BEACON_ENABLED);
  223. drv_leave_ibss(local, sdata);
  224. }
  225. presp = rcu_dereference_protected(ifibss->presp,
  226. lockdep_is_held(&sdata->wdev.mtx));
  227. RCU_INIT_POINTER(ifibss->presp, NULL);
  228. if (presp)
  229. kfree_rcu(presp, rcu_head);
  230. /* make a copy of the chandef, it could be modified below. */
  231. chandef = *req_chandef;
  232. chan = chandef.chan;
  233. if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
  234. NL80211_IFTYPE_ADHOC)) {
  235. if (chandef.width == NL80211_CHAN_WIDTH_5 ||
  236. chandef.width == NL80211_CHAN_WIDTH_10 ||
  237. chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
  238. chandef.width == NL80211_CHAN_WIDTH_20) {
  239. sdata_info(sdata,
  240. "Failed to join IBSS, beacons forbidden\n");
  241. return;
  242. }
  243. chandef.width = NL80211_CHAN_WIDTH_20;
  244. chandef.center_freq1 = chan->center_freq;
  245. /* check again for downgraded chandef */
  246. if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
  247. NL80211_IFTYPE_ADHOC)) {
  248. sdata_info(sdata,
  249. "Failed to join IBSS, beacons forbidden\n");
  250. return;
  251. }
  252. }
  253. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  254. &chandef, NL80211_IFTYPE_ADHOC);
  255. if (err < 0) {
  256. sdata_info(sdata,
  257. "Failed to join IBSS, invalid chandef\n");
  258. return;
  259. }
  260. if (err > 0 && !ifibss->userspace_handles_dfs) {
  261. sdata_info(sdata,
  262. "Failed to join IBSS, DFS channel without control program\n");
  263. return;
  264. }
  265. radar_required = err;
  266. mutex_lock(&local->mtx);
  267. if (ieee80211_vif_use_channel(sdata, &chandef,
  268. ifibss->fixed_channel ?
  269. IEEE80211_CHANCTX_SHARED :
  270. IEEE80211_CHANCTX_EXCLUSIVE)) {
  271. sdata_info(sdata, "Failed to join IBSS, no channel context\n");
  272. mutex_unlock(&local->mtx);
  273. return;
  274. }
  275. sdata->radar_required = radar_required;
  276. mutex_unlock(&local->mtx);
  277. memcpy(ifibss->bssid, bssid, ETH_ALEN);
  278. presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates,
  279. capability, tsf, &chandef,
  280. &have_higher_than_11mbit, NULL);
  281. if (!presp)
  282. return;
  283. rcu_assign_pointer(ifibss->presp, presp);
  284. mgmt = (void *)presp->head;
  285. sdata->vif.bss_conf.enable_beacon = true;
  286. sdata->vif.bss_conf.beacon_int = beacon_int;
  287. sdata->vif.bss_conf.basic_rates = basic_rates;
  288. sdata->vif.bss_conf.ssid_len = ifibss->ssid_len;
  289. memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len);
  290. bss_change = BSS_CHANGED_BEACON_INT;
  291. bss_change |= ieee80211_reset_erp_info(sdata);
  292. bss_change |= BSS_CHANGED_BSSID;
  293. bss_change |= BSS_CHANGED_BEACON;
  294. bss_change |= BSS_CHANGED_BEACON_ENABLED;
  295. bss_change |= BSS_CHANGED_BASIC_RATES;
  296. bss_change |= BSS_CHANGED_HT;
  297. bss_change |= BSS_CHANGED_IBSS;
  298. bss_change |= BSS_CHANGED_SSID;
  299. /*
  300. * In 5 GHz/802.11a, we can always use short slot time.
  301. * (IEEE 802.11-2012 18.3.8.7)
  302. *
  303. * In 2.4GHz, we must always use long slots in IBSS for compatibility
  304. * reasons.
  305. * (IEEE 802.11-2012 19.4.5)
  306. *
  307. * HT follows these specifications (IEEE 802.11-2012 20.3.18)
  308. */
  309. sdata->vif.bss_conf.use_short_slot = chan->band == NL80211_BAND_5GHZ;
  310. bss_change |= BSS_CHANGED_ERP_SLOT;
  311. /* cf. IEEE 802.11 9.2.12 */
  312. if (chan->band == NL80211_BAND_2GHZ && have_higher_than_11mbit)
  313. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  314. else
  315. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  316. ieee80211_set_wmm_default(sdata, true, false);
  317. sdata->vif.bss_conf.ibss_joined = true;
  318. sdata->vif.bss_conf.ibss_creator = creator;
  319. err = drv_join_ibss(local, sdata);
  320. if (err) {
  321. sdata->vif.bss_conf.ibss_joined = false;
  322. sdata->vif.bss_conf.ibss_creator = false;
  323. sdata->vif.bss_conf.enable_beacon = false;
  324. sdata->vif.bss_conf.ssid_len = 0;
  325. RCU_INIT_POINTER(ifibss->presp, NULL);
  326. kfree_rcu(presp, rcu_head);
  327. mutex_lock(&local->mtx);
  328. ieee80211_vif_release_channel(sdata);
  329. mutex_unlock(&local->mtx);
  330. sdata_info(sdata, "Failed to join IBSS, driver failure: %d\n",
  331. err);
  332. return;
  333. }
  334. ieee80211_bss_info_change_notify(sdata, bss_change);
  335. ifibss->state = IEEE80211_IBSS_MLME_JOINED;
  336. mod_timer(&ifibss->timer,
  337. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  338. bss_meta.chan = chan;
  339. bss_meta.scan_width = cfg80211_chandef_to_scan_width(&chandef);
  340. bss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta, mgmt,
  341. presp->head_len, GFP_KERNEL);
  342. cfg80211_put_bss(local->hw.wiphy, bss);
  343. netif_carrier_on(sdata->dev);
  344. cfg80211_ibss_joined(sdata->dev, ifibss->bssid, chan, GFP_KERNEL);
  345. }
  346. static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  347. struct ieee80211_bss *bss)
  348. {
  349. struct cfg80211_bss *cbss =
  350. container_of((void *)bss, struct cfg80211_bss, priv);
  351. struct ieee80211_supported_band *sband;
  352. struct cfg80211_chan_def chandef;
  353. u32 basic_rates;
  354. int i, j;
  355. u16 beacon_int = cbss->beacon_interval;
  356. const struct cfg80211_bss_ies *ies;
  357. enum nl80211_channel_type chan_type;
  358. u64 tsf;
  359. u32 rate_flags;
  360. int shift;
  361. sdata_assert_lock(sdata);
  362. if (beacon_int < 10)
  363. beacon_int = 10;
  364. switch (sdata->u.ibss.chandef.width) {
  365. case NL80211_CHAN_WIDTH_20_NOHT:
  366. case NL80211_CHAN_WIDTH_20:
  367. case NL80211_CHAN_WIDTH_40:
  368. chan_type = cfg80211_get_chandef_type(&sdata->u.ibss.chandef);
  369. cfg80211_chandef_create(&chandef, cbss->channel, chan_type);
  370. break;
  371. case NL80211_CHAN_WIDTH_5:
  372. case NL80211_CHAN_WIDTH_10:
  373. cfg80211_chandef_create(&chandef, cbss->channel,
  374. NL80211_CHAN_WIDTH_20_NOHT);
  375. chandef.width = sdata->u.ibss.chandef.width;
  376. break;
  377. case NL80211_CHAN_WIDTH_80:
  378. case NL80211_CHAN_WIDTH_80P80:
  379. case NL80211_CHAN_WIDTH_160:
  380. chandef = sdata->u.ibss.chandef;
  381. chandef.chan = cbss->channel;
  382. break;
  383. default:
  384. /* fall back to 20 MHz for unsupported modes */
  385. cfg80211_chandef_create(&chandef, cbss->channel,
  386. NL80211_CHAN_WIDTH_20_NOHT);
  387. break;
  388. }
  389. sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
  390. rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
  391. shift = ieee80211_vif_get_shift(&sdata->vif);
  392. basic_rates = 0;
  393. for (i = 0; i < bss->supp_rates_len; i++) {
  394. int rate = bss->supp_rates[i] & 0x7f;
  395. bool is_basic = !!(bss->supp_rates[i] & 0x80);
  396. for (j = 0; j < sband->n_bitrates; j++) {
  397. int brate;
  398. if ((rate_flags & sband->bitrates[j].flags)
  399. != rate_flags)
  400. continue;
  401. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  402. 5 * (1 << shift));
  403. if (brate == rate) {
  404. if (is_basic)
  405. basic_rates |= BIT(j);
  406. break;
  407. }
  408. }
  409. }
  410. rcu_read_lock();
  411. ies = rcu_dereference(cbss->ies);
  412. tsf = ies->tsf;
  413. rcu_read_unlock();
  414. __ieee80211_sta_join_ibss(sdata, cbss->bssid,
  415. beacon_int,
  416. &chandef,
  417. basic_rates,
  418. cbss->capability,
  419. tsf, false);
  420. }
  421. int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
  422. struct cfg80211_csa_settings *csa_settings)
  423. {
  424. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  425. struct beacon_data *presp, *old_presp;
  426. struct cfg80211_bss *cbss;
  427. const struct cfg80211_bss_ies *ies;
  428. u16 capability = WLAN_CAPABILITY_IBSS;
  429. u64 tsf;
  430. int ret = 0;
  431. sdata_assert_lock(sdata);
  432. if (ifibss->privacy)
  433. capability |= WLAN_CAPABILITY_PRIVACY;
  434. cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
  435. ifibss->bssid, ifibss->ssid,
  436. ifibss->ssid_len, IEEE80211_BSS_TYPE_IBSS,
  437. IEEE80211_PRIVACY(ifibss->privacy));
  438. if (WARN_ON(!cbss)) {
  439. ret = -EINVAL;
  440. goto out;
  441. }
  442. rcu_read_lock();
  443. ies = rcu_dereference(cbss->ies);
  444. tsf = ies->tsf;
  445. rcu_read_unlock();
  446. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  447. old_presp = rcu_dereference_protected(ifibss->presp,
  448. lockdep_is_held(&sdata->wdev.mtx));
  449. presp = ieee80211_ibss_build_presp(sdata,
  450. sdata->vif.bss_conf.beacon_int,
  451. sdata->vif.bss_conf.basic_rates,
  452. capability, tsf, &ifibss->chandef,
  453. NULL, csa_settings);
  454. if (!presp) {
  455. ret = -ENOMEM;
  456. goto out;
  457. }
  458. rcu_assign_pointer(ifibss->presp, presp);
  459. if (old_presp)
  460. kfree_rcu(old_presp, rcu_head);
  461. return BSS_CHANGED_BEACON;
  462. out:
  463. return ret;
  464. }
  465. int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata)
  466. {
  467. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  468. struct cfg80211_bss *cbss;
  469. int err, changed = 0;
  470. sdata_assert_lock(sdata);
  471. /* update cfg80211 bss information with the new channel */
  472. if (!is_zero_ether_addr(ifibss->bssid)) {
  473. cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
  474. ifibss->chandef.chan,
  475. ifibss->bssid, ifibss->ssid,
  476. ifibss->ssid_len,
  477. IEEE80211_BSS_TYPE_IBSS,
  478. IEEE80211_PRIVACY(ifibss->privacy));
  479. /* XXX: should not really modify cfg80211 data */
  480. if (cbss) {
  481. cbss->channel = sdata->csa_chandef.chan;
  482. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  483. }
  484. }
  485. ifibss->chandef = sdata->csa_chandef;
  486. /* generate the beacon */
  487. err = ieee80211_ibss_csa_beacon(sdata, NULL);
  488. if (err < 0)
  489. return err;
  490. changed |= err;
  491. return changed;
  492. }
  493. void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
  494. {
  495. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  496. cancel_work_sync(&ifibss->csa_connection_drop_work);
  497. }
  498. static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
  499. __acquires(RCU)
  500. {
  501. struct ieee80211_sub_if_data *sdata = sta->sdata;
  502. u8 addr[ETH_ALEN];
  503. memcpy(addr, sta->sta.addr, ETH_ALEN);
  504. ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
  505. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  506. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  507. /* authorize the station only if the network is not RSN protected. If
  508. * not wait for the userspace to authorize it */
  509. if (!sta->sdata->u.ibss.control_port)
  510. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  511. rate_control_rate_init(sta);
  512. /* If it fails, maybe we raced another insertion? */
  513. if (sta_info_insert_rcu(sta))
  514. return sta_info_get(sdata, addr);
  515. return sta;
  516. }
  517. static struct sta_info *
  518. ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
  519. const u8 *addr, u32 supp_rates)
  520. __acquires(RCU)
  521. {
  522. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  523. struct ieee80211_local *local = sdata->local;
  524. struct sta_info *sta;
  525. struct ieee80211_chanctx_conf *chanctx_conf;
  526. struct ieee80211_supported_band *sband;
  527. enum nl80211_bss_scan_width scan_width;
  528. int band;
  529. /*
  530. * XXX: Consider removing the least recently used entry and
  531. * allow new one to be added.
  532. */
  533. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  534. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  535. sdata->name, addr);
  536. rcu_read_lock();
  537. return NULL;
  538. }
  539. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
  540. rcu_read_lock();
  541. return NULL;
  542. }
  543. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
  544. rcu_read_lock();
  545. return NULL;
  546. }
  547. rcu_read_lock();
  548. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  549. if (WARN_ON_ONCE(!chanctx_conf))
  550. return NULL;
  551. band = chanctx_conf->def.chan->band;
  552. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  553. rcu_read_unlock();
  554. sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
  555. if (!sta) {
  556. rcu_read_lock();
  557. return NULL;
  558. }
  559. /* make sure mandatory rates are always added */
  560. sband = local->hw.wiphy->bands[band];
  561. sta->sta.supp_rates[band] = supp_rates |
  562. ieee80211_mandatory_rates(sband, scan_width);
  563. return ieee80211_ibss_finish_sta(sta);
  564. }
  565. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  566. {
  567. struct ieee80211_local *local = sdata->local;
  568. int active = 0;
  569. struct sta_info *sta;
  570. sdata_assert_lock(sdata);
  571. rcu_read_lock();
  572. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  573. unsigned long last_active = ieee80211_sta_last_active(sta);
  574. if (sta->sdata == sdata &&
  575. time_is_after_jiffies(last_active +
  576. IEEE80211_IBSS_MERGE_INTERVAL)) {
  577. active++;
  578. break;
  579. }
  580. }
  581. rcu_read_unlock();
  582. return active;
  583. }
  584. static void ieee80211_ibss_disconnect(struct ieee80211_sub_if_data *sdata)
  585. {
  586. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  587. struct ieee80211_local *local = sdata->local;
  588. struct cfg80211_bss *cbss;
  589. struct beacon_data *presp;
  590. struct sta_info *sta;
  591. if (!is_zero_ether_addr(ifibss->bssid)) {
  592. cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
  593. ifibss->bssid, ifibss->ssid,
  594. ifibss->ssid_len,
  595. IEEE80211_BSS_TYPE_IBSS,
  596. IEEE80211_PRIVACY(ifibss->privacy));
  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,
  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_update_sta_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. struct ieee80211_channel *channel)
  836. {
  837. struct sta_info *sta;
  838. enum nl80211_band band = rx_status->band;
  839. enum nl80211_bss_scan_width scan_width;
  840. struct ieee80211_local *local = sdata->local;
  841. struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
  842. bool rates_updated = false;
  843. u32 supp_rates = 0;
  844. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  845. return;
  846. if (!ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid))
  847. return;
  848. rcu_read_lock();
  849. sta = sta_info_get(sdata, mgmt->sa);
  850. if (elems->supp_rates) {
  851. supp_rates = ieee80211_sta_get_rates(sdata, elems,
  852. band, NULL);
  853. if (sta) {
  854. u32 prev_rates;
  855. prev_rates = sta->sta.supp_rates[band];
  856. /* make sure mandatory rates are always added */
  857. scan_width = NL80211_BSS_CHAN_WIDTH_20;
  858. if (rx_status->flag & RX_FLAG_5MHZ)
  859. scan_width = NL80211_BSS_CHAN_WIDTH_5;
  860. if (rx_status->flag & RX_FLAG_10MHZ)
  861. scan_width = NL80211_BSS_CHAN_WIDTH_10;
  862. sta->sta.supp_rates[band] = supp_rates |
  863. ieee80211_mandatory_rates(sband, scan_width);
  864. if (sta->sta.supp_rates[band] != prev_rates) {
  865. ibss_dbg(sdata,
  866. "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
  867. sta->sta.addr, prev_rates,
  868. sta->sta.supp_rates[band]);
  869. rates_updated = true;
  870. }
  871. } else {
  872. rcu_read_unlock();
  873. sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
  874. mgmt->sa, supp_rates);
  875. }
  876. }
  877. if (sta && !sta->sta.wme &&
  878. elems->wmm_info && local->hw.queues >= IEEE80211_NUM_ACS) {
  879. sta->sta.wme = true;
  880. ieee80211_check_fast_xmit(sta);
  881. }
  882. if (sta && elems->ht_operation && elems->ht_cap_elem &&
  883. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
  884. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
  885. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
  886. /* we both use HT */
  887. struct ieee80211_ht_cap htcap_ie;
  888. struct cfg80211_chan_def chandef;
  889. enum ieee80211_sta_rx_bandwidth bw = sta->sta.bandwidth;
  890. cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
  891. ieee80211_chandef_ht_oper(elems->ht_operation, &chandef);
  892. memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
  893. rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  894. &htcap_ie,
  895. sta);
  896. if (elems->vht_operation && elems->vht_cap_elem &&
  897. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20 &&
  898. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_40) {
  899. /* we both use VHT */
  900. struct ieee80211_vht_cap cap_ie;
  901. struct ieee80211_sta_vht_cap cap = sta->sta.vht_cap;
  902. ieee80211_chandef_vht_oper(elems->vht_operation,
  903. &chandef);
  904. memcpy(&cap_ie, elems->vht_cap_elem, sizeof(cap_ie));
  905. ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
  906. &cap_ie, sta);
  907. if (memcmp(&cap, &sta->sta.vht_cap, sizeof(cap)))
  908. rates_updated |= true;
  909. }
  910. if (bw != sta->sta.bandwidth)
  911. rates_updated |= true;
  912. if (!cfg80211_chandef_compatible(&sdata->u.ibss.chandef,
  913. &chandef))
  914. WARN_ON_ONCE(1);
  915. }
  916. if (sta && rates_updated) {
  917. u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
  918. u8 rx_nss = sta->sta.rx_nss;
  919. /* Force rx_nss recalculation */
  920. sta->sta.rx_nss = 0;
  921. rate_control_rate_init(sta);
  922. if (sta->sta.rx_nss != rx_nss)
  923. changed |= IEEE80211_RC_NSS_CHANGED;
  924. drv_sta_rc_update(local, sdata, &sta->sta, changed);
  925. }
  926. rcu_read_unlock();
  927. }
  928. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  929. struct ieee80211_mgmt *mgmt, size_t len,
  930. struct ieee80211_rx_status *rx_status,
  931. struct ieee802_11_elems *elems)
  932. {
  933. struct ieee80211_local *local = sdata->local;
  934. struct cfg80211_bss *cbss;
  935. struct ieee80211_bss *bss;
  936. struct ieee80211_channel *channel;
  937. u64 beacon_timestamp, rx_timestamp;
  938. u32 supp_rates = 0;
  939. enum nl80211_band band = rx_status->band;
  940. channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
  941. if (!channel)
  942. return;
  943. ieee80211_update_sta_info(sdata, mgmt, len, rx_status, elems, channel);
  944. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  945. channel);
  946. if (!bss)
  947. return;
  948. cbss = container_of((void *)bss, struct cfg80211_bss, priv);
  949. /* same for beacon and probe response */
  950. beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
  951. /* check if we need to merge IBSS */
  952. /* not an IBSS */
  953. if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
  954. goto put_bss;
  955. /* different channel */
  956. if (sdata->u.ibss.fixed_channel &&
  957. sdata->u.ibss.chandef.chan != cbss->channel)
  958. goto put_bss;
  959. /* different SSID */
  960. if (elems->ssid_len != sdata->u.ibss.ssid_len ||
  961. memcmp(elems->ssid, sdata->u.ibss.ssid,
  962. sdata->u.ibss.ssid_len))
  963. goto put_bss;
  964. /* process channel switch */
  965. if (sdata->vif.csa_active ||
  966. ieee80211_ibss_process_chanswitch(sdata, elems, true))
  967. goto put_bss;
  968. /* same BSSID */
  969. if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
  970. goto put_bss;
  971. /* we use a fixed BSSID */
  972. if (sdata->u.ibss.fixed_bssid)
  973. goto put_bss;
  974. if (ieee80211_have_rx_timestamp(rx_status)) {
  975. /* time when timestamp field was received */
  976. rx_timestamp =
  977. ieee80211_calculate_rx_timestamp(local, rx_status,
  978. len + FCS_LEN, 24);
  979. } else {
  980. /*
  981. * second best option: get current TSF
  982. * (will return -1 if not supported)
  983. */
  984. rx_timestamp = drv_get_tsf(local, sdata);
  985. }
  986. ibss_dbg(sdata,
  987. "RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  988. mgmt->sa, mgmt->bssid,
  989. (unsigned long long)rx_timestamp,
  990. (unsigned long long)beacon_timestamp,
  991. (unsigned long long)(rx_timestamp - beacon_timestamp),
  992. jiffies);
  993. if (beacon_timestamp > rx_timestamp) {
  994. ibss_dbg(sdata,
  995. "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
  996. mgmt->bssid);
  997. ieee80211_sta_join_ibss(sdata, bss);
  998. supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
  999. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
  1000. supp_rates);
  1001. rcu_read_unlock();
  1002. }
  1003. put_bss:
  1004. ieee80211_rx_bss_put(local, bss);
  1005. }
  1006. void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
  1007. const u8 *bssid, const u8 *addr,
  1008. u32 supp_rates)
  1009. {
  1010. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1011. struct ieee80211_local *local = sdata->local;
  1012. struct sta_info *sta;
  1013. struct ieee80211_chanctx_conf *chanctx_conf;
  1014. struct ieee80211_supported_band *sband;
  1015. enum nl80211_bss_scan_width scan_width;
  1016. int band;
  1017. /*
  1018. * XXX: Consider removing the least recently used entry and
  1019. * allow new one to be added.
  1020. */
  1021. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  1022. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  1023. sdata->name, addr);
  1024. return;
  1025. }
  1026. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
  1027. return;
  1028. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
  1029. return;
  1030. rcu_read_lock();
  1031. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1032. if (WARN_ON_ONCE(!chanctx_conf)) {
  1033. rcu_read_unlock();
  1034. return;
  1035. }
  1036. band = chanctx_conf->def.chan->band;
  1037. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  1038. rcu_read_unlock();
  1039. sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
  1040. if (!sta)
  1041. return;
  1042. /* make sure mandatory rates are always added */
  1043. sband = local->hw.wiphy->bands[band];
  1044. sta->sta.supp_rates[band] = supp_rates |
  1045. ieee80211_mandatory_rates(sband, scan_width);
  1046. spin_lock(&ifibss->incomplete_lock);
  1047. list_add(&sta->list, &ifibss->incomplete_stations);
  1048. spin_unlock(&ifibss->incomplete_lock);
  1049. ieee80211_queue_work(&local->hw, &sdata->work);
  1050. }
  1051. static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
  1052. {
  1053. struct ieee80211_local *local = sdata->local;
  1054. struct sta_info *sta, *tmp;
  1055. unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
  1056. unsigned long exp_rsn = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
  1057. mutex_lock(&local->sta_mtx);
  1058. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  1059. unsigned long last_active = ieee80211_sta_last_active(sta);
  1060. if (sdata != sta->sdata)
  1061. continue;
  1062. if (time_is_before_jiffies(last_active + exp_time) ||
  1063. (time_is_before_jiffies(last_active + exp_rsn) &&
  1064. sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
  1065. sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
  1066. sta->sta_state != IEEE80211_STA_AUTHORIZED ?
  1067. "not authorized " : "", sta->sta.addr);
  1068. WARN_ON(__sta_info_destroy(sta));
  1069. }
  1070. }
  1071. mutex_unlock(&local->sta_mtx);
  1072. }
  1073. /*
  1074. * This function is called with state == IEEE80211_IBSS_MLME_JOINED
  1075. */
  1076. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
  1077. {
  1078. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1079. enum nl80211_bss_scan_width scan_width;
  1080. sdata_assert_lock(sdata);
  1081. mod_timer(&ifibss->timer,
  1082. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  1083. ieee80211_ibss_sta_expire(sdata);
  1084. if (time_before(jiffies, ifibss->last_scan_completed +
  1085. IEEE80211_IBSS_MERGE_INTERVAL))
  1086. return;
  1087. if (ieee80211_sta_active_ibss(sdata))
  1088. return;
  1089. if (ifibss->fixed_channel)
  1090. return;
  1091. sdata_info(sdata,
  1092. "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
  1093. scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
  1094. ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
  1095. NULL, 0, scan_width);
  1096. }
  1097. static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
  1098. {
  1099. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1100. u8 bssid[ETH_ALEN];
  1101. u16 capability;
  1102. int i;
  1103. sdata_assert_lock(sdata);
  1104. if (ifibss->fixed_bssid) {
  1105. memcpy(bssid, ifibss->bssid, ETH_ALEN);
  1106. } else {
  1107. /* Generate random, not broadcast, locally administered BSSID. Mix in
  1108. * own MAC address to make sure that devices that do not have proper
  1109. * random number generator get different BSSID. */
  1110. get_random_bytes(bssid, ETH_ALEN);
  1111. for (i = 0; i < ETH_ALEN; i++)
  1112. bssid[i] ^= sdata->vif.addr[i];
  1113. bssid[0] &= ~0x01;
  1114. bssid[0] |= 0x02;
  1115. }
  1116. sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
  1117. capability = WLAN_CAPABILITY_IBSS;
  1118. if (ifibss->privacy)
  1119. capability |= WLAN_CAPABILITY_PRIVACY;
  1120. __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
  1121. &ifibss->chandef, ifibss->basic_rates,
  1122. capability, 0, true);
  1123. }
  1124. static unsigned ibss_setup_channels(struct wiphy *wiphy,
  1125. struct ieee80211_channel **channels,
  1126. unsigned int channels_max,
  1127. u32 center_freq, u32 width)
  1128. {
  1129. struct ieee80211_channel *chan = NULL;
  1130. unsigned int n_chan = 0;
  1131. u32 start_freq, end_freq, freq;
  1132. if (width <= 20) {
  1133. start_freq = center_freq;
  1134. end_freq = center_freq;
  1135. } else {
  1136. start_freq = center_freq - width / 2 + 10;
  1137. end_freq = center_freq + width / 2 - 10;
  1138. }
  1139. for (freq = start_freq; freq <= end_freq; freq += 20) {
  1140. chan = ieee80211_get_channel(wiphy, freq);
  1141. if (!chan)
  1142. continue;
  1143. if (n_chan >= channels_max)
  1144. return n_chan;
  1145. channels[n_chan] = chan;
  1146. n_chan++;
  1147. }
  1148. return n_chan;
  1149. }
  1150. static unsigned int
  1151. ieee80211_ibss_setup_scan_channels(struct wiphy *wiphy,
  1152. const struct cfg80211_chan_def *chandef,
  1153. struct ieee80211_channel **channels,
  1154. unsigned int channels_max)
  1155. {
  1156. unsigned int n_chan = 0;
  1157. u32 width, cf1, cf2 = 0;
  1158. switch (chandef->width) {
  1159. case NL80211_CHAN_WIDTH_40:
  1160. width = 40;
  1161. break;
  1162. case NL80211_CHAN_WIDTH_80P80:
  1163. cf2 = chandef->center_freq2;
  1164. /* fall through */
  1165. case NL80211_CHAN_WIDTH_80:
  1166. width = 80;
  1167. break;
  1168. case NL80211_CHAN_WIDTH_160:
  1169. width = 160;
  1170. break;
  1171. default:
  1172. width = 20;
  1173. break;
  1174. }
  1175. cf1 = chandef->center_freq1;
  1176. n_chan = ibss_setup_channels(wiphy, channels, channels_max, cf1, width);
  1177. if (cf2)
  1178. n_chan += ibss_setup_channels(wiphy, &channels[n_chan],
  1179. channels_max - n_chan, cf2,
  1180. width);
  1181. return n_chan;
  1182. }
  1183. /*
  1184. * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
  1185. */
  1186. static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
  1187. {
  1188. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1189. struct ieee80211_local *local = sdata->local;
  1190. struct cfg80211_bss *cbss;
  1191. struct ieee80211_channel *chan = NULL;
  1192. const u8 *bssid = NULL;
  1193. enum nl80211_bss_scan_width scan_width;
  1194. int active_ibss;
  1195. sdata_assert_lock(sdata);
  1196. active_ibss = ieee80211_sta_active_ibss(sdata);
  1197. ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
  1198. if (active_ibss)
  1199. return;
  1200. if (ifibss->fixed_bssid)
  1201. bssid = ifibss->bssid;
  1202. if (ifibss->fixed_channel)
  1203. chan = ifibss->chandef.chan;
  1204. if (!is_zero_ether_addr(ifibss->bssid))
  1205. bssid = ifibss->bssid;
  1206. cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
  1207. ifibss->ssid, ifibss->ssid_len,
  1208. IEEE80211_BSS_TYPE_IBSS,
  1209. IEEE80211_PRIVACY(ifibss->privacy));
  1210. if (cbss) {
  1211. struct ieee80211_bss *bss;
  1212. bss = (void *)cbss->priv;
  1213. ibss_dbg(sdata,
  1214. "sta_find_ibss: selected %pM current %pM\n",
  1215. cbss->bssid, ifibss->bssid);
  1216. sdata_info(sdata,
  1217. "Selected IBSS BSSID %pM based on configured SSID\n",
  1218. cbss->bssid);
  1219. ieee80211_sta_join_ibss(sdata, bss);
  1220. ieee80211_rx_bss_put(local, bss);
  1221. return;
  1222. }
  1223. /* if a fixed bssid and a fixed freq have been provided create the IBSS
  1224. * directly and do not waste time scanning
  1225. */
  1226. if (ifibss->fixed_bssid && ifibss->fixed_channel) {
  1227. sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
  1228. bssid);
  1229. ieee80211_sta_create_ibss(sdata);
  1230. return;
  1231. }
  1232. ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
  1233. /* Selected IBSS not found in current scan results - try to scan */
  1234. if (time_after(jiffies, ifibss->last_scan_completed +
  1235. IEEE80211_SCAN_INTERVAL)) {
  1236. struct ieee80211_channel *channels[8];
  1237. unsigned int num;
  1238. sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
  1239. scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
  1240. if (ifibss->fixed_channel) {
  1241. num = ieee80211_ibss_setup_scan_channels(local->hw.wiphy,
  1242. &ifibss->chandef,
  1243. channels,
  1244. ARRAY_SIZE(channels));
  1245. ieee80211_request_ibss_scan(sdata, ifibss->ssid,
  1246. ifibss->ssid_len, channels,
  1247. num, scan_width);
  1248. } else {
  1249. ieee80211_request_ibss_scan(sdata, ifibss->ssid,
  1250. ifibss->ssid_len, NULL,
  1251. 0, scan_width);
  1252. }
  1253. } else {
  1254. int interval = IEEE80211_SCAN_INTERVAL;
  1255. if (time_after(jiffies, ifibss->ibss_join_req +
  1256. IEEE80211_IBSS_JOIN_TIMEOUT))
  1257. ieee80211_sta_create_ibss(sdata);
  1258. mod_timer(&ifibss->timer,
  1259. round_jiffies(jiffies + interval));
  1260. }
  1261. }
  1262. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  1263. struct sk_buff *req)
  1264. {
  1265. struct ieee80211_mgmt *mgmt = (void *)req->data;
  1266. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1267. struct ieee80211_local *local = sdata->local;
  1268. int tx_last_beacon, len = req->len;
  1269. struct sk_buff *skb;
  1270. struct beacon_data *presp;
  1271. u8 *pos, *end;
  1272. sdata_assert_lock(sdata);
  1273. presp = rcu_dereference_protected(ifibss->presp,
  1274. lockdep_is_held(&sdata->wdev.mtx));
  1275. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  1276. len < 24 + 2 || !presp)
  1277. return;
  1278. tx_last_beacon = drv_tx_last_beacon(local);
  1279. ibss_dbg(sdata,
  1280. "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
  1281. mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
  1282. if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
  1283. return;
  1284. if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
  1285. !is_broadcast_ether_addr(mgmt->bssid))
  1286. return;
  1287. end = ((u8 *) mgmt) + len;
  1288. pos = mgmt->u.probe_req.variable;
  1289. if (pos[0] != WLAN_EID_SSID ||
  1290. pos + 2 + pos[1] > end) {
  1291. ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
  1292. mgmt->sa);
  1293. return;
  1294. }
  1295. if (pos[1] != 0 &&
  1296. (pos[1] != ifibss->ssid_len ||
  1297. memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
  1298. /* Ignore ProbeReq for foreign SSID */
  1299. return;
  1300. }
  1301. /* Reply with ProbeResp */
  1302. skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
  1303. if (!skb)
  1304. return;
  1305. skb_reserve(skb, local->tx_headroom);
  1306. memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
  1307. memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
  1308. ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
  1309. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1310. /* avoid excessive retries for probe request to wildcard SSIDs */
  1311. if (pos[1] == 0)
  1312. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
  1313. ieee80211_tx_skb(sdata, skb);
  1314. }
  1315. static
  1316. void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
  1317. struct ieee80211_mgmt *mgmt, size_t len,
  1318. struct ieee80211_rx_status *rx_status)
  1319. {
  1320. size_t baselen;
  1321. struct ieee802_11_elems elems;
  1322. BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
  1323. offsetof(typeof(mgmt->u.beacon), variable));
  1324. /*
  1325. * either beacon or probe_resp but the variable field is at the
  1326. * same offset
  1327. */
  1328. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1329. if (baselen > len)
  1330. return;
  1331. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1332. false, &elems);
  1333. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  1334. }
  1335. void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1336. struct sk_buff *skb)
  1337. {
  1338. struct ieee80211_rx_status *rx_status;
  1339. struct ieee80211_mgmt *mgmt;
  1340. u16 fc;
  1341. struct ieee802_11_elems elems;
  1342. int ies_len;
  1343. rx_status = IEEE80211_SKB_RXCB(skb);
  1344. mgmt = (struct ieee80211_mgmt *) skb->data;
  1345. fc = le16_to_cpu(mgmt->frame_control);
  1346. sdata_lock(sdata);
  1347. if (!sdata->u.ibss.ssid_len)
  1348. goto mgmt_out; /* not ready to merge yet */
  1349. switch (fc & IEEE80211_FCTL_STYPE) {
  1350. case IEEE80211_STYPE_PROBE_REQ:
  1351. ieee80211_rx_mgmt_probe_req(sdata, skb);
  1352. break;
  1353. case IEEE80211_STYPE_PROBE_RESP:
  1354. case IEEE80211_STYPE_BEACON:
  1355. ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
  1356. rx_status);
  1357. break;
  1358. case IEEE80211_STYPE_AUTH:
  1359. ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
  1360. break;
  1361. case IEEE80211_STYPE_DEAUTH:
  1362. ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
  1363. break;
  1364. case IEEE80211_STYPE_ACTION:
  1365. switch (mgmt->u.action.category) {
  1366. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1367. ies_len = skb->len -
  1368. offsetof(struct ieee80211_mgmt,
  1369. u.action.u.chan_switch.variable);
  1370. if (ies_len < 0)
  1371. break;
  1372. ieee802_11_parse_elems(
  1373. mgmt->u.action.u.chan_switch.variable,
  1374. ies_len, true, &elems);
  1375. if (elems.parse_error)
  1376. break;
  1377. ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt, skb->len,
  1378. rx_status, &elems);
  1379. break;
  1380. }
  1381. }
  1382. mgmt_out:
  1383. sdata_unlock(sdata);
  1384. }
  1385. void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
  1386. {
  1387. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1388. struct sta_info *sta;
  1389. sdata_lock(sdata);
  1390. /*
  1391. * Work could be scheduled after scan or similar
  1392. * when we aren't even joined (or trying) with a
  1393. * network.
  1394. */
  1395. if (!ifibss->ssid_len)
  1396. goto out;
  1397. spin_lock_bh(&ifibss->incomplete_lock);
  1398. while (!list_empty(&ifibss->incomplete_stations)) {
  1399. sta = list_first_entry(&ifibss->incomplete_stations,
  1400. struct sta_info, list);
  1401. list_del(&sta->list);
  1402. spin_unlock_bh(&ifibss->incomplete_lock);
  1403. ieee80211_ibss_finish_sta(sta);
  1404. rcu_read_unlock();
  1405. spin_lock_bh(&ifibss->incomplete_lock);
  1406. }
  1407. spin_unlock_bh(&ifibss->incomplete_lock);
  1408. switch (ifibss->state) {
  1409. case IEEE80211_IBSS_MLME_SEARCH:
  1410. ieee80211_sta_find_ibss(sdata);
  1411. break;
  1412. case IEEE80211_IBSS_MLME_JOINED:
  1413. ieee80211_sta_merge_ibss(sdata);
  1414. break;
  1415. default:
  1416. WARN_ON(1);
  1417. break;
  1418. }
  1419. out:
  1420. sdata_unlock(sdata);
  1421. }
  1422. static void ieee80211_ibss_timer(unsigned long data)
  1423. {
  1424. struct ieee80211_sub_if_data *sdata =
  1425. (struct ieee80211_sub_if_data *) data;
  1426. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1427. }
  1428. void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1429. {
  1430. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1431. setup_timer(&ifibss->timer, ieee80211_ibss_timer,
  1432. (unsigned long) sdata);
  1433. INIT_LIST_HEAD(&ifibss->incomplete_stations);
  1434. spin_lock_init(&ifibss->incomplete_lock);
  1435. INIT_WORK(&ifibss->csa_connection_drop_work,
  1436. ieee80211_csa_connection_drop_work);
  1437. }
  1438. /* scan finished notification */
  1439. void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
  1440. {
  1441. struct ieee80211_sub_if_data *sdata;
  1442. mutex_lock(&local->iflist_mtx);
  1443. list_for_each_entry(sdata, &local->interfaces, list) {
  1444. if (!ieee80211_sdata_running(sdata))
  1445. continue;
  1446. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  1447. continue;
  1448. sdata->u.ibss.last_scan_completed = jiffies;
  1449. }
  1450. mutex_unlock(&local->iflist_mtx);
  1451. }
  1452. int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
  1453. struct cfg80211_ibss_params *params)
  1454. {
  1455. u32 changed = 0;
  1456. u32 rate_flags;
  1457. struct ieee80211_supported_band *sband;
  1458. enum ieee80211_chanctx_mode chanmode;
  1459. struct ieee80211_local *local = sdata->local;
  1460. int radar_detect_width = 0;
  1461. int i;
  1462. int ret;
  1463. ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
  1464. &params->chandef,
  1465. sdata->wdev.iftype);
  1466. if (ret < 0)
  1467. return ret;
  1468. if (ret > 0) {
  1469. if (!params->userspace_handles_dfs)
  1470. return -EINVAL;
  1471. radar_detect_width = BIT(params->chandef.width);
  1472. }
  1473. chanmode = (params->channel_fixed && !ret) ?
  1474. IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
  1475. mutex_lock(&local->chanctx_mtx);
  1476. ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
  1477. radar_detect_width);
  1478. mutex_unlock(&local->chanctx_mtx);
  1479. if (ret < 0)
  1480. return ret;
  1481. if (params->bssid) {
  1482. memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
  1483. sdata->u.ibss.fixed_bssid = true;
  1484. } else
  1485. sdata->u.ibss.fixed_bssid = false;
  1486. sdata->u.ibss.privacy = params->privacy;
  1487. sdata->u.ibss.control_port = params->control_port;
  1488. sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
  1489. sdata->u.ibss.basic_rates = params->basic_rates;
  1490. sdata->u.ibss.last_scan_completed = jiffies;
  1491. /* fix basic_rates if channel does not support these rates */
  1492. rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
  1493. sband = local->hw.wiphy->bands[params->chandef.chan->band];
  1494. for (i = 0; i < sband->n_bitrates; i++) {
  1495. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1496. sdata->u.ibss.basic_rates &= ~BIT(i);
  1497. }
  1498. memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
  1499. sizeof(params->mcast_rate));
  1500. sdata->vif.bss_conf.beacon_int = params->beacon_interval;
  1501. sdata->u.ibss.chandef = params->chandef;
  1502. sdata->u.ibss.fixed_channel = params->channel_fixed;
  1503. if (params->ie) {
  1504. sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
  1505. GFP_KERNEL);
  1506. if (sdata->u.ibss.ie)
  1507. sdata->u.ibss.ie_len = params->ie_len;
  1508. }
  1509. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  1510. sdata->u.ibss.ibss_join_req = jiffies;
  1511. memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
  1512. sdata->u.ibss.ssid_len = params->ssid_len;
  1513. memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
  1514. sizeof(sdata->u.ibss.ht_capa));
  1515. memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
  1516. sizeof(sdata->u.ibss.ht_capa_mask));
  1517. /*
  1518. * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
  1519. * reserved, but an HT STA shall protect HT transmissions as though
  1520. * the HT Protection field were set to non-HT mixed mode.
  1521. *
  1522. * In an IBSS, the RIFS Mode field of the HT Operation element is
  1523. * also reserved, but an HT STA shall operate as though this field
  1524. * were set to 1.
  1525. */
  1526. sdata->vif.bss_conf.ht_operation_mode |=
  1527. IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
  1528. | IEEE80211_HT_PARAM_RIFS_MODE;
  1529. changed |= BSS_CHANGED_HT;
  1530. ieee80211_bss_info_change_notify(sdata, changed);
  1531. sdata->smps_mode = IEEE80211_SMPS_OFF;
  1532. sdata->needed_rx_chains = local->rx_chains;
  1533. ieee80211_queue_work(&local->hw, &sdata->work);
  1534. return 0;
  1535. }
  1536. int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
  1537. {
  1538. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1539. ieee80211_ibss_disconnect(sdata);
  1540. ifibss->ssid_len = 0;
  1541. eth_zero_addr(ifibss->bssid);
  1542. /* remove beacon */
  1543. kfree(sdata->u.ibss.ie);
  1544. /* on the next join, re-program HT parameters */
  1545. memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
  1546. memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
  1547. synchronize_rcu();
  1548. skb_queue_purge(&sdata->skb_queue);
  1549. del_timer_sync(&sdata->u.ibss.timer);
  1550. return 0;
  1551. }