tdls.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mac80211 TDLS handling code
  4. *
  5. * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2014, Intel Corporation
  7. * Copyright 2014 Intel Mobile Communications GmbH
  8. * Copyright 2015 - 2016 Intel Deutschland GmbH
  9. * Copyright (C) 2019, 2021-2025 Intel Corporation
  10. */
  11. #include <linux/ieee80211.h>
  12. #include <linux/log2.h>
  13. #include <net/cfg80211.h>
  14. #include <linux/rtnetlink.h>
  15. #include "ieee80211_i.h"
  16. #include "driver-ops.h"
  17. #include "rate.h"
  18. #include "wme.h"
  19. /* give usermode some time for retries in setting up the TDLS session */
  20. #define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
  21. void ieee80211_tdls_peer_del_work(struct wiphy *wiphy, struct wiphy_work *wk)
  22. {
  23. struct ieee80211_sub_if_data *sdata;
  24. struct ieee80211_local *local;
  25. sdata = container_of(wk, struct ieee80211_sub_if_data,
  26. u.mgd.tdls_peer_del_work.work);
  27. local = sdata->local;
  28. lockdep_assert_wiphy(local->hw.wiphy);
  29. if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
  30. tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->u.mgd.tdls_peer);
  31. sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
  32. eth_zero_addr(sdata->u.mgd.tdls_peer);
  33. }
  34. }
  35. static void ieee80211_tdls_add_ext_capab(struct ieee80211_link_data *link,
  36. struct sk_buff *skb)
  37. {
  38. struct ieee80211_sub_if_data *sdata = link->sdata;
  39. struct ieee80211_local *local = sdata->local;
  40. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  41. bool chan_switch = local->hw.wiphy->features &
  42. NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
  43. bool wider_band = ieee80211_hw_check(&local->hw, TDLS_WIDER_BW) &&
  44. !ifmgd->tdls_wider_bw_prohibited;
  45. bool buffer_sta = ieee80211_hw_check(&local->hw,
  46. SUPPORTS_TDLS_BUFFER_STA);
  47. struct ieee80211_supported_band *sband = ieee80211_get_link_sband(link);
  48. bool vht = sband && sband->vht_cap.vht_supported;
  49. u8 *pos = skb_put(skb, 10);
  50. *pos++ = WLAN_EID_EXT_CAPABILITY;
  51. *pos++ = 8; /* len */
  52. *pos++ = 0x0;
  53. *pos++ = 0x0;
  54. *pos++ = 0x0;
  55. *pos++ = (chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0) |
  56. (buffer_sta ? WLAN_EXT_CAPA4_TDLS_BUFFER_STA : 0);
  57. *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
  58. *pos++ = 0;
  59. *pos++ = 0;
  60. *pos++ = (vht && wider_band) ? WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED : 0;
  61. }
  62. static u8
  63. ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
  64. struct sk_buff *skb, u16 start, u16 end,
  65. u16 spacing)
  66. {
  67. u8 subband_cnt = 0, ch_cnt = 0;
  68. struct ieee80211_channel *ch;
  69. struct cfg80211_chan_def chandef;
  70. int i, subband_start;
  71. struct wiphy *wiphy = sdata->local->hw.wiphy;
  72. for (i = start; i <= end; i += spacing) {
  73. if (!ch_cnt)
  74. subband_start = i;
  75. ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
  76. if (ch) {
  77. /* we will be active on the channel */
  78. cfg80211_chandef_create(&chandef, ch,
  79. NL80211_CHAN_NO_HT);
  80. if (cfg80211_reg_can_beacon_relax(wiphy, &chandef,
  81. sdata->wdev.iftype)) {
  82. ch_cnt++;
  83. /*
  84. * check if the next channel is also part of
  85. * this allowed range
  86. */
  87. continue;
  88. }
  89. }
  90. /*
  91. * we've reached the end of a range, with allowed channels
  92. * found
  93. */
  94. if (ch_cnt) {
  95. u8 *pos = skb_put(skb, 2);
  96. *pos++ = ieee80211_frequency_to_channel(subband_start);
  97. *pos++ = ch_cnt;
  98. subband_cnt++;
  99. ch_cnt = 0;
  100. }
  101. }
  102. /* all channels in the requested range are allowed - add them here */
  103. if (ch_cnt) {
  104. u8 *pos = skb_put(skb, 2);
  105. *pos++ = ieee80211_frequency_to_channel(subband_start);
  106. *pos++ = ch_cnt;
  107. subband_cnt++;
  108. }
  109. return subband_cnt;
  110. }
  111. static void
  112. ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
  113. struct sk_buff *skb)
  114. {
  115. /*
  116. * Add possible channels for TDLS. These are channels that are allowed
  117. * to be active.
  118. */
  119. u8 subband_cnt;
  120. u8 *pos = skb_put(skb, 2);
  121. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  122. /*
  123. * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
  124. * this doesn't happen in real world scenarios.
  125. */
  126. /* 2GHz, with 5MHz spacing */
  127. subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
  128. /* 5GHz, with 20MHz spacing */
  129. subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
  130. /* length */
  131. *pos = 2 * subband_cnt;
  132. }
  133. static void ieee80211_tdls_add_oper_classes(struct ieee80211_link_data *link,
  134. struct sk_buff *skb)
  135. {
  136. u8 *pos;
  137. u8 op_class;
  138. if (!ieee80211_chandef_to_operating_class(&link->conf->chanreq.oper,
  139. &op_class))
  140. return;
  141. pos = skb_put(skb, 4);
  142. *pos++ = WLAN_EID_SUPPORTED_REGULATORY_CLASSES;
  143. *pos++ = 2; /* len */
  144. *pos++ = op_class;
  145. *pos++ = op_class; /* give current operating class as alternate too */
  146. }
  147. static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
  148. {
  149. u8 *pos = skb_put(skb, 3);
  150. *pos++ = WLAN_EID_BSS_COEX_2040;
  151. *pos++ = 1; /* len */
  152. *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
  153. }
  154. static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_link_data *link,
  155. u16 status_code)
  156. {
  157. struct ieee80211_supported_band *sband;
  158. /* The capability will be 0 when sending a failure code */
  159. if (status_code != 0)
  160. return 0;
  161. sband = ieee80211_get_link_sband(link);
  162. if (sband && sband->band == NL80211_BAND_2GHZ) {
  163. return WLAN_CAPABILITY_SHORT_SLOT_TIME |
  164. WLAN_CAPABILITY_SHORT_PREAMBLE;
  165. }
  166. return 0;
  167. }
  168. static void ieee80211_tdls_add_link_ie(struct ieee80211_link_data *link,
  169. struct sk_buff *skb, const u8 *peer,
  170. bool initiator)
  171. {
  172. struct ieee80211_sub_if_data *sdata = link->sdata;
  173. struct ieee80211_tdls_lnkie *lnkid;
  174. const u8 *init_addr, *rsp_addr;
  175. if (initiator) {
  176. init_addr = sdata->vif.addr;
  177. rsp_addr = peer;
  178. } else {
  179. init_addr = peer;
  180. rsp_addr = sdata->vif.addr;
  181. }
  182. lnkid = skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
  183. lnkid->ie_type = WLAN_EID_LINK_ID;
  184. lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
  185. memcpy(lnkid->bssid, link->u.mgd.bssid, ETH_ALEN);
  186. memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
  187. memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
  188. }
  189. static void
  190. ieee80211_tdls_add_aid(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  191. {
  192. u8 *pos = skb_put(skb, 4);
  193. *pos++ = WLAN_EID_AID;
  194. *pos++ = 2; /* len */
  195. put_unaligned_le16(sdata->vif.cfg.aid, pos);
  196. }
  197. /* translate numbering in the WMM parameter IE to the mac80211 notation */
  198. static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
  199. {
  200. switch (ac) {
  201. default:
  202. WARN_ON_ONCE(1);
  203. fallthrough;
  204. case 0:
  205. return IEEE80211_AC_BE;
  206. case 1:
  207. return IEEE80211_AC_BK;
  208. case 2:
  209. return IEEE80211_AC_VI;
  210. case 3:
  211. return IEEE80211_AC_VO;
  212. }
  213. }
  214. static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
  215. {
  216. u8 ret;
  217. ret = aifsn & 0x0f;
  218. if (acm)
  219. ret |= 0x10;
  220. ret |= (aci << 5) & 0x60;
  221. return ret;
  222. }
  223. static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
  224. {
  225. return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
  226. ((ilog2(cw_max + 1) << 0x4) & 0xf0);
  227. }
  228. static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
  229. struct sk_buff *skb)
  230. {
  231. struct ieee80211_wmm_param_ie *wmm;
  232. struct ieee80211_tx_queue_params *txq;
  233. int i;
  234. wmm = skb_put_zero(skb, sizeof(*wmm));
  235. wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
  236. wmm->len = sizeof(*wmm) - 2;
  237. wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
  238. wmm->oui[1] = 0x50;
  239. wmm->oui[2] = 0xf2;
  240. wmm->oui_type = 2; /* WME */
  241. wmm->oui_subtype = 1; /* WME param */
  242. wmm->version = 1; /* WME ver */
  243. wmm->qos_info = 0; /* U-APSD not in use */
  244. /*
  245. * Use the EDCA parameters defined for the BSS, or default if the AP
  246. * doesn't support it, as mandated by 802.11-2012 section 10.22.4
  247. */
  248. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  249. txq = &sdata->deflink.tx_conf[ieee80211_ac_from_wmm(i)];
  250. wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
  251. txq->acm, i);
  252. wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
  253. wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
  254. }
  255. }
  256. static void
  257. ieee80211_tdls_chandef_vht_upgrade(struct ieee80211_sub_if_data *sdata,
  258. struct sta_info *sta)
  259. {
  260. /* IEEE802.11ac-2013 Table E-4 */
  261. static const u16 centers_80mhz[] = { 5210, 5290, 5530, 5610, 5690, 5775 };
  262. struct cfg80211_chan_def uc = sta->tdls_chandef;
  263. enum nl80211_chan_width max_width =
  264. ieee80211_sta_cap_chan_bw(&sta->deflink);
  265. int i;
  266. /* only support upgrading non-narrow channels up to 80Mhz */
  267. if (max_width == NL80211_CHAN_WIDTH_5 ||
  268. max_width == NL80211_CHAN_WIDTH_10)
  269. return;
  270. if (max_width > NL80211_CHAN_WIDTH_80)
  271. max_width = NL80211_CHAN_WIDTH_80;
  272. if (uc.width >= max_width)
  273. return;
  274. /*
  275. * Channel usage constrains in the IEEE802.11ac-2013 specification only
  276. * allow expanding a 20MHz channel to 80MHz in a single way. In
  277. * addition, there are no 40MHz allowed channels that are not part of
  278. * the allowed 80MHz range in the 5GHz spectrum (the relevant one here).
  279. */
  280. for (i = 0; i < ARRAY_SIZE(centers_80mhz); i++)
  281. if (abs(uc.chan->center_freq - centers_80mhz[i]) <= 30) {
  282. uc.center_freq1 = centers_80mhz[i];
  283. uc.center_freq2 = 0;
  284. uc.width = NL80211_CHAN_WIDTH_80;
  285. break;
  286. }
  287. if (!uc.center_freq1)
  288. return;
  289. /* proceed to downgrade the chandef until usable or the same as AP BW */
  290. while (uc.width > max_width ||
  291. (uc.width > sta->tdls_chandef.width &&
  292. !cfg80211_reg_can_beacon_relax(sdata->local->hw.wiphy, &uc,
  293. sdata->wdev.iftype)))
  294. ieee80211_chandef_downgrade(&uc, NULL);
  295. if (!cfg80211_chandef_identical(&uc, &sta->tdls_chandef)) {
  296. tdls_dbg(sdata, "TDLS ch width upgraded %d -> %d\n",
  297. sta->tdls_chandef.width, uc.width);
  298. /*
  299. * the station is not yet authorized when BW upgrade is done,
  300. * locking is not required
  301. */
  302. sta->tdls_chandef = uc;
  303. }
  304. }
  305. static void
  306. ieee80211_tdls_add_setup_start_ies(struct ieee80211_link_data *link,
  307. struct sk_buff *skb, const u8 *peer,
  308. u8 action_code, bool initiator,
  309. const u8 *extra_ies, size_t extra_ies_len)
  310. {
  311. struct ieee80211_sub_if_data *sdata = link->sdata;
  312. struct ieee80211_supported_band *sband;
  313. struct ieee80211_local *local = sdata->local;
  314. struct ieee80211_sta_ht_cap ht_cap;
  315. struct ieee80211_sta_vht_cap vht_cap;
  316. const struct ieee80211_sta_he_cap *he_cap;
  317. const struct ieee80211_sta_eht_cap *eht_cap;
  318. struct sta_info *sta = NULL;
  319. size_t offset = 0, noffset;
  320. u8 *pos;
  321. sband = ieee80211_get_link_sband(link);
  322. if (WARN_ON_ONCE(!sband))
  323. return;
  324. ieee80211_put_srates_elem(skb, sband, 0, 0, WLAN_EID_SUPP_RATES);
  325. ieee80211_put_srates_elem(skb, sband, 0, 0, WLAN_EID_EXT_SUPP_RATES);
  326. ieee80211_tdls_add_supp_channels(sdata, skb);
  327. /* add any custom IEs that go before Extended Capabilities */
  328. if (extra_ies_len) {
  329. static const u8 before_ext_cap[] = {
  330. WLAN_EID_SUPP_RATES,
  331. WLAN_EID_COUNTRY,
  332. WLAN_EID_EXT_SUPP_RATES,
  333. WLAN_EID_SUPPORTED_CHANNELS,
  334. WLAN_EID_RSN,
  335. };
  336. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  337. before_ext_cap,
  338. ARRAY_SIZE(before_ext_cap),
  339. offset);
  340. skb_put_data(skb, extra_ies + offset, noffset - offset);
  341. offset = noffset;
  342. }
  343. ieee80211_tdls_add_ext_capab(link, skb);
  344. /* add the QoS element if we support it */
  345. if (local->hw.queues >= IEEE80211_NUM_ACS &&
  346. action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
  347. ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
  348. /* add any custom IEs that go before HT capabilities */
  349. if (extra_ies_len) {
  350. static const u8 before_ht_cap[] = {
  351. WLAN_EID_SUPP_RATES,
  352. WLAN_EID_COUNTRY,
  353. WLAN_EID_EXT_SUPP_RATES,
  354. WLAN_EID_SUPPORTED_CHANNELS,
  355. WLAN_EID_RSN,
  356. WLAN_EID_EXT_CAPABILITY,
  357. WLAN_EID_QOS_CAPA,
  358. WLAN_EID_FAST_BSS_TRANSITION,
  359. WLAN_EID_TIMEOUT_INTERVAL,
  360. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  361. };
  362. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  363. before_ht_cap,
  364. ARRAY_SIZE(before_ht_cap),
  365. offset);
  366. skb_put_data(skb, extra_ies + offset, noffset - offset);
  367. offset = noffset;
  368. }
  369. /* we should have the peer STA if we're already responding */
  370. if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
  371. sta = sta_info_get(sdata, peer);
  372. if (WARN_ON_ONCE(!sta))
  373. return;
  374. sta->tdls_chandef = link->conf->chanreq.oper;
  375. }
  376. ieee80211_tdls_add_oper_classes(link, skb);
  377. /*
  378. * with TDLS we can switch channels, and HT-caps are not necessarily
  379. * the same on all bands. The specification limits the setup to a
  380. * single HT-cap, so use the current band for now.
  381. */
  382. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  383. if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
  384. action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
  385. ht_cap.ht_supported) {
  386. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  387. /* disable SMPS in TDLS initiator */
  388. ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
  389. << IEEE80211_HT_CAP_SM_PS_SHIFT;
  390. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  391. ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  392. } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
  393. ht_cap.ht_supported && sta->sta.deflink.ht_cap.ht_supported) {
  394. /* the peer caps are already intersected with our own */
  395. memcpy(&ht_cap, &sta->sta.deflink.ht_cap, sizeof(ht_cap));
  396. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  397. ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  398. }
  399. if (ht_cap.ht_supported &&
  400. (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  401. ieee80211_tdls_add_bss_coex_ie(skb);
  402. ieee80211_tdls_add_link_ie(link, skb, peer, initiator);
  403. /* add any custom IEs that go before VHT capabilities */
  404. if (extra_ies_len) {
  405. static const u8 before_vht_cap[] = {
  406. WLAN_EID_SUPP_RATES,
  407. WLAN_EID_COUNTRY,
  408. WLAN_EID_EXT_SUPP_RATES,
  409. WLAN_EID_SUPPORTED_CHANNELS,
  410. WLAN_EID_RSN,
  411. WLAN_EID_EXT_CAPABILITY,
  412. WLAN_EID_QOS_CAPA,
  413. WLAN_EID_FAST_BSS_TRANSITION,
  414. WLAN_EID_TIMEOUT_INTERVAL,
  415. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  416. WLAN_EID_MULTI_BAND,
  417. };
  418. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  419. before_vht_cap,
  420. ARRAY_SIZE(before_vht_cap),
  421. offset);
  422. skb_put_data(skb, extra_ies + offset, noffset - offset);
  423. offset = noffset;
  424. }
  425. /* add AID if VHT, HE or EHT capabilities supported */
  426. memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
  427. he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
  428. eht_cap = ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
  429. if ((vht_cap.vht_supported || he_cap || eht_cap) &&
  430. (action_code == WLAN_TDLS_SETUP_REQUEST ||
  431. action_code == WLAN_TDLS_SETUP_RESPONSE))
  432. ieee80211_tdls_add_aid(sdata, skb);
  433. /* build the VHT-cap similarly to the HT-cap */
  434. if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
  435. action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
  436. vht_cap.vht_supported) {
  437. ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
  438. pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
  439. ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
  440. } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
  441. vht_cap.vht_supported && sta->sta.deflink.vht_cap.vht_supported) {
  442. /* the peer caps are already intersected with our own */
  443. memcpy(&vht_cap, &sta->sta.deflink.vht_cap, sizeof(vht_cap));
  444. pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
  445. ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
  446. /*
  447. * if both peers support WIDER_BW, we can expand the chandef to
  448. * a wider compatible one, up to 80MHz
  449. */
  450. if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
  451. ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
  452. }
  453. /* add any custom IEs that go before HE capabilities */
  454. if (extra_ies_len) {
  455. static const u8 before_he_cap[] = {
  456. WLAN_EID_EXTENSION,
  457. WLAN_EID_EXT_FILS_REQ_PARAMS,
  458. WLAN_EID_AP_CSN,
  459. };
  460. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  461. before_he_cap,
  462. ARRAY_SIZE(before_he_cap),
  463. offset);
  464. skb_put_data(skb, extra_ies + offset, noffset - offset);
  465. offset = noffset;
  466. }
  467. /* build the HE-cap from sband */
  468. if (action_code == WLAN_TDLS_SETUP_REQUEST ||
  469. action_code == WLAN_TDLS_SETUP_RESPONSE ||
  470. action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
  471. ieee80211_put_he_cap(skb, sdata, sband, NULL);
  472. /* Build HE 6Ghz capa IE from sband */
  473. if (sband->band == NL80211_BAND_6GHZ)
  474. ieee80211_put_he_6ghz_cap(skb, sdata, link->smps_mode);
  475. }
  476. /* add any custom IEs that go before EHT capabilities */
  477. if (extra_ies_len) {
  478. static const u8 before_he_cap[] = {
  479. WLAN_EID_EXTENSION,
  480. WLAN_EID_EXT_FILS_REQ_PARAMS,
  481. WLAN_EID_AP_CSN,
  482. };
  483. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  484. before_he_cap,
  485. ARRAY_SIZE(before_he_cap),
  486. offset);
  487. skb_put_data(skb, extra_ies + offset, noffset - offset);
  488. offset = noffset;
  489. }
  490. /* build the EHT-cap from sband */
  491. if (action_code == WLAN_TDLS_SETUP_REQUEST ||
  492. action_code == WLAN_TDLS_SETUP_RESPONSE ||
  493. action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
  494. ieee80211_put_eht_cap(skb, sdata, sband, NULL);
  495. /* add any remaining IEs */
  496. if (extra_ies_len) {
  497. noffset = extra_ies_len;
  498. skb_put_data(skb, extra_ies + offset, noffset - offset);
  499. }
  500. }
  501. static void
  502. ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_link_data *link,
  503. struct sk_buff *skb, const u8 *peer,
  504. bool initiator, const u8 *extra_ies,
  505. size_t extra_ies_len)
  506. {
  507. struct ieee80211_sub_if_data *sdata = link->sdata;
  508. struct ieee80211_local *local = sdata->local;
  509. size_t offset = 0, noffset;
  510. struct sta_info *sta, *ap_sta;
  511. struct ieee80211_supported_band *sband;
  512. u8 *pos;
  513. sband = ieee80211_get_link_sband(link);
  514. if (WARN_ON_ONCE(!sband))
  515. return;
  516. sta = sta_info_get(sdata, peer);
  517. ap_sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
  518. if (WARN_ON_ONCE(!sta || !ap_sta))
  519. return;
  520. sta->tdls_chandef = link->conf->chanreq.oper;
  521. /* add any custom IEs that go before the QoS IE */
  522. if (extra_ies_len) {
  523. static const u8 before_qos[] = {
  524. WLAN_EID_RSN,
  525. };
  526. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  527. before_qos,
  528. ARRAY_SIZE(before_qos),
  529. offset);
  530. skb_put_data(skb, extra_ies + offset, noffset - offset);
  531. offset = noffset;
  532. }
  533. /* add the QoS param IE if both the peer and we support it */
  534. if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
  535. ieee80211_tdls_add_wmm_param_ie(sdata, skb);
  536. /* add any custom IEs that go before HT operation */
  537. if (extra_ies_len) {
  538. static const u8 before_ht_op[] = {
  539. WLAN_EID_RSN,
  540. WLAN_EID_QOS_CAPA,
  541. WLAN_EID_FAST_BSS_TRANSITION,
  542. WLAN_EID_TIMEOUT_INTERVAL,
  543. };
  544. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  545. before_ht_op,
  546. ARRAY_SIZE(before_ht_op),
  547. offset);
  548. skb_put_data(skb, extra_ies + offset, noffset - offset);
  549. offset = noffset;
  550. }
  551. /*
  552. * if HT support is only added in TDLS, we need an HT-operation IE.
  553. * add the IE as required by IEEE802.11-2012 9.23.3.2.
  554. */
  555. if (!ap_sta->sta.deflink.ht_cap.ht_supported && sta->sta.deflink.ht_cap.ht_supported) {
  556. u16 prot = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED |
  557. IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
  558. IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
  559. pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
  560. ieee80211_ie_build_ht_oper(pos, &sta->sta.deflink.ht_cap,
  561. &link->conf->chanreq.oper, prot,
  562. true);
  563. }
  564. ieee80211_tdls_add_link_ie(link, skb, peer, initiator);
  565. /* only include VHT-operation if not on the 2.4GHz band */
  566. if (sband->band != NL80211_BAND_2GHZ &&
  567. sta->sta.deflink.vht_cap.vht_supported) {
  568. /*
  569. * if both peers support WIDER_BW, we can expand the chandef to
  570. * a wider compatible one, up to 80MHz
  571. */
  572. if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
  573. ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
  574. pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
  575. ieee80211_ie_build_vht_oper(pos, &sta->sta.deflink.vht_cap,
  576. &sta->tdls_chandef);
  577. }
  578. /* add any remaining IEs */
  579. if (extra_ies_len) {
  580. noffset = extra_ies_len;
  581. skb_put_data(skb, extra_ies + offset, noffset - offset);
  582. }
  583. }
  584. static void
  585. ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_link_data *link,
  586. struct sk_buff *skb, const u8 *peer,
  587. bool initiator, const u8 *extra_ies,
  588. size_t extra_ies_len, u8 oper_class,
  589. struct cfg80211_chan_def *chandef)
  590. {
  591. struct ieee80211_tdls_data *tf;
  592. size_t offset = 0, noffset;
  593. if (WARN_ON_ONCE(!chandef))
  594. return;
  595. tf = (void *)skb->data;
  596. tf->u.chan_switch_req.target_channel =
  597. ieee80211_frequency_to_channel(chandef->chan->center_freq);
  598. tf->u.chan_switch_req.oper_class = oper_class;
  599. if (extra_ies_len) {
  600. static const u8 before_lnkie[] = {
  601. WLAN_EID_SECONDARY_CHANNEL_OFFSET,
  602. };
  603. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  604. before_lnkie,
  605. ARRAY_SIZE(before_lnkie),
  606. offset);
  607. skb_put_data(skb, extra_ies + offset, noffset - offset);
  608. offset = noffset;
  609. }
  610. ieee80211_tdls_add_link_ie(link, skb, peer, initiator);
  611. /* add any remaining IEs */
  612. if (extra_ies_len) {
  613. noffset = extra_ies_len;
  614. skb_put_data(skb, extra_ies + offset, noffset - offset);
  615. }
  616. }
  617. static void
  618. ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_link_data *link,
  619. struct sk_buff *skb, const u8 *peer,
  620. u16 status_code, bool initiator,
  621. const u8 *extra_ies,
  622. size_t extra_ies_len)
  623. {
  624. if (status_code == 0)
  625. ieee80211_tdls_add_link_ie(link, skb, peer, initiator);
  626. if (extra_ies_len)
  627. skb_put_data(skb, extra_ies, extra_ies_len);
  628. }
  629. static void ieee80211_tdls_add_ies(struct ieee80211_link_data *link,
  630. struct sk_buff *skb, const u8 *peer,
  631. u8 action_code, u16 status_code,
  632. bool initiator, const u8 *extra_ies,
  633. size_t extra_ies_len, u8 oper_class,
  634. struct cfg80211_chan_def *chandef)
  635. {
  636. switch (action_code) {
  637. case WLAN_TDLS_SETUP_REQUEST:
  638. case WLAN_TDLS_SETUP_RESPONSE:
  639. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  640. if (status_code == 0)
  641. ieee80211_tdls_add_setup_start_ies(link,
  642. skb, peer,
  643. action_code,
  644. initiator,
  645. extra_ies,
  646. extra_ies_len);
  647. break;
  648. case WLAN_TDLS_SETUP_CONFIRM:
  649. if (status_code == 0)
  650. ieee80211_tdls_add_setup_cfm_ies(link, skb, peer,
  651. initiator, extra_ies,
  652. extra_ies_len);
  653. break;
  654. case WLAN_TDLS_TEARDOWN:
  655. case WLAN_TDLS_DISCOVERY_REQUEST:
  656. if (extra_ies_len)
  657. skb_put_data(skb, extra_ies, extra_ies_len);
  658. if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
  659. ieee80211_tdls_add_link_ie(link, skb,
  660. peer, initiator);
  661. break;
  662. case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  663. ieee80211_tdls_add_chan_switch_req_ies(link, skb, peer,
  664. initiator, extra_ies,
  665. extra_ies_len,
  666. oper_class, chandef);
  667. break;
  668. case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  669. ieee80211_tdls_add_chan_switch_resp_ies(link, skb, peer,
  670. status_code,
  671. initiator, extra_ies,
  672. extra_ies_len);
  673. break;
  674. }
  675. }
  676. static int
  677. ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
  678. struct ieee80211_link_data *link,
  679. const u8 *peer, u8 action_code, u8 dialog_token,
  680. u16 status_code, struct sk_buff *skb)
  681. {
  682. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  683. struct ieee80211_tdls_data *tf;
  684. tf = skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
  685. memcpy(tf->da, peer, ETH_ALEN);
  686. memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
  687. tf->ether_type = cpu_to_be16(ETH_P_TDLS);
  688. tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
  689. /* network header is after the ethernet header */
  690. skb_set_network_header(skb, ETH_HLEN);
  691. switch (action_code) {
  692. case WLAN_TDLS_SETUP_REQUEST:
  693. tf->category = WLAN_CATEGORY_TDLS;
  694. tf->action_code = WLAN_TDLS_SETUP_REQUEST;
  695. skb_put(skb, sizeof(tf->u.setup_req));
  696. tf->u.setup_req.dialog_token = dialog_token;
  697. tf->u.setup_req.capability =
  698. cpu_to_le16(ieee80211_get_tdls_sta_capab(link,
  699. status_code));
  700. break;
  701. case WLAN_TDLS_SETUP_RESPONSE:
  702. tf->category = WLAN_CATEGORY_TDLS;
  703. tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
  704. skb_put(skb, sizeof(tf->u.setup_resp));
  705. tf->u.setup_resp.status_code = cpu_to_le16(status_code);
  706. tf->u.setup_resp.dialog_token = dialog_token;
  707. tf->u.setup_resp.capability =
  708. cpu_to_le16(ieee80211_get_tdls_sta_capab(link,
  709. status_code));
  710. break;
  711. case WLAN_TDLS_SETUP_CONFIRM:
  712. tf->category = WLAN_CATEGORY_TDLS;
  713. tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
  714. skb_put(skb, sizeof(tf->u.setup_cfm));
  715. tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
  716. tf->u.setup_cfm.dialog_token = dialog_token;
  717. break;
  718. case WLAN_TDLS_TEARDOWN:
  719. tf->category = WLAN_CATEGORY_TDLS;
  720. tf->action_code = WLAN_TDLS_TEARDOWN;
  721. skb_put(skb, sizeof(tf->u.teardown));
  722. tf->u.teardown.reason_code = cpu_to_le16(status_code);
  723. break;
  724. case WLAN_TDLS_DISCOVERY_REQUEST:
  725. tf->category = WLAN_CATEGORY_TDLS;
  726. tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
  727. skb_put(skb, sizeof(tf->u.discover_req));
  728. tf->u.discover_req.dialog_token = dialog_token;
  729. break;
  730. case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  731. tf->category = WLAN_CATEGORY_TDLS;
  732. tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
  733. skb_put(skb, sizeof(tf->u.chan_switch_req));
  734. break;
  735. case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  736. tf->category = WLAN_CATEGORY_TDLS;
  737. tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
  738. skb_put(skb, sizeof(tf->u.chan_switch_resp));
  739. tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
  740. break;
  741. default:
  742. return -EINVAL;
  743. }
  744. return 0;
  745. }
  746. static int
  747. ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
  748. const u8 *peer, struct ieee80211_link_data *link,
  749. u8 action_code, u8 dialog_token,
  750. u16 status_code, struct sk_buff *skb)
  751. {
  752. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  753. struct ieee80211_mgmt *mgmt;
  754. mgmt = skb_put_zero(skb, 24);
  755. memcpy(mgmt->da, peer, ETH_ALEN);
  756. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  757. memcpy(mgmt->bssid, link->u.mgd.bssid, ETH_ALEN);
  758. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  759. IEEE80211_STYPE_ACTION);
  760. switch (action_code) {
  761. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  762. skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
  763. mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
  764. mgmt->u.action.u.tdls_discover_resp.action_code =
  765. WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
  766. mgmt->u.action.u.tdls_discover_resp.dialog_token =
  767. dialog_token;
  768. mgmt->u.action.u.tdls_discover_resp.capability =
  769. cpu_to_le16(ieee80211_get_tdls_sta_capab(link,
  770. status_code));
  771. break;
  772. default:
  773. return -EINVAL;
  774. }
  775. return 0;
  776. }
  777. static struct sk_buff *
  778. ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
  779. const u8 *peer, int link_id,
  780. u8 action_code, u8 dialog_token,
  781. u16 status_code, bool initiator,
  782. const u8 *extra_ies, size_t extra_ies_len,
  783. u8 oper_class,
  784. struct cfg80211_chan_def *chandef)
  785. {
  786. struct ieee80211_local *local = sdata->local;
  787. struct sk_buff *skb;
  788. int ret;
  789. struct ieee80211_link_data *link;
  790. link_id = link_id >= 0 ? link_id : 0;
  791. rcu_read_lock();
  792. link = rcu_dereference(sdata->link[link_id]);
  793. if (WARN_ON(!link))
  794. goto unlock;
  795. skb = netdev_alloc_skb(sdata->dev,
  796. local->hw.extra_tx_headroom +
  797. max(sizeof(struct ieee80211_mgmt),
  798. sizeof(struct ieee80211_tdls_data)) +
  799. 50 + /* supported rates */
  800. 10 + /* ext capab */
  801. 26 + /* max(WMM-info, WMM-param) */
  802. 2 + max(sizeof(struct ieee80211_ht_cap),
  803. sizeof(struct ieee80211_ht_operation)) +
  804. 2 + max(sizeof(struct ieee80211_vht_cap),
  805. sizeof(struct ieee80211_vht_operation)) +
  806. 2 + 1 + sizeof(struct ieee80211_he_cap_elem) +
  807. sizeof(struct ieee80211_he_mcs_nss_supp) +
  808. IEEE80211_HE_PPE_THRES_MAX_LEN +
  809. 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
  810. 2 + 1 + sizeof(struct ieee80211_eht_cap_elem) +
  811. sizeof(struct ieee80211_eht_mcs_nss_supp) +
  812. IEEE80211_EHT_PPE_THRES_MAX_LEN +
  813. 50 + /* supported channels */
  814. 3 + /* 40/20 BSS coex */
  815. 4 + /* AID */
  816. 4 + /* oper classes */
  817. extra_ies_len +
  818. sizeof(struct ieee80211_tdls_lnkie));
  819. if (!skb)
  820. goto unlock;
  821. skb_reserve(skb, local->hw.extra_tx_headroom);
  822. switch (action_code) {
  823. case WLAN_TDLS_SETUP_REQUEST:
  824. case WLAN_TDLS_SETUP_RESPONSE:
  825. case WLAN_TDLS_SETUP_CONFIRM:
  826. case WLAN_TDLS_TEARDOWN:
  827. case WLAN_TDLS_DISCOVERY_REQUEST:
  828. case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  829. case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  830. ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
  831. sdata->dev, link, peer,
  832. action_code, dialog_token,
  833. status_code, skb);
  834. break;
  835. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  836. ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
  837. peer, link, action_code,
  838. dialog_token, status_code,
  839. skb);
  840. break;
  841. default:
  842. ret = -EOPNOTSUPP;
  843. break;
  844. }
  845. if (ret < 0)
  846. goto fail;
  847. ieee80211_tdls_add_ies(link, skb, peer, action_code, status_code,
  848. initiator, extra_ies, extra_ies_len, oper_class,
  849. chandef);
  850. rcu_read_unlock();
  851. return skb;
  852. fail:
  853. dev_kfree_skb(skb);
  854. unlock:
  855. rcu_read_unlock();
  856. return NULL;
  857. }
  858. static int
  859. ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
  860. const u8 *peer, int link_id,
  861. u8 action_code, u8 dialog_token,
  862. u16 status_code, u32 peer_capability,
  863. bool initiator, const u8 *extra_ies,
  864. size_t extra_ies_len, u8 oper_class,
  865. struct cfg80211_chan_def *chandef)
  866. {
  867. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  868. struct sk_buff *skb = NULL;
  869. struct sta_info *sta;
  870. u32 flags = 0;
  871. int ret = 0;
  872. rcu_read_lock();
  873. sta = sta_info_get(sdata, peer);
  874. /* infer the initiator if we can, to support old userspace */
  875. switch (action_code) {
  876. case WLAN_TDLS_SETUP_REQUEST:
  877. if (sta) {
  878. set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
  879. sta->sta.tdls_initiator = false;
  880. }
  881. fallthrough;
  882. case WLAN_TDLS_SETUP_CONFIRM:
  883. case WLAN_TDLS_DISCOVERY_REQUEST:
  884. initiator = true;
  885. break;
  886. case WLAN_TDLS_SETUP_RESPONSE:
  887. /*
  888. * In some testing scenarios, we send a request and response.
  889. * Make the last packet sent take effect for the initiator
  890. * value.
  891. */
  892. if (sta) {
  893. clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
  894. sta->sta.tdls_initiator = true;
  895. }
  896. fallthrough;
  897. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  898. initiator = false;
  899. break;
  900. case WLAN_TDLS_TEARDOWN:
  901. case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  902. case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  903. /* any value is ok */
  904. break;
  905. default:
  906. ret = -EOPNOTSUPP;
  907. break;
  908. }
  909. if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
  910. initiator = true;
  911. rcu_read_unlock();
  912. if (ret < 0)
  913. goto fail;
  914. skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer,
  915. link_id, action_code,
  916. dialog_token, status_code,
  917. initiator, extra_ies,
  918. extra_ies_len, oper_class,
  919. chandef);
  920. if (!skb) {
  921. ret = -EINVAL;
  922. goto fail;
  923. }
  924. if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
  925. ieee80211_tx_skb_tid(sdata, skb, 7, link_id);
  926. return 0;
  927. }
  928. /*
  929. * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
  930. * we should default to AC_VI.
  931. */
  932. switch (action_code) {
  933. case WLAN_TDLS_SETUP_REQUEST:
  934. case WLAN_TDLS_SETUP_RESPONSE:
  935. skb->priority = 256 + 2;
  936. break;
  937. default:
  938. skb->priority = 256 + 5;
  939. break;
  940. }
  941. /*
  942. * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
  943. * Later, if no ACK is returned from peer, we will re-send the teardown
  944. * packet through the AP.
  945. */
  946. if ((action_code == WLAN_TDLS_TEARDOWN) &&
  947. ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
  948. bool try_resend; /* Should we keep skb for possible resend */
  949. /* If not sending directly to peer - no point in keeping skb */
  950. rcu_read_lock();
  951. sta = sta_info_get(sdata, peer);
  952. try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
  953. rcu_read_unlock();
  954. spin_lock_bh(&sdata->u.mgd.teardown_lock);
  955. if (try_resend && !sdata->u.mgd.teardown_skb) {
  956. /* Mark it as requiring TX status callback */
  957. flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
  958. IEEE80211_TX_INTFL_MLME_CONN_TX;
  959. /*
  960. * skb is copied since mac80211 will later set
  961. * properties that might not be the same as the AP,
  962. * such as encryption, QoS, addresses, etc.
  963. *
  964. * No problem if skb_copy() fails, so no need to check.
  965. */
  966. sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
  967. sdata->u.mgd.orig_teardown_skb = skb;
  968. }
  969. spin_unlock_bh(&sdata->u.mgd.teardown_lock);
  970. }
  971. /* disable bottom halves when entering the Tx path */
  972. local_bh_disable();
  973. __ieee80211_subif_start_xmit(skb, dev, flags,
  974. IEEE80211_TX_CTRL_MLO_LINK_UNSPEC, NULL);
  975. local_bh_enable();
  976. return ret;
  977. fail:
  978. dev_kfree_skb(skb);
  979. return ret;
  980. }
  981. static int
  982. ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
  983. const u8 *peer, int link_id,
  984. u8 action_code, u8 dialog_token,
  985. u16 status_code, u32 peer_capability, bool initiator,
  986. const u8 *extra_ies, size_t extra_ies_len)
  987. {
  988. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  989. struct ieee80211_local *local = sdata->local;
  990. enum ieee80211_smps_mode smps_mode =
  991. sdata->deflink.u.mgd.driver_smps_mode;
  992. int ret;
  993. /* don't support setup with forced SMPS mode that's not off */
  994. if (smps_mode != IEEE80211_SMPS_AUTOMATIC &&
  995. smps_mode != IEEE80211_SMPS_OFF) {
  996. tdls_dbg(sdata, "Aborting TDLS setup due to SMPS mode %d\n",
  997. smps_mode);
  998. return -EOPNOTSUPP;
  999. }
  1000. lockdep_assert_wiphy(local->hw.wiphy);
  1001. /* we don't support concurrent TDLS peer setups */
  1002. if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
  1003. !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
  1004. ret = -EBUSY;
  1005. goto out_unlock;
  1006. }
  1007. /*
  1008. * make sure we have a STA representing the peer so we drop or buffer
  1009. * non-TDLS-setup frames to the peer. We can't send other packets
  1010. * during setup through the AP path.
  1011. * Allow error packets to be sent - sometimes we don't even add a STA
  1012. * before failing the setup.
  1013. */
  1014. if (status_code == 0) {
  1015. rcu_read_lock();
  1016. if (!sta_info_get(sdata, peer)) {
  1017. rcu_read_unlock();
  1018. ret = -ENOLINK;
  1019. goto out_unlock;
  1020. }
  1021. rcu_read_unlock();
  1022. }
  1023. ieee80211_flush_queues(local, sdata, false);
  1024. memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
  1025. /* we cannot take the mutex while preparing the setup packet */
  1026. ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
  1027. link_id, action_code,
  1028. dialog_token, status_code,
  1029. peer_capability, initiator,
  1030. extra_ies, extra_ies_len, 0,
  1031. NULL);
  1032. if (ret < 0) {
  1033. eth_zero_addr(sdata->u.mgd.tdls_peer);
  1034. return ret;
  1035. }
  1036. wiphy_delayed_work_queue(sdata->local->hw.wiphy,
  1037. &sdata->u.mgd.tdls_peer_del_work,
  1038. TDLS_PEER_SETUP_TIMEOUT);
  1039. return 0;
  1040. out_unlock:
  1041. return ret;
  1042. }
  1043. static int
  1044. ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
  1045. const u8 *peer, int link_id,
  1046. u8 action_code, u8 dialog_token,
  1047. u16 status_code, u32 peer_capability,
  1048. bool initiator, const u8 *extra_ies,
  1049. size_t extra_ies_len)
  1050. {
  1051. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1052. struct ieee80211_local *local = sdata->local;
  1053. struct sta_info *sta;
  1054. int ret;
  1055. /*
  1056. * No packets can be transmitted to the peer via the AP during setup -
  1057. * the STA is set as a TDLS peer, but is not authorized.
  1058. * During teardown, we prevent direct transmissions by stopping the
  1059. * queues and flushing all direct packets.
  1060. */
  1061. ieee80211_stop_vif_queues(local, sdata,
  1062. IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
  1063. ieee80211_flush_queues(local, sdata, false);
  1064. ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
  1065. link_id, action_code,
  1066. dialog_token, status_code,
  1067. peer_capability, initiator,
  1068. extra_ies, extra_ies_len, 0,
  1069. NULL);
  1070. if (ret < 0)
  1071. sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
  1072. ret);
  1073. /*
  1074. * Remove the STA AUTH flag to force further traffic through the AP. If
  1075. * the STA was unreachable, it was already removed.
  1076. */
  1077. rcu_read_lock();
  1078. sta = sta_info_get(sdata, peer);
  1079. if (sta)
  1080. clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
  1081. rcu_read_unlock();
  1082. ieee80211_wake_vif_queues(local, sdata,
  1083. IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
  1084. return 0;
  1085. }
  1086. int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
  1087. const u8 *peer, int link_id,
  1088. u8 action_code, u8 dialog_token, u16 status_code,
  1089. u32 peer_capability, bool initiator,
  1090. const u8 *extra_ies, size_t extra_ies_len)
  1091. {
  1092. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1093. int ret;
  1094. if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
  1095. return -EOPNOTSUPP;
  1096. /* make sure we are in managed mode, and associated */
  1097. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  1098. !sdata->u.mgd.associated)
  1099. return -EINVAL;
  1100. switch (action_code) {
  1101. case WLAN_TDLS_SETUP_REQUEST:
  1102. case WLAN_TDLS_SETUP_RESPONSE:
  1103. ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer,
  1104. link_id, action_code,
  1105. dialog_token, status_code,
  1106. peer_capability, initiator,
  1107. extra_ies, extra_ies_len);
  1108. break;
  1109. case WLAN_TDLS_TEARDOWN:
  1110. ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer, link_id,
  1111. action_code, dialog_token,
  1112. status_code,
  1113. peer_capability, initiator,
  1114. extra_ies, extra_ies_len);
  1115. break;
  1116. case WLAN_TDLS_DISCOVERY_REQUEST:
  1117. /*
  1118. * Protect the discovery so we can hear the TDLS discovery
  1119. * response frame. It is transmitted directly and not buffered
  1120. * by the AP.
  1121. */
  1122. drv_mgd_protect_tdls_discover(sdata->local, sdata, link_id);
  1123. fallthrough;
  1124. case WLAN_TDLS_SETUP_CONFIRM:
  1125. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  1126. /* no special handling */
  1127. ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
  1128. link_id, action_code,
  1129. dialog_token,
  1130. status_code,
  1131. peer_capability,
  1132. initiator, extra_ies,
  1133. extra_ies_len, 0, NULL);
  1134. break;
  1135. default:
  1136. ret = -EOPNOTSUPP;
  1137. break;
  1138. }
  1139. tdls_dbg(sdata, "TDLS mgmt action %d peer %pM link_id %d status %d\n",
  1140. action_code, peer, link_id, ret);
  1141. return ret;
  1142. }
  1143. static void iee80211_tdls_recalc_chanctx(struct ieee80211_sub_if_data *sdata,
  1144. struct sta_info *sta)
  1145. {
  1146. struct ieee80211_local *local = sdata->local;
  1147. struct ieee80211_chanctx_conf *conf;
  1148. struct ieee80211_chanctx *ctx;
  1149. enum nl80211_chan_width width;
  1150. struct ieee80211_supported_band *sband;
  1151. lockdep_assert_wiphy(local->hw.wiphy);
  1152. conf = rcu_dereference_protected(sdata->vif.bss_conf.chanctx_conf,
  1153. lockdep_is_held(&local->hw.wiphy->mtx));
  1154. if (conf) {
  1155. width = conf->def.width;
  1156. sband = local->hw.wiphy->bands[conf->def.chan->band];
  1157. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1158. ieee80211_recalc_chanctx_chantype(local, ctx);
  1159. /* if width changed and a peer is given, update its BW */
  1160. if (width != conf->def.width && sta &&
  1161. test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW)) {
  1162. enum ieee80211_sta_rx_bandwidth bw;
  1163. bw = ieee80211_chan_width_to_rx_bw(conf->def.width);
  1164. bw = min(bw, ieee80211_sta_cap_rx_bw(&sta->deflink));
  1165. if (bw != sta->sta.deflink.bandwidth) {
  1166. sta->sta.deflink.bandwidth = bw;
  1167. rate_control_rate_update(local, sband,
  1168. &sta->deflink,
  1169. IEEE80211_RC_BW_CHANGED);
  1170. /*
  1171. * if a TDLS peer BW was updated, we need to
  1172. * recalc the chandef width again, to get the
  1173. * correct chanctx min_def
  1174. */
  1175. ieee80211_recalc_chanctx_chantype(local, ctx);
  1176. }
  1177. }
  1178. }
  1179. }
  1180. static int iee80211_tdls_have_ht_peers(struct ieee80211_sub_if_data *sdata)
  1181. {
  1182. struct sta_info *sta;
  1183. bool result = false;
  1184. rcu_read_lock();
  1185. list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
  1186. if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
  1187. !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
  1188. !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH) ||
  1189. !sta->sta.deflink.ht_cap.ht_supported)
  1190. continue;
  1191. result = true;
  1192. break;
  1193. }
  1194. rcu_read_unlock();
  1195. return result;
  1196. }
  1197. static void
  1198. iee80211_tdls_recalc_ht_protection(struct ieee80211_sub_if_data *sdata,
  1199. struct sta_info *sta)
  1200. {
  1201. bool tdls_ht;
  1202. u16 protection = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED |
  1203. IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
  1204. IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
  1205. u16 opmode;
  1206. /* Nothing to do if the BSS connection uses (at least) HT */
  1207. if (sdata->deflink.u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT)
  1208. return;
  1209. tdls_ht = (sta && sta->sta.deflink.ht_cap.ht_supported) ||
  1210. iee80211_tdls_have_ht_peers(sdata);
  1211. opmode = sdata->vif.bss_conf.ht_operation_mode;
  1212. if (tdls_ht)
  1213. opmode |= protection;
  1214. else
  1215. opmode &= ~protection;
  1216. if (opmode == sdata->vif.bss_conf.ht_operation_mode)
  1217. return;
  1218. sdata->vif.bss_conf.ht_operation_mode = opmode;
  1219. ieee80211_link_info_change_notify(sdata, &sdata->deflink,
  1220. BSS_CHANGED_HT);
  1221. }
  1222. int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
  1223. const u8 *peer, enum nl80211_tdls_operation oper)
  1224. {
  1225. struct sta_info *sta;
  1226. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1227. struct ieee80211_local *local = sdata->local;
  1228. int ret;
  1229. lockdep_assert_wiphy(local->hw.wiphy);
  1230. if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
  1231. return -EOPNOTSUPP;
  1232. if (sdata->vif.type != NL80211_IFTYPE_STATION || !sdata->vif.cfg.assoc)
  1233. return -EINVAL;
  1234. switch (oper) {
  1235. case NL80211_TDLS_ENABLE_LINK:
  1236. case NL80211_TDLS_DISABLE_LINK:
  1237. break;
  1238. case NL80211_TDLS_TEARDOWN:
  1239. case NL80211_TDLS_SETUP:
  1240. case NL80211_TDLS_DISCOVERY_REQ:
  1241. /* We don't support in-driver setup/teardown/discovery */
  1242. return -EOPNOTSUPP;
  1243. }
  1244. /* protect possible bss_conf changes and avoid concurrency in
  1245. * ieee80211_bss_info_change_notify()
  1246. */
  1247. tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
  1248. switch (oper) {
  1249. case NL80211_TDLS_ENABLE_LINK:
  1250. if (sdata->vif.bss_conf.csa_active) {
  1251. tdls_dbg(sdata, "TDLS: disallow link during CSA\n");
  1252. return -EBUSY;
  1253. }
  1254. sta = sta_info_get(sdata, peer);
  1255. if (!sta || !sta->sta.tdls)
  1256. return -ENOLINK;
  1257. iee80211_tdls_recalc_chanctx(sdata, sta);
  1258. iee80211_tdls_recalc_ht_protection(sdata, sta);
  1259. set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
  1260. WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
  1261. !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
  1262. break;
  1263. case NL80211_TDLS_DISABLE_LINK:
  1264. /*
  1265. * The teardown message in ieee80211_tdls_mgmt_teardown() was
  1266. * created while the queues were stopped, so it might still be
  1267. * pending. Before flushing the queues we need to be sure the
  1268. * message is handled by the tasklet handling pending messages,
  1269. * otherwise we might start destroying the station before
  1270. * sending the teardown packet.
  1271. * Note that this only forces the tasklet to flush pendings -
  1272. * not to stop the tasklet from rescheduling itself.
  1273. */
  1274. tasklet_kill(&local->tx_pending_tasklet);
  1275. /* flush a potentially queued teardown packet */
  1276. ieee80211_flush_queues(local, sdata, false);
  1277. ret = sta_info_destroy_addr(sdata, peer);
  1278. iee80211_tdls_recalc_ht_protection(sdata, NULL);
  1279. iee80211_tdls_recalc_chanctx(sdata, NULL);
  1280. if (ret)
  1281. return ret;
  1282. break;
  1283. default:
  1284. return -EOPNOTSUPP;
  1285. }
  1286. if (ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
  1287. wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
  1288. &sdata->u.mgd.tdls_peer_del_work);
  1289. eth_zero_addr(sdata->u.mgd.tdls_peer);
  1290. }
  1291. wiphy_work_queue(sdata->local->hw.wiphy,
  1292. &sdata->deflink.u.mgd.request_smps_work);
  1293. return 0;
  1294. }
  1295. void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
  1296. enum nl80211_tdls_operation oper,
  1297. u16 reason_code, gfp_t gfp)
  1298. {
  1299. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1300. if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc) {
  1301. sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
  1302. oper);
  1303. return;
  1304. }
  1305. cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
  1306. }
  1307. EXPORT_SYMBOL(ieee80211_tdls_oper_request);
  1308. static void
  1309. iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
  1310. {
  1311. struct ieee80211_ch_switch_timing *ch_sw;
  1312. *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
  1313. *buf++ = sizeof(struct ieee80211_ch_switch_timing);
  1314. ch_sw = (void *)buf;
  1315. ch_sw->switch_time = cpu_to_le16(switch_time);
  1316. ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
  1317. }
  1318. /* find switch timing IE in SKB ready for Tx */
  1319. static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
  1320. {
  1321. struct ieee80211_tdls_data *tf;
  1322. const u8 *ie_start;
  1323. /*
  1324. * Get the offset for the new location of the switch timing IE.
  1325. * The SKB network header will now point to the "payload_type"
  1326. * element of the TDLS data frame struct.
  1327. */
  1328. tf = container_of(skb->data + skb_network_offset(skb),
  1329. struct ieee80211_tdls_data, payload_type);
  1330. ie_start = tf->u.chan_switch_req.variable;
  1331. return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
  1332. skb->len - (ie_start - skb->data));
  1333. }
  1334. static struct sk_buff *
  1335. ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
  1336. struct cfg80211_chan_def *chandef,
  1337. u32 *ch_sw_tm_ie_offset)
  1338. {
  1339. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1340. u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
  1341. 2 + sizeof(struct ieee80211_ch_switch_timing)];
  1342. int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
  1343. u8 *pos = extra_ies;
  1344. struct sk_buff *skb;
  1345. int link_id = sta->sta.valid_links ? ffs(sta->sta.valid_links) - 1 : 0;
  1346. /*
  1347. * if chandef points to a wide channel add a Secondary-Channel
  1348. * Offset information element
  1349. */
  1350. if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1351. struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
  1352. bool ht40plus;
  1353. *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
  1354. *pos++ = sizeof(*sec_chan_ie);
  1355. sec_chan_ie = (void *)pos;
  1356. ht40plus = cfg80211_get_chandef_type(chandef) ==
  1357. NL80211_CHAN_HT40PLUS;
  1358. sec_chan_ie->sec_chan_offs = ht40plus ?
  1359. IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
  1360. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1361. pos += sizeof(*sec_chan_ie);
  1362. extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
  1363. }
  1364. /* just set the values to 0, this is a template */
  1365. iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
  1366. skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
  1367. link_id,
  1368. WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
  1369. 0, 0, !sta->sta.tdls_initiator,
  1370. extra_ies, extra_ies_len,
  1371. oper_class, chandef);
  1372. if (!skb)
  1373. return NULL;
  1374. skb = ieee80211_build_data_template(sdata, skb, 0);
  1375. if (IS_ERR(skb)) {
  1376. tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
  1377. return NULL;
  1378. }
  1379. if (ch_sw_tm_ie_offset) {
  1380. const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
  1381. if (!tm_ie) {
  1382. tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
  1383. dev_kfree_skb_any(skb);
  1384. return NULL;
  1385. }
  1386. *ch_sw_tm_ie_offset = tm_ie - skb->data;
  1387. }
  1388. tdls_dbg(sdata,
  1389. "TDLS channel switch request template for %pM ch %d width %d\n",
  1390. sta->sta.addr, chandef->chan->center_freq, chandef->width);
  1391. return skb;
  1392. }
  1393. int
  1394. ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
  1395. const u8 *addr, u8 oper_class,
  1396. struct cfg80211_chan_def *chandef)
  1397. {
  1398. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1399. struct ieee80211_local *local = sdata->local;
  1400. struct sta_info *sta;
  1401. struct sk_buff *skb = NULL;
  1402. u32 ch_sw_tm_ie;
  1403. int ret;
  1404. lockdep_assert_wiphy(local->hw.wiphy);
  1405. if (chandef->chan->freq_offset)
  1406. /* this may work, but is untested */
  1407. return -EOPNOTSUPP;
  1408. sta = sta_info_get(sdata, addr);
  1409. if (!sta) {
  1410. tdls_dbg(sdata,
  1411. "Invalid TDLS peer %pM for channel switch request\n",
  1412. addr);
  1413. ret = -ENOENT;
  1414. goto out;
  1415. }
  1416. if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
  1417. tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
  1418. addr);
  1419. ret = -EOPNOTSUPP;
  1420. goto out;
  1421. }
  1422. skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
  1423. &ch_sw_tm_ie);
  1424. if (!skb) {
  1425. ret = -ENOENT;
  1426. goto out;
  1427. }
  1428. ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
  1429. chandef, skb, ch_sw_tm_ie);
  1430. if (!ret)
  1431. set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
  1432. out:
  1433. dev_kfree_skb_any(skb);
  1434. return ret;
  1435. }
  1436. void
  1437. ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
  1438. struct net_device *dev,
  1439. const u8 *addr)
  1440. {
  1441. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1442. struct ieee80211_local *local = sdata->local;
  1443. struct sta_info *sta;
  1444. lockdep_assert_wiphy(local->hw.wiphy);
  1445. sta = sta_info_get(sdata, addr);
  1446. if (!sta) {
  1447. tdls_dbg(sdata,
  1448. "Invalid TDLS peer %pM for channel switch cancel\n",
  1449. addr);
  1450. return;
  1451. }
  1452. if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
  1453. tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
  1454. addr);
  1455. return;
  1456. }
  1457. drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
  1458. clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
  1459. }
  1460. static struct sk_buff *
  1461. ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
  1462. u32 *ch_sw_tm_ie_offset)
  1463. {
  1464. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1465. struct sk_buff *skb;
  1466. u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
  1467. int link_id = sta->sta.valid_links ? ffs(sta->sta.valid_links) - 1 : 0;
  1468. /* initial timing are always zero in the template */
  1469. iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
  1470. skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
  1471. link_id,
  1472. WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
  1473. 0, 0, !sta->sta.tdls_initiator,
  1474. extra_ies, sizeof(extra_ies), 0, NULL);
  1475. if (!skb)
  1476. return NULL;
  1477. skb = ieee80211_build_data_template(sdata, skb, 0);
  1478. if (IS_ERR(skb)) {
  1479. tdls_dbg(sdata,
  1480. "Failed building TDLS channel switch resp frame\n");
  1481. return NULL;
  1482. }
  1483. if (ch_sw_tm_ie_offset) {
  1484. const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
  1485. if (!tm_ie) {
  1486. tdls_dbg(sdata,
  1487. "No switch timing IE in TDLS switch resp\n");
  1488. dev_kfree_skb_any(skb);
  1489. return NULL;
  1490. }
  1491. *ch_sw_tm_ie_offset = tm_ie - skb->data;
  1492. }
  1493. tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
  1494. sta->sta.addr);
  1495. return skb;
  1496. }
  1497. static int
  1498. ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
  1499. struct sk_buff *skb)
  1500. {
  1501. struct ieee80211_local *local = sdata->local;
  1502. struct ieee802_11_elems *elems = NULL;
  1503. struct sta_info *sta;
  1504. struct ieee80211_tdls_data *tf = (void *)skb->data;
  1505. bool local_initiator;
  1506. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  1507. int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
  1508. struct ieee80211_tdls_ch_sw_params params = {};
  1509. int ret;
  1510. lockdep_assert_wiphy(local->hw.wiphy);
  1511. params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
  1512. params.timestamp = rx_status->device_timestamp;
  1513. if (skb->len < baselen) {
  1514. tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
  1515. skb->len);
  1516. return -EINVAL;
  1517. }
  1518. sta = sta_info_get(sdata, tf->sa);
  1519. if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
  1520. tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
  1521. tf->sa);
  1522. ret = -EINVAL;
  1523. goto out;
  1524. }
  1525. params.sta = &sta->sta;
  1526. params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
  1527. if (params.status != 0) {
  1528. ret = 0;
  1529. goto call_drv;
  1530. }
  1531. elems = ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
  1532. skb->len - baselen,
  1533. IEEE80211_FTYPE_MGMT |
  1534. IEEE80211_STYPE_ACTION,
  1535. NULL);
  1536. if (!elems) {
  1537. ret = -ENOMEM;
  1538. goto out;
  1539. }
  1540. if (elems->parse_error) {
  1541. tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
  1542. ret = -EINVAL;
  1543. goto out;
  1544. }
  1545. if (!elems->ch_sw_timing || !elems->lnk_id) {
  1546. tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
  1547. ret = -EINVAL;
  1548. goto out;
  1549. }
  1550. /* validate the initiator is set correctly */
  1551. local_initiator =
  1552. !memcmp(elems->lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
  1553. if (local_initiator == sta->sta.tdls_initiator) {
  1554. tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
  1555. ret = -EINVAL;
  1556. goto out;
  1557. }
  1558. params.switch_time = le16_to_cpu(elems->ch_sw_timing->switch_time);
  1559. params.switch_timeout = le16_to_cpu(elems->ch_sw_timing->switch_timeout);
  1560. params.tmpl_skb =
  1561. ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
  1562. if (!params.tmpl_skb) {
  1563. ret = -ENOENT;
  1564. goto out;
  1565. }
  1566. ret = 0;
  1567. call_drv:
  1568. drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
  1569. tdls_dbg(sdata,
  1570. "TDLS channel switch response received from %pM status %d\n",
  1571. tf->sa, params.status);
  1572. out:
  1573. dev_kfree_skb_any(params.tmpl_skb);
  1574. kfree(elems);
  1575. return ret;
  1576. }
  1577. static int
  1578. ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
  1579. struct sk_buff *skb)
  1580. {
  1581. struct ieee80211_local *local = sdata->local;
  1582. struct ieee802_11_elems *elems;
  1583. struct cfg80211_chan_def chandef;
  1584. struct ieee80211_channel *chan;
  1585. enum nl80211_channel_type chan_type;
  1586. int freq;
  1587. u8 target_channel, oper_class;
  1588. bool local_initiator;
  1589. struct sta_info *sta;
  1590. enum nl80211_band band;
  1591. struct ieee80211_tdls_data *tf = (void *)skb->data;
  1592. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  1593. int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
  1594. struct ieee80211_tdls_ch_sw_params params = {};
  1595. int ret = 0;
  1596. lockdep_assert_wiphy(local->hw.wiphy);
  1597. params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
  1598. params.timestamp = rx_status->device_timestamp;
  1599. if (skb->len < baselen) {
  1600. tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
  1601. skb->len);
  1602. return -EINVAL;
  1603. }
  1604. target_channel = tf->u.chan_switch_req.target_channel;
  1605. oper_class = tf->u.chan_switch_req.oper_class;
  1606. /*
  1607. * We can't easily infer the channel band. The operating class is
  1608. * ambiguous - there are multiple tables (US/Europe/JP/Global). The
  1609. * solution here is to treat channels with number >14 as 5GHz ones,
  1610. * and specifically check for the (oper_class, channel) combinations
  1611. * where this doesn't hold. These are thankfully unique according to
  1612. * IEEE802.11-2012.
  1613. * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
  1614. * valid here.
  1615. */
  1616. if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
  1617. oper_class == 4 || oper_class == 5 || oper_class == 6) &&
  1618. target_channel < 14)
  1619. band = NL80211_BAND_5GHZ;
  1620. else
  1621. band = target_channel < 14 ? NL80211_BAND_2GHZ :
  1622. NL80211_BAND_5GHZ;
  1623. freq = ieee80211_channel_to_frequency(target_channel, band);
  1624. if (freq == 0) {
  1625. tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
  1626. target_channel);
  1627. return -EINVAL;
  1628. }
  1629. chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
  1630. if (!chan) {
  1631. tdls_dbg(sdata,
  1632. "Unsupported channel for TDLS chan switch: %d\n",
  1633. target_channel);
  1634. return -EINVAL;
  1635. }
  1636. elems = ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
  1637. skb->len - baselen,
  1638. IEEE80211_FTYPE_MGMT |
  1639. IEEE80211_STYPE_ACTION,
  1640. NULL);
  1641. if (!elems)
  1642. return -ENOMEM;
  1643. if (elems->parse_error) {
  1644. tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
  1645. ret = -EINVAL;
  1646. goto free;
  1647. }
  1648. if (!elems->ch_sw_timing || !elems->lnk_id) {
  1649. tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
  1650. ret = -EINVAL;
  1651. goto free;
  1652. }
  1653. if (!elems->sec_chan_offs) {
  1654. chan_type = NL80211_CHAN_HT20;
  1655. } else {
  1656. switch (elems->sec_chan_offs->sec_chan_offs) {
  1657. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1658. chan_type = NL80211_CHAN_HT40PLUS;
  1659. break;
  1660. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1661. chan_type = NL80211_CHAN_HT40MINUS;
  1662. break;
  1663. default:
  1664. chan_type = NL80211_CHAN_HT20;
  1665. break;
  1666. }
  1667. }
  1668. cfg80211_chandef_create(&chandef, chan, chan_type);
  1669. /* we will be active on the TDLS link */
  1670. if (!cfg80211_reg_can_beacon_relax(sdata->local->hw.wiphy, &chandef,
  1671. sdata->wdev.iftype)) {
  1672. tdls_dbg(sdata, "TDLS chan switch to forbidden channel\n");
  1673. ret = -EINVAL;
  1674. goto free;
  1675. }
  1676. sta = sta_info_get(sdata, tf->sa);
  1677. if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
  1678. tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
  1679. tf->sa);
  1680. ret = -EINVAL;
  1681. goto out;
  1682. }
  1683. params.sta = &sta->sta;
  1684. /* validate the initiator is set correctly */
  1685. local_initiator =
  1686. !memcmp(elems->lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
  1687. if (local_initiator == sta->sta.tdls_initiator) {
  1688. tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
  1689. ret = -EINVAL;
  1690. goto out;
  1691. }
  1692. /* peer should have known better */
  1693. if (!sta->sta.deflink.ht_cap.ht_supported && elems->sec_chan_offs &&
  1694. elems->sec_chan_offs->sec_chan_offs) {
  1695. tdls_dbg(sdata, "TDLS chan switch - wide chan unsupported\n");
  1696. ret = -EOPNOTSUPP;
  1697. goto out;
  1698. }
  1699. params.chandef = &chandef;
  1700. params.switch_time = le16_to_cpu(elems->ch_sw_timing->switch_time);
  1701. params.switch_timeout = le16_to_cpu(elems->ch_sw_timing->switch_timeout);
  1702. params.tmpl_skb =
  1703. ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
  1704. &params.ch_sw_tm_ie);
  1705. if (!params.tmpl_skb) {
  1706. ret = -ENOENT;
  1707. goto out;
  1708. }
  1709. drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
  1710. tdls_dbg(sdata,
  1711. "TDLS ch switch request received from %pM ch %d width %d\n",
  1712. tf->sa, params.chandef->chan->center_freq,
  1713. params.chandef->width);
  1714. out:
  1715. dev_kfree_skb_any(params.tmpl_skb);
  1716. free:
  1717. kfree(elems);
  1718. return ret;
  1719. }
  1720. void
  1721. ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
  1722. struct sk_buff *skb)
  1723. {
  1724. struct ieee80211_tdls_data *tf = (void *)skb->data;
  1725. struct wiphy *wiphy = sdata->local->hw.wiphy;
  1726. lockdep_assert_wiphy(wiphy);
  1727. /* make sure the driver supports it */
  1728. if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
  1729. return;
  1730. /* we want to access the entire packet */
  1731. if (skb_linearize(skb))
  1732. return;
  1733. /*
  1734. * The packet/size was already validated by mac80211 Rx path, only look
  1735. * at the action type.
  1736. */
  1737. switch (tf->action_code) {
  1738. case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  1739. ieee80211_process_tdls_channel_switch_req(sdata, skb);
  1740. break;
  1741. case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  1742. ieee80211_process_tdls_channel_switch_resp(sdata, skb);
  1743. break;
  1744. default:
  1745. WARN_ON_ONCE(1);
  1746. return;
  1747. }
  1748. }
  1749. void ieee80211_teardown_tdls_peers(struct ieee80211_link_data *link)
  1750. {
  1751. struct ieee80211_sub_if_data *sdata = link->sdata;
  1752. struct sta_info *sta;
  1753. u16 reason = WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED;
  1754. rcu_read_lock();
  1755. list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
  1756. if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
  1757. !test_sta_flag(sta, WLAN_STA_AUTHORIZED))
  1758. continue;
  1759. if (sta->deflink.link_id != link->link_id)
  1760. continue;
  1761. ieee80211_tdls_oper_request(&sdata->vif, sta->sta.addr,
  1762. NL80211_TDLS_TEARDOWN, reason,
  1763. GFP_ATOMIC);
  1764. }
  1765. rcu_read_unlock();
  1766. }
  1767. void ieee80211_tdls_handle_disconnect(struct ieee80211_sub_if_data *sdata,
  1768. const u8 *peer, u16 reason)
  1769. {
  1770. struct ieee80211_sta *sta;
  1771. rcu_read_lock();
  1772. sta = ieee80211_find_sta(&sdata->vif, peer);
  1773. if (!sta || !sta->tdls) {
  1774. rcu_read_unlock();
  1775. return;
  1776. }
  1777. rcu_read_unlock();
  1778. tdls_dbg(sdata, "disconnected from TDLS peer %pM (Reason: %u=%s)\n",
  1779. peer, reason,
  1780. ieee80211_get_reason_code_string(reason));
  1781. ieee80211_tdls_oper_request(&sdata->vif, peer,
  1782. NL80211_TDLS_TEARDOWN,
  1783. WLAN_REASON_TDLS_TEARDOWN_UNREACHABLE,
  1784. GFP_ATOMIC);
  1785. }