tdls.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * mac80211 TDLS handling code
  3. *
  4. * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright 2014, Intel Corporation
  6. * Copyright 2014 Intel Mobile Communications GmbH
  7. *
  8. * This file is GPLv2 as found in COPYING.
  9. */
  10. #include <linux/ieee80211.h>
  11. #include <linux/log2.h>
  12. #include <net/cfg80211.h>
  13. #include "ieee80211_i.h"
  14. #include "driver-ops.h"
  15. /* give usermode some time for retries in setting up the TDLS session */
  16. #define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
  17. void ieee80211_tdls_peer_del_work(struct work_struct *wk)
  18. {
  19. struct ieee80211_sub_if_data *sdata;
  20. struct ieee80211_local *local;
  21. sdata = container_of(wk, struct ieee80211_sub_if_data,
  22. u.mgd.tdls_peer_del_work.work);
  23. local = sdata->local;
  24. mutex_lock(&local->mtx);
  25. if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
  26. tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->u.mgd.tdls_peer);
  27. sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
  28. eth_zero_addr(sdata->u.mgd.tdls_peer);
  29. }
  30. mutex_unlock(&local->mtx);
  31. }
  32. static void ieee80211_tdls_add_ext_capab(struct sk_buff *skb)
  33. {
  34. u8 *pos = (void *)skb_put(skb, 7);
  35. *pos++ = WLAN_EID_EXT_CAPABILITY;
  36. *pos++ = 5; /* len */
  37. *pos++ = 0x0;
  38. *pos++ = 0x0;
  39. *pos++ = 0x0;
  40. *pos++ = 0x0;
  41. *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
  42. }
  43. static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
  44. u16 status_code)
  45. {
  46. struct ieee80211_local *local = sdata->local;
  47. u16 capab;
  48. /* The capability will be 0 when sending a failure code */
  49. if (status_code != 0)
  50. return 0;
  51. capab = 0;
  52. if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
  53. return capab;
  54. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  55. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  56. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  57. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  58. return capab;
  59. }
  60. static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
  61. struct sk_buff *skb, const u8 *peer,
  62. bool initiator)
  63. {
  64. struct ieee80211_tdls_lnkie *lnkid;
  65. const u8 *init_addr, *rsp_addr;
  66. if (initiator) {
  67. init_addr = sdata->vif.addr;
  68. rsp_addr = peer;
  69. } else {
  70. init_addr = peer;
  71. rsp_addr = sdata->vif.addr;
  72. }
  73. lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
  74. lnkid->ie_type = WLAN_EID_LINK_ID;
  75. lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
  76. memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  77. memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
  78. memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
  79. }
  80. /* translate numbering in the WMM parameter IE to the mac80211 notation */
  81. static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
  82. {
  83. switch (ac) {
  84. default:
  85. WARN_ON_ONCE(1);
  86. case 0:
  87. return IEEE80211_AC_BE;
  88. case 1:
  89. return IEEE80211_AC_BK;
  90. case 2:
  91. return IEEE80211_AC_VI;
  92. case 3:
  93. return IEEE80211_AC_VO;
  94. }
  95. }
  96. static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
  97. {
  98. u8 ret;
  99. ret = aifsn & 0x0f;
  100. if (acm)
  101. ret |= 0x10;
  102. ret |= (aci << 5) & 0x60;
  103. return ret;
  104. }
  105. static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
  106. {
  107. return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
  108. ((ilog2(cw_max + 1) << 0x4) & 0xf0);
  109. }
  110. static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
  111. struct sk_buff *skb)
  112. {
  113. struct ieee80211_wmm_param_ie *wmm;
  114. struct ieee80211_tx_queue_params *txq;
  115. int i;
  116. wmm = (void *)skb_put(skb, sizeof(*wmm));
  117. memset(wmm, 0, sizeof(*wmm));
  118. wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
  119. wmm->len = sizeof(*wmm) - 2;
  120. wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
  121. wmm->oui[1] = 0x50;
  122. wmm->oui[2] = 0xf2;
  123. wmm->oui_type = 2; /* WME */
  124. wmm->oui_subtype = 1; /* WME param */
  125. wmm->version = 1; /* WME ver */
  126. wmm->qos_info = 0; /* U-APSD not in use */
  127. /*
  128. * Use the EDCA parameters defined for the BSS, or default if the AP
  129. * doesn't support it, as mandated by 802.11-2012 section 10.22.4
  130. */
  131. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  132. txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
  133. wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
  134. txq->acm, i);
  135. wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
  136. wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
  137. }
  138. }
  139. static void
  140. ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
  141. struct sk_buff *skb, const u8 *peer,
  142. u8 action_code, bool initiator,
  143. const u8 *extra_ies, size_t extra_ies_len)
  144. {
  145. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  146. struct ieee80211_local *local = sdata->local;
  147. struct ieee80211_supported_band *sband;
  148. struct ieee80211_sta_ht_cap ht_cap;
  149. struct sta_info *sta = NULL;
  150. size_t offset = 0, noffset;
  151. u8 *pos;
  152. rcu_read_lock();
  153. /* we should have the peer STA if we're already responding */
  154. if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
  155. sta = sta_info_get(sdata, peer);
  156. if (WARN_ON_ONCE(!sta)) {
  157. rcu_read_unlock();
  158. return;
  159. }
  160. }
  161. ieee80211_add_srates_ie(sdata, skb, false, band);
  162. ieee80211_add_ext_srates_ie(sdata, skb, false, band);
  163. /* add any custom IEs that go before Extended Capabilities */
  164. if (extra_ies_len) {
  165. static const u8 before_ext_cap[] = {
  166. WLAN_EID_SUPP_RATES,
  167. WLAN_EID_COUNTRY,
  168. WLAN_EID_EXT_SUPP_RATES,
  169. WLAN_EID_SUPPORTED_CHANNELS,
  170. WLAN_EID_RSN,
  171. };
  172. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  173. before_ext_cap,
  174. ARRAY_SIZE(before_ext_cap),
  175. offset);
  176. pos = skb_put(skb, noffset - offset);
  177. memcpy(pos, extra_ies + offset, noffset - offset);
  178. offset = noffset;
  179. }
  180. ieee80211_tdls_add_ext_capab(skb);
  181. /* add the QoS element if we support it */
  182. if (local->hw.queues >= IEEE80211_NUM_ACS &&
  183. action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
  184. ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
  185. /* add any custom IEs that go before HT capabilities */
  186. if (extra_ies_len) {
  187. static const u8 before_ht_cap[] = {
  188. WLAN_EID_SUPP_RATES,
  189. WLAN_EID_COUNTRY,
  190. WLAN_EID_EXT_SUPP_RATES,
  191. WLAN_EID_SUPPORTED_CHANNELS,
  192. WLAN_EID_RSN,
  193. WLAN_EID_EXT_CAPABILITY,
  194. WLAN_EID_QOS_CAPA,
  195. WLAN_EID_FAST_BSS_TRANSITION,
  196. WLAN_EID_TIMEOUT_INTERVAL,
  197. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  198. };
  199. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  200. before_ht_cap,
  201. ARRAY_SIZE(before_ht_cap),
  202. offset);
  203. pos = skb_put(skb, noffset - offset);
  204. memcpy(pos, extra_ies + offset, noffset - offset);
  205. offset = noffset;
  206. }
  207. /*
  208. * with TDLS we can switch channels, and HT-caps are not necessarily
  209. * the same on all bands. The specification limits the setup to a
  210. * single HT-cap, so use the current band for now.
  211. */
  212. sband = local->hw.wiphy->bands[band];
  213. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  214. if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
  215. action_code == WLAN_TDLS_SETUP_RESPONSE) &&
  216. ht_cap.ht_supported && (!sta || sta->sta.ht_cap.ht_supported)) {
  217. if (action_code == WLAN_TDLS_SETUP_REQUEST) {
  218. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  219. /* disable SMPS in TDLS initiator */
  220. ht_cap.cap |= (WLAN_HT_CAP_SM_PS_DISABLED
  221. << IEEE80211_HT_CAP_SM_PS_SHIFT);
  222. } else {
  223. /* disable SMPS in TDLS responder */
  224. sta->sta.ht_cap.cap |=
  225. (WLAN_HT_CAP_SM_PS_DISABLED
  226. << IEEE80211_HT_CAP_SM_PS_SHIFT);
  227. /* the peer caps are already intersected with our own */
  228. memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
  229. }
  230. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  231. ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  232. }
  233. rcu_read_unlock();
  234. /* add any remaining IEs */
  235. if (extra_ies_len) {
  236. noffset = extra_ies_len;
  237. pos = skb_put(skb, noffset - offset);
  238. memcpy(pos, extra_ies + offset, noffset - offset);
  239. }
  240. ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  241. }
  242. static void
  243. ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
  244. struct sk_buff *skb, const u8 *peer,
  245. bool initiator, const u8 *extra_ies,
  246. size_t extra_ies_len)
  247. {
  248. struct ieee80211_local *local = sdata->local;
  249. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  250. size_t offset = 0, noffset;
  251. struct sta_info *sta, *ap_sta;
  252. u8 *pos;
  253. rcu_read_lock();
  254. sta = sta_info_get(sdata, peer);
  255. ap_sta = sta_info_get(sdata, ifmgd->bssid);
  256. if (WARN_ON_ONCE(!sta || !ap_sta)) {
  257. rcu_read_unlock();
  258. return;
  259. }
  260. /* add any custom IEs that go before the QoS IE */
  261. if (extra_ies_len) {
  262. static const u8 before_qos[] = {
  263. WLAN_EID_RSN,
  264. };
  265. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  266. before_qos,
  267. ARRAY_SIZE(before_qos),
  268. offset);
  269. pos = skb_put(skb, noffset - offset);
  270. memcpy(pos, extra_ies + offset, noffset - offset);
  271. offset = noffset;
  272. }
  273. /* add the QoS param IE if both the peer and we support it */
  274. if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
  275. ieee80211_tdls_add_wmm_param_ie(sdata, skb);
  276. /* add any custom IEs that go before HT operation */
  277. if (extra_ies_len) {
  278. static const u8 before_ht_op[] = {
  279. WLAN_EID_RSN,
  280. WLAN_EID_QOS_CAPA,
  281. WLAN_EID_FAST_BSS_TRANSITION,
  282. WLAN_EID_TIMEOUT_INTERVAL,
  283. };
  284. noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  285. before_ht_op,
  286. ARRAY_SIZE(before_ht_op),
  287. offset);
  288. pos = skb_put(skb, noffset - offset);
  289. memcpy(pos, extra_ies + offset, noffset - offset);
  290. offset = noffset;
  291. }
  292. /* if HT support is only added in TDLS, we need an HT-operation IE */
  293. if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
  294. struct ieee80211_chanctx_conf *chanctx_conf =
  295. rcu_dereference(sdata->vif.chanctx_conf);
  296. if (!WARN_ON(!chanctx_conf)) {
  297. pos = skb_put(skb, 2 +
  298. sizeof(struct ieee80211_ht_operation));
  299. /* send an empty HT operation IE */
  300. ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
  301. &chanctx_conf->def, 0);
  302. }
  303. }
  304. rcu_read_unlock();
  305. /* add any remaining IEs */
  306. if (extra_ies_len) {
  307. noffset = extra_ies_len;
  308. pos = skb_put(skb, noffset - offset);
  309. memcpy(pos, extra_ies + offset, noffset - offset);
  310. }
  311. ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  312. }
  313. static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
  314. struct sk_buff *skb, const u8 *peer,
  315. u8 action_code, u16 status_code,
  316. bool initiator, const u8 *extra_ies,
  317. size_t extra_ies_len)
  318. {
  319. switch (action_code) {
  320. case WLAN_TDLS_SETUP_REQUEST:
  321. case WLAN_TDLS_SETUP_RESPONSE:
  322. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  323. if (status_code == 0)
  324. ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
  325. action_code,
  326. initiator,
  327. extra_ies,
  328. extra_ies_len);
  329. break;
  330. case WLAN_TDLS_SETUP_CONFIRM:
  331. if (status_code == 0)
  332. ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
  333. initiator, extra_ies,
  334. extra_ies_len);
  335. break;
  336. case WLAN_TDLS_TEARDOWN:
  337. case WLAN_TDLS_DISCOVERY_REQUEST:
  338. if (extra_ies_len)
  339. memcpy(skb_put(skb, extra_ies_len), extra_ies,
  340. extra_ies_len);
  341. if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
  342. ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  343. break;
  344. }
  345. }
  346. static int
  347. ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
  348. const u8 *peer, u8 action_code, u8 dialog_token,
  349. u16 status_code, struct sk_buff *skb)
  350. {
  351. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  352. struct ieee80211_tdls_data *tf;
  353. tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
  354. memcpy(tf->da, peer, ETH_ALEN);
  355. memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
  356. tf->ether_type = cpu_to_be16(ETH_P_TDLS);
  357. tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
  358. /* network header is after the ethernet header */
  359. skb_set_network_header(skb, ETH_HLEN);
  360. switch (action_code) {
  361. case WLAN_TDLS_SETUP_REQUEST:
  362. tf->category = WLAN_CATEGORY_TDLS;
  363. tf->action_code = WLAN_TDLS_SETUP_REQUEST;
  364. skb_put(skb, sizeof(tf->u.setup_req));
  365. tf->u.setup_req.dialog_token = dialog_token;
  366. tf->u.setup_req.capability =
  367. cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
  368. status_code));
  369. break;
  370. case WLAN_TDLS_SETUP_RESPONSE:
  371. tf->category = WLAN_CATEGORY_TDLS;
  372. tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
  373. skb_put(skb, sizeof(tf->u.setup_resp));
  374. tf->u.setup_resp.status_code = cpu_to_le16(status_code);
  375. tf->u.setup_resp.dialog_token = dialog_token;
  376. tf->u.setup_resp.capability =
  377. cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
  378. status_code));
  379. break;
  380. case WLAN_TDLS_SETUP_CONFIRM:
  381. tf->category = WLAN_CATEGORY_TDLS;
  382. tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
  383. skb_put(skb, sizeof(tf->u.setup_cfm));
  384. tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
  385. tf->u.setup_cfm.dialog_token = dialog_token;
  386. break;
  387. case WLAN_TDLS_TEARDOWN:
  388. tf->category = WLAN_CATEGORY_TDLS;
  389. tf->action_code = WLAN_TDLS_TEARDOWN;
  390. skb_put(skb, sizeof(tf->u.teardown));
  391. tf->u.teardown.reason_code = cpu_to_le16(status_code);
  392. break;
  393. case WLAN_TDLS_DISCOVERY_REQUEST:
  394. tf->category = WLAN_CATEGORY_TDLS;
  395. tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
  396. skb_put(skb, sizeof(tf->u.discover_req));
  397. tf->u.discover_req.dialog_token = dialog_token;
  398. break;
  399. default:
  400. return -EINVAL;
  401. }
  402. return 0;
  403. }
  404. static int
  405. ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
  406. const u8 *peer, u8 action_code, u8 dialog_token,
  407. u16 status_code, struct sk_buff *skb)
  408. {
  409. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  410. struct ieee80211_mgmt *mgmt;
  411. mgmt = (void *)skb_put(skb, 24);
  412. memset(mgmt, 0, 24);
  413. memcpy(mgmt->da, peer, ETH_ALEN);
  414. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  415. memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  416. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  417. IEEE80211_STYPE_ACTION);
  418. switch (action_code) {
  419. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  420. skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
  421. mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
  422. mgmt->u.action.u.tdls_discover_resp.action_code =
  423. WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
  424. mgmt->u.action.u.tdls_discover_resp.dialog_token =
  425. dialog_token;
  426. mgmt->u.action.u.tdls_discover_resp.capability =
  427. cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
  428. status_code));
  429. break;
  430. default:
  431. return -EINVAL;
  432. }
  433. return 0;
  434. }
  435. static int
  436. ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
  437. const u8 *peer, u8 action_code,
  438. u8 dialog_token, u16 status_code,
  439. u32 peer_capability, bool initiator,
  440. const u8 *extra_ies, size_t extra_ies_len)
  441. {
  442. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  443. struct ieee80211_local *local = sdata->local;
  444. struct sk_buff *skb = NULL;
  445. bool send_direct;
  446. struct sta_info *sta;
  447. int ret;
  448. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  449. max(sizeof(struct ieee80211_mgmt),
  450. sizeof(struct ieee80211_tdls_data)) +
  451. 50 + /* supported rates */
  452. 7 + /* ext capab */
  453. 26 + /* max(WMM-info, WMM-param) */
  454. 2 + max(sizeof(struct ieee80211_ht_cap),
  455. sizeof(struct ieee80211_ht_operation)) +
  456. extra_ies_len +
  457. sizeof(struct ieee80211_tdls_lnkie));
  458. if (!skb)
  459. return -ENOMEM;
  460. skb_reserve(skb, local->hw.extra_tx_headroom);
  461. switch (action_code) {
  462. case WLAN_TDLS_SETUP_REQUEST:
  463. case WLAN_TDLS_SETUP_RESPONSE:
  464. case WLAN_TDLS_SETUP_CONFIRM:
  465. case WLAN_TDLS_TEARDOWN:
  466. case WLAN_TDLS_DISCOVERY_REQUEST:
  467. ret = ieee80211_prep_tdls_encap_data(wiphy, dev, peer,
  468. action_code, dialog_token,
  469. status_code, skb);
  470. send_direct = false;
  471. break;
  472. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  473. ret = ieee80211_prep_tdls_direct(wiphy, dev, peer, action_code,
  474. dialog_token, status_code,
  475. skb);
  476. send_direct = true;
  477. break;
  478. default:
  479. ret = -ENOTSUPP;
  480. break;
  481. }
  482. if (ret < 0)
  483. goto fail;
  484. rcu_read_lock();
  485. sta = sta_info_get(sdata, peer);
  486. /* infer the initiator if we can, to support old userspace */
  487. switch (action_code) {
  488. case WLAN_TDLS_SETUP_REQUEST:
  489. if (sta)
  490. set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
  491. /* fall-through */
  492. case WLAN_TDLS_SETUP_CONFIRM:
  493. case WLAN_TDLS_DISCOVERY_REQUEST:
  494. initiator = true;
  495. break;
  496. case WLAN_TDLS_SETUP_RESPONSE:
  497. /*
  498. * In some testing scenarios, we send a request and response.
  499. * Make the last packet sent take effect for the initiator
  500. * value.
  501. */
  502. if (sta)
  503. clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
  504. /* fall-through */
  505. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  506. initiator = false;
  507. break;
  508. case WLAN_TDLS_TEARDOWN:
  509. /* any value is ok */
  510. break;
  511. default:
  512. ret = -ENOTSUPP;
  513. break;
  514. }
  515. if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
  516. initiator = true;
  517. rcu_read_unlock();
  518. if (ret < 0)
  519. goto fail;
  520. ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
  521. initiator, extra_ies, extra_ies_len);
  522. if (send_direct) {
  523. ieee80211_tx_skb(sdata, skb);
  524. return 0;
  525. }
  526. /*
  527. * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
  528. * we should default to AC_VI.
  529. */
  530. switch (action_code) {
  531. case WLAN_TDLS_SETUP_REQUEST:
  532. case WLAN_TDLS_SETUP_RESPONSE:
  533. skb_set_queue_mapping(skb, IEEE80211_AC_BK);
  534. skb->priority = 2;
  535. break;
  536. default:
  537. skb_set_queue_mapping(skb, IEEE80211_AC_VI);
  538. skb->priority = 5;
  539. break;
  540. }
  541. /* disable bottom halves when entering the Tx path */
  542. local_bh_disable();
  543. ret = ieee80211_subif_start_xmit(skb, dev);
  544. local_bh_enable();
  545. return ret;
  546. fail:
  547. dev_kfree_skb(skb);
  548. return ret;
  549. }
  550. static int
  551. ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
  552. const u8 *peer, u8 action_code, u8 dialog_token,
  553. u16 status_code, u32 peer_capability, bool initiator,
  554. const u8 *extra_ies, size_t extra_ies_len)
  555. {
  556. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  557. struct ieee80211_local *local = sdata->local;
  558. int ret;
  559. mutex_lock(&local->mtx);
  560. /* we don't support concurrent TDLS peer setups */
  561. if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
  562. !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
  563. ret = -EBUSY;
  564. goto exit;
  565. }
  566. /*
  567. * make sure we have a STA representing the peer so we drop or buffer
  568. * non-TDLS-setup frames to the peer. We can't send other packets
  569. * during setup through the AP path.
  570. * Allow error packets to be sent - sometimes we don't even add a STA
  571. * before failing the setup.
  572. */
  573. if (status_code == 0) {
  574. rcu_read_lock();
  575. if (!sta_info_get(sdata, peer)) {
  576. rcu_read_unlock();
  577. ret = -ENOLINK;
  578. goto exit;
  579. }
  580. rcu_read_unlock();
  581. }
  582. ieee80211_flush_queues(local, sdata);
  583. ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
  584. dialog_token, status_code,
  585. peer_capability, initiator,
  586. extra_ies, extra_ies_len);
  587. if (ret < 0)
  588. goto exit;
  589. memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
  590. ieee80211_queue_delayed_work(&sdata->local->hw,
  591. &sdata->u.mgd.tdls_peer_del_work,
  592. TDLS_PEER_SETUP_TIMEOUT);
  593. exit:
  594. mutex_unlock(&local->mtx);
  595. return ret;
  596. }
  597. static int
  598. ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
  599. const u8 *peer, u8 action_code, u8 dialog_token,
  600. u16 status_code, u32 peer_capability,
  601. bool initiator, const u8 *extra_ies,
  602. size_t extra_ies_len)
  603. {
  604. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  605. struct ieee80211_local *local = sdata->local;
  606. struct sta_info *sta;
  607. int ret;
  608. /*
  609. * No packets can be transmitted to the peer via the AP during setup -
  610. * the STA is set as a TDLS peer, but is not authorized.
  611. * During teardown, we prevent direct transmissions by stopping the
  612. * queues and flushing all direct packets.
  613. */
  614. ieee80211_stop_vif_queues(local, sdata,
  615. IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
  616. ieee80211_flush_queues(local, sdata);
  617. ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
  618. dialog_token, status_code,
  619. peer_capability, initiator,
  620. extra_ies, extra_ies_len);
  621. if (ret < 0)
  622. sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
  623. ret);
  624. /*
  625. * Remove the STA AUTH flag to force further traffic through the AP. If
  626. * the STA was unreachable, it was already removed.
  627. */
  628. rcu_read_lock();
  629. sta = sta_info_get(sdata, peer);
  630. if (sta)
  631. clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
  632. rcu_read_unlock();
  633. ieee80211_wake_vif_queues(local, sdata,
  634. IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
  635. return 0;
  636. }
  637. int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
  638. const u8 *peer, u8 action_code, u8 dialog_token,
  639. u16 status_code, u32 peer_capability,
  640. bool initiator, const u8 *extra_ies,
  641. size_t extra_ies_len)
  642. {
  643. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  644. int ret;
  645. if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
  646. return -ENOTSUPP;
  647. /* make sure we are in managed mode, and associated */
  648. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  649. !sdata->u.mgd.associated)
  650. return -EINVAL;
  651. switch (action_code) {
  652. case WLAN_TDLS_SETUP_REQUEST:
  653. case WLAN_TDLS_SETUP_RESPONSE:
  654. ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
  655. dialog_token, status_code,
  656. peer_capability, initiator,
  657. extra_ies, extra_ies_len);
  658. break;
  659. case WLAN_TDLS_TEARDOWN:
  660. ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
  661. action_code, dialog_token,
  662. status_code,
  663. peer_capability, initiator,
  664. extra_ies, extra_ies_len);
  665. break;
  666. case WLAN_TDLS_DISCOVERY_REQUEST:
  667. /*
  668. * Protect the discovery so we can hear the TDLS discovery
  669. * response frame. It is transmitted directly and not buffered
  670. * by the AP.
  671. */
  672. drv_mgd_protect_tdls_discover(sdata->local, sdata);
  673. /* fall-through */
  674. case WLAN_TDLS_SETUP_CONFIRM:
  675. case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  676. /* no special handling */
  677. ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
  678. action_code,
  679. dialog_token,
  680. status_code,
  681. peer_capability,
  682. initiator, extra_ies,
  683. extra_ies_len);
  684. break;
  685. default:
  686. ret = -EOPNOTSUPP;
  687. break;
  688. }
  689. tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
  690. action_code, peer, ret);
  691. return ret;
  692. }
  693. int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
  694. const u8 *peer, enum nl80211_tdls_operation oper)
  695. {
  696. struct sta_info *sta;
  697. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  698. struct ieee80211_local *local = sdata->local;
  699. int ret;
  700. if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
  701. return -ENOTSUPP;
  702. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  703. return -EINVAL;
  704. switch (oper) {
  705. case NL80211_TDLS_ENABLE_LINK:
  706. case NL80211_TDLS_DISABLE_LINK:
  707. break;
  708. case NL80211_TDLS_TEARDOWN:
  709. case NL80211_TDLS_SETUP:
  710. case NL80211_TDLS_DISCOVERY_REQ:
  711. /* We don't support in-driver setup/teardown/discovery */
  712. return -ENOTSUPP;
  713. }
  714. mutex_lock(&local->mtx);
  715. tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
  716. switch (oper) {
  717. case NL80211_TDLS_ENABLE_LINK:
  718. rcu_read_lock();
  719. sta = sta_info_get(sdata, peer);
  720. if (!sta) {
  721. rcu_read_unlock();
  722. ret = -ENOLINK;
  723. break;
  724. }
  725. set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
  726. rcu_read_unlock();
  727. WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
  728. !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
  729. ret = 0;
  730. break;
  731. case NL80211_TDLS_DISABLE_LINK:
  732. /*
  733. * The teardown message in ieee80211_tdls_mgmt_teardown() was
  734. * created while the queues were stopped, so it might still be
  735. * pending. Before flushing the queues we need to be sure the
  736. * message is handled by the tasklet handling pending messages,
  737. * otherwise we might start destroying the station before
  738. * sending the teardown packet.
  739. * Note that this only forces the tasklet to flush pendings -
  740. * not to stop the tasklet from rescheduling itself.
  741. */
  742. tasklet_kill(&local->tx_pending_tasklet);
  743. /* flush a potentially queued teardown packet */
  744. ieee80211_flush_queues(local, sdata);
  745. ret = sta_info_destroy_addr(sdata, peer);
  746. break;
  747. default:
  748. ret = -ENOTSUPP;
  749. break;
  750. }
  751. if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
  752. cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
  753. eth_zero_addr(sdata->u.mgd.tdls_peer);
  754. }
  755. mutex_unlock(&local->mtx);
  756. return ret;
  757. }
  758. void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
  759. enum nl80211_tdls_operation oper,
  760. u16 reason_code, gfp_t gfp)
  761. {
  762. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  763. if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
  764. sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
  765. oper);
  766. return;
  767. }
  768. cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
  769. }
  770. EXPORT_SYMBOL(ieee80211_tdls_oper_request);