tdls.c 54 KB

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