ibss.c 48 KB

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