ibss.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/rtnetlink.h>
  22. #include <net/mac80211.h>
  23. #include "ieee80211_i.h"
  24. #include "driver-ops.h"
  25. #include "rate.h"
  26. #define IEEE80211_SCAN_INTERVAL (2 * HZ)
  27. #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
  28. #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
  29. #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
  30. #define IEEE80211_IBSS_RSN_INACTIVITY_LIMIT (10 * HZ)
  31. #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
  32. static struct beacon_data *
  33. ieee80211_ibss_build_presp(struct ieee80211_sub_if_data *sdata,
  34. const int beacon_int, const u32 basic_rates,
  35. const u16 capability, u64 tsf,
  36. struct cfg80211_chan_def *chandef,
  37. bool *have_higher_than_11mbit,
  38. struct cfg80211_csa_settings *csa_settings)
  39. {
  40. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  41. struct ieee80211_local *local = sdata->local;
  42. int rates_n = 0, i, ri;
  43. struct ieee80211_mgmt *mgmt;
  44. u8 *pos;
  45. struct ieee80211_supported_band *sband;
  46. u32 rate_flags, rates = 0, rates_added = 0;
  47. struct beacon_data *presp;
  48. int frame_len;
  49. int shift;
  50. /* Build IBSS probe response */
  51. frame_len = sizeof(struct ieee80211_hdr_3addr) +
  52. 12 /* struct ieee80211_mgmt.u.beacon */ +
  53. 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
  54. 2 + 8 /* max Supported Rates */ +
  55. 3 /* max DS params */ +
  56. 4 /* IBSS params */ +
  57. 5 /* Channel Switch Announcement */ +
  58. 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  59. 2 + sizeof(struct ieee80211_ht_cap) +
  60. 2 + sizeof(struct ieee80211_ht_operation) +
  61. ifibss->ie_len;
  62. presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
  63. if (!presp)
  64. return NULL;
  65. presp->head = (void *)(presp + 1);
  66. mgmt = (void *) presp->head;
  67. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  68. IEEE80211_STYPE_PROBE_RESP);
  69. eth_broadcast_addr(mgmt->da);
  70. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  71. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  72. mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
  73. mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
  74. mgmt->u.beacon.capab_info = cpu_to_le16(capability);
  75. pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
  76. *pos++ = WLAN_EID_SSID;
  77. *pos++ = ifibss->ssid_len;
  78. memcpy(pos, ifibss->ssid, ifibss->ssid_len);
  79. pos += ifibss->ssid_len;
  80. sband = local->hw.wiphy->bands[chandef->chan->band];
  81. rate_flags = ieee80211_chandef_rate_flags(chandef);
  82. shift = ieee80211_chandef_get_shift(chandef);
  83. rates_n = 0;
  84. if (have_higher_than_11mbit)
  85. *have_higher_than_11mbit = false;
  86. for (i = 0; i < sband->n_bitrates; i++) {
  87. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  88. continue;
  89. if (sband->bitrates[i].bitrate > 110 &&
  90. have_higher_than_11mbit)
  91. *have_higher_than_11mbit = true;
  92. rates |= BIT(i);
  93. rates_n++;
  94. }
  95. *pos++ = WLAN_EID_SUPP_RATES;
  96. *pos++ = min_t(int, 8, rates_n);
  97. for (ri = 0; ri < sband->n_bitrates; ri++) {
  98. int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
  99. 5 * (1 << shift));
  100. u8 basic = 0;
  101. if (!(rates & BIT(ri)))
  102. continue;
  103. if (basic_rates & BIT(ri))
  104. basic = 0x80;
  105. *pos++ = basic | (u8) rate;
  106. if (++rates_added == 8) {
  107. ri++; /* continue at next rate for EXT_SUPP_RATES */
  108. break;
  109. }
  110. }
  111. if (sband->band == IEEE80211_BAND_2GHZ) {
  112. *pos++ = WLAN_EID_DS_PARAMS;
  113. *pos++ = 1;
  114. *pos++ = ieee80211_frequency_to_channel(
  115. chandef->chan->center_freq);
  116. }
  117. *pos++ = WLAN_EID_IBSS_PARAMS;
  118. *pos++ = 2;
  119. /* FIX: set ATIM window based on scan results */
  120. *pos++ = 0;
  121. *pos++ = 0;
  122. if (csa_settings) {
  123. *pos++ = WLAN_EID_CHANNEL_SWITCH;
  124. *pos++ = 3;
  125. *pos++ = csa_settings->block_tx ? 1 : 0;
  126. *pos++ = ieee80211_frequency_to_channel(
  127. csa_settings->chandef.chan->center_freq);
  128. presp->csa_counter_offsets[0] = (pos - presp->head);
  129. *pos++ = csa_settings->count;
  130. presp->csa_current_counter = csa_settings->count;
  131. }
  132. /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
  133. if (rates_n > 8) {
  134. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  135. *pos++ = rates_n - 8;
  136. for (; ri < sband->n_bitrates; ri++) {
  137. int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
  138. 5 * (1 << shift));
  139. u8 basic = 0;
  140. if (!(rates & BIT(ri)))
  141. continue;
  142. if (basic_rates & BIT(ri))
  143. basic = 0x80;
  144. *pos++ = basic | (u8) rate;
  145. }
  146. }
  147. if (ifibss->ie_len) {
  148. memcpy(pos, ifibss->ie, ifibss->ie_len);
  149. pos += ifibss->ie_len;
  150. }
  151. /* add HT capability and information IEs */
  152. if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  153. chandef->width != NL80211_CHAN_WIDTH_5 &&
  154. chandef->width != NL80211_CHAN_WIDTH_10 &&
  155. sband->ht_cap.ht_supported) {
  156. struct ieee80211_sta_ht_cap ht_cap;
  157. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  158. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  159. pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  160. /*
  161. * Note: According to 802.11n-2009 9.13.3.1, HT Protection
  162. * field and RIFS Mode are reserved in IBSS mode, therefore
  163. * keep them at 0
  164. */
  165. pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
  166. chandef, 0, false);
  167. /* add VHT capability and information IEs */
  168. if (chandef->width != NL80211_CHAN_WIDTH_20 &&
  169. chandef->width != NL80211_CHAN_WIDTH_40 &&
  170. sband->vht_cap.vht_supported) {
  171. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  172. sband->vht_cap.cap);
  173. pos = ieee80211_ie_build_vht_oper(pos, &sband->vht_cap,
  174. chandef);
  175. }
  176. }
  177. if (local->hw.queues >= IEEE80211_NUM_ACS)
  178. pos = ieee80211_add_wmm_info_ie(pos, 0); /* U-APSD not in use */
  179. presp->head_len = pos - presp->head;
  180. if (WARN_ON(presp->head_len > frame_len))
  181. goto error;
  182. return presp;
  183. error:
  184. kfree(presp);
  185. return NULL;
  186. }
  187. static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  188. const u8 *bssid, const int beacon_int,
  189. struct cfg80211_chan_def *req_chandef,
  190. const u32 basic_rates,
  191. const u16 capability, u64 tsf,
  192. bool creator)
  193. {
  194. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  195. struct ieee80211_local *local = sdata->local;
  196. struct ieee80211_mgmt *mgmt;
  197. struct cfg80211_bss *bss;
  198. u32 bss_change;
  199. struct cfg80211_chan_def chandef;
  200. struct ieee80211_channel *chan;
  201. struct beacon_data *presp;
  202. struct cfg80211_inform_bss bss_meta = {};
  203. bool have_higher_than_11mbit;
  204. bool radar_required;
  205. int err;
  206. sdata_assert_lock(sdata);
  207. /* Reset own TSF to allow time synchronization work. */
  208. drv_reset_tsf(local, sdata);
  209. if (!ether_addr_equal(ifibss->bssid, bssid))
  210. sta_info_flush(sdata);
  211. /* if merging, indicate to driver that we leave the old IBSS */
  212. if (sdata->vif.bss_conf.ibss_joined) {
  213. sdata->vif.bss_conf.ibss_joined = false;
  214. sdata->vif.bss_conf.ibss_creator = false;
  215. sdata->vif.bss_conf.enable_beacon = false;
  216. netif_carrier_off(sdata->dev);
  217. ieee80211_bss_info_change_notify(sdata,
  218. BSS_CHANGED_IBSS |
  219. BSS_CHANGED_BEACON_ENABLED);
  220. drv_leave_ibss(local, sdata);
  221. }
  222. presp = rcu_dereference_protected(ifibss->presp,
  223. lockdep_is_held(&sdata->wdev.mtx));
  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_vif_use_channel(sdata, &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->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.bss_conf.ssid_len = ifibss->ssid_len;
  286. memcpy(sdata->vif.bss_conf.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 == IEEE80211_BAND_5GHZ;
  307. bss_change |= BSS_CHANGED_ERP_SLOT;
  308. /* cf. IEEE 802.11 9.2.12 */
  309. if (chan->band == IEEE80211_BAND_2GHZ && have_higher_than_11mbit)
  310. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  311. else
  312. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  313. ieee80211_set_wmm_default(sdata, true, false);
  314. sdata->vif.bss_conf.ibss_joined = true;
  315. sdata->vif.bss_conf.ibss_creator = creator;
  316. err = drv_join_ibss(local, sdata);
  317. if (err) {
  318. sdata->vif.bss_conf.ibss_joined = false;
  319. sdata->vif.bss_conf.ibss_creator = false;
  320. sdata->vif.bss_conf.enable_beacon = false;
  321. sdata->vif.bss_conf.ssid_len = 0;
  322. RCU_INIT_POINTER(ifibss->presp, NULL);
  323. kfree_rcu(presp, rcu_head);
  324. mutex_lock(&local->mtx);
  325. ieee80211_vif_release_channel(sdata);
  326. mutex_unlock(&local->mtx);
  327. sdata_info(sdata, "Failed to join IBSS, driver failure: %d\n",
  328. err);
  329. return;
  330. }
  331. ieee80211_bss_info_change_notify(sdata, bss_change);
  332. ifibss->state = IEEE80211_IBSS_MLME_JOINED;
  333. mod_timer(&ifibss->timer,
  334. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  335. bss_meta.chan = chan;
  336. bss_meta.scan_width = cfg80211_chandef_to_scan_width(&chandef);
  337. bss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta, mgmt,
  338. presp->head_len, GFP_KERNEL);
  339. cfg80211_put_bss(local->hw.wiphy, bss);
  340. netif_carrier_on(sdata->dev);
  341. cfg80211_ibss_joined(sdata->dev, ifibss->bssid, chan, GFP_KERNEL);
  342. }
  343. static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  344. struct ieee80211_bss *bss)
  345. {
  346. struct cfg80211_bss *cbss =
  347. container_of((void *)bss, struct cfg80211_bss, priv);
  348. struct ieee80211_supported_band *sband;
  349. struct cfg80211_chan_def chandef;
  350. u32 basic_rates;
  351. int i, j;
  352. u16 beacon_int = cbss->beacon_interval;
  353. const struct cfg80211_bss_ies *ies;
  354. enum nl80211_channel_type chan_type;
  355. u64 tsf;
  356. u32 rate_flags;
  357. int shift;
  358. sdata_assert_lock(sdata);
  359. if (beacon_int < 10)
  360. beacon_int = 10;
  361. switch (sdata->u.ibss.chandef.width) {
  362. case NL80211_CHAN_WIDTH_20_NOHT:
  363. case NL80211_CHAN_WIDTH_20:
  364. case NL80211_CHAN_WIDTH_40:
  365. chan_type = cfg80211_get_chandef_type(&sdata->u.ibss.chandef);
  366. cfg80211_chandef_create(&chandef, cbss->channel, chan_type);
  367. break;
  368. case NL80211_CHAN_WIDTH_5:
  369. case NL80211_CHAN_WIDTH_10:
  370. cfg80211_chandef_create(&chandef, cbss->channel,
  371. NL80211_CHAN_WIDTH_20_NOHT);
  372. chandef.width = sdata->u.ibss.chandef.width;
  373. break;
  374. case NL80211_CHAN_WIDTH_80:
  375. case NL80211_CHAN_WIDTH_80P80:
  376. case NL80211_CHAN_WIDTH_160:
  377. chandef = sdata->u.ibss.chandef;
  378. chandef.chan = cbss->channel;
  379. break;
  380. default:
  381. /* fall back to 20 MHz for unsupported modes */
  382. cfg80211_chandef_create(&chandef, cbss->channel,
  383. NL80211_CHAN_WIDTH_20_NOHT);
  384. break;
  385. }
  386. sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
  387. rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
  388. shift = ieee80211_vif_get_shift(&sdata->vif);
  389. basic_rates = 0;
  390. for (i = 0; i < bss->supp_rates_len; i++) {
  391. int rate = bss->supp_rates[i] & 0x7f;
  392. bool is_basic = !!(bss->supp_rates[i] & 0x80);
  393. for (j = 0; j < sband->n_bitrates; j++) {
  394. int brate;
  395. if ((rate_flags & sband->bitrates[j].flags)
  396. != rate_flags)
  397. continue;
  398. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  399. 5 * (1 << shift));
  400. if (brate == rate) {
  401. if (is_basic)
  402. basic_rates |= BIT(j);
  403. break;
  404. }
  405. }
  406. }
  407. rcu_read_lock();
  408. ies = rcu_dereference(cbss->ies);
  409. tsf = ies->tsf;
  410. rcu_read_unlock();
  411. __ieee80211_sta_join_ibss(sdata, cbss->bssid,
  412. beacon_int,
  413. &chandef,
  414. basic_rates,
  415. cbss->capability,
  416. tsf, false);
  417. }
  418. int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
  419. struct cfg80211_csa_settings *csa_settings)
  420. {
  421. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  422. struct beacon_data *presp, *old_presp;
  423. struct cfg80211_bss *cbss;
  424. const struct cfg80211_bss_ies *ies;
  425. u16 capability = 0;
  426. u64 tsf;
  427. int ret = 0;
  428. sdata_assert_lock(sdata);
  429. if (ifibss->privacy)
  430. capability = WLAN_CAPABILITY_PRIVACY;
  431. cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
  432. ifibss->bssid, ifibss->ssid,
  433. ifibss->ssid_len, IEEE80211_BSS_TYPE_IBSS,
  434. IEEE80211_PRIVACY(ifibss->privacy));
  435. if (WARN_ON(!cbss)) {
  436. ret = -EINVAL;
  437. goto out;
  438. }
  439. rcu_read_lock();
  440. ies = rcu_dereference(cbss->ies);
  441. tsf = ies->tsf;
  442. rcu_read_unlock();
  443. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  444. old_presp = rcu_dereference_protected(ifibss->presp,
  445. lockdep_is_held(&sdata->wdev.mtx));
  446. presp = ieee80211_ibss_build_presp(sdata,
  447. sdata->vif.bss_conf.beacon_int,
  448. sdata->vif.bss_conf.basic_rates,
  449. capability, tsf, &ifibss->chandef,
  450. NULL, csa_settings);
  451. if (!presp) {
  452. ret = -ENOMEM;
  453. goto out;
  454. }
  455. rcu_assign_pointer(ifibss->presp, presp);
  456. if (old_presp)
  457. kfree_rcu(old_presp, rcu_head);
  458. return BSS_CHANGED_BEACON;
  459. out:
  460. return ret;
  461. }
  462. int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata)
  463. {
  464. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  465. struct cfg80211_bss *cbss;
  466. int err, changed = 0;
  467. sdata_assert_lock(sdata);
  468. /* update cfg80211 bss information with the new channel */
  469. if (!is_zero_ether_addr(ifibss->bssid)) {
  470. cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
  471. ifibss->chandef.chan,
  472. ifibss->bssid, ifibss->ssid,
  473. ifibss->ssid_len,
  474. IEEE80211_BSS_TYPE_IBSS,
  475. IEEE80211_PRIVACY(ifibss->privacy));
  476. /* XXX: should not really modify cfg80211 data */
  477. if (cbss) {
  478. cbss->channel = sdata->csa_chandef.chan;
  479. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  480. }
  481. }
  482. ifibss->chandef = sdata->csa_chandef;
  483. /* generate the beacon */
  484. err = ieee80211_ibss_csa_beacon(sdata, NULL);
  485. if (err < 0)
  486. return err;
  487. changed |= err;
  488. return changed;
  489. }
  490. void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
  491. {
  492. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  493. cancel_work_sync(&ifibss->csa_connection_drop_work);
  494. }
  495. static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
  496. __acquires(RCU)
  497. {
  498. struct ieee80211_sub_if_data *sdata = sta->sdata;
  499. u8 addr[ETH_ALEN];
  500. memcpy(addr, sta->sta.addr, ETH_ALEN);
  501. ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
  502. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  503. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  504. /* authorize the station only if the network is not RSN protected. If
  505. * not wait for the userspace to authorize it */
  506. if (!sta->sdata->u.ibss.control_port)
  507. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  508. rate_control_rate_init(sta);
  509. /* If it fails, maybe we raced another insertion? */
  510. if (sta_info_insert_rcu(sta))
  511. return sta_info_get(sdata, addr);
  512. return sta;
  513. }
  514. static struct sta_info *
  515. ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
  516. const u8 *addr, u32 supp_rates)
  517. __acquires(RCU)
  518. {
  519. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  520. struct ieee80211_local *local = sdata->local;
  521. struct sta_info *sta;
  522. struct ieee80211_chanctx_conf *chanctx_conf;
  523. struct ieee80211_supported_band *sband;
  524. enum nl80211_bss_scan_width scan_width;
  525. int band;
  526. /*
  527. * XXX: Consider removing the least recently used entry and
  528. * allow new one to be added.
  529. */
  530. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  531. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  532. sdata->name, addr);
  533. rcu_read_lock();
  534. return NULL;
  535. }
  536. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
  537. rcu_read_lock();
  538. return NULL;
  539. }
  540. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
  541. rcu_read_lock();
  542. return NULL;
  543. }
  544. rcu_read_lock();
  545. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  546. if (WARN_ON_ONCE(!chanctx_conf))
  547. return NULL;
  548. band = chanctx_conf->def.chan->band;
  549. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  550. rcu_read_unlock();
  551. sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
  552. if (!sta) {
  553. rcu_read_lock();
  554. return NULL;
  555. }
  556. sta->rx_stats.last_rx = jiffies;
  557. /* make sure mandatory rates are always added */
  558. sband = local->hw.wiphy->bands[band];
  559. sta->sta.supp_rates[band] = supp_rates |
  560. ieee80211_mandatory_rates(sband, scan_width);
  561. return ieee80211_ibss_finish_sta(sta);
  562. }
  563. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  564. {
  565. struct ieee80211_local *local = sdata->local;
  566. int active = 0;
  567. struct sta_info *sta;
  568. sdata_assert_lock(sdata);
  569. rcu_read_lock();
  570. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  571. if (sta->sdata == sdata &&
  572. time_after(sta->rx_stats.last_rx +
  573. IEEE80211_IBSS_MERGE_INTERVAL,
  574. jiffies)) {
  575. active++;
  576. break;
  577. }
  578. }
  579. rcu_read_unlock();
  580. return active;
  581. }
  582. static void ieee80211_ibss_disconnect(struct ieee80211_sub_if_data *sdata)
  583. {
  584. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  585. struct ieee80211_local *local = sdata->local;
  586. struct cfg80211_bss *cbss;
  587. struct beacon_data *presp;
  588. struct sta_info *sta;
  589. if (!is_zero_ether_addr(ifibss->bssid)) {
  590. cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
  591. ifibss->bssid, ifibss->ssid,
  592. ifibss->ssid_len,
  593. IEEE80211_BSS_TYPE_IBSS,
  594. IEEE80211_PRIVACY(ifibss->privacy));
  595. if (cbss) {
  596. cfg80211_unlink_bss(local->hw.wiphy, cbss);
  597. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  598. }
  599. }
  600. ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
  601. sta_info_flush(sdata);
  602. spin_lock_bh(&ifibss->incomplete_lock);
  603. while (!list_empty(&ifibss->incomplete_stations)) {
  604. sta = list_first_entry(&ifibss->incomplete_stations,
  605. struct sta_info, list);
  606. list_del(&sta->list);
  607. spin_unlock_bh(&ifibss->incomplete_lock);
  608. sta_info_free(local, sta);
  609. spin_lock_bh(&ifibss->incomplete_lock);
  610. }
  611. spin_unlock_bh(&ifibss->incomplete_lock);
  612. netif_carrier_off(sdata->dev);
  613. sdata->vif.bss_conf.ibss_joined = false;
  614. sdata->vif.bss_conf.ibss_creator = false;
  615. sdata->vif.bss_conf.enable_beacon = false;
  616. sdata->vif.bss_conf.ssid_len = 0;
  617. /* remove beacon */
  618. presp = rcu_dereference_protected(ifibss->presp,
  619. lockdep_is_held(&sdata->wdev.mtx));
  620. RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
  621. if (presp)
  622. kfree_rcu(presp, rcu_head);
  623. clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
  624. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
  625. BSS_CHANGED_IBSS);
  626. drv_leave_ibss(local, sdata);
  627. mutex_lock(&local->mtx);
  628. ieee80211_vif_release_channel(sdata);
  629. mutex_unlock(&local->mtx);
  630. }
  631. static void ieee80211_csa_connection_drop_work(struct work_struct *work)
  632. {
  633. struct ieee80211_sub_if_data *sdata =
  634. container_of(work, struct ieee80211_sub_if_data,
  635. u.ibss.csa_connection_drop_work);
  636. sdata_lock(sdata);
  637. ieee80211_ibss_disconnect(sdata);
  638. synchronize_rcu();
  639. skb_queue_purge(&sdata->skb_queue);
  640. /* trigger a scan to find another IBSS network to join */
  641. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  642. sdata_unlock(sdata);
  643. }
  644. static void ieee80211_ibss_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
  645. {
  646. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  647. int err;
  648. /* if the current channel is a DFS channel, mark the channel as
  649. * unavailable.
  650. */
  651. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  652. &ifibss->chandef,
  653. NL80211_IFTYPE_ADHOC);
  654. if (err > 0)
  655. cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
  656. GFP_ATOMIC);
  657. }
  658. static bool
  659. ieee80211_ibss_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  660. struct ieee802_11_elems *elems,
  661. bool beacon)
  662. {
  663. struct cfg80211_csa_settings params;
  664. struct ieee80211_csa_ie csa_ie;
  665. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  666. enum nl80211_channel_type ch_type;
  667. int err;
  668. u32 sta_flags;
  669. sdata_assert_lock(sdata);
  670. sta_flags = IEEE80211_STA_DISABLE_VHT;
  671. switch (ifibss->chandef.width) {
  672. case NL80211_CHAN_WIDTH_5:
  673. case NL80211_CHAN_WIDTH_10:
  674. case NL80211_CHAN_WIDTH_20_NOHT:
  675. sta_flags |= IEEE80211_STA_DISABLE_HT;
  676. /* fall through */
  677. case NL80211_CHAN_WIDTH_20:
  678. sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
  679. break;
  680. default:
  681. break;
  682. }
  683. memset(&params, 0, sizeof(params));
  684. memset(&csa_ie, 0, sizeof(csa_ie));
  685. err = ieee80211_parse_ch_switch_ie(sdata, elems,
  686. ifibss->chandef.chan->band,
  687. sta_flags, ifibss->bssid, &csa_ie);
  688. /* can't switch to destination channel, fail */
  689. if (err < 0)
  690. goto disconnect;
  691. /* did not contain a CSA */
  692. if (err)
  693. return false;
  694. /* channel switch is not supported, disconnect */
  695. if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
  696. goto disconnect;
  697. params.count = csa_ie.count;
  698. params.chandef = csa_ie.chandef;
  699. switch (ifibss->chandef.width) {
  700. case NL80211_CHAN_WIDTH_20_NOHT:
  701. case NL80211_CHAN_WIDTH_20:
  702. case NL80211_CHAN_WIDTH_40:
  703. /* keep our current HT mode (HT20/HT40+/HT40-), even if
  704. * another mode has been announced. The mode is not adopted
  705. * within the beacon while doing CSA and we should therefore
  706. * keep the mode which we announce.
  707. */
  708. ch_type = cfg80211_get_chandef_type(&ifibss->chandef);
  709. cfg80211_chandef_create(&params.chandef, params.chandef.chan,
  710. ch_type);
  711. break;
  712. case NL80211_CHAN_WIDTH_5:
  713. case NL80211_CHAN_WIDTH_10:
  714. if (params.chandef.width != ifibss->chandef.width) {
  715. sdata_info(sdata,
  716. "IBSS %pM received channel switch from incompatible channel width (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
  717. ifibss->bssid,
  718. params.chandef.chan->center_freq,
  719. params.chandef.width,
  720. params.chandef.center_freq1,
  721. params.chandef.center_freq2);
  722. goto disconnect;
  723. }
  724. break;
  725. default:
  726. /* should not happen, sta_flags should prevent VHT modes. */
  727. WARN_ON(1);
  728. goto disconnect;
  729. }
  730. if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
  731. NL80211_IFTYPE_ADHOC)) {
  732. sdata_info(sdata,
  733. "IBSS %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
  734. ifibss->bssid,
  735. params.chandef.chan->center_freq,
  736. params.chandef.width,
  737. params.chandef.center_freq1,
  738. params.chandef.center_freq2);
  739. goto disconnect;
  740. }
  741. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  742. &params.chandef,
  743. NL80211_IFTYPE_ADHOC);
  744. if (err < 0)
  745. goto disconnect;
  746. if (err > 0 && !ifibss->userspace_handles_dfs) {
  747. /* IBSS-DFS only allowed with a control program */
  748. goto disconnect;
  749. }
  750. params.radar_required = err;
  751. if (cfg80211_chandef_identical(&params.chandef,
  752. &sdata->vif.bss_conf.chandef)) {
  753. ibss_dbg(sdata,
  754. "received csa with an identical chandef, ignoring\n");
  755. return true;
  756. }
  757. /* all checks done, now perform the channel switch. */
  758. ibss_dbg(sdata,
  759. "received channel switch announcement to go to channel %d MHz\n",
  760. params.chandef.chan->center_freq);
  761. params.block_tx = !!csa_ie.mode;
  762. if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
  763. &params))
  764. goto disconnect;
  765. ieee80211_ibss_csa_mark_radar(sdata);
  766. return true;
  767. disconnect:
  768. ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
  769. ieee80211_queue_work(&sdata->local->hw,
  770. &ifibss->csa_connection_drop_work);
  771. ieee80211_ibss_csa_mark_radar(sdata);
  772. return true;
  773. }
  774. static void
  775. ieee80211_rx_mgmt_spectrum_mgmt(struct ieee80211_sub_if_data *sdata,
  776. struct ieee80211_mgmt *mgmt, size_t len,
  777. struct ieee80211_rx_status *rx_status,
  778. struct ieee802_11_elems *elems)
  779. {
  780. int required_len;
  781. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  782. return;
  783. /* CSA is the only action we handle for now */
  784. if (mgmt->u.action.u.measurement.action_code !=
  785. WLAN_ACTION_SPCT_CHL_SWITCH)
  786. return;
  787. required_len = IEEE80211_MIN_ACTION_SIZE +
  788. sizeof(mgmt->u.action.u.chan_switch);
  789. if (len < required_len)
  790. return;
  791. if (!sdata->vif.csa_active)
  792. ieee80211_ibss_process_chanswitch(sdata, elems, false);
  793. }
  794. static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
  795. struct ieee80211_mgmt *mgmt,
  796. size_t len)
  797. {
  798. u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
  799. if (len < IEEE80211_DEAUTH_FRAME_LEN)
  800. return;
  801. ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM BSSID=%pM (reason: %d)\n",
  802. mgmt->sa, mgmt->da, mgmt->bssid, reason);
  803. sta_info_destroy_addr(sdata, mgmt->sa);
  804. }
  805. static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
  806. struct ieee80211_mgmt *mgmt,
  807. size_t len)
  808. {
  809. u16 auth_alg, auth_transaction;
  810. sdata_assert_lock(sdata);
  811. if (len < 24 + 6)
  812. return;
  813. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  814. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  815. ibss_dbg(sdata,
  816. "RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
  817. mgmt->sa, mgmt->da, mgmt->bssid, auth_transaction);
  818. if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
  819. return;
  820. /*
  821. * IEEE 802.11 standard does not require authentication in IBSS
  822. * networks and most implementations do not seem to use it.
  823. * However, try to reply to authentication attempts if someone
  824. * has actually implemented this.
  825. */
  826. ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
  827. mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
  828. }
  829. static void ieee80211_update_sta_info(struct ieee80211_sub_if_data *sdata,
  830. struct ieee80211_mgmt *mgmt, size_t len,
  831. struct ieee80211_rx_status *rx_status,
  832. struct ieee802_11_elems *elems,
  833. struct ieee80211_channel *channel)
  834. {
  835. struct sta_info *sta;
  836. enum ieee80211_band band = rx_status->band;
  837. enum nl80211_bss_scan_width scan_width;
  838. struct ieee80211_local *local = sdata->local;
  839. struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
  840. bool rates_updated = false;
  841. u32 supp_rates = 0;
  842. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  843. return;
  844. if (!ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid))
  845. return;
  846. rcu_read_lock();
  847. sta = sta_info_get(sdata, mgmt->sa);
  848. if (elems->supp_rates) {
  849. supp_rates = ieee80211_sta_get_rates(sdata, elems,
  850. band, NULL);
  851. if (sta) {
  852. u32 prev_rates;
  853. prev_rates = sta->sta.supp_rates[band];
  854. /* make sure mandatory rates are always added */
  855. scan_width = NL80211_BSS_CHAN_WIDTH_20;
  856. if (rx_status->flag & RX_FLAG_5MHZ)
  857. scan_width = NL80211_BSS_CHAN_WIDTH_5;
  858. if (rx_status->flag & RX_FLAG_10MHZ)
  859. scan_width = NL80211_BSS_CHAN_WIDTH_10;
  860. sta->sta.supp_rates[band] = supp_rates |
  861. ieee80211_mandatory_rates(sband, scan_width);
  862. if (sta->sta.supp_rates[band] != prev_rates) {
  863. ibss_dbg(sdata,
  864. "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
  865. sta->sta.addr, prev_rates,
  866. sta->sta.supp_rates[band]);
  867. rates_updated = true;
  868. }
  869. } else {
  870. rcu_read_unlock();
  871. sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
  872. mgmt->sa, supp_rates);
  873. }
  874. }
  875. if (sta && !sta->sta.wme &&
  876. elems->wmm_info && local->hw.queues >= IEEE80211_NUM_ACS) {
  877. sta->sta.wme = true;
  878. ieee80211_check_fast_xmit(sta);
  879. }
  880. if (sta && elems->ht_operation && elems->ht_cap_elem &&
  881. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
  882. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
  883. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
  884. /* we both use HT */
  885. struct ieee80211_ht_cap htcap_ie;
  886. struct cfg80211_chan_def chandef;
  887. enum ieee80211_sta_rx_bandwidth bw = sta->sta.bandwidth;
  888. ieee80211_ht_oper_to_chandef(channel,
  889. elems->ht_operation,
  890. &chandef);
  891. memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
  892. rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  893. &htcap_ie,
  894. sta);
  895. if (elems->vht_operation && elems->vht_cap_elem &&
  896. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20 &&
  897. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_40) {
  898. /* we both use VHT */
  899. struct ieee80211_vht_cap cap_ie;
  900. struct ieee80211_sta_vht_cap cap = sta->sta.vht_cap;
  901. ieee80211_vht_oper_to_chandef(channel,
  902. 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 ieee80211_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. sta->rx_stats.last_rx = jiffies;
  1043. /* make sure mandatory rates are always added */
  1044. sband = local->hw.wiphy->bands[band];
  1045. sta->sta.supp_rates[band] = supp_rates |
  1046. ieee80211_mandatory_rates(sband, scan_width);
  1047. spin_lock(&ifibss->incomplete_lock);
  1048. list_add(&sta->list, &ifibss->incomplete_stations);
  1049. spin_unlock(&ifibss->incomplete_lock);
  1050. ieee80211_queue_work(&local->hw, &sdata->work);
  1051. }
  1052. static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
  1053. {
  1054. struct ieee80211_local *local = sdata->local;
  1055. struct sta_info *sta, *tmp;
  1056. unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
  1057. unsigned long exp_rsn = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
  1058. mutex_lock(&local->sta_mtx);
  1059. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  1060. if (sdata != sta->sdata)
  1061. continue;
  1062. if (time_after(jiffies, sta->rx_stats.last_rx + exp_time) ||
  1063. (time_after(jiffies, sta->rx_stats.last_rx + 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. num = ieee80211_ibss_setup_scan_channels(local->hw.wiphy,
  1240. &ifibss->chandef,
  1241. channels,
  1242. ARRAY_SIZE(channels));
  1243. scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
  1244. ieee80211_request_ibss_scan(sdata, ifibss->ssid,
  1245. ifibss->ssid_len, channels, num,
  1246. scan_width);
  1247. } else {
  1248. int interval = IEEE80211_SCAN_INTERVAL;
  1249. if (time_after(jiffies, ifibss->ibss_join_req +
  1250. IEEE80211_IBSS_JOIN_TIMEOUT))
  1251. ieee80211_sta_create_ibss(sdata);
  1252. mod_timer(&ifibss->timer,
  1253. round_jiffies(jiffies + interval));
  1254. }
  1255. }
  1256. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  1257. struct sk_buff *req)
  1258. {
  1259. struct ieee80211_mgmt *mgmt = (void *)req->data;
  1260. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1261. struct ieee80211_local *local = sdata->local;
  1262. int tx_last_beacon, len = req->len;
  1263. struct sk_buff *skb;
  1264. struct beacon_data *presp;
  1265. u8 *pos, *end;
  1266. sdata_assert_lock(sdata);
  1267. presp = rcu_dereference_protected(ifibss->presp,
  1268. lockdep_is_held(&sdata->wdev.mtx));
  1269. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  1270. len < 24 + 2 || !presp)
  1271. return;
  1272. tx_last_beacon = drv_tx_last_beacon(local);
  1273. ibss_dbg(sdata,
  1274. "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
  1275. mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
  1276. if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
  1277. return;
  1278. if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
  1279. !is_broadcast_ether_addr(mgmt->bssid))
  1280. return;
  1281. end = ((u8 *) mgmt) + len;
  1282. pos = mgmt->u.probe_req.variable;
  1283. if (pos[0] != WLAN_EID_SSID ||
  1284. pos + 2 + pos[1] > end) {
  1285. ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
  1286. mgmt->sa);
  1287. return;
  1288. }
  1289. if (pos[1] != 0 &&
  1290. (pos[1] != ifibss->ssid_len ||
  1291. memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
  1292. /* Ignore ProbeReq for foreign SSID */
  1293. return;
  1294. }
  1295. /* Reply with ProbeResp */
  1296. skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
  1297. if (!skb)
  1298. return;
  1299. skb_reserve(skb, local->tx_headroom);
  1300. memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
  1301. memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
  1302. ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
  1303. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1304. /* avoid excessive retries for probe request to wildcard SSIDs */
  1305. if (pos[1] == 0)
  1306. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
  1307. ieee80211_tx_skb(sdata, skb);
  1308. }
  1309. static
  1310. void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
  1311. struct ieee80211_mgmt *mgmt, size_t len,
  1312. struct ieee80211_rx_status *rx_status)
  1313. {
  1314. size_t baselen;
  1315. struct ieee802_11_elems elems;
  1316. BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
  1317. offsetof(typeof(mgmt->u.beacon), variable));
  1318. /*
  1319. * either beacon or probe_resp but the variable field is at the
  1320. * same offset
  1321. */
  1322. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1323. if (baselen > len)
  1324. return;
  1325. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1326. false, &elems);
  1327. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  1328. }
  1329. void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1330. struct sk_buff *skb)
  1331. {
  1332. struct ieee80211_rx_status *rx_status;
  1333. struct ieee80211_mgmt *mgmt;
  1334. u16 fc;
  1335. struct ieee802_11_elems elems;
  1336. int ies_len;
  1337. rx_status = IEEE80211_SKB_RXCB(skb);
  1338. mgmt = (struct ieee80211_mgmt *) skb->data;
  1339. fc = le16_to_cpu(mgmt->frame_control);
  1340. sdata_lock(sdata);
  1341. if (!sdata->u.ibss.ssid_len)
  1342. goto mgmt_out; /* not ready to merge yet */
  1343. switch (fc & IEEE80211_FCTL_STYPE) {
  1344. case IEEE80211_STYPE_PROBE_REQ:
  1345. ieee80211_rx_mgmt_probe_req(sdata, skb);
  1346. break;
  1347. case IEEE80211_STYPE_PROBE_RESP:
  1348. case IEEE80211_STYPE_BEACON:
  1349. ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
  1350. rx_status);
  1351. break;
  1352. case IEEE80211_STYPE_AUTH:
  1353. ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
  1354. break;
  1355. case IEEE80211_STYPE_DEAUTH:
  1356. ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
  1357. break;
  1358. case IEEE80211_STYPE_ACTION:
  1359. switch (mgmt->u.action.category) {
  1360. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1361. ies_len = skb->len -
  1362. offsetof(struct ieee80211_mgmt,
  1363. u.action.u.chan_switch.variable);
  1364. if (ies_len < 0)
  1365. break;
  1366. ieee802_11_parse_elems(
  1367. mgmt->u.action.u.chan_switch.variable,
  1368. ies_len, true, &elems);
  1369. if (elems.parse_error)
  1370. break;
  1371. ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt, skb->len,
  1372. rx_status, &elems);
  1373. break;
  1374. }
  1375. }
  1376. mgmt_out:
  1377. sdata_unlock(sdata);
  1378. }
  1379. void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
  1380. {
  1381. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1382. struct sta_info *sta;
  1383. sdata_lock(sdata);
  1384. /*
  1385. * Work could be scheduled after scan or similar
  1386. * when we aren't even joined (or trying) with a
  1387. * network.
  1388. */
  1389. if (!ifibss->ssid_len)
  1390. goto out;
  1391. spin_lock_bh(&ifibss->incomplete_lock);
  1392. while (!list_empty(&ifibss->incomplete_stations)) {
  1393. sta = list_first_entry(&ifibss->incomplete_stations,
  1394. struct sta_info, list);
  1395. list_del(&sta->list);
  1396. spin_unlock_bh(&ifibss->incomplete_lock);
  1397. ieee80211_ibss_finish_sta(sta);
  1398. rcu_read_unlock();
  1399. spin_lock_bh(&ifibss->incomplete_lock);
  1400. }
  1401. spin_unlock_bh(&ifibss->incomplete_lock);
  1402. switch (ifibss->state) {
  1403. case IEEE80211_IBSS_MLME_SEARCH:
  1404. ieee80211_sta_find_ibss(sdata);
  1405. break;
  1406. case IEEE80211_IBSS_MLME_JOINED:
  1407. ieee80211_sta_merge_ibss(sdata);
  1408. break;
  1409. default:
  1410. WARN_ON(1);
  1411. break;
  1412. }
  1413. out:
  1414. sdata_unlock(sdata);
  1415. }
  1416. static void ieee80211_ibss_timer(unsigned long data)
  1417. {
  1418. struct ieee80211_sub_if_data *sdata =
  1419. (struct ieee80211_sub_if_data *) data;
  1420. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1421. }
  1422. void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1423. {
  1424. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1425. setup_timer(&ifibss->timer, ieee80211_ibss_timer,
  1426. (unsigned long) sdata);
  1427. INIT_LIST_HEAD(&ifibss->incomplete_stations);
  1428. spin_lock_init(&ifibss->incomplete_lock);
  1429. INIT_WORK(&ifibss->csa_connection_drop_work,
  1430. ieee80211_csa_connection_drop_work);
  1431. }
  1432. /* scan finished notification */
  1433. void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
  1434. {
  1435. struct ieee80211_sub_if_data *sdata;
  1436. mutex_lock(&local->iflist_mtx);
  1437. list_for_each_entry(sdata, &local->interfaces, list) {
  1438. if (!ieee80211_sdata_running(sdata))
  1439. continue;
  1440. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  1441. continue;
  1442. sdata->u.ibss.last_scan_completed = jiffies;
  1443. ieee80211_queue_work(&local->hw, &sdata->work);
  1444. }
  1445. mutex_unlock(&local->iflist_mtx);
  1446. }
  1447. int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
  1448. struct cfg80211_ibss_params *params)
  1449. {
  1450. u32 changed = 0;
  1451. u32 rate_flags;
  1452. struct ieee80211_supported_band *sband;
  1453. enum ieee80211_chanctx_mode chanmode;
  1454. struct ieee80211_local *local = sdata->local;
  1455. int radar_detect_width = 0;
  1456. int i;
  1457. int ret;
  1458. ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
  1459. &params->chandef,
  1460. sdata->wdev.iftype);
  1461. if (ret < 0)
  1462. return ret;
  1463. if (ret > 0) {
  1464. if (!params->userspace_handles_dfs)
  1465. return -EINVAL;
  1466. radar_detect_width = BIT(params->chandef.width);
  1467. }
  1468. chanmode = (params->channel_fixed && !ret) ?
  1469. IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
  1470. mutex_lock(&local->chanctx_mtx);
  1471. ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
  1472. radar_detect_width);
  1473. mutex_unlock(&local->chanctx_mtx);
  1474. if (ret < 0)
  1475. return ret;
  1476. if (params->bssid) {
  1477. memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
  1478. sdata->u.ibss.fixed_bssid = true;
  1479. } else
  1480. sdata->u.ibss.fixed_bssid = false;
  1481. sdata->u.ibss.privacy = params->privacy;
  1482. sdata->u.ibss.control_port = params->control_port;
  1483. sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
  1484. sdata->u.ibss.basic_rates = params->basic_rates;
  1485. sdata->u.ibss.last_scan_completed = jiffies;
  1486. /* fix basic_rates if channel does not support these rates */
  1487. rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
  1488. sband = local->hw.wiphy->bands[params->chandef.chan->band];
  1489. for (i = 0; i < sband->n_bitrates; i++) {
  1490. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1491. sdata->u.ibss.basic_rates &= ~BIT(i);
  1492. }
  1493. memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
  1494. sizeof(params->mcast_rate));
  1495. sdata->vif.bss_conf.beacon_int = params->beacon_interval;
  1496. sdata->u.ibss.chandef = params->chandef;
  1497. sdata->u.ibss.fixed_channel = params->channel_fixed;
  1498. if (params->ie) {
  1499. sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
  1500. GFP_KERNEL);
  1501. if (sdata->u.ibss.ie)
  1502. sdata->u.ibss.ie_len = params->ie_len;
  1503. }
  1504. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  1505. sdata->u.ibss.ibss_join_req = jiffies;
  1506. memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
  1507. sdata->u.ibss.ssid_len = params->ssid_len;
  1508. memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
  1509. sizeof(sdata->u.ibss.ht_capa));
  1510. memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
  1511. sizeof(sdata->u.ibss.ht_capa_mask));
  1512. /*
  1513. * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
  1514. * reserved, but an HT STA shall protect HT transmissions as though
  1515. * the HT Protection field were set to non-HT mixed mode.
  1516. *
  1517. * In an IBSS, the RIFS Mode field of the HT Operation element is
  1518. * also reserved, but an HT STA shall operate as though this field
  1519. * were set to 1.
  1520. */
  1521. sdata->vif.bss_conf.ht_operation_mode |=
  1522. IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
  1523. | IEEE80211_HT_PARAM_RIFS_MODE;
  1524. changed |= BSS_CHANGED_HT;
  1525. ieee80211_bss_info_change_notify(sdata, changed);
  1526. sdata->smps_mode = IEEE80211_SMPS_OFF;
  1527. sdata->needed_rx_chains = local->rx_chains;
  1528. ieee80211_queue_work(&local->hw, &sdata->work);
  1529. return 0;
  1530. }
  1531. int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
  1532. {
  1533. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1534. ieee80211_ibss_disconnect(sdata);
  1535. ifibss->ssid_len = 0;
  1536. eth_zero_addr(ifibss->bssid);
  1537. /* remove beacon */
  1538. kfree(sdata->u.ibss.ie);
  1539. /* on the next join, re-program HT parameters */
  1540. memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
  1541. memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
  1542. synchronize_rcu();
  1543. skb_queue_purge(&sdata->skb_queue);
  1544. del_timer_sync(&sdata->u.ibss.timer);
  1545. return 0;
  1546. }