tdls.c 55 KB

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