ibss.c 51 KB

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