tdls.c 56 KB

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