ibss.c 51 KB

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