tdls.c 24 KB

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