ibss.c 50 KB

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