tdls.c 44 KB

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