tdls.c 47 KB

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