ibss.c 47 KB

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