tdls.c 54 KB

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