tdls.c 55 KB

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