tdls.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. *
  8. * This file is GPLv2 as found in COPYING.
  9. */
  10. #include <linux/ieee80211.h>
  11. #include <linux/log2.h>
  12. #include <net/cfg80211.h>
  13. #include "ieee80211_i.h"
  14. #include "driver-ops.h"
  15. /* give usermode some time for retries in setting up the TDLS session */
  16. #define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
  17. void ieee80211_tdls_peer_del_work(struct work_struct *wk)
  18. {
  19. struct ieee80211_sub_if_data *sdata;
  20. struct ieee80211_local *local;
  21. sdata = container_of(wk, struct ieee80211_sub_if_data,
  22. u.mgd.tdls_peer_del_work.work);
  23. local = sdata->local;
  24. mutex_lock(&local->mtx);
  25. if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
  26. tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->u.mgd.tdls_peer);
  27. sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
  28. eth_zero_addr(sdata->u.mgd.tdls_peer);
  29. }
  30. mutex_unlock(&local->mtx);
  31. }
  32. static void ieee80211_tdls_add_ext_capab(struct ieee80211_local *local,
  33. struct sk_buff *skb)
  34. {
  35. u8 *pos = (void *)skb_put(skb, 7);
  36. bool chan_switch = local->hw.wiphy->features &
  37. NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
  38. *pos++ = WLAN_EID_EXT_CAPABILITY;
  39. *pos++ = 5; /* len */
  40. *pos++ = 0x0;
  41. *pos++ = 0x0;
  42. *pos++ = 0x0;
  43. *pos++ = chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0;
  44. *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
  45. }
  46. static u8
  47. ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
  48. struct sk_buff *skb, u16 start, u16 end,
  49. u16 spacing)
  50. {
  51. u8 subband_cnt = 0, ch_cnt = 0;
  52. struct ieee80211_channel *ch;
  53. struct cfg80211_chan_def chandef;
  54. int i, subband_start;
  55. for (i = start; i <= end; i += spacing) {
  56. if (!ch_cnt)
  57. subband_start = i;
  58. ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
  59. if (ch) {
  60. /* we will be active on the channel */
  61. cfg80211_chandef_create(&chandef, ch,
  62. NL80211_CHAN_NO_HT);
  63. if (cfg80211_reg_can_beacon(sdata->local->hw.wiphy,
  64. &chandef,
  65. sdata->wdev.iftype)) {
  66. ch_cnt++;
  67. /*
  68. * check if the next channel is also part of
  69. * this allowed range
  70. */
  71. continue;
  72. }
  73. }
  74. /*
  75. * we've reached the end of a range, with allowed channels
  76. * found
  77. */
  78. if (ch_cnt) {
  79. u8 *pos = skb_put(skb, 2);
  80. *pos++ = ieee80211_frequency_to_channel(subband_start);
  81. *pos++ = ch_cnt;
  82. subband_cnt++;
  83. ch_cnt = 0;
  84. }
  85. }
  86. /* all channels in the requested range are allowed - add them here */
  87. if (ch_cnt) {
  88. u8 *pos = skb_put(skb, 2);
  89. *pos++ = ieee80211_frequency_to_channel(subband_start);
  90. *pos++ = ch_cnt;
  91. subband_cnt++;
  92. }
  93. return subband_cnt;
  94. }
  95. static void
  96. ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
  97. struct sk_buff *skb)
  98. {
  99. /*
  100. * Add possible channels for TDLS. These are channels that are allowed
  101. * to be active.
  102. */
  103. u8 subband_cnt;
  104. u8 *pos = skb_put(skb, 2);
  105. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  106. /*
  107. * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
  108. * this doesn't happen in real world scenarios.
  109. */
  110. /* 2GHz, with 5MHz spacing */
  111. subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
  112. /* 5GHz, with 20MHz spacing */
  113. subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
  114. /* length */
  115. *pos = 2 * subband_cnt;
  116. }
  117. static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
  118. {
  119. u8 *pos = (void *)skb_put(skb, 3);
  120. *pos++ = WLAN_EID_BSS_COEX_2040;
  121. *pos++ = 1; /* len */
  122. *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
  123. }
  124. static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
  125. u16 status_code)
  126. {
  127. struct ieee80211_local *local = sdata->local;
  128. u16 capab;
  129. /* The capability will be 0 when sending a failure code */
  130. if (status_code != 0)
  131. return 0;
  132. capab = 0;
  133. if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
  134. return capab;
  135. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  136. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  137. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  138. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  139. return capab;
  140. }
  141. static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
  142. struct sk_buff *skb, const u8 *peer,
  143. bool initiator)
  144. {
  145. struct ieee80211_tdls_lnkie *lnkid;
  146. const u8 *init_addr, *rsp_addr;
  147. if (initiator) {
  148. init_addr = sdata->vif.addr;
  149. rsp_addr = peer;
  150. } else {
  151. init_addr = peer;
  152. rsp_addr = sdata->vif.addr;
  153. }
  154. lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
  155. lnkid->ie_type = WLAN_EID_LINK_ID;
  156. lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
  157. memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  158. memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
  159. memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
  160. }
  161. /* translate numbering in the WMM parameter IE to the mac80211 notation */
  162. static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
  163. {
  164. switch (ac) {
  165. default:
  166. WARN_ON_ONCE(1);
  167. case 0:
  168. return IEEE80211_AC_BE;
  169. case 1:
  170. return IEEE80211_AC_BK;
  171. case 2:
  172. return IEEE80211_AC_VI;
  173. case 3:
  174. return IEEE80211_AC_VO;
  175. }
  176. }
  177. static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
  178. {
  179. u8 ret;
  180. ret = aifsn & 0x0f;
  181. if (acm)
  182. ret |= 0x10;
  183. ret |= (aci << 5) & 0x60;
  184. return ret;
  185. }
  186. static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
  187. {
  188. return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
  189. ((ilog2(cw_max + 1) << 0x4) & 0xf0);
  190. }
  191. static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
  192. struct sk_buff *skb)
  193. {
  194. struct ieee80211_wmm_param_ie *wmm;
  195. struct ieee80211_tx_queue_params *txq;
  196. int i;
  197. wmm = (void *)skb_put(skb, sizeof(*wmm));
  198. memset(wmm, 0, sizeof(*wmm));
  199. wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
  200. wmm->len = sizeof(*wmm) - 2;
  201. wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
  202. wmm->oui[1] = 0x50;
  203. wmm->oui[2] = 0xf2;
  204. wmm->oui_type = 2; /* WME */
  205. wmm->oui_subtype = 1; /* WME param */
  206. wmm->version = 1; /* WME ver */
  207. wmm->qos_info = 0; /* U-APSD not in use */
  208. /*
  209. * Use the EDCA parameters defined for the BSS, or default if the AP
  210. * doesn't support it, as mandated by 802.11-2012 section 10.22.4
  211. */
  212. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  213. txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
  214. wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
  215. txq->acm, i);
  216. wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
  217. wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
  218. }
  219. }
  220. static void
  221. ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
  222. struct sk_buff *skb, const u8 *peer,
  223. u8 action_code, bool initiator,
  224. const u8 *extra_ies, size_t extra_ies_len)
  225. {
  226. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  227. struct ieee80211_local *local = sdata->local;
  228. struct ieee80211_supported_band *sband;
  229. struct ieee80211_sta_ht_cap ht_cap;
  230. struct sta_info *sta = NULL;
  231. size_t offset = 0, noffset;
  232. u8 *pos;
  233. rcu_read_lock();
  234. /* we should have the peer STA if we're already responding */
  235. if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
  236. sta = sta_info_get(sdata, peer);
  237. if (WARN_ON_ONCE(!sta)) {
  238. rcu_read_unlock();
  239. return;
  240. }
  241. }
  242. ieee80211_add_srates_ie(sdata, skb, false, band);
  243. ieee80211_add_ext_srates_ie(sdata, skb, false, band);
  244. ieee80211_tdls_add_supp_channels(sdata, skb);
  245. /* add any custom IEs that go before Extended Capabilities */
  246. if (extra_ies_len) {
  247. static const u8 before_ext_cap[] = {
  248. WLAN_EID_SUPP_RATES,
  249. WLAN_EID_COUNTRY,
  250. WLAN_EID_EXT_SUPP_RATES,
  251. WLAN_EID_SUPPORTED_CHANNELS,
  252. WLAN_EID_RSN,
  253. };
  254. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  255. before_ext_cap,
  256. ARRAY_SIZE(before_ext_cap),
  257. offset);
  258. pos = skb_put(skb, noffset - offset);
  259. memcpy(pos, extra_ies + offset, noffset - offset);
  260. offset = noffset;
  261. }
  262. ieee80211_tdls_add_ext_capab(local, skb);
  263. /* add the QoS element if we support it */
  264. if (local->hw.queues >= IEEE80211_NUM_ACS &&
  265. action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
  266. ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
  267. /* add any custom IEs that go before HT capabilities */
  268. if (extra_ies_len) {
  269. static const u8 before_ht_cap[] = {
  270. WLAN_EID_SUPP_RATES,
  271. WLAN_EID_COUNTRY,
  272. WLAN_EID_EXT_SUPP_RATES,
  273. WLAN_EID_SUPPORTED_CHANNELS,
  274. WLAN_EID_RSN,
  275. WLAN_EID_EXT_CAPABILITY,
  276. WLAN_EID_QOS_CAPA,
  277. WLAN_EID_FAST_BSS_TRANSITION,
  278. WLAN_EID_TIMEOUT_INTERVAL,
  279. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  280. };
  281. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  282. before_ht_cap,
  283. ARRAY_SIZE(before_ht_cap),
  284. offset);
  285. pos = skb_put(skb, noffset - offset);
  286. memcpy(pos, extra_ies + offset, noffset - offset);
  287. offset = noffset;
  288. }
  289. /*
  290. * with TDLS we can switch channels, and HT-caps are not necessarily
  291. * the same on all bands. The specification limits the setup to a
  292. * single HT-cap, so use the current band for now.
  293. */
  294. sband = local->hw.wiphy->bands[band];
  295. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  296. if (action_code == WLAN_TDLS_SETUP_REQUEST && ht_cap.ht_supported) {
  297. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  298. /* disable SMPS in TDLS initiator */
  299. ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
  300. << IEEE80211_HT_CAP_SM_PS_SHIFT;
  301. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  302. ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  303. } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
  304. ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
  305. /* disable SMPS in TDLS responder */
  306. sta->sta.ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
  307. << IEEE80211_HT_CAP_SM_PS_SHIFT;
  308. /* the peer caps are already intersected with our own */
  309. memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
  310. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  311. ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  312. }
  313. rcu_read_unlock();
  314. if (ht_cap.ht_supported &&
  315. (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  316. ieee80211_tdls_add_bss_coex_ie(skb);
  317. /* add any remaining IEs */
  318. if (extra_ies_len) {
  319. noffset = extra_ies_len;
  320. pos = skb_put(skb, noffset - offset);
  321. memcpy(pos, extra_ies + offset, noffset - offset);
  322. }
  323. ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  324. }
  325. static void
  326. ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
  327. struct sk_buff *skb, const u8 *peer,
  328. bool initiator, const u8 *extra_ies,
  329. size_t extra_ies_len)
  330. {
  331. struct ieee80211_local *local = sdata->local;
  332. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  333. size_t offset = 0, noffset;
  334. struct sta_info *sta, *ap_sta;
  335. u8 *pos;
  336. rcu_read_lock();
  337. sta = sta_info_get(sdata, peer);
  338. ap_sta = sta_info_get(sdata, ifmgd->bssid);
  339. if (WARN_ON_ONCE(!sta || !ap_sta)) {
  340. rcu_read_unlock();
  341. return;
  342. }
  343. /* add any custom IEs that go before the QoS IE */
  344. if (extra_ies_len) {
  345. static const u8 before_qos[] = {
  346. WLAN_EID_RSN,
  347. };
  348. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  349. before_qos,
  350. ARRAY_SIZE(before_qos),
  351. offset);
  352. pos = skb_put(skb, noffset - offset);
  353. memcpy(pos, extra_ies + offset, noffset - offset);
  354. offset = noffset;
  355. }
  356. /* add the QoS param IE if both the peer and we support it */
  357. if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
  358. ieee80211_tdls_add_wmm_param_ie(sdata, skb);
  359. /* add any custom IEs that go before HT operation */
  360. if (extra_ies_len) {
  361. static const u8 before_ht_op[] = {
  362. WLAN_EID_RSN,
  363. WLAN_EID_QOS_CAPA,
  364. WLAN_EID_FAST_BSS_TRANSITION,
  365. WLAN_EID_TIMEOUT_INTERVAL,
  366. };
  367. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  368. before_ht_op,
  369. ARRAY_SIZE(before_ht_op),
  370. offset);
  371. pos = skb_put(skb, noffset - offset);
  372. memcpy(pos, extra_ies + offset, noffset - offset);
  373. offset = noffset;
  374. }
  375. /* if HT support is only added in TDLS, we need an HT-operation IE */
  376. if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
  377. struct ieee80211_chanctx_conf *chanctx_conf =
  378. rcu_dereference(sdata->vif.chanctx_conf);
  379. if (!WARN_ON(!chanctx_conf)) {
  380. pos = skb_put(skb, 2 +
  381. sizeof(struct ieee80211_ht_operation));
  382. /* send an empty HT operation IE */
  383. ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
  384. &chanctx_conf->def, 0);
  385. }
  386. }
  387. rcu_read_unlock();
  388. /* add any remaining IEs */
  389. if (extra_ies_len) {
  390. noffset = extra_ies_len;
  391. pos = skb_put(skb, noffset - offset);
  392. memcpy(pos, extra_ies + offset, noffset - offset);
  393. }
  394. ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  395. }
  396. static void
  397. ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_sub_if_data *sdata,
  398. struct sk_buff *skb, const u8 *peer,
  399. bool initiator, const u8 *extra_ies,
  400. size_t extra_ies_len, u8 oper_class,
  401. struct cfg80211_chan_def *chandef)
  402. {
  403. struct ieee80211_tdls_data *tf;
  404. size_t offset = 0, noffset;
  405. u8 *pos;
  406. if (WARN_ON_ONCE(!chandef))
  407. return;
  408. tf = (void *)skb->data;
  409. tf->u.chan_switch_req.target_channel =
  410. ieee80211_frequency_to_channel(chandef->chan->center_freq);
  411. tf->u.chan_switch_req.oper_class = oper_class;
  412. if (extra_ies_len) {
  413. static const u8 before_lnkie[] = {
  414. WLAN_EID_SECONDARY_CHANNEL_OFFSET,
  415. };
  416. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  417. before_lnkie,
  418. ARRAY_SIZE(before_lnkie),
  419. offset);
  420. pos = skb_put(skb, noffset - offset);
  421. memcpy(pos, extra_ies + offset, noffset - offset);
  422. offset = noffset;
  423. }
  424. ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  425. /* add any remaining IEs */
  426. if (extra_ies_len) {
  427. noffset = extra_ies_len;
  428. pos = skb_put(skb, noffset - offset);
  429. memcpy(pos, extra_ies + offset, noffset - offset);
  430. }
  431. }
  432. static void
  433. ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_sub_if_data *sdata,
  434. struct sk_buff *skb, const u8 *peer,
  435. u16 status_code, bool initiator,
  436. const u8 *extra_ies,
  437. size_t extra_ies_len)
  438. {
  439. if (status_code == 0)
  440. ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  441. if (extra_ies_len)
  442. memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
  443. }
  444. static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
  445. struct sk_buff *skb, const u8 *peer,
  446. u8 action_code, u16 status_code,
  447. bool initiator, const u8 *extra_ies,
  448. size_t extra_ies_len, u8 oper_class,
  449. struct cfg80211_chan_def *chandef)
  450. {
  451. switch (action_code) {
  452. case WLAN_TDLS_SETUP_REQUEST:
  453. case WLAN_TDLS_SETUP_RESPONSE:
  454. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  455. if (status_code == 0)
  456. ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
  457. action_code,
  458. initiator,
  459. extra_ies,
  460. extra_ies_len);
  461. break;
  462. case WLAN_TDLS_SETUP_CONFIRM:
  463. if (status_code == 0)
  464. ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
  465. initiator, extra_ies,
  466. extra_ies_len);
  467. break;
  468. case WLAN_TDLS_TEARDOWN:
  469. case WLAN_TDLS_DISCOVERY_REQUEST:
  470. if (extra_ies_len)
  471. memcpy(skb_put(skb, extra_ies_len), extra_ies,
  472. extra_ies_len);
  473. if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
  474. ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  475. break;
  476. case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  477. ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
  478. initiator, extra_ies,
  479. extra_ies_len,
  480. oper_class, chandef);
  481. break;
  482. case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  483. ieee80211_tdls_add_chan_switch_resp_ies(sdata, skb, peer,
  484. status_code,
  485. initiator, extra_ies,
  486. extra_ies_len);
  487. break;
  488. }
  489. }
  490. static int
  491. ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
  492. const u8 *peer, u8 action_code, u8 dialog_token,
  493. u16 status_code, struct sk_buff *skb)
  494. {
  495. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  496. struct ieee80211_tdls_data *tf;
  497. tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
  498. memcpy(tf->da, peer, ETH_ALEN);
  499. memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
  500. tf->ether_type = cpu_to_be16(ETH_P_TDLS);
  501. tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
  502. /* network header is after the ethernet header */
  503. skb_set_network_header(skb, ETH_HLEN);
  504. switch (action_code) {
  505. case WLAN_TDLS_SETUP_REQUEST:
  506. tf->category = WLAN_CATEGORY_TDLS;
  507. tf->action_code = WLAN_TDLS_SETUP_REQUEST;
  508. skb_put(skb, sizeof(tf->u.setup_req));
  509. tf->u.setup_req.dialog_token = dialog_token;
  510. tf->u.setup_req.capability =
  511. cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
  512. status_code));
  513. break;
  514. case WLAN_TDLS_SETUP_RESPONSE:
  515. tf->category = WLAN_CATEGORY_TDLS;
  516. tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
  517. skb_put(skb, sizeof(tf->u.setup_resp));
  518. tf->u.setup_resp.status_code = cpu_to_le16(status_code);
  519. tf->u.setup_resp.dialog_token = dialog_token;
  520. tf->u.setup_resp.capability =
  521. cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
  522. status_code));
  523. break;
  524. case WLAN_TDLS_SETUP_CONFIRM:
  525. tf->category = WLAN_CATEGORY_TDLS;
  526. tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
  527. skb_put(skb, sizeof(tf->u.setup_cfm));
  528. tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
  529. tf->u.setup_cfm.dialog_token = dialog_token;
  530. break;
  531. case WLAN_TDLS_TEARDOWN:
  532. tf->category = WLAN_CATEGORY_TDLS;
  533. tf->action_code = WLAN_TDLS_TEARDOWN;
  534. skb_put(skb, sizeof(tf->u.teardown));
  535. tf->u.teardown.reason_code = cpu_to_le16(status_code);
  536. break;
  537. case WLAN_TDLS_DISCOVERY_REQUEST:
  538. tf->category = WLAN_CATEGORY_TDLS;
  539. tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
  540. skb_put(skb, sizeof(tf->u.discover_req));
  541. tf->u.discover_req.dialog_token = dialog_token;
  542. break;
  543. case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  544. tf->category = WLAN_CATEGORY_TDLS;
  545. tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
  546. skb_put(skb, sizeof(tf->u.chan_switch_req));
  547. break;
  548. case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  549. tf->category = WLAN_CATEGORY_TDLS;
  550. tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
  551. skb_put(skb, sizeof(tf->u.chan_switch_resp));
  552. tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
  553. break;
  554. default:
  555. return -EINVAL;
  556. }
  557. return 0;
  558. }
  559. static int
  560. ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
  561. const u8 *peer, u8 action_code, u8 dialog_token,
  562. u16 status_code, struct sk_buff *skb)
  563. {
  564. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  565. struct ieee80211_mgmt *mgmt;
  566. mgmt = (void *)skb_put(skb, 24);
  567. memset(mgmt, 0, 24);
  568. memcpy(mgmt->da, peer, ETH_ALEN);
  569. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  570. memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  571. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  572. IEEE80211_STYPE_ACTION);
  573. switch (action_code) {
  574. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  575. skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
  576. mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
  577. mgmt->u.action.u.tdls_discover_resp.action_code =
  578. WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
  579. mgmt->u.action.u.tdls_discover_resp.dialog_token =
  580. dialog_token;
  581. mgmt->u.action.u.tdls_discover_resp.capability =
  582. cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
  583. status_code));
  584. break;
  585. default:
  586. return -EINVAL;
  587. }
  588. return 0;
  589. }
  590. static struct sk_buff *
  591. ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
  592. const u8 *peer, u8 action_code,
  593. u8 dialog_token, u16 status_code,
  594. bool initiator, const u8 *extra_ies,
  595. size_t extra_ies_len, u8 oper_class,
  596. struct cfg80211_chan_def *chandef)
  597. {
  598. struct ieee80211_local *local = sdata->local;
  599. struct sk_buff *skb;
  600. int ret;
  601. skb = netdev_alloc_skb(sdata->dev,
  602. local->hw.extra_tx_headroom +
  603. max(sizeof(struct ieee80211_mgmt),
  604. sizeof(struct ieee80211_tdls_data)) +
  605. 50 + /* supported rates */
  606. 7 + /* ext capab */
  607. 26 + /* max(WMM-info, WMM-param) */
  608. 2 + max(sizeof(struct ieee80211_ht_cap),
  609. sizeof(struct ieee80211_ht_operation)) +
  610. 50 + /* supported channels */
  611. 3 + /* 40/20 BSS coex */
  612. extra_ies_len +
  613. sizeof(struct ieee80211_tdls_lnkie));
  614. if (!skb)
  615. return NULL;
  616. skb_reserve(skb, local->hw.extra_tx_headroom);
  617. switch (action_code) {
  618. case WLAN_TDLS_SETUP_REQUEST:
  619. case WLAN_TDLS_SETUP_RESPONSE:
  620. case WLAN_TDLS_SETUP_CONFIRM:
  621. case WLAN_TDLS_TEARDOWN:
  622. case WLAN_TDLS_DISCOVERY_REQUEST:
  623. case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  624. case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  625. ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
  626. sdata->dev, peer,
  627. action_code, dialog_token,
  628. status_code, skb);
  629. break;
  630. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  631. ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
  632. peer, action_code,
  633. dialog_token, status_code,
  634. skb);
  635. break;
  636. default:
  637. ret = -ENOTSUPP;
  638. break;
  639. }
  640. if (ret < 0)
  641. goto fail;
  642. ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
  643. initiator, extra_ies, extra_ies_len, oper_class,
  644. chandef);
  645. return skb;
  646. fail:
  647. dev_kfree_skb(skb);
  648. return NULL;
  649. }
  650. static int
  651. ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
  652. const u8 *peer, u8 action_code, u8 dialog_token,
  653. u16 status_code, u32 peer_capability,
  654. bool initiator, const u8 *extra_ies,
  655. size_t extra_ies_len, u8 oper_class,
  656. struct cfg80211_chan_def *chandef)
  657. {
  658. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  659. struct sk_buff *skb = NULL;
  660. struct sta_info *sta;
  661. u32 flags = 0;
  662. int ret = 0;
  663. rcu_read_lock();
  664. sta = sta_info_get(sdata, peer);
  665. /* infer the initiator if we can, to support old userspace */
  666. switch (action_code) {
  667. case WLAN_TDLS_SETUP_REQUEST:
  668. if (sta) {
  669. set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
  670. sta->sta.tdls_initiator = false;
  671. }
  672. /* fall-through */
  673. case WLAN_TDLS_SETUP_CONFIRM:
  674. case WLAN_TDLS_DISCOVERY_REQUEST:
  675. initiator = true;
  676. break;
  677. case WLAN_TDLS_SETUP_RESPONSE:
  678. /*
  679. * In some testing scenarios, we send a request and response.
  680. * Make the last packet sent take effect for the initiator
  681. * value.
  682. */
  683. if (sta) {
  684. clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
  685. sta->sta.tdls_initiator = true;
  686. }
  687. /* fall-through */
  688. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  689. initiator = false;
  690. break;
  691. case WLAN_TDLS_TEARDOWN:
  692. case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  693. case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  694. /* any value is ok */
  695. break;
  696. default:
  697. ret = -ENOTSUPP;
  698. break;
  699. }
  700. if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
  701. initiator = true;
  702. rcu_read_unlock();
  703. if (ret < 0)
  704. goto fail;
  705. skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer, action_code,
  706. dialog_token, status_code,
  707. initiator, extra_ies,
  708. extra_ies_len, oper_class,
  709. chandef);
  710. if (!skb) {
  711. ret = -EINVAL;
  712. goto fail;
  713. }
  714. if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
  715. ieee80211_tx_skb(sdata, skb);
  716. return 0;
  717. }
  718. /*
  719. * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
  720. * we should default to AC_VI.
  721. */
  722. switch (action_code) {
  723. case WLAN_TDLS_SETUP_REQUEST:
  724. case WLAN_TDLS_SETUP_RESPONSE:
  725. skb_set_queue_mapping(skb, IEEE80211_AC_BK);
  726. skb->priority = 2;
  727. break;
  728. default:
  729. skb_set_queue_mapping(skb, IEEE80211_AC_VI);
  730. skb->priority = 5;
  731. break;
  732. }
  733. /*
  734. * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
  735. * Later, if no ACK is returned from peer, we will re-send the teardown
  736. * packet through the AP.
  737. */
  738. if ((action_code == WLAN_TDLS_TEARDOWN) &&
  739. (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
  740. bool try_resend; /* Should we keep skb for possible resend */
  741. /* If not sending directly to peer - no point in keeping skb */
  742. rcu_read_lock();
  743. sta = sta_info_get(sdata, peer);
  744. try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
  745. rcu_read_unlock();
  746. spin_lock_bh(&sdata->u.mgd.teardown_lock);
  747. if (try_resend && !sdata->u.mgd.teardown_skb) {
  748. /* Mark it as requiring TX status callback */
  749. flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
  750. IEEE80211_TX_INTFL_MLME_CONN_TX;
  751. /*
  752. * skb is copied since mac80211 will later set
  753. * properties that might not be the same as the AP,
  754. * such as encryption, QoS, addresses, etc.
  755. *
  756. * No problem if skb_copy() fails, so no need to check.
  757. */
  758. sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
  759. sdata->u.mgd.orig_teardown_skb = skb;
  760. }
  761. spin_unlock_bh(&sdata->u.mgd.teardown_lock);
  762. }
  763. /* disable bottom halves when entering the Tx path */
  764. local_bh_disable();
  765. __ieee80211_subif_start_xmit(skb, dev, flags);
  766. local_bh_enable();
  767. return ret;
  768. fail:
  769. dev_kfree_skb(skb);
  770. return ret;
  771. }
  772. static int
  773. ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
  774. const u8 *peer, u8 action_code, u8 dialog_token,
  775. u16 status_code, u32 peer_capability, bool initiator,
  776. const u8 *extra_ies, size_t extra_ies_len)
  777. {
  778. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  779. struct ieee80211_local *local = sdata->local;
  780. int ret;
  781. mutex_lock(&local->mtx);
  782. /* we don't support concurrent TDLS peer setups */
  783. if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
  784. !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
  785. ret = -EBUSY;
  786. goto exit;
  787. }
  788. /*
  789. * make sure we have a STA representing the peer so we drop or buffer
  790. * non-TDLS-setup frames to the peer. We can't send other packets
  791. * during setup through the AP path.
  792. * Allow error packets to be sent - sometimes we don't even add a STA
  793. * before failing the setup.
  794. */
  795. if (status_code == 0) {
  796. rcu_read_lock();
  797. if (!sta_info_get(sdata, peer)) {
  798. rcu_read_unlock();
  799. ret = -ENOLINK;
  800. goto exit;
  801. }
  802. rcu_read_unlock();
  803. }
  804. ieee80211_flush_queues(local, sdata, false);
  805. ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
  806. dialog_token, status_code,
  807. peer_capability, initiator,
  808. extra_ies, extra_ies_len, 0,
  809. NULL);
  810. if (ret < 0)
  811. goto exit;
  812. memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
  813. ieee80211_queue_delayed_work(&sdata->local->hw,
  814. &sdata->u.mgd.tdls_peer_del_work,
  815. TDLS_PEER_SETUP_TIMEOUT);
  816. exit:
  817. mutex_unlock(&local->mtx);
  818. return ret;
  819. }
  820. static int
  821. ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
  822. const u8 *peer, u8 action_code, u8 dialog_token,
  823. u16 status_code, u32 peer_capability,
  824. bool initiator, const u8 *extra_ies,
  825. size_t extra_ies_len)
  826. {
  827. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  828. struct ieee80211_local *local = sdata->local;
  829. struct sta_info *sta;
  830. int ret;
  831. /*
  832. * No packets can be transmitted to the peer via the AP during setup -
  833. * the STA is set as a TDLS peer, but is not authorized.
  834. * During teardown, we prevent direct transmissions by stopping the
  835. * queues and flushing all direct packets.
  836. */
  837. ieee80211_stop_vif_queues(local, sdata,
  838. IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
  839. ieee80211_flush_queues(local, sdata, false);
  840. ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
  841. dialog_token, status_code,
  842. peer_capability, initiator,
  843. extra_ies, extra_ies_len, 0,
  844. NULL);
  845. if (ret < 0)
  846. sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
  847. ret);
  848. /*
  849. * Remove the STA AUTH flag to force further traffic through the AP. If
  850. * the STA was unreachable, it was already removed.
  851. */
  852. rcu_read_lock();
  853. sta = sta_info_get(sdata, peer);
  854. if (sta)
  855. clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
  856. rcu_read_unlock();
  857. ieee80211_wake_vif_queues(local, sdata,
  858. IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
  859. return 0;
  860. }
  861. int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
  862. const u8 *peer, u8 action_code, u8 dialog_token,
  863. u16 status_code, u32 peer_capability,
  864. bool initiator, const u8 *extra_ies,
  865. size_t extra_ies_len)
  866. {
  867. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  868. int ret;
  869. if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
  870. return -ENOTSUPP;
  871. /* make sure we are in managed mode, and associated */
  872. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  873. !sdata->u.mgd.associated)
  874. return -EINVAL;
  875. switch (action_code) {
  876. case WLAN_TDLS_SETUP_REQUEST:
  877. case WLAN_TDLS_SETUP_RESPONSE:
  878. ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
  879. dialog_token, status_code,
  880. peer_capability, initiator,
  881. extra_ies, extra_ies_len);
  882. break;
  883. case WLAN_TDLS_TEARDOWN:
  884. ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
  885. action_code, dialog_token,
  886. status_code,
  887. peer_capability, initiator,
  888. extra_ies, extra_ies_len);
  889. break;
  890. case WLAN_TDLS_DISCOVERY_REQUEST:
  891. /*
  892. * Protect the discovery so we can hear the TDLS discovery
  893. * response frame. It is transmitted directly and not buffered
  894. * by the AP.
  895. */
  896. drv_mgd_protect_tdls_discover(sdata->local, sdata);
  897. /* fall-through */
  898. case WLAN_TDLS_SETUP_CONFIRM:
  899. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  900. /* no special handling */
  901. ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
  902. action_code,
  903. dialog_token,
  904. status_code,
  905. peer_capability,
  906. initiator, extra_ies,
  907. extra_ies_len, 0, NULL);
  908. break;
  909. default:
  910. ret = -EOPNOTSUPP;
  911. break;
  912. }
  913. tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
  914. action_code, peer, ret);
  915. return ret;
  916. }
  917. int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
  918. const u8 *peer, enum nl80211_tdls_operation oper)
  919. {
  920. struct sta_info *sta;
  921. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  922. struct ieee80211_local *local = sdata->local;
  923. int ret;
  924. if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
  925. return -ENOTSUPP;
  926. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  927. return -EINVAL;
  928. switch (oper) {
  929. case NL80211_TDLS_ENABLE_LINK:
  930. case NL80211_TDLS_DISABLE_LINK:
  931. break;
  932. case NL80211_TDLS_TEARDOWN:
  933. case NL80211_TDLS_SETUP:
  934. case NL80211_TDLS_DISCOVERY_REQ:
  935. /* We don't support in-driver setup/teardown/discovery */
  936. return -ENOTSUPP;
  937. }
  938. mutex_lock(&local->mtx);
  939. tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
  940. switch (oper) {
  941. case NL80211_TDLS_ENABLE_LINK:
  942. rcu_read_lock();
  943. sta = sta_info_get(sdata, peer);
  944. if (!sta) {
  945. rcu_read_unlock();
  946. ret = -ENOLINK;
  947. break;
  948. }
  949. set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
  950. rcu_read_unlock();
  951. WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
  952. !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
  953. ret = 0;
  954. break;
  955. case NL80211_TDLS_DISABLE_LINK:
  956. /*
  957. * The teardown message in ieee80211_tdls_mgmt_teardown() was
  958. * created while the queues were stopped, so it might still be
  959. * pending. Before flushing the queues we need to be sure the
  960. * message is handled by the tasklet handling pending messages,
  961. * otherwise we might start destroying the station before
  962. * sending the teardown packet.
  963. * Note that this only forces the tasklet to flush pendings -
  964. * not to stop the tasklet from rescheduling itself.
  965. */
  966. tasklet_kill(&local->tx_pending_tasklet);
  967. /* flush a potentially queued teardown packet */
  968. ieee80211_flush_queues(local, sdata, false);
  969. ret = sta_info_destroy_addr(sdata, peer);
  970. break;
  971. default:
  972. ret = -ENOTSUPP;
  973. break;
  974. }
  975. if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
  976. cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
  977. eth_zero_addr(sdata->u.mgd.tdls_peer);
  978. }
  979. mutex_unlock(&local->mtx);
  980. return ret;
  981. }
  982. void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
  983. enum nl80211_tdls_operation oper,
  984. u16 reason_code, gfp_t gfp)
  985. {
  986. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  987. if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
  988. sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
  989. oper);
  990. return;
  991. }
  992. cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
  993. }
  994. EXPORT_SYMBOL(ieee80211_tdls_oper_request);
  995. static void
  996. iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
  997. {
  998. struct ieee80211_ch_switch_timing *ch_sw;
  999. *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
  1000. *buf++ = sizeof(struct ieee80211_ch_switch_timing);
  1001. ch_sw = (void *)buf;
  1002. ch_sw->switch_time = cpu_to_le16(switch_time);
  1003. ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
  1004. }
  1005. /* find switch timing IE in SKB ready for Tx */
  1006. static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
  1007. {
  1008. struct ieee80211_tdls_data *tf;
  1009. const u8 *ie_start;
  1010. /*
  1011. * Get the offset for the new location of the switch timing IE.
  1012. * The SKB network header will now point to the "payload_type"
  1013. * element of the TDLS data frame struct.
  1014. */
  1015. tf = container_of(skb->data + skb_network_offset(skb),
  1016. struct ieee80211_tdls_data, payload_type);
  1017. ie_start = tf->u.chan_switch_req.variable;
  1018. return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
  1019. skb->len - (ie_start - skb->data));
  1020. }
  1021. static struct sk_buff *
  1022. ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
  1023. struct cfg80211_chan_def *chandef,
  1024. u32 *ch_sw_tm_ie_offset)
  1025. {
  1026. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1027. u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
  1028. 2 + sizeof(struct ieee80211_ch_switch_timing)];
  1029. int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
  1030. u8 *pos = extra_ies;
  1031. struct sk_buff *skb;
  1032. /*
  1033. * if chandef points to a wide channel add a Secondary-Channel
  1034. * Offset information element
  1035. */
  1036. if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1037. struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
  1038. bool ht40plus;
  1039. *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
  1040. *pos++ = sizeof(*sec_chan_ie);
  1041. sec_chan_ie = (void *)pos;
  1042. ht40plus = cfg80211_get_chandef_type(chandef) ==
  1043. NL80211_CHAN_HT40PLUS;
  1044. sec_chan_ie->sec_chan_offs = ht40plus ?
  1045. IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
  1046. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1047. pos += sizeof(*sec_chan_ie);
  1048. extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
  1049. }
  1050. /* just set the values to 0, this is a template */
  1051. iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
  1052. skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
  1053. WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
  1054. 0, 0, !sta->sta.tdls_initiator,
  1055. extra_ies, extra_ies_len,
  1056. oper_class, chandef);
  1057. if (!skb)
  1058. return NULL;
  1059. skb = ieee80211_build_data_template(sdata, skb, 0);
  1060. if (IS_ERR(skb)) {
  1061. tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
  1062. return NULL;
  1063. }
  1064. if (ch_sw_tm_ie_offset) {
  1065. const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
  1066. if (!tm_ie) {
  1067. tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
  1068. dev_kfree_skb_any(skb);
  1069. return NULL;
  1070. }
  1071. *ch_sw_tm_ie_offset = tm_ie - skb->data;
  1072. }
  1073. tdls_dbg(sdata,
  1074. "TDLS channel switch request template for %pM ch %d width %d\n",
  1075. sta->sta.addr, chandef->chan->center_freq, chandef->width);
  1076. return skb;
  1077. }
  1078. int
  1079. ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
  1080. const u8 *addr, u8 oper_class,
  1081. struct cfg80211_chan_def *chandef)
  1082. {
  1083. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1084. struct ieee80211_local *local = sdata->local;
  1085. struct sta_info *sta;
  1086. struct sk_buff *skb = NULL;
  1087. u32 ch_sw_tm_ie;
  1088. int ret;
  1089. mutex_lock(&local->sta_mtx);
  1090. sta = sta_info_get(sdata, addr);
  1091. if (!sta) {
  1092. tdls_dbg(sdata,
  1093. "Invalid TDLS peer %pM for channel switch request\n",
  1094. addr);
  1095. ret = -ENOENT;
  1096. goto out;
  1097. }
  1098. if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
  1099. tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
  1100. addr);
  1101. ret = -ENOTSUPP;
  1102. goto out;
  1103. }
  1104. skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
  1105. &ch_sw_tm_ie);
  1106. if (!skb) {
  1107. ret = -ENOENT;
  1108. goto out;
  1109. }
  1110. ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
  1111. chandef, skb, ch_sw_tm_ie);
  1112. if (!ret)
  1113. set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
  1114. out:
  1115. mutex_unlock(&local->sta_mtx);
  1116. dev_kfree_skb_any(skb);
  1117. return ret;
  1118. }
  1119. void
  1120. ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
  1121. struct net_device *dev,
  1122. const u8 *addr)
  1123. {
  1124. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1125. struct ieee80211_local *local = sdata->local;
  1126. struct sta_info *sta;
  1127. mutex_lock(&local->sta_mtx);
  1128. sta = sta_info_get(sdata, addr);
  1129. if (!sta) {
  1130. tdls_dbg(sdata,
  1131. "Invalid TDLS peer %pM for channel switch cancel\n",
  1132. addr);
  1133. goto out;
  1134. }
  1135. if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
  1136. tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
  1137. addr);
  1138. goto out;
  1139. }
  1140. drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
  1141. clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
  1142. out:
  1143. mutex_unlock(&local->sta_mtx);
  1144. }
  1145. static struct sk_buff *
  1146. ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
  1147. u32 *ch_sw_tm_ie_offset)
  1148. {
  1149. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1150. struct sk_buff *skb;
  1151. u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
  1152. /* initial timing are always zero in the template */
  1153. iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
  1154. skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
  1155. WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
  1156. 0, 0, !sta->sta.tdls_initiator,
  1157. extra_ies, sizeof(extra_ies), 0, NULL);
  1158. if (!skb)
  1159. return NULL;
  1160. skb = ieee80211_build_data_template(sdata, skb, 0);
  1161. if (IS_ERR(skb)) {
  1162. tdls_dbg(sdata,
  1163. "Failed building TDLS channel switch resp frame\n");
  1164. return NULL;
  1165. }
  1166. if (ch_sw_tm_ie_offset) {
  1167. const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
  1168. if (!tm_ie) {
  1169. tdls_dbg(sdata,
  1170. "No switch timing IE in TDLS switch resp\n");
  1171. dev_kfree_skb_any(skb);
  1172. return NULL;
  1173. }
  1174. *ch_sw_tm_ie_offset = tm_ie - skb->data;
  1175. }
  1176. tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
  1177. sta->sta.addr);
  1178. return skb;
  1179. }
  1180. static int
  1181. ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
  1182. struct sk_buff *skb)
  1183. {
  1184. struct ieee80211_local *local = sdata->local;
  1185. struct ieee802_11_elems elems;
  1186. struct sta_info *sta;
  1187. struct ieee80211_tdls_data *tf = (void *)skb->data;
  1188. bool local_initiator;
  1189. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  1190. int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
  1191. struct ieee80211_tdls_ch_sw_params params = {};
  1192. int ret;
  1193. params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
  1194. params.timestamp = rx_status->device_timestamp;
  1195. if (skb->len < baselen) {
  1196. tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
  1197. skb->len);
  1198. return -EINVAL;
  1199. }
  1200. mutex_lock(&local->sta_mtx);
  1201. sta = sta_info_get(sdata, tf->sa);
  1202. if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
  1203. tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
  1204. tf->sa);
  1205. ret = -EINVAL;
  1206. goto out;
  1207. }
  1208. params.sta = &sta->sta;
  1209. params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
  1210. if (params.status != 0) {
  1211. ret = 0;
  1212. goto call_drv;
  1213. }
  1214. ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
  1215. skb->len - baselen, false, &elems);
  1216. if (elems.parse_error) {
  1217. tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
  1218. ret = -EINVAL;
  1219. goto out;
  1220. }
  1221. if (!elems.ch_sw_timing || !elems.lnk_id) {
  1222. tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
  1223. ret = -EINVAL;
  1224. goto out;
  1225. }
  1226. /* validate the initiator is set correctly */
  1227. local_initiator =
  1228. !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
  1229. if (local_initiator == sta->sta.tdls_initiator) {
  1230. tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
  1231. ret = -EINVAL;
  1232. goto out;
  1233. }
  1234. params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
  1235. params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
  1236. params.tmpl_skb =
  1237. ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
  1238. if (!params.tmpl_skb) {
  1239. ret = -ENOENT;
  1240. goto out;
  1241. }
  1242. call_drv:
  1243. drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
  1244. tdls_dbg(sdata,
  1245. "TDLS channel switch response received from %pM status %d\n",
  1246. tf->sa, params.status);
  1247. out:
  1248. mutex_unlock(&local->sta_mtx);
  1249. dev_kfree_skb_any(params.tmpl_skb);
  1250. return ret;
  1251. }
  1252. static int
  1253. ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
  1254. struct sk_buff *skb)
  1255. {
  1256. struct ieee80211_local *local = sdata->local;
  1257. struct ieee802_11_elems elems;
  1258. struct cfg80211_chan_def chandef;
  1259. struct ieee80211_channel *chan;
  1260. enum nl80211_channel_type chan_type;
  1261. int freq;
  1262. u8 target_channel, oper_class;
  1263. bool local_initiator;
  1264. struct sta_info *sta;
  1265. enum ieee80211_band band;
  1266. struct ieee80211_tdls_data *tf = (void *)skb->data;
  1267. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  1268. int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
  1269. struct ieee80211_tdls_ch_sw_params params = {};
  1270. int ret = 0;
  1271. params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
  1272. params.timestamp = rx_status->device_timestamp;
  1273. if (skb->len < baselen) {
  1274. tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
  1275. skb->len);
  1276. return -EINVAL;
  1277. }
  1278. target_channel = tf->u.chan_switch_req.target_channel;
  1279. oper_class = tf->u.chan_switch_req.oper_class;
  1280. /*
  1281. * We can't easily infer the channel band. The operating class is
  1282. * ambiguous - there are multiple tables (US/Europe/JP/Global). The
  1283. * solution here is to treat channels with number >14 as 5GHz ones,
  1284. * and specifically check for the (oper_class, channel) combinations
  1285. * where this doesn't hold. These are thankfully unique according to
  1286. * IEEE802.11-2012.
  1287. * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
  1288. * valid here.
  1289. */
  1290. if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
  1291. oper_class == 4 || oper_class == 5 || oper_class == 6) &&
  1292. target_channel < 14)
  1293. band = IEEE80211_BAND_5GHZ;
  1294. else
  1295. band = target_channel < 14 ? IEEE80211_BAND_2GHZ :
  1296. IEEE80211_BAND_5GHZ;
  1297. freq = ieee80211_channel_to_frequency(target_channel, band);
  1298. if (freq == 0) {
  1299. tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
  1300. target_channel);
  1301. return -EINVAL;
  1302. }
  1303. chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
  1304. if (!chan) {
  1305. tdls_dbg(sdata,
  1306. "Unsupported channel for TDLS chan switch: %d\n",
  1307. target_channel);
  1308. return -EINVAL;
  1309. }
  1310. ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
  1311. skb->len - baselen, false, &elems);
  1312. if (elems.parse_error) {
  1313. tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
  1314. return -EINVAL;
  1315. }
  1316. if (!elems.ch_sw_timing || !elems.lnk_id) {
  1317. tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
  1318. return -EINVAL;
  1319. }
  1320. mutex_lock(&local->sta_mtx);
  1321. sta = sta_info_get(sdata, tf->sa);
  1322. if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
  1323. tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
  1324. tf->sa);
  1325. ret = -EINVAL;
  1326. goto out;
  1327. }
  1328. params.sta = &sta->sta;
  1329. /* validate the initiator is set correctly */
  1330. local_initiator =
  1331. !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
  1332. if (local_initiator == sta->sta.tdls_initiator) {
  1333. tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
  1334. ret = -EINVAL;
  1335. goto out;
  1336. }
  1337. if (!sta->sta.ht_cap.ht_supported) {
  1338. chan_type = NL80211_CHAN_NO_HT;
  1339. } else if (!elems.sec_chan_offs) {
  1340. chan_type = NL80211_CHAN_HT20;
  1341. } else {
  1342. switch (elems.sec_chan_offs->sec_chan_offs) {
  1343. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1344. chan_type = NL80211_CHAN_HT40PLUS;
  1345. break;
  1346. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1347. chan_type = NL80211_CHAN_HT40MINUS;
  1348. break;
  1349. default:
  1350. chan_type = NL80211_CHAN_HT20;
  1351. break;
  1352. }
  1353. }
  1354. cfg80211_chandef_create(&chandef, chan, chan_type);
  1355. params.chandef = &chandef;
  1356. params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
  1357. params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
  1358. params.tmpl_skb =
  1359. ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
  1360. &params.ch_sw_tm_ie);
  1361. if (!params.tmpl_skb) {
  1362. ret = -ENOENT;
  1363. goto out;
  1364. }
  1365. drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
  1366. tdls_dbg(sdata,
  1367. "TDLS ch switch request received from %pM ch %d width %d\n",
  1368. tf->sa, params.chandef->chan->center_freq,
  1369. params.chandef->width);
  1370. out:
  1371. mutex_unlock(&local->sta_mtx);
  1372. dev_kfree_skb_any(params.tmpl_skb);
  1373. return ret;
  1374. }
  1375. void ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
  1376. struct sk_buff *skb)
  1377. {
  1378. struct ieee80211_tdls_data *tf = (void *)skb->data;
  1379. struct wiphy *wiphy = sdata->local->hw.wiphy;
  1380. /* make sure the driver supports it */
  1381. if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
  1382. return;
  1383. /* we want to access the entire packet */
  1384. if (skb_linearize(skb))
  1385. return;
  1386. /*
  1387. * The packet/size was already validated by mac80211 Rx path, only look
  1388. * at the action type.
  1389. */
  1390. switch (tf->action_code) {
  1391. case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  1392. ieee80211_process_tdls_channel_switch_req(sdata, skb);
  1393. break;
  1394. case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  1395. ieee80211_process_tdls_channel_switch_resp(sdata, skb);
  1396. break;
  1397. default:
  1398. WARN_ON_ONCE(1);
  1399. return;
  1400. }
  1401. }