tdls.c 48 KB

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