ibss.c 47 KB

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