tdls.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023
  1. /*
  2. * wpa_supplicant - TDLS
  3. * Copyright (c) 2010-2011, Atheros Communications
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "utils/os.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/ieee802_11_common.h"
  14. #include "crypto/sha256.h"
  15. #include "crypto/crypto.h"
  16. #include "crypto/aes_wrap.h"
  17. #include "rsn_supp/wpa.h"
  18. #include "rsn_supp/wpa_ie.h"
  19. #include "rsn_supp/wpa_i.h"
  20. #include "drivers/driver.h"
  21. #include "l2_packet/l2_packet.h"
  22. #ifdef CONFIG_TDLS_TESTING
  23. #define TDLS_TESTING_LONG_FRAME BIT(0)
  24. #define TDLS_TESTING_ALT_RSN_IE BIT(1)
  25. #define TDLS_TESTING_DIFF_BSSID BIT(2)
  26. #define TDLS_TESTING_SHORT_LIFETIME BIT(3)
  27. #define TDLS_TESTING_WRONG_LIFETIME_RESP BIT(4)
  28. #define TDLS_TESTING_WRONG_LIFETIME_CONF BIT(5)
  29. #define TDLS_TESTING_LONG_LIFETIME BIT(6)
  30. #define TDLS_TESTING_CONCURRENT_INIT BIT(7)
  31. #define TDLS_TESTING_NO_TPK_EXPIRATION BIT(8)
  32. #define TDLS_TESTING_DECLINE_RESP BIT(9)
  33. #define TDLS_TESTING_IGNORE_AP_PROHIBIT BIT(10)
  34. #define TDLS_TESTING_WRONG_MIC BIT(11)
  35. unsigned int tdls_testing = 0;
  36. #endif /* CONFIG_TDLS_TESTING */
  37. #define TPK_LIFETIME 43200 /* 12 hours */
  38. #define TPK_M1_RETRY_COUNT 3
  39. #define TPK_M1_TIMEOUT 5000 /* in milliseconds */
  40. #define TPK_M2_RETRY_COUNT 10
  41. #define TPK_M2_TIMEOUT 500 /* in milliseconds */
  42. #define TDLS_MIC_LEN 16
  43. #define TDLS_TIMEOUT_LEN 4
  44. struct wpa_tdls_ftie {
  45. u8 ie_type; /* FTIE */
  46. u8 ie_len;
  47. u8 mic_ctrl[2];
  48. u8 mic[TDLS_MIC_LEN];
  49. u8 Anonce[WPA_NONCE_LEN]; /* Responder Nonce in TDLS */
  50. u8 Snonce[WPA_NONCE_LEN]; /* Initiator Nonce in TDLS */
  51. /* followed by optional elements */
  52. } STRUCT_PACKED;
  53. struct wpa_tdls_timeoutie {
  54. u8 ie_type; /* Timeout IE */
  55. u8 ie_len;
  56. u8 interval_type;
  57. u8 value[TDLS_TIMEOUT_LEN];
  58. } STRUCT_PACKED;
  59. struct wpa_tdls_lnkid {
  60. u8 ie_type; /* Link Identifier IE */
  61. u8 ie_len;
  62. u8 bssid[ETH_ALEN];
  63. u8 init_sta[ETH_ALEN];
  64. u8 resp_sta[ETH_ALEN];
  65. } STRUCT_PACKED;
  66. /* TDLS frame headers as per IEEE Std 802.11z-2010 */
  67. struct wpa_tdls_frame {
  68. u8 payloadtype; /* IEEE80211_TDLS_RFTYPE */
  69. u8 category; /* Category */
  70. u8 action; /* Action (enum tdls_frame_type) */
  71. } STRUCT_PACKED;
  72. static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs);
  73. static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx);
  74. static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer);
  75. static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
  76. struct wpa_tdls_peer *peer);
  77. static int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr,
  78. u16 reason_code);
  79. #define TDLS_MAX_IE_LEN 80
  80. #define IEEE80211_MAX_SUPP_RATES 32
  81. struct wpa_tdls_peer {
  82. struct wpa_tdls_peer *next;
  83. unsigned int reconfig_key:1;
  84. int initiator; /* whether this end was initiator for TDLS setup */
  85. u8 addr[ETH_ALEN]; /* other end MAC address */
  86. u8 inonce[WPA_NONCE_LEN]; /* Initiator Nonce */
  87. u8 rnonce[WPA_NONCE_LEN]; /* Responder Nonce */
  88. u8 rsnie_i[TDLS_MAX_IE_LEN]; /* Initiator RSN IE */
  89. size_t rsnie_i_len;
  90. u8 rsnie_p[TDLS_MAX_IE_LEN]; /* Peer RSN IE */
  91. size_t rsnie_p_len;
  92. u32 lifetime;
  93. int cipher; /* Selected cipher (WPA_CIPHER_*) */
  94. u8 dtoken;
  95. struct tpk {
  96. u8 kck[16]; /* TPK-KCK */
  97. u8 tk[16]; /* TPK-TK; assuming only CCMP will be used */
  98. } tpk;
  99. int tpk_set;
  100. int tpk_success;
  101. int tpk_in_progress;
  102. struct tpk_timer {
  103. u8 dest[ETH_ALEN];
  104. int count; /* Retry Count */
  105. int timer; /* Timeout in milliseconds */
  106. u8 action_code; /* TDLS frame type */
  107. u8 dialog_token;
  108. u16 status_code;
  109. u32 peer_capab;
  110. int buf_len; /* length of TPK message for retransmission */
  111. u8 *buf; /* buffer for TPK message */
  112. } sm_tmr;
  113. u16 capability;
  114. u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
  115. size_t supp_rates_len;
  116. struct ieee80211_ht_capabilities *ht_capabilities;
  117. struct ieee80211_vht_capabilities *vht_capabilities;
  118. u8 qos_info;
  119. u16 aid;
  120. u8 *ext_capab;
  121. size_t ext_capab_len;
  122. u8 *supp_channels;
  123. size_t supp_channels_len;
  124. u8 *supp_oper_classes;
  125. size_t supp_oper_classes_len;
  126. u8 wmm_capable;
  127. /* channel switch currently enabled */
  128. int chan_switch_enabled;
  129. };
  130. static int wpa_tdls_get_privacy(struct wpa_sm *sm)
  131. {
  132. /*
  133. * Get info needed from supplicant to check if the current BSS supports
  134. * security. Other than OPEN mode, rest are considered secured
  135. * WEP/WPA/WPA2 hence TDLS frames are processed for TPK handshake.
  136. */
  137. return sm->pairwise_cipher != WPA_CIPHER_NONE;
  138. }
  139. static u8 * wpa_add_ie(u8 *pos, const u8 *ie, size_t ie_len)
  140. {
  141. os_memcpy(pos, ie, ie_len);
  142. return pos + ie_len;
  143. }
  144. static int wpa_tdls_del_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
  145. {
  146. if (wpa_sm_set_key(sm, WPA_ALG_NONE, peer->addr,
  147. 0, 0, NULL, 0, NULL, 0) < 0) {
  148. wpa_printf(MSG_WARNING, "TDLS: Failed to delete TPK-TK from "
  149. "the driver");
  150. return -1;
  151. }
  152. return 0;
  153. }
  154. static int wpa_tdls_set_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
  155. {
  156. u8 key_len;
  157. u8 rsc[6];
  158. enum wpa_alg alg;
  159. os_memset(rsc, 0, 6);
  160. switch (peer->cipher) {
  161. case WPA_CIPHER_CCMP:
  162. alg = WPA_ALG_CCMP;
  163. key_len = 16;
  164. break;
  165. case WPA_CIPHER_NONE:
  166. wpa_printf(MSG_DEBUG, "TDLS: Pairwise Cipher Suite: "
  167. "NONE - do not use pairwise keys");
  168. return -1;
  169. default:
  170. wpa_printf(MSG_WARNING, "TDLS: Unsupported pairwise cipher %d",
  171. sm->pairwise_cipher);
  172. return -1;
  173. }
  174. if (wpa_sm_set_key(sm, alg, peer->addr, -1, 1,
  175. rsc, sizeof(rsc), peer->tpk.tk, key_len) < 0) {
  176. wpa_printf(MSG_WARNING, "TDLS: Failed to set TPK to the "
  177. "driver");
  178. return -1;
  179. }
  180. return 0;
  181. }
  182. static int wpa_tdls_send_tpk_msg(struct wpa_sm *sm, const u8 *dst,
  183. u8 action_code, u8 dialog_token,
  184. u16 status_code, u32 peer_capab,
  185. int initiator, const u8 *buf, size_t len)
  186. {
  187. return wpa_sm_send_tdls_mgmt(sm, dst, action_code, dialog_token,
  188. status_code, peer_capab, initiator, buf,
  189. len);
  190. }
  191. static int wpa_tdls_tpk_send(struct wpa_sm *sm, const u8 *dest, u8 action_code,
  192. u8 dialog_token, u16 status_code, u32 peer_capab,
  193. int initiator, const u8 *msg, size_t msg_len)
  194. {
  195. struct wpa_tdls_peer *peer;
  196. wpa_printf(MSG_DEBUG, "TDLS: TPK send dest=" MACSTR " action_code=%u "
  197. "dialog_token=%u status_code=%u peer_capab=%u initiator=%d "
  198. "msg_len=%u",
  199. MAC2STR(dest), action_code, dialog_token, status_code,
  200. peer_capab, initiator, (unsigned int) msg_len);
  201. if (wpa_tdls_send_tpk_msg(sm, dest, action_code, dialog_token,
  202. status_code, peer_capab, initiator, msg,
  203. msg_len)) {
  204. wpa_printf(MSG_INFO, "TDLS: Failed to send message "
  205. "(action_code=%u)", action_code);
  206. return -1;
  207. }
  208. if (action_code == WLAN_TDLS_SETUP_CONFIRM ||
  209. action_code == WLAN_TDLS_TEARDOWN ||
  210. action_code == WLAN_TDLS_DISCOVERY_REQUEST ||
  211. action_code == WLAN_TDLS_DISCOVERY_RESPONSE)
  212. return 0; /* No retries */
  213. for (peer = sm->tdls; peer; peer = peer->next) {
  214. if (os_memcmp(peer->addr, dest, ETH_ALEN) == 0)
  215. break;
  216. }
  217. if (peer == NULL) {
  218. wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
  219. "retry " MACSTR, MAC2STR(dest));
  220. return 0;
  221. }
  222. eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
  223. if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
  224. peer->sm_tmr.count = TPK_M2_RETRY_COUNT;
  225. peer->sm_tmr.timer = TPK_M2_TIMEOUT;
  226. } else {
  227. peer->sm_tmr.count = TPK_M1_RETRY_COUNT;
  228. peer->sm_tmr.timer = TPK_M1_TIMEOUT;
  229. }
  230. /* Copy message to resend on timeout */
  231. os_memcpy(peer->sm_tmr.dest, dest, ETH_ALEN);
  232. peer->sm_tmr.action_code = action_code;
  233. peer->sm_tmr.dialog_token = dialog_token;
  234. peer->sm_tmr.status_code = status_code;
  235. peer->sm_tmr.peer_capab = peer_capab;
  236. peer->sm_tmr.buf_len = msg_len;
  237. os_free(peer->sm_tmr.buf);
  238. peer->sm_tmr.buf = os_memdup(msg, msg_len);
  239. if (peer->sm_tmr.buf == NULL)
  240. return -1;
  241. wpa_printf(MSG_DEBUG, "TDLS: Retry timeout registered "
  242. "(action_code=%u)", action_code);
  243. eloop_register_timeout(peer->sm_tmr.timer / 1000,
  244. (peer->sm_tmr.timer % 1000) * 1000,
  245. wpa_tdls_tpk_retry_timeout, sm, peer);
  246. return 0;
  247. }
  248. static int wpa_tdls_do_teardown(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
  249. u16 reason_code)
  250. {
  251. int ret;
  252. ret = wpa_tdls_send_teardown(sm, peer->addr, reason_code);
  253. /* disable the link after teardown was sent */
  254. wpa_tdls_disable_peer_link(sm, peer);
  255. return ret;
  256. }
  257. static void wpa_tdls_tpk_retry_timeout(void *eloop_ctx, void *timeout_ctx)
  258. {
  259. struct wpa_sm *sm = eloop_ctx;
  260. struct wpa_tdls_peer *peer = timeout_ctx;
  261. if (peer->sm_tmr.count) {
  262. peer->sm_tmr.count--;
  263. wpa_printf(MSG_INFO, "TDLS: Retrying sending of message "
  264. "(action_code=%u)",
  265. peer->sm_tmr.action_code);
  266. if (peer->sm_tmr.buf == NULL) {
  267. wpa_printf(MSG_INFO, "TDLS: No retry buffer available "
  268. "for action_code=%u",
  269. peer->sm_tmr.action_code);
  270. eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm,
  271. peer);
  272. return;
  273. }
  274. /* resend TPK Handshake Message to Peer */
  275. if (wpa_tdls_send_tpk_msg(sm, peer->sm_tmr.dest,
  276. peer->sm_tmr.action_code,
  277. peer->sm_tmr.dialog_token,
  278. peer->sm_tmr.status_code,
  279. peer->sm_tmr.peer_capab,
  280. peer->initiator,
  281. peer->sm_tmr.buf,
  282. peer->sm_tmr.buf_len)) {
  283. wpa_printf(MSG_INFO, "TDLS: Failed to retry "
  284. "transmission");
  285. }
  286. eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
  287. eloop_register_timeout(peer->sm_tmr.timer / 1000,
  288. (peer->sm_tmr.timer % 1000) * 1000,
  289. wpa_tdls_tpk_retry_timeout, sm, peer);
  290. } else {
  291. eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
  292. wpa_printf(MSG_DEBUG, "TDLS: Sending Teardown Request");
  293. wpa_tdls_do_teardown(sm, peer,
  294. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  295. }
  296. }
  297. static void wpa_tdls_tpk_retry_timeout_cancel(struct wpa_sm *sm,
  298. struct wpa_tdls_peer *peer,
  299. u8 action_code)
  300. {
  301. if (action_code == peer->sm_tmr.action_code) {
  302. wpa_printf(MSG_DEBUG, "TDLS: Retry timeout cancelled for "
  303. "action_code=%u", action_code);
  304. /* Cancel Timeout registered */
  305. eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
  306. /* free all resources meant for retry */
  307. os_free(peer->sm_tmr.buf);
  308. peer->sm_tmr.buf = NULL;
  309. peer->sm_tmr.count = 0;
  310. peer->sm_tmr.timer = 0;
  311. peer->sm_tmr.buf_len = 0;
  312. peer->sm_tmr.action_code = 0xff;
  313. } else {
  314. wpa_printf(MSG_INFO, "TDLS: Error in cancelling retry timeout "
  315. "(Unknown action_code=%u)", action_code);
  316. }
  317. }
  318. static void wpa_tdls_generate_tpk(struct wpa_tdls_peer *peer,
  319. const u8 *own_addr, const u8 *bssid)
  320. {
  321. u8 key_input[SHA256_MAC_LEN];
  322. const u8 *nonce[2];
  323. size_t len[2];
  324. u8 data[3 * ETH_ALEN];
  325. /* IEEE Std 802.11z-2010 8.5.9.1:
  326. * TPK-Key-Input = SHA-256(min(SNonce, ANonce) || max(SNonce, ANonce))
  327. */
  328. len[0] = WPA_NONCE_LEN;
  329. len[1] = WPA_NONCE_LEN;
  330. if (os_memcmp(peer->inonce, peer->rnonce, WPA_NONCE_LEN) < 0) {
  331. nonce[0] = peer->inonce;
  332. nonce[1] = peer->rnonce;
  333. } else {
  334. nonce[0] = peer->rnonce;
  335. nonce[1] = peer->inonce;
  336. }
  337. wpa_hexdump(MSG_DEBUG, "TDLS: min(Nonce)", nonce[0], WPA_NONCE_LEN);
  338. wpa_hexdump(MSG_DEBUG, "TDLS: max(Nonce)", nonce[1], WPA_NONCE_LEN);
  339. sha256_vector(2, nonce, len, key_input);
  340. wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-Key-Input",
  341. key_input, SHA256_MAC_LEN);
  342. /*
  343. * TPK-Key-Data = KDF-N_KEY(TPK-Key-Input, "TDLS PMK",
  344. * min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID || N_KEY)
  345. * TODO: is N_KEY really included in KDF Context and if so, in which
  346. * presentation format (little endian 16-bit?) is it used? It gets
  347. * added by the KDF anyway..
  348. */
  349. if (os_memcmp(own_addr, peer->addr, ETH_ALEN) < 0) {
  350. os_memcpy(data, own_addr, ETH_ALEN);
  351. os_memcpy(data + ETH_ALEN, peer->addr, ETH_ALEN);
  352. } else {
  353. os_memcpy(data, peer->addr, ETH_ALEN);
  354. os_memcpy(data + ETH_ALEN, own_addr, ETH_ALEN);
  355. }
  356. os_memcpy(data + 2 * ETH_ALEN, bssid, ETH_ALEN);
  357. wpa_hexdump(MSG_DEBUG, "TDLS: KDF Context", data, sizeof(data));
  358. sha256_prf(key_input, SHA256_MAC_LEN, "TDLS PMK", data, sizeof(data),
  359. (u8 *) &peer->tpk, sizeof(peer->tpk));
  360. wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-KCK",
  361. peer->tpk.kck, sizeof(peer->tpk.kck));
  362. wpa_hexdump_key(MSG_DEBUG, "TDLS: TPK-TK",
  363. peer->tpk.tk, sizeof(peer->tpk.tk));
  364. peer->tpk_set = 1;
  365. }
  366. /**
  367. * wpa_tdls_ftie_mic - Calculate TDLS FTIE MIC
  368. * @kck: TPK-KCK
  369. * @lnkid: Pointer to the beginning of Link Identifier IE
  370. * @rsnie: Pointer to the beginning of RSN IE used for handshake
  371. * @timeoutie: Pointer to the beginning of Timeout IE used for handshake
  372. * @ftie: Pointer to the beginning of FT IE
  373. * @mic: Pointer for writing MIC
  374. *
  375. * Calculate MIC for TDLS frame.
  376. */
  377. static int wpa_tdls_ftie_mic(const u8 *kck, u8 trans_seq, const u8 *lnkid,
  378. const u8 *rsnie, const u8 *timeoutie,
  379. const u8 *ftie, u8 *mic)
  380. {
  381. u8 *buf, *pos;
  382. struct wpa_tdls_ftie *_ftie;
  383. const struct wpa_tdls_lnkid *_lnkid;
  384. int ret;
  385. int len = 2 * ETH_ALEN + 1 + 2 + lnkid[1] + 2 + rsnie[1] +
  386. 2 + timeoutie[1] + 2 + ftie[1];
  387. buf = os_zalloc(len);
  388. if (!buf) {
  389. wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
  390. return -1;
  391. }
  392. pos = buf;
  393. _lnkid = (const struct wpa_tdls_lnkid *) lnkid;
  394. /* 1) TDLS initiator STA MAC address */
  395. os_memcpy(pos, _lnkid->init_sta, ETH_ALEN);
  396. pos += ETH_ALEN;
  397. /* 2) TDLS responder STA MAC address */
  398. os_memcpy(pos, _lnkid->resp_sta, ETH_ALEN);
  399. pos += ETH_ALEN;
  400. /* 3) Transaction Sequence number */
  401. *pos++ = trans_seq;
  402. /* 4) Link Identifier IE */
  403. os_memcpy(pos, lnkid, 2 + lnkid[1]);
  404. pos += 2 + lnkid[1];
  405. /* 5) RSN IE */
  406. os_memcpy(pos, rsnie, 2 + rsnie[1]);
  407. pos += 2 + rsnie[1];
  408. /* 6) Timeout Interval IE */
  409. os_memcpy(pos, timeoutie, 2 + timeoutie[1]);
  410. pos += 2 + timeoutie[1];
  411. /* 7) FTIE, with the MIC field of the FTIE set to 0 */
  412. os_memcpy(pos, ftie, 2 + ftie[1]);
  413. _ftie = (struct wpa_tdls_ftie *) pos;
  414. os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
  415. pos += 2 + ftie[1];
  416. wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
  417. wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
  418. ret = omac1_aes_128(kck, buf, pos - buf, mic);
  419. os_free(buf);
  420. wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
  421. return ret;
  422. }
  423. /**
  424. * wpa_tdls_key_mic_teardown - Calculate TDLS FTIE MIC for Teardown frame
  425. * @kck: TPK-KCK
  426. * @trans_seq: Transaction Sequence Number (4 - Teardown)
  427. * @rcode: Reason code for Teardown
  428. * @dtoken: Dialog Token used for that particular link
  429. * @lnkid: Pointer to the beginning of Link Identifier IE
  430. * @ftie: Pointer to the beginning of FT IE
  431. * @mic: Pointer for writing MIC
  432. *
  433. * Calculate MIC for TDLS frame.
  434. */
  435. static int wpa_tdls_key_mic_teardown(const u8 *kck, u8 trans_seq, u16 rcode,
  436. u8 dtoken, const u8 *lnkid,
  437. const u8 *ftie, u8 *mic)
  438. {
  439. u8 *buf, *pos;
  440. struct wpa_tdls_ftie *_ftie;
  441. int ret;
  442. int len;
  443. if (lnkid == NULL)
  444. return -1;
  445. len = 2 + lnkid[1] + sizeof(rcode) + sizeof(dtoken) +
  446. sizeof(trans_seq) + 2 + ftie[1];
  447. buf = os_zalloc(len);
  448. if (!buf) {
  449. wpa_printf(MSG_WARNING, "TDLS: No memory for MIC calculation");
  450. return -1;
  451. }
  452. pos = buf;
  453. /* 1) Link Identifier IE */
  454. os_memcpy(pos, lnkid, 2 + lnkid[1]);
  455. pos += 2 + lnkid[1];
  456. /* 2) Reason Code */
  457. WPA_PUT_LE16(pos, rcode);
  458. pos += sizeof(rcode);
  459. /* 3) Dialog token */
  460. *pos++ = dtoken;
  461. /* 4) Transaction Sequence number */
  462. *pos++ = trans_seq;
  463. /* 7) FTIE, with the MIC field of the FTIE set to 0 */
  464. os_memcpy(pos, ftie, 2 + ftie[1]);
  465. _ftie = (struct wpa_tdls_ftie *) pos;
  466. os_memset(_ftie->mic, 0, TDLS_MIC_LEN);
  467. pos += 2 + ftie[1];
  468. wpa_hexdump(MSG_DEBUG, "TDLS: Data for FTIE MIC", buf, pos - buf);
  469. wpa_hexdump_key(MSG_DEBUG, "TDLS: KCK", kck, 16);
  470. ret = omac1_aes_128(kck, buf, pos - buf, mic);
  471. os_free(buf);
  472. wpa_hexdump(MSG_DEBUG, "TDLS: FTIE MIC", mic, 16);
  473. return ret;
  474. }
  475. static int wpa_supplicant_verify_tdls_mic(u8 trans_seq,
  476. struct wpa_tdls_peer *peer,
  477. const u8 *lnkid, const u8 *timeoutie,
  478. const struct wpa_tdls_ftie *ftie)
  479. {
  480. u8 mic[16];
  481. if (peer->tpk_set) {
  482. wpa_tdls_ftie_mic(peer->tpk.kck, trans_seq, lnkid,
  483. peer->rsnie_p, timeoutie, (u8 *) ftie,
  484. mic);
  485. if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
  486. wpa_printf(MSG_INFO, "TDLS: Invalid MIC in FTIE - "
  487. "dropping packet");
  488. wpa_hexdump(MSG_DEBUG, "TDLS: Received MIC",
  489. ftie->mic, 16);
  490. wpa_hexdump(MSG_DEBUG, "TDLS: Calculated MIC",
  491. mic, 16);
  492. return -1;
  493. }
  494. } else {
  495. wpa_printf(MSG_WARNING, "TDLS: Could not verify TDLS MIC, "
  496. "TPK not set - dropping packet");
  497. return -1;
  498. }
  499. return 0;
  500. }
  501. static int wpa_supplicant_verify_tdls_mic_teardown(
  502. u8 trans_seq, u16 rcode, u8 dtoken, struct wpa_tdls_peer *peer,
  503. const u8 *lnkid, const struct wpa_tdls_ftie *ftie)
  504. {
  505. u8 mic[16];
  506. if (peer->tpk_set) {
  507. wpa_tdls_key_mic_teardown(peer->tpk.kck, trans_seq, rcode,
  508. dtoken, lnkid, (u8 *) ftie, mic);
  509. if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
  510. wpa_printf(MSG_INFO, "TDLS: Invalid MIC in Teardown - "
  511. "dropping packet");
  512. return -1;
  513. }
  514. } else {
  515. wpa_printf(MSG_INFO, "TDLS: Could not verify TDLS Teardown "
  516. "MIC, TPK not set - dropping packet");
  517. return -1;
  518. }
  519. return 0;
  520. }
  521. static void wpa_tdls_tpk_timeout(void *eloop_ctx, void *timeout_ctx)
  522. {
  523. struct wpa_sm *sm = eloop_ctx;
  524. struct wpa_tdls_peer *peer = timeout_ctx;
  525. /*
  526. * On TPK lifetime expiration, we have an option of either tearing down
  527. * the direct link or trying to re-initiate it. The selection of what
  528. * to do is not strictly speaking controlled by our role in the expired
  529. * link, but for now, use that to select whether to renew or tear down
  530. * the link.
  531. */
  532. if (peer->initiator) {
  533. u8 addr[ETH_ALEN];
  534. wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
  535. " - try to renew", MAC2STR(peer->addr));
  536. /* cache the peer address before do_teardown */
  537. os_memcpy(addr, peer->addr, ETH_ALEN);
  538. wpa_tdls_do_teardown(sm, peer,
  539. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  540. wpa_tdls_start(sm, addr);
  541. } else {
  542. wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime expired for " MACSTR
  543. " - tear down", MAC2STR(peer->addr));
  544. wpa_tdls_do_teardown(sm, peer,
  545. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  546. }
  547. }
  548. static void wpa_tdls_peer_remove_from_list(struct wpa_sm *sm,
  549. struct wpa_tdls_peer *peer)
  550. {
  551. struct wpa_tdls_peer *cur, *prev;
  552. cur = sm->tdls;
  553. prev = NULL;
  554. while (cur && cur != peer) {
  555. prev = cur;
  556. cur = cur->next;
  557. }
  558. if (cur != peer) {
  559. wpa_printf(MSG_ERROR, "TDLS: Could not find peer " MACSTR
  560. " to remove it from the list",
  561. MAC2STR(peer->addr));
  562. return;
  563. }
  564. if (prev)
  565. prev->next = peer->next;
  566. else
  567. sm->tdls = peer->next;
  568. }
  569. static void wpa_tdls_peer_clear(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
  570. {
  571. wpa_printf(MSG_DEBUG, "TDLS: Clear state for peer " MACSTR,
  572. MAC2STR(peer->addr));
  573. eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
  574. eloop_cancel_timeout(wpa_tdls_tpk_retry_timeout, sm, peer);
  575. peer->reconfig_key = 0;
  576. peer->initiator = 0;
  577. peer->tpk_in_progress = 0;
  578. os_free(peer->sm_tmr.buf);
  579. peer->sm_tmr.buf = NULL;
  580. os_free(peer->ht_capabilities);
  581. peer->ht_capabilities = NULL;
  582. os_free(peer->vht_capabilities);
  583. peer->vht_capabilities = NULL;
  584. os_free(peer->ext_capab);
  585. peer->ext_capab = NULL;
  586. os_free(peer->supp_channels);
  587. peer->supp_channels = NULL;
  588. os_free(peer->supp_oper_classes);
  589. peer->supp_oper_classes = NULL;
  590. peer->rsnie_i_len = peer->rsnie_p_len = 0;
  591. peer->cipher = 0;
  592. peer->qos_info = 0;
  593. peer->wmm_capable = 0;
  594. peer->tpk_set = peer->tpk_success = 0;
  595. peer->chan_switch_enabled = 0;
  596. os_memset(&peer->tpk, 0, sizeof(peer->tpk));
  597. os_memset(peer->inonce, 0, WPA_NONCE_LEN);
  598. os_memset(peer->rnonce, 0, WPA_NONCE_LEN);
  599. }
  600. static void wpa_tdls_peer_free(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
  601. {
  602. wpa_tdls_peer_clear(sm, peer);
  603. wpa_tdls_peer_remove_from_list(sm, peer);
  604. os_free(peer);
  605. }
  606. static void wpa_tdls_linkid(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
  607. struct wpa_tdls_lnkid *lnkid)
  608. {
  609. lnkid->ie_type = WLAN_EID_LINK_ID;
  610. lnkid->ie_len = 3 * ETH_ALEN;
  611. os_memcpy(lnkid->bssid, sm->bssid, ETH_ALEN);
  612. if (peer->initiator) {
  613. os_memcpy(lnkid->init_sta, sm->own_addr, ETH_ALEN);
  614. os_memcpy(lnkid->resp_sta, peer->addr, ETH_ALEN);
  615. } else {
  616. os_memcpy(lnkid->init_sta, peer->addr, ETH_ALEN);
  617. os_memcpy(lnkid->resp_sta, sm->own_addr, ETH_ALEN);
  618. }
  619. }
  620. static int wpa_tdls_send_teardown(struct wpa_sm *sm, const u8 *addr,
  621. u16 reason_code)
  622. {
  623. struct wpa_tdls_peer *peer;
  624. struct wpa_tdls_ftie *ftie;
  625. struct wpa_tdls_lnkid lnkid;
  626. u8 dialog_token;
  627. u8 *rbuf, *pos;
  628. int ielen;
  629. if (sm->tdls_disabled || !sm->tdls_supported)
  630. return -1;
  631. /* Find the node and free from the list */
  632. for (peer = sm->tdls; peer; peer = peer->next) {
  633. if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
  634. break;
  635. }
  636. if (peer == NULL) {
  637. wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
  638. "Teardown " MACSTR, MAC2STR(addr));
  639. return 0;
  640. }
  641. /* Cancel active channel switch before teardown */
  642. if (peer->chan_switch_enabled) {
  643. wpa_printf(MSG_DEBUG, "TDLS: First returning link with " MACSTR
  644. " to base channel", MAC2STR(addr));
  645. wpa_sm_tdls_disable_channel_switch(sm, peer->addr);
  646. }
  647. dialog_token = peer->dtoken;
  648. wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown for " MACSTR,
  649. MAC2STR(addr));
  650. ielen = 0;
  651. if (wpa_tdls_get_privacy(sm) && peer->tpk_set && peer->tpk_success) {
  652. /* To add FTIE for Teardown request and compute MIC */
  653. ielen += sizeof(*ftie);
  654. #ifdef CONFIG_TDLS_TESTING
  655. if (tdls_testing & TDLS_TESTING_LONG_FRAME)
  656. ielen += 170;
  657. #endif /* CONFIG_TDLS_TESTING */
  658. }
  659. rbuf = os_zalloc(ielen + 1);
  660. if (rbuf == NULL)
  661. return -1;
  662. pos = rbuf;
  663. if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
  664. goto skip_ies;
  665. ftie = (struct wpa_tdls_ftie *) pos;
  666. ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
  667. /* Using the recent nonce which should be for CONFIRM frame */
  668. os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
  669. os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
  670. ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
  671. pos = (u8 *) (ftie + 1);
  672. #ifdef CONFIG_TDLS_TESTING
  673. if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
  674. wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
  675. "FTIE");
  676. ftie->ie_len += 170;
  677. *pos++ = 255; /* FTIE subelem */
  678. *pos++ = 168; /* FTIE subelem length */
  679. pos += 168;
  680. }
  681. #endif /* CONFIG_TDLS_TESTING */
  682. wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TDLS Teardown handshake",
  683. (u8 *) ftie, pos - (u8 *) ftie);
  684. /* compute MIC before sending */
  685. wpa_tdls_linkid(sm, peer, &lnkid);
  686. wpa_tdls_key_mic_teardown(peer->tpk.kck, 4, reason_code,
  687. dialog_token, (u8 *) &lnkid, (u8 *) ftie,
  688. ftie->mic);
  689. skip_ies:
  690. /* TODO: register for a Timeout handler, if Teardown is not received at
  691. * the other end, then try again another time */
  692. /* request driver to send Teardown using this FTIE */
  693. wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_TEARDOWN, 0,
  694. reason_code, 0, peer->initiator, rbuf, pos - rbuf);
  695. os_free(rbuf);
  696. return 0;
  697. }
  698. int wpa_tdls_teardown_link(struct wpa_sm *sm, const u8 *addr, u16 reason_code)
  699. {
  700. struct wpa_tdls_peer *peer;
  701. if (sm->tdls_disabled || !sm->tdls_supported)
  702. return -1;
  703. for (peer = sm->tdls; peer; peer = peer->next) {
  704. if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
  705. break;
  706. }
  707. if (peer == NULL) {
  708. wpa_printf(MSG_DEBUG, "TDLS: Could not find peer " MACSTR
  709. " for link Teardown", MAC2STR(addr));
  710. return -1;
  711. }
  712. if (!peer->tpk_success) {
  713. wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
  714. " not connected - cannot Teardown link", MAC2STR(addr));
  715. return -1;
  716. }
  717. return wpa_tdls_do_teardown(sm, peer, reason_code);
  718. }
  719. static void wpa_tdls_disable_peer_link(struct wpa_sm *sm,
  720. struct wpa_tdls_peer *peer)
  721. {
  722. wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
  723. wpa_tdls_peer_free(sm, peer);
  724. }
  725. void wpa_tdls_disable_unreachable_link(struct wpa_sm *sm, const u8 *addr)
  726. {
  727. struct wpa_tdls_peer *peer;
  728. for (peer = sm->tdls; peer; peer = peer->next) {
  729. if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
  730. break;
  731. }
  732. if (!peer || !peer->tpk_success) {
  733. wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
  734. " not connected - cannot teardown unreachable link",
  735. MAC2STR(addr));
  736. return;
  737. }
  738. if (wpa_tdls_is_external_setup(sm)) {
  739. /*
  740. * Get us on the base channel, disable the link, send a
  741. * teardown packet through the AP, and then reset link data.
  742. */
  743. if (peer->chan_switch_enabled)
  744. wpa_sm_tdls_disable_channel_switch(sm, peer->addr);
  745. wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, addr);
  746. wpa_tdls_send_teardown(sm, addr,
  747. WLAN_REASON_TDLS_TEARDOWN_UNREACHABLE);
  748. wpa_tdls_peer_free(sm, peer);
  749. } else {
  750. wpa_tdls_disable_peer_link(sm, peer);
  751. }
  752. }
  753. const char * wpa_tdls_get_link_status(struct wpa_sm *sm, const u8 *addr)
  754. {
  755. struct wpa_tdls_peer *peer;
  756. if (sm->tdls_disabled || !sm->tdls_supported)
  757. return "disabled";
  758. for (peer = sm->tdls; peer; peer = peer->next) {
  759. if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
  760. break;
  761. }
  762. if (peer == NULL)
  763. return "peer does not exist";
  764. if (!peer->tpk_success)
  765. return "peer not connected";
  766. return "connected";
  767. }
  768. static int wpa_tdls_recv_teardown(struct wpa_sm *sm, const u8 *src_addr,
  769. const u8 *buf, size_t len)
  770. {
  771. struct wpa_tdls_peer *peer = NULL;
  772. struct wpa_tdls_ftie *ftie;
  773. struct wpa_tdls_lnkid *lnkid;
  774. struct wpa_eapol_ie_parse kde;
  775. u16 reason_code;
  776. const u8 *pos;
  777. int ielen;
  778. /* Find the node and free from the list */
  779. for (peer = sm->tdls; peer; peer = peer->next) {
  780. if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
  781. break;
  782. }
  783. if (peer == NULL) {
  784. wpa_printf(MSG_INFO, "TDLS: No matching entry found for "
  785. "Teardown " MACSTR, MAC2STR(src_addr));
  786. return 0;
  787. }
  788. pos = buf;
  789. pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
  790. reason_code = WPA_GET_LE16(pos);
  791. pos += 2;
  792. wpa_printf(MSG_DEBUG, "TDLS: TDLS Teardown Request from " MACSTR
  793. " (reason code %u)", MAC2STR(src_addr), reason_code);
  794. ielen = len - (pos - buf); /* start of IE in buf */
  795. /*
  796. * Don't reject the message if failing to parse IEs. The IEs we need are
  797. * explicitly checked below. Some APs may add arbitrary padding to the
  798. * end of short TDLS frames and that would look like invalid IEs.
  799. */
  800. if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0)
  801. wpa_printf(MSG_DEBUG,
  802. "TDLS: Failed to parse IEs in Teardown - ignore as an interop workaround");
  803. if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
  804. wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TDLS "
  805. "Teardown");
  806. return -1;
  807. }
  808. lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
  809. if (!wpa_tdls_get_privacy(sm) || !peer->tpk_set || !peer->tpk_success)
  810. goto skip_ftie;
  811. if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
  812. wpa_printf(MSG_INFO, "TDLS: No FTIE in TDLS Teardown");
  813. return -1;
  814. }
  815. ftie = (struct wpa_tdls_ftie *) kde.ftie;
  816. /* Process MIC check to see if TDLS Teardown is right */
  817. if (wpa_supplicant_verify_tdls_mic_teardown(4, reason_code,
  818. peer->dtoken, peer,
  819. (u8 *) lnkid, ftie) < 0) {
  820. wpa_printf(MSG_DEBUG, "TDLS: MIC failure for TDLS "
  821. "Teardown Request from " MACSTR, MAC2STR(src_addr));
  822. return -1;
  823. }
  824. skip_ftie:
  825. /*
  826. * Request the driver to disable the direct link and clear associated
  827. * keys.
  828. */
  829. wpa_tdls_disable_peer_link(sm, peer);
  830. return 0;
  831. }
  832. /**
  833. * wpa_tdls_send_error - To send suitable TDLS status response with
  834. * appropriate status code mentioning reason for error/failure.
  835. * @dst - MAC addr of Peer station
  836. * @tdls_action - TDLS frame type for which error code is sent
  837. * @initiator - was this end the initiator of the connection
  838. * @status - status code mentioning reason
  839. */
  840. static int wpa_tdls_send_error(struct wpa_sm *sm, const u8 *dst,
  841. u8 tdls_action, u8 dialog_token, int initiator,
  842. u16 status)
  843. {
  844. wpa_printf(MSG_DEBUG, "TDLS: Sending error to " MACSTR
  845. " (action=%u status=%u)",
  846. MAC2STR(dst), tdls_action, status);
  847. return wpa_tdls_tpk_send(sm, dst, tdls_action, dialog_token, status,
  848. 0, initiator, NULL, 0);
  849. }
  850. static struct wpa_tdls_peer *
  851. wpa_tdls_add_peer(struct wpa_sm *sm, const u8 *addr, int *existing)
  852. {
  853. struct wpa_tdls_peer *peer;
  854. if (existing)
  855. *existing = 0;
  856. for (peer = sm->tdls; peer; peer = peer->next) {
  857. if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0) {
  858. if (existing)
  859. *existing = 1;
  860. return peer; /* re-use existing entry */
  861. }
  862. }
  863. wpa_printf(MSG_INFO, "TDLS: Creating peer entry for " MACSTR,
  864. MAC2STR(addr));
  865. peer = os_zalloc(sizeof(*peer));
  866. if (peer == NULL)
  867. return NULL;
  868. os_memcpy(peer->addr, addr, ETH_ALEN);
  869. peer->next = sm->tdls;
  870. sm->tdls = peer;
  871. return peer;
  872. }
  873. static int wpa_tdls_send_tpk_m1(struct wpa_sm *sm,
  874. struct wpa_tdls_peer *peer)
  875. {
  876. size_t buf_len;
  877. struct wpa_tdls_timeoutie timeoutie;
  878. u16 rsn_capab;
  879. struct wpa_tdls_ftie *ftie;
  880. u8 *rbuf, *pos, *count_pos;
  881. u16 count;
  882. struct rsn_ie_hdr *hdr;
  883. int status;
  884. if (!wpa_tdls_get_privacy(sm)) {
  885. wpa_printf(MSG_DEBUG, "TDLS: No security used on the link");
  886. peer->rsnie_i_len = 0;
  887. goto skip_rsnie;
  888. }
  889. /*
  890. * TPK Handshake Message 1:
  891. * FTIE: ANonce=0, SNonce=initiator nonce MIC=0, DataKDs=(RSNIE_I,
  892. * Timeout Interval IE))
  893. */
  894. /* Filling RSN IE */
  895. hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
  896. hdr->elem_id = WLAN_EID_RSN;
  897. WPA_PUT_LE16(hdr->version, RSN_VERSION);
  898. pos = (u8 *) (hdr + 1);
  899. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
  900. pos += RSN_SELECTOR_LEN;
  901. count_pos = pos;
  902. pos += 2;
  903. count = 0;
  904. /*
  905. * AES-CCMP is the default Encryption preferred for TDLS, so
  906. * RSN IE is filled only with CCMP CIPHER
  907. * Note: TKIP is not used to encrypt TDLS link.
  908. *
  909. * Regardless of the cipher used on the AP connection, select CCMP
  910. * here.
  911. */
  912. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
  913. pos += RSN_SELECTOR_LEN;
  914. count++;
  915. WPA_PUT_LE16(count_pos, count);
  916. WPA_PUT_LE16(pos, 1);
  917. pos += 2;
  918. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
  919. pos += RSN_SELECTOR_LEN;
  920. rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
  921. rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
  922. #ifdef CONFIG_TDLS_TESTING
  923. if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
  924. wpa_printf(MSG_DEBUG, "TDLS: Use alternative RSN IE for "
  925. "testing");
  926. rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
  927. }
  928. #endif /* CONFIG_TDLS_TESTING */
  929. WPA_PUT_LE16(pos, rsn_capab);
  930. pos += 2;
  931. #ifdef CONFIG_TDLS_TESTING
  932. if (tdls_testing & TDLS_TESTING_ALT_RSN_IE) {
  933. /* Number of PMKIDs */
  934. *pos++ = 0x00;
  935. *pos++ = 0x00;
  936. }
  937. #endif /* CONFIG_TDLS_TESTING */
  938. hdr->len = (pos - peer->rsnie_i) - 2;
  939. peer->rsnie_i_len = pos - peer->rsnie_i;
  940. wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
  941. peer->rsnie_i, peer->rsnie_i_len);
  942. skip_rsnie:
  943. buf_len = 0;
  944. if (wpa_tdls_get_privacy(sm))
  945. buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
  946. sizeof(struct wpa_tdls_timeoutie);
  947. #ifdef CONFIG_TDLS_TESTING
  948. if (wpa_tdls_get_privacy(sm) &&
  949. (tdls_testing & TDLS_TESTING_LONG_FRAME))
  950. buf_len += 170;
  951. if (tdls_testing & TDLS_TESTING_DIFF_BSSID)
  952. buf_len += sizeof(struct wpa_tdls_lnkid);
  953. #endif /* CONFIG_TDLS_TESTING */
  954. rbuf = os_zalloc(buf_len + 1);
  955. if (rbuf == NULL) {
  956. wpa_tdls_peer_free(sm, peer);
  957. return -1;
  958. }
  959. pos = rbuf;
  960. if (!wpa_tdls_get_privacy(sm))
  961. goto skip_ies;
  962. /* Initiator RSN IE */
  963. pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
  964. ftie = (struct wpa_tdls_ftie *) pos;
  965. ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
  966. ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
  967. if (os_get_random(peer->inonce, WPA_NONCE_LEN)) {
  968. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  969. "TDLS: Failed to get random data for initiator Nonce");
  970. os_free(rbuf);
  971. wpa_tdls_peer_free(sm, peer);
  972. return -1;
  973. }
  974. wpa_hexdump(MSG_DEBUG, "TDLS: Initiator Nonce for TPK handshake",
  975. peer->inonce, WPA_NONCE_LEN);
  976. os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
  977. wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK Handshake M1",
  978. (u8 *) ftie, sizeof(struct wpa_tdls_ftie));
  979. pos = (u8 *) (ftie + 1);
  980. #ifdef CONFIG_TDLS_TESTING
  981. if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
  982. wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
  983. "FTIE");
  984. ftie->ie_len += 170;
  985. *pos++ = 255; /* FTIE subelem */
  986. *pos++ = 168; /* FTIE subelem length */
  987. pos += 168;
  988. }
  989. #endif /* CONFIG_TDLS_TESTING */
  990. /* Lifetime */
  991. peer->lifetime = TPK_LIFETIME;
  992. #ifdef CONFIG_TDLS_TESTING
  993. if (tdls_testing & TDLS_TESTING_SHORT_LIFETIME) {
  994. wpa_printf(MSG_DEBUG, "TDLS: Testing - use short TPK "
  995. "lifetime");
  996. peer->lifetime = 301;
  997. }
  998. if (tdls_testing & TDLS_TESTING_LONG_LIFETIME) {
  999. wpa_printf(MSG_DEBUG, "TDLS: Testing - use long TPK "
  1000. "lifetime");
  1001. peer->lifetime = 0xffffffff;
  1002. }
  1003. #endif /* CONFIG_TDLS_TESTING */
  1004. pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
  1005. sizeof(timeoutie), peer->lifetime);
  1006. wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
  1007. skip_ies:
  1008. #ifdef CONFIG_TDLS_TESTING
  1009. if (tdls_testing & TDLS_TESTING_DIFF_BSSID) {
  1010. struct wpa_tdls_lnkid *l = (struct wpa_tdls_lnkid *) pos;
  1011. wpa_printf(MSG_DEBUG, "TDLS: Testing - use incorrect BSSID in "
  1012. "Link Identifier");
  1013. wpa_tdls_linkid(sm, peer, l);
  1014. l->bssid[5] ^= 0x01;
  1015. pos += sizeof(*l);
  1016. }
  1017. #endif /* CONFIG_TDLS_TESTING */
  1018. wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Request / TPK "
  1019. "Handshake Message 1 (peer " MACSTR ")",
  1020. MAC2STR(peer->addr));
  1021. status = wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_SETUP_REQUEST,
  1022. 1, 0, 0, peer->initiator, rbuf, pos - rbuf);
  1023. os_free(rbuf);
  1024. return status;
  1025. }
  1026. static int wpa_tdls_send_tpk_m2(struct wpa_sm *sm,
  1027. const unsigned char *src_addr, u8 dtoken,
  1028. struct wpa_tdls_lnkid *lnkid,
  1029. const struct wpa_tdls_peer *peer)
  1030. {
  1031. u8 *rbuf, *pos;
  1032. size_t buf_len;
  1033. u32 lifetime;
  1034. struct wpa_tdls_timeoutie timeoutie;
  1035. struct wpa_tdls_ftie *ftie;
  1036. int status;
  1037. buf_len = 0;
  1038. if (wpa_tdls_get_privacy(sm)) {
  1039. /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
  1040. * Lifetime */
  1041. buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
  1042. sizeof(struct wpa_tdls_timeoutie);
  1043. #ifdef CONFIG_TDLS_TESTING
  1044. if (tdls_testing & TDLS_TESTING_LONG_FRAME)
  1045. buf_len += 170;
  1046. #endif /* CONFIG_TDLS_TESTING */
  1047. }
  1048. rbuf = os_zalloc(buf_len + 1);
  1049. if (rbuf == NULL)
  1050. return -1;
  1051. pos = rbuf;
  1052. if (!wpa_tdls_get_privacy(sm))
  1053. goto skip_ies;
  1054. /* Peer RSN IE */
  1055. pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
  1056. ftie = (struct wpa_tdls_ftie *) pos;
  1057. ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
  1058. /* TODO: ftie->mic_control to set 2-RESPONSE */
  1059. os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
  1060. os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
  1061. ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
  1062. wpa_hexdump(MSG_DEBUG, "TDLS: FTIE for TPK M2",
  1063. (u8 *) ftie, sizeof(*ftie));
  1064. pos = (u8 *) (ftie + 1);
  1065. #ifdef CONFIG_TDLS_TESTING
  1066. if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
  1067. wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
  1068. "FTIE");
  1069. ftie->ie_len += 170;
  1070. *pos++ = 255; /* FTIE subelem */
  1071. *pos++ = 168; /* FTIE subelem length */
  1072. pos += 168;
  1073. }
  1074. #endif /* CONFIG_TDLS_TESTING */
  1075. /* Lifetime */
  1076. lifetime = peer->lifetime;
  1077. #ifdef CONFIG_TDLS_TESTING
  1078. if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_RESP) {
  1079. wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
  1080. "lifetime in response");
  1081. lifetime++;
  1082. }
  1083. #endif /* CONFIG_TDLS_TESTING */
  1084. pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
  1085. sizeof(timeoutie), lifetime);
  1086. wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds from initiator",
  1087. lifetime);
  1088. /* compute MIC before sending */
  1089. wpa_tdls_ftie_mic(peer->tpk.kck, 2, (u8 *) lnkid, peer->rsnie_p,
  1090. (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
  1091. #ifdef CONFIG_TDLS_TESTING
  1092. if (tdls_testing & TDLS_TESTING_WRONG_MIC) {
  1093. wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong MIC");
  1094. ftie->mic[0] ^= 0x01;
  1095. }
  1096. #endif /* CONFIG_TDLS_TESTING */
  1097. skip_ies:
  1098. status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE,
  1099. dtoken, 0, 0, peer->initiator, rbuf,
  1100. pos - rbuf);
  1101. os_free(rbuf);
  1102. return status;
  1103. }
  1104. static int wpa_tdls_send_tpk_m3(struct wpa_sm *sm,
  1105. const unsigned char *src_addr, u8 dtoken,
  1106. struct wpa_tdls_lnkid *lnkid,
  1107. const struct wpa_tdls_peer *peer)
  1108. {
  1109. u8 *rbuf, *pos;
  1110. size_t buf_len;
  1111. struct wpa_tdls_ftie *ftie;
  1112. struct wpa_tdls_timeoutie timeoutie;
  1113. u32 lifetime;
  1114. int status;
  1115. u32 peer_capab = 0;
  1116. buf_len = 0;
  1117. if (wpa_tdls_get_privacy(sm)) {
  1118. /* Peer RSN IE, FTIE(Initiator Nonce, Responder Nonce),
  1119. * Lifetime */
  1120. buf_len += peer->rsnie_i_len + sizeof(struct wpa_tdls_ftie) +
  1121. sizeof(struct wpa_tdls_timeoutie);
  1122. #ifdef CONFIG_TDLS_TESTING
  1123. if (tdls_testing & TDLS_TESTING_LONG_FRAME)
  1124. buf_len += 170;
  1125. #endif /* CONFIG_TDLS_TESTING */
  1126. }
  1127. rbuf = os_zalloc(buf_len + 1);
  1128. if (rbuf == NULL)
  1129. return -1;
  1130. pos = rbuf;
  1131. if (!wpa_tdls_get_privacy(sm))
  1132. goto skip_ies;
  1133. /* Peer RSN IE */
  1134. pos = wpa_add_ie(pos, peer->rsnie_p, peer->rsnie_p_len);
  1135. ftie = (struct wpa_tdls_ftie *) pos;
  1136. ftie->ie_type = WLAN_EID_FAST_BSS_TRANSITION;
  1137. /*TODO: ftie->mic_control to set 3-CONFIRM */
  1138. os_memcpy(ftie->Anonce, peer->rnonce, WPA_NONCE_LEN);
  1139. os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
  1140. ftie->ie_len = sizeof(struct wpa_tdls_ftie) - 2;
  1141. pos = (u8 *) (ftie + 1);
  1142. #ifdef CONFIG_TDLS_TESTING
  1143. if (tdls_testing & TDLS_TESTING_LONG_FRAME) {
  1144. wpa_printf(MSG_DEBUG, "TDLS: Testing - add extra subelem to "
  1145. "FTIE");
  1146. ftie->ie_len += 170;
  1147. *pos++ = 255; /* FTIE subelem */
  1148. *pos++ = 168; /* FTIE subelem length */
  1149. pos += 168;
  1150. }
  1151. #endif /* CONFIG_TDLS_TESTING */
  1152. /* Lifetime */
  1153. lifetime = peer->lifetime;
  1154. #ifdef CONFIG_TDLS_TESTING
  1155. if (tdls_testing & TDLS_TESTING_WRONG_LIFETIME_CONF) {
  1156. wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong TPK "
  1157. "lifetime in confirm");
  1158. lifetime++;
  1159. }
  1160. #endif /* CONFIG_TDLS_TESTING */
  1161. pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
  1162. sizeof(timeoutie), lifetime);
  1163. wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds",
  1164. lifetime);
  1165. /* compute MIC before sending */
  1166. wpa_tdls_ftie_mic(peer->tpk.kck, 3, (u8 *) lnkid, peer->rsnie_p,
  1167. (u8 *) &timeoutie, (u8 *) ftie, ftie->mic);
  1168. #ifdef CONFIG_TDLS_TESTING
  1169. if (tdls_testing & TDLS_TESTING_WRONG_MIC) {
  1170. wpa_printf(MSG_DEBUG, "TDLS: Testing - use wrong MIC");
  1171. ftie->mic[0] ^= 0x01;
  1172. }
  1173. #endif /* CONFIG_TDLS_TESTING */
  1174. skip_ies:
  1175. if (peer->vht_capabilities)
  1176. peer_capab |= TDLS_PEER_VHT;
  1177. if (peer->ht_capabilities)
  1178. peer_capab |= TDLS_PEER_HT;
  1179. if (peer->wmm_capable)
  1180. peer_capab |= TDLS_PEER_WMM;
  1181. status = wpa_tdls_tpk_send(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM,
  1182. dtoken, 0, peer_capab, peer->initiator,
  1183. rbuf, pos - rbuf);
  1184. os_free(rbuf);
  1185. return status;
  1186. }
  1187. static int wpa_tdls_send_discovery_response(struct wpa_sm *sm,
  1188. struct wpa_tdls_peer *peer,
  1189. u8 dialog_token)
  1190. {
  1191. size_t buf_len = 0;
  1192. struct wpa_tdls_timeoutie timeoutie;
  1193. u16 rsn_capab;
  1194. u8 *rbuf, *pos, *count_pos;
  1195. u16 count;
  1196. struct rsn_ie_hdr *hdr;
  1197. int status;
  1198. wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Discovery Response "
  1199. "(peer " MACSTR ")", MAC2STR(peer->addr));
  1200. if (!wpa_tdls_get_privacy(sm))
  1201. goto skip_rsn_ies;
  1202. /* Filling RSN IE */
  1203. hdr = (struct rsn_ie_hdr *) peer->rsnie_i;
  1204. hdr->elem_id = WLAN_EID_RSN;
  1205. WPA_PUT_LE16(hdr->version, RSN_VERSION);
  1206. pos = (u8 *) (hdr + 1);
  1207. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
  1208. pos += RSN_SELECTOR_LEN;
  1209. count_pos = pos;
  1210. pos += 2;
  1211. count = 0;
  1212. /*
  1213. * AES-CCMP is the default encryption preferred for TDLS, so
  1214. * RSN IE is filled only with CCMP cipher suite.
  1215. * Note: TKIP is not used to encrypt TDLS link.
  1216. *
  1217. * Regardless of the cipher used on the AP connection, select CCMP
  1218. * here.
  1219. */
  1220. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
  1221. pos += RSN_SELECTOR_LEN;
  1222. count++;
  1223. WPA_PUT_LE16(count_pos, count);
  1224. WPA_PUT_LE16(pos, 1);
  1225. pos += 2;
  1226. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
  1227. pos += RSN_SELECTOR_LEN;
  1228. rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
  1229. rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
  1230. WPA_PUT_LE16(pos, rsn_capab);
  1231. pos += 2;
  1232. hdr->len = (pos - (u8 *) hdr) - 2;
  1233. peer->rsnie_i_len = pos - peer->rsnie_i;
  1234. wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for Discovery Response",
  1235. (u8 *) hdr, hdr->len + 2);
  1236. skip_rsn_ies:
  1237. buf_len = 0;
  1238. if (wpa_tdls_get_privacy(sm)) {
  1239. /* Peer RSN IE, Lifetime */
  1240. buf_len += peer->rsnie_i_len +
  1241. sizeof(struct wpa_tdls_timeoutie);
  1242. }
  1243. rbuf = os_zalloc(buf_len + 1);
  1244. if (rbuf == NULL) {
  1245. wpa_tdls_peer_free(sm, peer);
  1246. return -1;
  1247. }
  1248. pos = rbuf;
  1249. if (!wpa_tdls_get_privacy(sm))
  1250. goto skip_ies;
  1251. /* Initiator RSN IE */
  1252. pos = wpa_add_ie(pos, peer->rsnie_i, peer->rsnie_i_len);
  1253. /* Lifetime */
  1254. peer->lifetime = TPK_LIFETIME;
  1255. pos = wpa_add_tdls_timeoutie(pos, (u8 *) &timeoutie,
  1256. sizeof(timeoutie), peer->lifetime);
  1257. wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", peer->lifetime);
  1258. skip_ies:
  1259. status = wpa_tdls_tpk_send(sm, peer->addr, WLAN_TDLS_DISCOVERY_RESPONSE,
  1260. dialog_token, 0, 0, 0, rbuf, pos - rbuf);
  1261. os_free(rbuf);
  1262. return status;
  1263. }
  1264. static int
  1265. wpa_tdls_process_discovery_request(struct wpa_sm *sm, const u8 *addr,
  1266. const u8 *buf, size_t len)
  1267. {
  1268. struct wpa_eapol_ie_parse kde;
  1269. const struct wpa_tdls_lnkid *lnkid;
  1270. struct wpa_tdls_peer *peer;
  1271. size_t min_req_len = sizeof(struct wpa_tdls_frame) +
  1272. 1 /* dialog token */ + sizeof(struct wpa_tdls_lnkid);
  1273. u8 dialog_token;
  1274. wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from " MACSTR,
  1275. MAC2STR(addr));
  1276. if (len < min_req_len) {
  1277. wpa_printf(MSG_DEBUG, "TDLS Discovery Request is too short: "
  1278. "%d", (int) len);
  1279. return -1;
  1280. }
  1281. dialog_token = buf[sizeof(struct wpa_tdls_frame)];
  1282. /*
  1283. * Some APs will tack on a weird IE to the end of a TDLS
  1284. * discovery request packet. This needn't fail the response,
  1285. * since the required IE are verified separately.
  1286. */
  1287. if (wpa_supplicant_parse_ies(buf + sizeof(struct wpa_tdls_frame) + 1,
  1288. len - (sizeof(struct wpa_tdls_frame) + 1),
  1289. &kde) < 0) {
  1290. wpa_printf(MSG_DEBUG,
  1291. "TDLS: Failed to parse IEs in Discovery Request - ignore as an interop workaround");
  1292. }
  1293. if (!kde.lnkid) {
  1294. wpa_printf(MSG_DEBUG, "TDLS: Link ID not found in Discovery "
  1295. "Request");
  1296. return -1;
  1297. }
  1298. lnkid = (const struct wpa_tdls_lnkid *) kde.lnkid;
  1299. if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
  1300. wpa_printf(MSG_DEBUG, "TDLS: Discovery Request from different "
  1301. " BSS " MACSTR, MAC2STR(lnkid->bssid));
  1302. return -1;
  1303. }
  1304. peer = wpa_tdls_add_peer(sm, addr, NULL);
  1305. if (peer == NULL)
  1306. return -1;
  1307. return wpa_tdls_send_discovery_response(sm, peer, dialog_token);
  1308. }
  1309. int wpa_tdls_send_discovery_request(struct wpa_sm *sm, const u8 *addr)
  1310. {
  1311. if (sm->tdls_disabled || !sm->tdls_supported)
  1312. return -1;
  1313. wpa_printf(MSG_DEBUG, "TDLS: Sending Discovery Request to peer "
  1314. MACSTR, MAC2STR(addr));
  1315. return wpa_tdls_tpk_send(sm, addr, WLAN_TDLS_DISCOVERY_REQUEST,
  1316. 1, 0, 0, 1, NULL, 0);
  1317. }
  1318. static int copy_supp_rates(const struct wpa_eapol_ie_parse *kde,
  1319. struct wpa_tdls_peer *peer)
  1320. {
  1321. if (!kde->supp_rates) {
  1322. wpa_printf(MSG_DEBUG, "TDLS: No supported rates received");
  1323. return -1;
  1324. }
  1325. peer->supp_rates_len = merge_byte_arrays(
  1326. peer->supp_rates, sizeof(peer->supp_rates),
  1327. kde->supp_rates + 2, kde->supp_rates_len - 2,
  1328. kde->ext_supp_rates ? kde->ext_supp_rates + 2 : NULL,
  1329. kde->ext_supp_rates_len - 2);
  1330. return 0;
  1331. }
  1332. static int copy_peer_ht_capab(const struct wpa_eapol_ie_parse *kde,
  1333. struct wpa_tdls_peer *peer)
  1334. {
  1335. if (!kde->ht_capabilities) {
  1336. wpa_printf(MSG_DEBUG, "TDLS: No supported ht capabilities "
  1337. "received");
  1338. return 0;
  1339. }
  1340. if (!peer->ht_capabilities) {
  1341. peer->ht_capabilities =
  1342. os_zalloc(sizeof(struct ieee80211_ht_capabilities));
  1343. if (peer->ht_capabilities == NULL)
  1344. return -1;
  1345. }
  1346. os_memcpy(peer->ht_capabilities, kde->ht_capabilities,
  1347. sizeof(struct ieee80211_ht_capabilities));
  1348. wpa_hexdump(MSG_DEBUG, "TDLS: Peer HT capabilities",
  1349. (u8 *) peer->ht_capabilities,
  1350. sizeof(struct ieee80211_ht_capabilities));
  1351. return 0;
  1352. }
  1353. static int copy_peer_vht_capab(const struct wpa_eapol_ie_parse *kde,
  1354. struct wpa_tdls_peer *peer)
  1355. {
  1356. if (!kde->vht_capabilities) {
  1357. wpa_printf(MSG_DEBUG, "TDLS: No supported vht capabilities "
  1358. "received");
  1359. return 0;
  1360. }
  1361. if (!peer->vht_capabilities) {
  1362. peer->vht_capabilities =
  1363. os_zalloc(sizeof(struct ieee80211_vht_capabilities));
  1364. if (peer->vht_capabilities == NULL)
  1365. return -1;
  1366. }
  1367. os_memcpy(peer->vht_capabilities, kde->vht_capabilities,
  1368. sizeof(struct ieee80211_vht_capabilities));
  1369. wpa_hexdump(MSG_DEBUG, "TDLS: Peer VHT capabilities",
  1370. (u8 *) peer->vht_capabilities,
  1371. sizeof(struct ieee80211_vht_capabilities));
  1372. return 0;
  1373. }
  1374. static int copy_peer_ext_capab(const struct wpa_eapol_ie_parse *kde,
  1375. struct wpa_tdls_peer *peer)
  1376. {
  1377. if (!kde->ext_capab) {
  1378. wpa_printf(MSG_DEBUG, "TDLS: No extended capabilities "
  1379. "received");
  1380. return 0;
  1381. }
  1382. if (!peer->ext_capab || peer->ext_capab_len < kde->ext_capab_len - 2) {
  1383. /* Need to allocate buffer to fit the new information */
  1384. os_free(peer->ext_capab);
  1385. peer->ext_capab = os_zalloc(kde->ext_capab_len - 2);
  1386. if (peer->ext_capab == NULL)
  1387. return -1;
  1388. }
  1389. peer->ext_capab_len = kde->ext_capab_len - 2;
  1390. os_memcpy(peer->ext_capab, kde->ext_capab + 2, peer->ext_capab_len);
  1391. return 0;
  1392. }
  1393. static int copy_peer_wmm_capab(const struct wpa_eapol_ie_parse *kde,
  1394. struct wpa_tdls_peer *peer)
  1395. {
  1396. struct wmm_information_element *wmm;
  1397. if (!kde->wmm) {
  1398. wpa_printf(MSG_DEBUG, "TDLS: No supported WMM capabilities received");
  1399. return 0;
  1400. }
  1401. if (kde->wmm_len < sizeof(struct wmm_information_element)) {
  1402. wpa_printf(MSG_DEBUG, "TDLS: Invalid supported WMM capabilities received");
  1403. return -1;
  1404. }
  1405. wmm = (struct wmm_information_element *) kde->wmm;
  1406. peer->qos_info = wmm->qos_info;
  1407. peer->wmm_capable = 1;
  1408. wpa_printf(MSG_DEBUG, "TDLS: Peer WMM QOS Info 0x%x", peer->qos_info);
  1409. return 0;
  1410. }
  1411. static int copy_peer_supp_channels(const struct wpa_eapol_ie_parse *kde,
  1412. struct wpa_tdls_peer *peer)
  1413. {
  1414. if (!kde->supp_channels) {
  1415. wpa_printf(MSG_DEBUG, "TDLS: No supported channels received");
  1416. return 0;
  1417. }
  1418. if (!peer->supp_channels ||
  1419. peer->supp_channels_len < kde->supp_channels_len) {
  1420. os_free(peer->supp_channels);
  1421. peer->supp_channels = os_zalloc(kde->supp_channels_len);
  1422. if (peer->supp_channels == NULL)
  1423. return -1;
  1424. }
  1425. peer->supp_channels_len = kde->supp_channels_len;
  1426. os_memcpy(peer->supp_channels, kde->supp_channels,
  1427. peer->supp_channels_len);
  1428. wpa_hexdump(MSG_DEBUG, "TDLS: Peer Supported Channels",
  1429. (u8 *) peer->supp_channels, peer->supp_channels_len);
  1430. return 0;
  1431. }
  1432. static int copy_peer_supp_oper_classes(const struct wpa_eapol_ie_parse *kde,
  1433. struct wpa_tdls_peer *peer)
  1434. {
  1435. if (!kde->supp_oper_classes) {
  1436. wpa_printf(MSG_DEBUG, "TDLS: No supported operating classes received");
  1437. return 0;
  1438. }
  1439. if (!peer->supp_oper_classes ||
  1440. peer->supp_oper_classes_len < kde->supp_oper_classes_len) {
  1441. os_free(peer->supp_oper_classes);
  1442. peer->supp_oper_classes = os_zalloc(kde->supp_oper_classes_len);
  1443. if (peer->supp_oper_classes == NULL)
  1444. return -1;
  1445. }
  1446. peer->supp_oper_classes_len = kde->supp_oper_classes_len;
  1447. os_memcpy(peer->supp_oper_classes, kde->supp_oper_classes,
  1448. peer->supp_oper_classes_len);
  1449. wpa_hexdump(MSG_DEBUG, "TDLS: Peer Supported Operating Classes",
  1450. (u8 *) peer->supp_oper_classes,
  1451. peer->supp_oper_classes_len);
  1452. return 0;
  1453. }
  1454. static int wpa_tdls_addset_peer(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
  1455. int add)
  1456. {
  1457. return wpa_sm_tdls_peer_addset(sm, peer->addr, add, peer->aid,
  1458. peer->capability,
  1459. peer->supp_rates, peer->supp_rates_len,
  1460. peer->ht_capabilities,
  1461. peer->vht_capabilities,
  1462. peer->qos_info, peer->wmm_capable,
  1463. peer->ext_capab, peer->ext_capab_len,
  1464. peer->supp_channels,
  1465. peer->supp_channels_len,
  1466. peer->supp_oper_classes,
  1467. peer->supp_oper_classes_len);
  1468. }
  1469. static int wpa_tdls_process_tpk_m1(struct wpa_sm *sm, const u8 *src_addr,
  1470. const u8 *buf, size_t len)
  1471. {
  1472. struct wpa_tdls_peer *peer;
  1473. struct wpa_eapol_ie_parse kde;
  1474. struct wpa_ie_data ie;
  1475. int cipher;
  1476. const u8 *cpos;
  1477. struct wpa_tdls_ftie *ftie = NULL;
  1478. struct wpa_tdls_timeoutie *timeoutie;
  1479. struct wpa_tdls_lnkid *lnkid;
  1480. u32 lifetime = 0;
  1481. #if 0
  1482. struct rsn_ie_hdr *hdr;
  1483. u8 *pos;
  1484. u16 rsn_capab;
  1485. u16 rsn_ver;
  1486. #endif
  1487. u8 dtoken;
  1488. u16 ielen;
  1489. u16 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1490. int tdls_prohibited = sm->tdls_prohibited;
  1491. int existing_peer = 0;
  1492. if (len < 3 + 3)
  1493. return -1;
  1494. cpos = buf;
  1495. cpos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
  1496. /* driver had already verified the frame format */
  1497. dtoken = *cpos++; /* dialog token */
  1498. wpa_printf(MSG_INFO, "TDLS: Dialog Token in TPK M1 %d", dtoken);
  1499. peer = wpa_tdls_add_peer(sm, src_addr, &existing_peer);
  1500. if (peer == NULL)
  1501. goto error;
  1502. /* If found, use existing entry instead of adding a new one;
  1503. * how to handle the case where both ends initiate at the
  1504. * same time? */
  1505. if (existing_peer) {
  1506. if (peer->tpk_success) {
  1507. wpa_printf(MSG_DEBUG, "TDLS: TDLS Setup Request while "
  1508. "direct link is enabled - tear down the "
  1509. "old link first");
  1510. wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
  1511. wpa_tdls_peer_clear(sm, peer);
  1512. } else if (peer->initiator) {
  1513. /*
  1514. * An entry is already present, so check if we already
  1515. * sent a TDLS Setup Request. If so, compare MAC
  1516. * addresses and let the STA with the lower MAC address
  1517. * continue as the initiator. The other negotiation is
  1518. * terminated.
  1519. */
  1520. if (os_memcmp(sm->own_addr, src_addr, ETH_ALEN) < 0) {
  1521. wpa_printf(MSG_DEBUG, "TDLS: Discard request "
  1522. "from peer with higher address "
  1523. MACSTR, MAC2STR(src_addr));
  1524. return -1;
  1525. } else {
  1526. wpa_printf(MSG_DEBUG, "TDLS: Accept request "
  1527. "from peer with lower address "
  1528. MACSTR " (terminate previously "
  1529. "initiated negotiation",
  1530. MAC2STR(src_addr));
  1531. wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK,
  1532. peer->addr);
  1533. wpa_tdls_peer_clear(sm, peer);
  1534. }
  1535. }
  1536. }
  1537. /* capability information */
  1538. peer->capability = WPA_GET_LE16(cpos);
  1539. cpos += 2;
  1540. ielen = len - (cpos - buf); /* start of IE in buf */
  1541. /*
  1542. * Don't reject the message if failing to parse IEs. The IEs we need are
  1543. * explicitly checked below. Some APs may add arbitrary padding to the
  1544. * end of short TDLS frames and that would look like invalid IEs.
  1545. */
  1546. if (wpa_supplicant_parse_ies(cpos, ielen, &kde) < 0)
  1547. wpa_printf(MSG_DEBUG,
  1548. "TDLS: Failed to parse IEs in TPK M1 - ignore as an interop workaround");
  1549. if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
  1550. wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
  1551. "TPK M1");
  1552. goto error;
  1553. }
  1554. wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M1",
  1555. kde.lnkid, kde.lnkid_len);
  1556. lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
  1557. if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
  1558. wpa_printf(MSG_INFO, "TDLS: TPK M1 from diff BSS");
  1559. status = WLAN_STATUS_REQUEST_DECLINED;
  1560. goto error;
  1561. }
  1562. wpa_printf(MSG_DEBUG, "TDLS: TPK M1 - TPK initiator " MACSTR,
  1563. MAC2STR(src_addr));
  1564. if (copy_supp_rates(&kde, peer) < 0)
  1565. goto error;
  1566. if (copy_peer_ht_capab(&kde, peer) < 0)
  1567. goto error;
  1568. if (copy_peer_vht_capab(&kde, peer) < 0)
  1569. goto error;
  1570. if (copy_peer_ext_capab(&kde, peer) < 0)
  1571. goto error;
  1572. if (copy_peer_supp_channels(&kde, peer) < 0)
  1573. goto error;
  1574. if (copy_peer_supp_oper_classes(&kde, peer) < 0)
  1575. goto error;
  1576. peer->qos_info = kde.qosinfo;
  1577. /* Overwrite with the qos_info obtained in WMM IE */
  1578. if (copy_peer_wmm_capab(&kde, peer) < 0)
  1579. goto error;
  1580. peer->aid = kde.aid;
  1581. #ifdef CONFIG_TDLS_TESTING
  1582. if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
  1583. peer = wpa_tdls_add_peer(sm, src_addr, NULL);
  1584. if (peer == NULL)
  1585. goto error;
  1586. wpa_printf(MSG_DEBUG, "TDLS: Testing concurrent initiation of "
  1587. "TDLS setup - send own request");
  1588. peer->initiator = 1;
  1589. wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL,
  1590. NULL, 0, 0, NULL, 0, NULL, 0, NULL, 0);
  1591. wpa_tdls_send_tpk_m1(sm, peer);
  1592. }
  1593. if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
  1594. tdls_prohibited) {
  1595. wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
  1596. "on TDLS");
  1597. tdls_prohibited = 0;
  1598. }
  1599. #endif /* CONFIG_TDLS_TESTING */
  1600. if (tdls_prohibited) {
  1601. wpa_printf(MSG_INFO, "TDLS: TDLS prohibited in this BSS");
  1602. status = WLAN_STATUS_REQUEST_DECLINED;
  1603. goto error;
  1604. }
  1605. if (!wpa_tdls_get_privacy(sm)) {
  1606. if (kde.rsn_ie) {
  1607. wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M1 while "
  1608. "security is disabled");
  1609. status = WLAN_STATUS_SECURITY_DISABLED;
  1610. goto error;
  1611. }
  1612. goto skip_rsn;
  1613. }
  1614. if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
  1615. kde.rsn_ie == NULL) {
  1616. wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M1");
  1617. status = WLAN_STATUS_INVALID_PARAMETERS;
  1618. goto error;
  1619. }
  1620. if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
  1621. wpa_printf(MSG_INFO, "TDLS: Too long Initiator RSN IE in "
  1622. "TPK M1");
  1623. status = WLAN_STATUS_INVALID_RSNIE;
  1624. goto error;
  1625. }
  1626. if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
  1627. wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M1");
  1628. status = WLAN_STATUS_INVALID_RSNIE;
  1629. goto error;
  1630. }
  1631. cipher = ie.pairwise_cipher;
  1632. if (cipher & WPA_CIPHER_CCMP) {
  1633. wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
  1634. cipher = WPA_CIPHER_CCMP;
  1635. } else {
  1636. wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M1");
  1637. status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  1638. goto error;
  1639. }
  1640. if ((ie.capabilities &
  1641. (WPA_CAPABILITY_NO_PAIRWISE | WPA_CAPABILITY_PEERKEY_ENABLED)) !=
  1642. WPA_CAPABILITY_PEERKEY_ENABLED) {
  1643. wpa_printf(MSG_INFO, "TDLS: Invalid RSN Capabilities in "
  1644. "TPK M1");
  1645. status = WLAN_STATUS_INVALID_RSN_IE_CAPAB;
  1646. goto error;
  1647. }
  1648. /* Lifetime */
  1649. if (kde.key_lifetime == NULL) {
  1650. wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M1");
  1651. status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
  1652. goto error;
  1653. }
  1654. timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
  1655. lifetime = WPA_GET_LE32(timeoutie->value);
  1656. wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds", lifetime);
  1657. if (lifetime < 300) {
  1658. wpa_printf(MSG_INFO, "TDLS: Too short TPK lifetime");
  1659. status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
  1660. goto error;
  1661. }
  1662. skip_rsn:
  1663. #ifdef CONFIG_TDLS_TESTING
  1664. if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT) {
  1665. if (os_memcmp(sm->own_addr, peer->addr, ETH_ALEN) < 0) {
  1666. /*
  1667. * The request frame from us is going to win, so do not
  1668. * replace information based on this request frame from
  1669. * the peer.
  1670. */
  1671. goto skip_rsn_check;
  1672. }
  1673. }
  1674. #endif /* CONFIG_TDLS_TESTING */
  1675. peer->initiator = 0; /* Need to check */
  1676. peer->dtoken = dtoken;
  1677. if (!wpa_tdls_get_privacy(sm)) {
  1678. peer->rsnie_i_len = 0;
  1679. peer->rsnie_p_len = 0;
  1680. peer->cipher = WPA_CIPHER_NONE;
  1681. goto skip_rsn_check;
  1682. }
  1683. ftie = (struct wpa_tdls_ftie *) kde.ftie;
  1684. os_memcpy(peer->rsnie_i, kde.rsn_ie, kde.rsn_ie_len);
  1685. peer->rsnie_i_len = kde.rsn_ie_len;
  1686. peer->cipher = cipher;
  1687. if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
  1688. /*
  1689. * There is no point in updating the RNonce for every obtained
  1690. * TPK M1 frame (e.g., retransmission due to timeout) with the
  1691. * same INonce (SNonce in FTIE). However, if the TPK M1 is
  1692. * retransmitted with a different INonce, update the RNonce
  1693. * since this is for a new TDLS session.
  1694. */
  1695. wpa_printf(MSG_DEBUG,
  1696. "TDLS: New TPK M1 INonce - generate new RNonce");
  1697. os_memcpy(peer->inonce, ftie->Snonce, WPA_NONCE_LEN);
  1698. if (os_get_random(peer->rnonce, WPA_NONCE_LEN)) {
  1699. wpa_msg(sm->ctx->ctx, MSG_WARNING,
  1700. "TDLS: Failed to get random data for responder nonce");
  1701. goto error;
  1702. }
  1703. }
  1704. #if 0
  1705. /* get version info from RSNIE received from Peer */
  1706. hdr = (struct rsn_ie_hdr *) kde.rsn_ie;
  1707. rsn_ver = WPA_GET_LE16(hdr->version);
  1708. /* use min(peer's version, out version) */
  1709. if (rsn_ver > RSN_VERSION)
  1710. rsn_ver = RSN_VERSION;
  1711. hdr = (struct rsn_ie_hdr *) peer->rsnie_p;
  1712. hdr->elem_id = WLAN_EID_RSN;
  1713. WPA_PUT_LE16(hdr->version, rsn_ver);
  1714. pos = (u8 *) (hdr + 1);
  1715. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
  1716. pos += RSN_SELECTOR_LEN;
  1717. /* Include only the selected cipher in pairwise cipher suite */
  1718. WPA_PUT_LE16(pos, 1);
  1719. pos += 2;
  1720. if (cipher == WPA_CIPHER_CCMP)
  1721. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
  1722. pos += RSN_SELECTOR_LEN;
  1723. WPA_PUT_LE16(pos, 1);
  1724. pos += 2;
  1725. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE);
  1726. pos += RSN_SELECTOR_LEN;
  1727. rsn_capab = WPA_CAPABILITY_PEERKEY_ENABLED;
  1728. rsn_capab |= RSN_NUM_REPLAY_COUNTERS_16 << 2;
  1729. WPA_PUT_LE16(pos, rsn_capab);
  1730. pos += 2;
  1731. hdr->len = (pos - peer->rsnie_p) - 2;
  1732. peer->rsnie_p_len = pos - peer->rsnie_p;
  1733. #endif
  1734. /* temp fix: validation of RSNIE later */
  1735. os_memcpy(peer->rsnie_p, peer->rsnie_i, peer->rsnie_i_len);
  1736. peer->rsnie_p_len = peer->rsnie_i_len;
  1737. wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE for TPK handshake",
  1738. peer->rsnie_p, peer->rsnie_p_len);
  1739. peer->lifetime = lifetime;
  1740. wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
  1741. skip_rsn_check:
  1742. #ifdef CONFIG_TDLS_TESTING
  1743. if (tdls_testing & TDLS_TESTING_CONCURRENT_INIT)
  1744. goto skip_add_peer;
  1745. #endif /* CONFIG_TDLS_TESTING */
  1746. /* add supported rates, capabilities, and qos_info to the TDLS peer */
  1747. if (wpa_tdls_addset_peer(sm, peer, 1) < 0)
  1748. goto error;
  1749. #ifdef CONFIG_TDLS_TESTING
  1750. skip_add_peer:
  1751. #endif /* CONFIG_TDLS_TESTING */
  1752. peer->tpk_in_progress = 1;
  1753. wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Response / TPK M2");
  1754. if (wpa_tdls_send_tpk_m2(sm, src_addr, dtoken, lnkid, peer) < 0) {
  1755. wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
  1756. goto error;
  1757. }
  1758. return 0;
  1759. error:
  1760. wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_RESPONSE, dtoken, 0,
  1761. status);
  1762. if (peer)
  1763. wpa_tdls_peer_free(sm, peer);
  1764. return -1;
  1765. }
  1766. static int wpa_tdls_enable_link(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
  1767. {
  1768. peer->tpk_success = 1;
  1769. peer->tpk_in_progress = 0;
  1770. eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
  1771. if (wpa_tdls_get_privacy(sm)) {
  1772. u32 lifetime = peer->lifetime;
  1773. /*
  1774. * Start the initiator process a bit earlier to avoid race
  1775. * condition with the responder sending teardown request.
  1776. */
  1777. if (lifetime > 3 && peer->initiator)
  1778. lifetime -= 3;
  1779. eloop_register_timeout(lifetime, 0, wpa_tdls_tpk_timeout,
  1780. sm, peer);
  1781. #ifdef CONFIG_TDLS_TESTING
  1782. if (tdls_testing & TDLS_TESTING_NO_TPK_EXPIRATION) {
  1783. wpa_printf(MSG_DEBUG, "TDLS: Testing - disable TPK "
  1784. "expiration");
  1785. eloop_cancel_timeout(wpa_tdls_tpk_timeout, sm, peer);
  1786. }
  1787. #endif /* CONFIG_TDLS_TESTING */
  1788. }
  1789. if (peer->reconfig_key && wpa_tdls_set_key(sm, peer) < 0) {
  1790. wpa_printf(MSG_INFO, "TDLS: Could not configure key to the "
  1791. "driver");
  1792. return -1;
  1793. }
  1794. peer->reconfig_key = 0;
  1795. return wpa_sm_tdls_oper(sm, TDLS_ENABLE_LINK, peer->addr);
  1796. }
  1797. static int wpa_tdls_process_tpk_m2(struct wpa_sm *sm, const u8 *src_addr,
  1798. const u8 *buf, size_t len)
  1799. {
  1800. struct wpa_tdls_peer *peer;
  1801. struct wpa_eapol_ie_parse kde;
  1802. struct wpa_ie_data ie;
  1803. int cipher;
  1804. struct wpa_tdls_ftie *ftie;
  1805. struct wpa_tdls_timeoutie *timeoutie;
  1806. struct wpa_tdls_lnkid *lnkid;
  1807. u32 lifetime;
  1808. u8 dtoken;
  1809. int ielen;
  1810. u16 status;
  1811. const u8 *pos;
  1812. int ret = 0;
  1813. wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Response / TPK M2 "
  1814. "(Peer " MACSTR ")", MAC2STR(src_addr));
  1815. for (peer = sm->tdls; peer; peer = peer->next) {
  1816. if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
  1817. break;
  1818. }
  1819. if (peer == NULL) {
  1820. wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
  1821. "TPK M2: " MACSTR, MAC2STR(src_addr));
  1822. return -1;
  1823. }
  1824. if (!peer->initiator) {
  1825. /*
  1826. * This may happen if both devices try to initiate TDLS at the
  1827. * same time and we accept the TPK M1 from the peer in
  1828. * wpa_tdls_process_tpk_m1() and clear our previous state.
  1829. */
  1830. wpa_printf(MSG_INFO, "TDLS: We were not the initiator, so "
  1831. "ignore TPK M2 from " MACSTR, MAC2STR(src_addr));
  1832. return -1;
  1833. }
  1834. if (peer->tpk_success) {
  1835. wpa_printf(MSG_INFO, "TDLS: Ignore incoming TPK M2 retry, from "
  1836. MACSTR " as TPK M3 was already sent",
  1837. MAC2STR(src_addr));
  1838. return 0;
  1839. }
  1840. wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_REQUEST);
  1841. if (len < 3 + 2 + 1) {
  1842. wpa_tdls_disable_peer_link(sm, peer);
  1843. return -1;
  1844. }
  1845. pos = buf;
  1846. pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
  1847. status = WPA_GET_LE16(pos);
  1848. pos += 2 /* status code */;
  1849. if (status != WLAN_STATUS_SUCCESS) {
  1850. wpa_printf(MSG_INFO, "TDLS: Status code in TPK M2: %u",
  1851. status);
  1852. wpa_tdls_disable_peer_link(sm, peer);
  1853. return -1;
  1854. }
  1855. status = WLAN_STATUS_UNSPECIFIED_FAILURE;
  1856. /* TODO: need to verify dialog token matches here or in kernel */
  1857. dtoken = *pos++; /* dialog token */
  1858. wpa_printf(MSG_DEBUG, "TDLS: Dialog Token in TPK M2 %d", dtoken);
  1859. if (len < 3 + 2 + 1 + 2) {
  1860. wpa_tdls_disable_peer_link(sm, peer);
  1861. return -1;
  1862. }
  1863. /* capability information */
  1864. peer->capability = WPA_GET_LE16(pos);
  1865. pos += 2;
  1866. ielen = len - (pos - buf); /* start of IE in buf */
  1867. /*
  1868. * Don't reject the message if failing to parse IEs. The IEs we need are
  1869. * explicitly checked below. Some APs may add arbitrary padding to the
  1870. * end of short TDLS frames and that would look like invalid IEs.
  1871. */
  1872. if (wpa_supplicant_parse_ies(pos, ielen, &kde) < 0)
  1873. wpa_printf(MSG_DEBUG,
  1874. "TDLS: Failed to parse IEs in TPK M2 - ignore as an interop workaround");
  1875. #ifdef CONFIG_TDLS_TESTING
  1876. if (tdls_testing & TDLS_TESTING_DECLINE_RESP) {
  1877. wpa_printf(MSG_DEBUG, "TDLS: Testing - decline response");
  1878. status = WLAN_STATUS_REQUEST_DECLINED;
  1879. goto error;
  1880. }
  1881. #endif /* CONFIG_TDLS_TESTING */
  1882. if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
  1883. wpa_printf(MSG_INFO, "TDLS: No valid Link Identifier IE in "
  1884. "TPK M2");
  1885. goto error;
  1886. }
  1887. wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M2",
  1888. kde.lnkid, kde.lnkid_len);
  1889. lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
  1890. if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
  1891. wpa_printf(MSG_INFO, "TDLS: TPK M2 from different BSS");
  1892. status = WLAN_STATUS_NOT_IN_SAME_BSS;
  1893. goto error;
  1894. }
  1895. if (copy_supp_rates(&kde, peer) < 0)
  1896. goto error;
  1897. if (copy_peer_ht_capab(&kde, peer) < 0)
  1898. goto error;
  1899. if (copy_peer_vht_capab(&kde, peer) < 0)
  1900. goto error;
  1901. if (copy_peer_ext_capab(&kde, peer) < 0)
  1902. goto error;
  1903. if (copy_peer_supp_channels(&kde, peer) < 0)
  1904. goto error;
  1905. if (copy_peer_supp_oper_classes(&kde, peer) < 0)
  1906. goto error;
  1907. peer->qos_info = kde.qosinfo;
  1908. /* Overwrite with the qos_info obtained in WMM IE */
  1909. if (copy_peer_wmm_capab(&kde, peer) < 0)
  1910. goto error;
  1911. peer->aid = kde.aid;
  1912. if (!wpa_tdls_get_privacy(sm)) {
  1913. peer->rsnie_p_len = 0;
  1914. peer->cipher = WPA_CIPHER_NONE;
  1915. goto skip_rsn;
  1916. }
  1917. if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie) ||
  1918. kde.rsn_ie == NULL) {
  1919. wpa_printf(MSG_INFO, "TDLS: No FTIE or RSN IE in TPK M2");
  1920. status = WLAN_STATUS_INVALID_PARAMETERS;
  1921. goto error;
  1922. }
  1923. wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
  1924. kde.rsn_ie, kde.rsn_ie_len);
  1925. if (kde.rsn_ie_len > TDLS_MAX_IE_LEN) {
  1926. wpa_printf(MSG_INFO,
  1927. "TDLS: Too long Responder RSN IE in TPK M2");
  1928. status = WLAN_STATUS_INVALID_RSNIE;
  1929. goto error;
  1930. }
  1931. /*
  1932. * FIX: bitwise comparison of RSN IE is not the correct way of
  1933. * validation this. It can be different, but certain fields must
  1934. * match. Since we list only a single pairwise cipher in TPK M1, the
  1935. * memcmp is likely to work in most cases, though.
  1936. */
  1937. if (kde.rsn_ie_len != peer->rsnie_i_len ||
  1938. os_memcmp(peer->rsnie_i, kde.rsn_ie, peer->rsnie_i_len) != 0) {
  1939. wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M2 does "
  1940. "not match with RSN IE used in TPK M1");
  1941. wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Sent in TPK M1",
  1942. peer->rsnie_i, peer->rsnie_i_len);
  1943. wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M2",
  1944. kde.rsn_ie, kde.rsn_ie_len);
  1945. status = WLAN_STATUS_INVALID_RSNIE;
  1946. goto error;
  1947. }
  1948. if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
  1949. wpa_printf(MSG_INFO, "TDLS: Failed to parse RSN IE in TPK M2");
  1950. status = WLAN_STATUS_INVALID_RSNIE;
  1951. goto error;
  1952. }
  1953. cipher = ie.pairwise_cipher;
  1954. if (cipher == WPA_CIPHER_CCMP) {
  1955. wpa_printf(MSG_DEBUG, "TDLS: Using CCMP for direct link");
  1956. cipher = WPA_CIPHER_CCMP;
  1957. } else {
  1958. wpa_printf(MSG_INFO, "TDLS: No acceptable cipher in TPK M2");
  1959. status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  1960. goto error;
  1961. }
  1962. wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M2",
  1963. kde.ftie, sizeof(*ftie));
  1964. ftie = (struct wpa_tdls_ftie *) kde.ftie;
  1965. if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
  1966. wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M2 does "
  1967. "not match with FTIE SNonce used in TPK M1");
  1968. /* Silently discard the frame */
  1969. return -1;
  1970. }
  1971. /* Responder Nonce and RSN IE */
  1972. os_memcpy(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN);
  1973. os_memcpy(peer->rsnie_p, kde.rsn_ie, kde.rsn_ie_len);
  1974. peer->rsnie_p_len = kde.rsn_ie_len;
  1975. peer->cipher = cipher;
  1976. /* Lifetime */
  1977. if (kde.key_lifetime == NULL) {
  1978. wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M2");
  1979. status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
  1980. goto error;
  1981. }
  1982. timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
  1983. lifetime = WPA_GET_LE32(timeoutie->value);
  1984. wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M2",
  1985. lifetime);
  1986. if (lifetime != peer->lifetime) {
  1987. wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
  1988. "TPK M2 (expected %u)", lifetime, peer->lifetime);
  1989. status = WLAN_STATUS_UNACCEPTABLE_LIFETIME;
  1990. goto error;
  1991. }
  1992. wpa_tdls_generate_tpk(peer, sm->own_addr, sm->bssid);
  1993. /* Process MIC check to see if TPK M2 is right */
  1994. if (wpa_supplicant_verify_tdls_mic(2, peer, (u8 *) lnkid,
  1995. (u8 *) timeoutie, ftie) < 0) {
  1996. /* Discard the frame */
  1997. wpa_tdls_del_key(sm, peer);
  1998. wpa_tdls_disable_peer_link(sm, peer);
  1999. return -1;
  2000. }
  2001. if (wpa_tdls_set_key(sm, peer) < 0) {
  2002. /*
  2003. * Some drivers may not be able to config the key prior to full
  2004. * STA entry having been configured.
  2005. */
  2006. wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
  2007. "STA entry is complete");
  2008. peer->reconfig_key = 1;
  2009. }
  2010. skip_rsn:
  2011. peer->dtoken = dtoken;
  2012. /* add supported rates, capabilities, and qos_info to the TDLS peer */
  2013. if (wpa_tdls_addset_peer(sm, peer, 0) < 0)
  2014. goto error;
  2015. wpa_printf(MSG_DEBUG, "TDLS: Sending TDLS Setup Confirm / "
  2016. "TPK Handshake Message 3");
  2017. if (wpa_tdls_send_tpk_m3(sm, src_addr, dtoken, lnkid, peer) < 0)
  2018. goto error_no_msg;
  2019. if (!peer->tpk_success) {
  2020. /*
  2021. * Enable Link only when tpk_success is 0, signifying that this
  2022. * processing of TPK M2 frame is not because of a retransmission
  2023. * during TDLS setup handshake.
  2024. */
  2025. ret = wpa_tdls_enable_link(sm, peer);
  2026. if (ret < 0) {
  2027. wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
  2028. wpa_tdls_do_teardown(
  2029. sm, peer,
  2030. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  2031. }
  2032. }
  2033. return ret;
  2034. error:
  2035. wpa_tdls_send_error(sm, src_addr, WLAN_TDLS_SETUP_CONFIRM, dtoken, 1,
  2036. status);
  2037. error_no_msg:
  2038. wpa_tdls_disable_peer_link(sm, peer);
  2039. return -1;
  2040. }
  2041. static int wpa_tdls_process_tpk_m3(struct wpa_sm *sm, const u8 *src_addr,
  2042. const u8 *buf, size_t len)
  2043. {
  2044. struct wpa_tdls_peer *peer;
  2045. struct wpa_eapol_ie_parse kde;
  2046. struct wpa_tdls_ftie *ftie;
  2047. struct wpa_tdls_timeoutie *timeoutie;
  2048. struct wpa_tdls_lnkid *lnkid;
  2049. int ielen;
  2050. u16 status;
  2051. const u8 *pos;
  2052. u32 lifetime;
  2053. int ret = 0;
  2054. wpa_printf(MSG_DEBUG, "TDLS: Received TDLS Setup Confirm / TPK M3 "
  2055. "(Peer " MACSTR ")", MAC2STR(src_addr));
  2056. for (peer = sm->tdls; peer; peer = peer->next) {
  2057. if (os_memcmp(peer->addr, src_addr, ETH_ALEN) == 0)
  2058. break;
  2059. }
  2060. if (peer == NULL) {
  2061. wpa_printf(MSG_INFO, "TDLS: No matching peer found for "
  2062. "TPK M3: " MACSTR, MAC2STR(src_addr));
  2063. return -1;
  2064. }
  2065. wpa_tdls_tpk_retry_timeout_cancel(sm, peer, WLAN_TDLS_SETUP_RESPONSE);
  2066. if (len < 3 + 3)
  2067. goto error;
  2068. pos = buf;
  2069. pos += 1 /* pkt_type */ + 1 /* Category */ + 1 /* Action */;
  2070. status = WPA_GET_LE16(pos);
  2071. if (status != 0) {
  2072. wpa_printf(MSG_INFO, "TDLS: Status code in TPK M3: %u",
  2073. status);
  2074. goto error;
  2075. }
  2076. pos += 2 /* status code */ + 1 /* dialog token */;
  2077. ielen = len - (pos - buf); /* start of IE in buf */
  2078. /*
  2079. * Don't reject the message if failing to parse IEs. The IEs we need are
  2080. * explicitly checked below. Some APs piggy-back broken IEs to the end
  2081. * of a TDLS Confirm packet, which will fail the link if we don't ignore
  2082. * this error.
  2083. */
  2084. if (wpa_supplicant_parse_ies((const u8 *) pos, ielen, &kde) < 0) {
  2085. wpa_printf(MSG_DEBUG,
  2086. "TDLS: Failed to parse KDEs in TPK M3 - ignore as an interop workaround");
  2087. }
  2088. if (kde.lnkid == NULL || kde.lnkid_len < 3 * ETH_ALEN) {
  2089. wpa_printf(MSG_INFO, "TDLS: No Link Identifier IE in TPK M3");
  2090. goto error;
  2091. }
  2092. wpa_hexdump(MSG_DEBUG, "TDLS: Link ID Received from TPK M3",
  2093. (u8 *) kde.lnkid, kde.lnkid_len);
  2094. lnkid = (struct wpa_tdls_lnkid *) kde.lnkid;
  2095. if (os_memcmp(sm->bssid, lnkid->bssid, ETH_ALEN) != 0) {
  2096. wpa_printf(MSG_INFO, "TDLS: TPK M3 from diff BSS");
  2097. goto error;
  2098. }
  2099. if (!wpa_tdls_get_privacy(sm))
  2100. goto skip_rsn;
  2101. if (kde.ftie == NULL || kde.ftie_len < sizeof(*ftie)) {
  2102. wpa_printf(MSG_INFO, "TDLS: No FTIE in TPK M3");
  2103. goto error;
  2104. }
  2105. wpa_hexdump(MSG_DEBUG, "TDLS: FTIE Received from TPK M3",
  2106. kde.ftie, sizeof(*ftie));
  2107. ftie = (struct wpa_tdls_ftie *) kde.ftie;
  2108. if (kde.rsn_ie == NULL) {
  2109. wpa_printf(MSG_INFO, "TDLS: No RSN IE in TPK M3");
  2110. goto error;
  2111. }
  2112. wpa_hexdump(MSG_DEBUG, "TDLS: RSN IE Received from TPK M3",
  2113. kde.rsn_ie, kde.rsn_ie_len);
  2114. if (kde.rsn_ie_len != peer->rsnie_p_len ||
  2115. os_memcmp(kde.rsn_ie, peer->rsnie_p, peer->rsnie_p_len) != 0) {
  2116. wpa_printf(MSG_INFO, "TDLS: RSN IE in TPK M3 does not match "
  2117. "with the one sent in TPK M2");
  2118. goto error;
  2119. }
  2120. if (os_memcmp(peer->rnonce, ftie->Anonce, WPA_NONCE_LEN) != 0) {
  2121. wpa_printf(MSG_INFO, "TDLS: FTIE ANonce in TPK M3 does "
  2122. "not match with FTIE ANonce used in TPK M2");
  2123. goto error;
  2124. }
  2125. if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
  2126. wpa_printf(MSG_INFO, "TDLS: FTIE SNonce in TPK M3 does not "
  2127. "match with FTIE SNonce used in TPK M1");
  2128. goto error;
  2129. }
  2130. if (kde.key_lifetime == NULL) {
  2131. wpa_printf(MSG_INFO, "TDLS: No Key Lifetime IE in TPK M3");
  2132. goto error;
  2133. }
  2134. timeoutie = (struct wpa_tdls_timeoutie *) kde.key_lifetime;
  2135. wpa_hexdump(MSG_DEBUG, "TDLS: Timeout IE Received from TPK M3",
  2136. (u8 *) timeoutie, sizeof(*timeoutie));
  2137. lifetime = WPA_GET_LE32(timeoutie->value);
  2138. wpa_printf(MSG_DEBUG, "TDLS: TPK lifetime %u seconds in TPK M3",
  2139. lifetime);
  2140. if (lifetime != peer->lifetime) {
  2141. wpa_printf(MSG_INFO, "TDLS: Unexpected TPK lifetime %u in "
  2142. "TPK M3 (expected %u)", lifetime, peer->lifetime);
  2143. goto error;
  2144. }
  2145. if (wpa_supplicant_verify_tdls_mic(3, peer, (u8 *) lnkid,
  2146. (u8 *) timeoutie, ftie) < 0) {
  2147. wpa_tdls_del_key(sm, peer);
  2148. goto error;
  2149. }
  2150. if (wpa_tdls_set_key(sm, peer) < 0) {
  2151. /*
  2152. * Some drivers may not be able to config the key prior to full
  2153. * STA entry having been configured.
  2154. */
  2155. wpa_printf(MSG_DEBUG, "TDLS: Try to configure TPK again after "
  2156. "STA entry is complete");
  2157. peer->reconfig_key = 1;
  2158. }
  2159. skip_rsn:
  2160. /* add supported rates, capabilities, and qos_info to the TDLS peer */
  2161. if (wpa_tdls_addset_peer(sm, peer, 0) < 0)
  2162. goto error;
  2163. if (!peer->tpk_success) {
  2164. /*
  2165. * Enable Link only when tpk_success is 0, signifying that this
  2166. * processing of TPK M3 frame is not because of a retransmission
  2167. * during TDLS setup handshake.
  2168. */
  2169. ret = wpa_tdls_enable_link(sm, peer);
  2170. if (ret < 0) {
  2171. wpa_printf(MSG_DEBUG, "TDLS: Could not enable link");
  2172. goto error;
  2173. }
  2174. }
  2175. return ret;
  2176. error:
  2177. wpa_tdls_do_teardown(sm, peer, WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  2178. return -1;
  2179. }
  2180. static u8 * wpa_add_tdls_timeoutie(u8 *pos, u8 *ie, size_t ie_len, u32 tsecs)
  2181. {
  2182. struct wpa_tdls_timeoutie *lifetime = (struct wpa_tdls_timeoutie *) ie;
  2183. os_memset(lifetime, 0, ie_len);
  2184. lifetime->ie_type = WLAN_EID_TIMEOUT_INTERVAL;
  2185. lifetime->ie_len = sizeof(struct wpa_tdls_timeoutie) - 2;
  2186. lifetime->interval_type = WLAN_TIMEOUT_KEY_LIFETIME;
  2187. WPA_PUT_LE32(lifetime->value, tsecs);
  2188. os_memcpy(pos, ie, ie_len);
  2189. return pos + ie_len;
  2190. }
  2191. /**
  2192. * wpa_tdls_start - Initiate TDLS handshake (send TPK Handshake Message 1)
  2193. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2194. * @peer: MAC address of the peer STA
  2195. * Returns: 0 on success, or -1 on failure
  2196. *
  2197. * Send TPK Handshake Message 1 info to driver to start TDLS
  2198. * handshake with the peer.
  2199. */
  2200. int wpa_tdls_start(struct wpa_sm *sm, const u8 *addr)
  2201. {
  2202. struct wpa_tdls_peer *peer;
  2203. int tdls_prohibited = sm->tdls_prohibited;
  2204. if (sm->tdls_disabled || !sm->tdls_supported)
  2205. return -1;
  2206. #ifdef CONFIG_TDLS_TESTING
  2207. if ((tdls_testing & TDLS_TESTING_IGNORE_AP_PROHIBIT) &&
  2208. tdls_prohibited) {
  2209. wpa_printf(MSG_DEBUG, "TDLS: Testing - ignore AP prohibition "
  2210. "on TDLS");
  2211. tdls_prohibited = 0;
  2212. }
  2213. #endif /* CONFIG_TDLS_TESTING */
  2214. if (tdls_prohibited) {
  2215. wpa_printf(MSG_DEBUG, "TDLS: TDLS is prohibited in this BSS - "
  2216. "reject request to start setup");
  2217. return -1;
  2218. }
  2219. peer = wpa_tdls_add_peer(sm, addr, NULL);
  2220. if (peer == NULL)
  2221. return -1;
  2222. if (peer->tpk_in_progress) {
  2223. wpa_printf(MSG_DEBUG, "TDLS: Setup is already in progress with the peer");
  2224. return 0;
  2225. }
  2226. peer->initiator = 1;
  2227. /* add the peer to the driver as a "setup in progress" peer */
  2228. if (wpa_sm_tdls_peer_addset(sm, peer->addr, 1, 0, 0, NULL, 0, NULL,
  2229. NULL, 0, 0, NULL, 0, NULL, 0, NULL, 0)) {
  2230. wpa_tdls_disable_peer_link(sm, peer);
  2231. return -1;
  2232. }
  2233. peer->tpk_in_progress = 1;
  2234. if (wpa_tdls_send_tpk_m1(sm, peer) < 0) {
  2235. wpa_tdls_disable_peer_link(sm, peer);
  2236. return -1;
  2237. }
  2238. return 0;
  2239. }
  2240. void wpa_tdls_remove(struct wpa_sm *sm, const u8 *addr)
  2241. {
  2242. struct wpa_tdls_peer *peer;
  2243. if (sm->tdls_disabled || !sm->tdls_supported)
  2244. return;
  2245. for (peer = sm->tdls; peer; peer = peer->next) {
  2246. if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
  2247. break;
  2248. }
  2249. if (peer == NULL || !peer->tpk_success)
  2250. return;
  2251. if (sm->tdls_external_setup) {
  2252. /*
  2253. * Disable previous link to allow renegotiation to be completed
  2254. * on AP path.
  2255. */
  2256. wpa_tdls_do_teardown(sm, peer,
  2257. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  2258. }
  2259. }
  2260. /**
  2261. * wpa_supplicant_rx_tdls - Receive TDLS data frame
  2262. *
  2263. * This function is called to receive TDLS (ethertype = 0x890d) data frames.
  2264. */
  2265. static void wpa_supplicant_rx_tdls(void *ctx, const u8 *src_addr,
  2266. const u8 *buf, size_t len)
  2267. {
  2268. struct wpa_sm *sm = ctx;
  2269. struct wpa_tdls_frame *tf;
  2270. wpa_hexdump(MSG_DEBUG, "TDLS: Received Data frame encapsulation",
  2271. buf, len);
  2272. if (sm->tdls_disabled || !sm->tdls_supported) {
  2273. wpa_printf(MSG_DEBUG, "TDLS: Discard message - TDLS disabled "
  2274. "or unsupported by driver");
  2275. return;
  2276. }
  2277. if (os_memcmp(src_addr, sm->own_addr, ETH_ALEN) == 0) {
  2278. wpa_printf(MSG_DEBUG, "TDLS: Discard copy of own message");
  2279. return;
  2280. }
  2281. if (len < sizeof(*tf)) {
  2282. wpa_printf(MSG_INFO, "TDLS: Drop too short frame");
  2283. return;
  2284. }
  2285. /* Check to make sure its a valid encapsulated TDLS frame */
  2286. tf = (struct wpa_tdls_frame *) buf;
  2287. if (tf->payloadtype != 2 /* TDLS_RFTYPE */ ||
  2288. tf->category != WLAN_ACTION_TDLS) {
  2289. wpa_printf(MSG_INFO, "TDLS: Invalid frame - payloadtype=%u "
  2290. "category=%u action=%u",
  2291. tf->payloadtype, tf->category, tf->action);
  2292. return;
  2293. }
  2294. switch (tf->action) {
  2295. case WLAN_TDLS_SETUP_REQUEST:
  2296. wpa_tdls_process_tpk_m1(sm, src_addr, buf, len);
  2297. break;
  2298. case WLAN_TDLS_SETUP_RESPONSE:
  2299. wpa_tdls_process_tpk_m2(sm, src_addr, buf, len);
  2300. break;
  2301. case WLAN_TDLS_SETUP_CONFIRM:
  2302. wpa_tdls_process_tpk_m3(sm, src_addr, buf, len);
  2303. break;
  2304. case WLAN_TDLS_TEARDOWN:
  2305. wpa_tdls_recv_teardown(sm, src_addr, buf, len);
  2306. break;
  2307. case WLAN_TDLS_DISCOVERY_REQUEST:
  2308. wpa_tdls_process_discovery_request(sm, src_addr, buf, len);
  2309. break;
  2310. default:
  2311. /* Kernel code will process remaining frames */
  2312. wpa_printf(MSG_DEBUG, "TDLS: Ignore TDLS frame action code %u",
  2313. tf->action);
  2314. break;
  2315. }
  2316. }
  2317. /**
  2318. * wpa_tdls_init - Initialize driver interface parameters for TDLS
  2319. * @wpa_s: Pointer to wpa_supplicant data
  2320. * Returns: 0 on success, -1 on failure
  2321. *
  2322. * This function is called to initialize driver interface parameters for TDLS.
  2323. * wpa_drv_init() must have been called before this function to initialize the
  2324. * driver interface.
  2325. */
  2326. int wpa_tdls_init(struct wpa_sm *sm)
  2327. {
  2328. if (sm == NULL)
  2329. return -1;
  2330. sm->l2_tdls = l2_packet_init(sm->bridge_ifname ? sm->bridge_ifname :
  2331. sm->ifname,
  2332. sm->own_addr,
  2333. ETH_P_80211_ENCAP, wpa_supplicant_rx_tdls,
  2334. sm, 0);
  2335. if (sm->l2_tdls == NULL) {
  2336. wpa_printf(MSG_ERROR, "TDLS: Failed to open l2_packet "
  2337. "connection");
  2338. return -1;
  2339. }
  2340. /*
  2341. * Drivers that support TDLS but don't implement the get_capa callback
  2342. * are assumed to perform everything internally
  2343. */
  2344. if (wpa_sm_tdls_get_capa(sm, &sm->tdls_supported,
  2345. &sm->tdls_external_setup,
  2346. &sm->tdls_chan_switch) < 0) {
  2347. sm->tdls_supported = 1;
  2348. sm->tdls_external_setup = 0;
  2349. }
  2350. wpa_printf(MSG_DEBUG, "TDLS: TDLS operation%s supported by "
  2351. "driver", sm->tdls_supported ? "" : " not");
  2352. wpa_printf(MSG_DEBUG, "TDLS: Driver uses %s link setup",
  2353. sm->tdls_external_setup ? "external" : "internal");
  2354. wpa_printf(MSG_DEBUG, "TDLS: Driver %s TDLS channel switching",
  2355. sm->tdls_chan_switch ? "supports" : "does not support");
  2356. return 0;
  2357. }
  2358. void wpa_tdls_teardown_peers(struct wpa_sm *sm)
  2359. {
  2360. struct wpa_tdls_peer *peer, *tmp;
  2361. if (!sm)
  2362. return;
  2363. peer = sm->tdls;
  2364. wpa_printf(MSG_DEBUG, "TDLS: Tear down peers");
  2365. while (peer) {
  2366. tmp = peer->next;
  2367. wpa_printf(MSG_DEBUG, "TDLS: Tear down peer " MACSTR,
  2368. MAC2STR(peer->addr));
  2369. if (sm->tdls_external_setup)
  2370. wpa_tdls_do_teardown(sm, peer,
  2371. WLAN_REASON_DEAUTH_LEAVING);
  2372. else
  2373. wpa_sm_tdls_oper(sm, TDLS_TEARDOWN, peer->addr);
  2374. peer = tmp;
  2375. }
  2376. }
  2377. static void wpa_tdls_remove_peers(struct wpa_sm *sm)
  2378. {
  2379. struct wpa_tdls_peer *peer, *tmp;
  2380. peer = sm->tdls;
  2381. while (peer) {
  2382. int res;
  2383. tmp = peer->next;
  2384. res = wpa_sm_tdls_oper(sm, TDLS_DISABLE_LINK, peer->addr);
  2385. wpa_printf(MSG_DEBUG, "TDLS: Remove peer " MACSTR " (res=%d)",
  2386. MAC2STR(peer->addr), res);
  2387. wpa_tdls_peer_free(sm, peer);
  2388. peer = tmp;
  2389. }
  2390. }
  2391. /**
  2392. * wpa_tdls_deinit - Deinitialize driver interface parameters for TDLS
  2393. *
  2394. * This function is called to recover driver interface parameters for TDLS
  2395. * and frees resources allocated for it.
  2396. */
  2397. void wpa_tdls_deinit(struct wpa_sm *sm)
  2398. {
  2399. if (sm == NULL)
  2400. return;
  2401. if (sm->l2_tdls)
  2402. l2_packet_deinit(sm->l2_tdls);
  2403. sm->l2_tdls = NULL;
  2404. wpa_tdls_remove_peers(sm);
  2405. }
  2406. void wpa_tdls_assoc(struct wpa_sm *sm)
  2407. {
  2408. wpa_printf(MSG_DEBUG, "TDLS: Remove peers on association");
  2409. wpa_tdls_remove_peers(sm);
  2410. }
  2411. void wpa_tdls_disassoc(struct wpa_sm *sm)
  2412. {
  2413. wpa_printf(MSG_DEBUG, "TDLS: Remove peers on disassociation");
  2414. wpa_tdls_remove_peers(sm);
  2415. }
  2416. static int wpa_tdls_prohibited(struct ieee802_11_elems *elems)
  2417. {
  2418. /* bit 38 - TDLS Prohibited */
  2419. return !!(elems->ext_capab[4] & 0x40);
  2420. }
  2421. static int wpa_tdls_chan_switch_prohibited(struct ieee802_11_elems *elems)
  2422. {
  2423. /* bit 39 - TDLS Channel Switch Prohibited */
  2424. return !!(elems->ext_capab[4] & 0x80);
  2425. }
  2426. void wpa_tdls_ap_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
  2427. {
  2428. struct ieee802_11_elems elems;
  2429. sm->tdls_prohibited = 0;
  2430. sm->tdls_chan_switch_prohibited = 0;
  2431. if (ies == NULL ||
  2432. ieee802_11_parse_elems(ies, len, &elems, 0) == ParseFailed ||
  2433. elems.ext_capab == NULL || elems.ext_capab_len < 5)
  2434. return;
  2435. sm->tdls_prohibited = wpa_tdls_prohibited(&elems);
  2436. wpa_printf(MSG_DEBUG, "TDLS: TDLS is %s in the target BSS",
  2437. sm->tdls_prohibited ? "prohibited" : "allowed");
  2438. sm->tdls_chan_switch_prohibited =
  2439. wpa_tdls_chan_switch_prohibited(&elems);
  2440. wpa_printf(MSG_DEBUG, "TDLS: TDLS channel switch %s in the target BSS",
  2441. sm->tdls_chan_switch_prohibited ? "prohibited" : "allowed");
  2442. }
  2443. void wpa_tdls_assoc_resp_ies(struct wpa_sm *sm, const u8 *ies, size_t len)
  2444. {
  2445. struct ieee802_11_elems elems;
  2446. if (ies == NULL ||
  2447. ieee802_11_parse_elems(ies, len, &elems, 0) == ParseFailed ||
  2448. elems.ext_capab == NULL || elems.ext_capab_len < 5)
  2449. return;
  2450. if (!sm->tdls_prohibited && wpa_tdls_prohibited(&elems)) {
  2451. wpa_printf(MSG_DEBUG, "TDLS: TDLS prohibited based on "
  2452. "(Re)Association Response IEs");
  2453. sm->tdls_prohibited = 1;
  2454. }
  2455. if (!sm->tdls_chan_switch_prohibited &&
  2456. wpa_tdls_chan_switch_prohibited(&elems)) {
  2457. wpa_printf(MSG_DEBUG,
  2458. "TDLS: TDLS channel switch prohibited based on (Re)Association Response IEs");
  2459. sm->tdls_chan_switch_prohibited = 1;
  2460. }
  2461. }
  2462. void wpa_tdls_enable(struct wpa_sm *sm, int enabled)
  2463. {
  2464. wpa_printf(MSG_DEBUG, "TDLS: %s", enabled ? "enabled" : "disabled");
  2465. sm->tdls_disabled = !enabled;
  2466. }
  2467. int wpa_tdls_is_external_setup(struct wpa_sm *sm)
  2468. {
  2469. return sm->tdls_external_setup;
  2470. }
  2471. int wpa_tdls_enable_chan_switch(struct wpa_sm *sm, const u8 *addr,
  2472. u8 oper_class,
  2473. struct hostapd_freq_params *freq_params)
  2474. {
  2475. struct wpa_tdls_peer *peer;
  2476. int ret;
  2477. if (sm->tdls_disabled || !sm->tdls_supported)
  2478. return -1;
  2479. if (!sm->tdls_chan_switch) {
  2480. wpa_printf(MSG_DEBUG,
  2481. "TDLS: Channel switching not supported by the driver");
  2482. return -1;
  2483. }
  2484. if (sm->tdls_chan_switch_prohibited) {
  2485. wpa_printf(MSG_DEBUG,
  2486. "TDLS: Channel switching is prohibited in this BSS - reject request to switch channel");
  2487. return -1;
  2488. }
  2489. for (peer = sm->tdls; peer; peer = peer->next) {
  2490. if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
  2491. break;
  2492. }
  2493. if (peer == NULL || !peer->tpk_success) {
  2494. wpa_printf(MSG_ERROR, "TDLS: Peer " MACSTR
  2495. " not found for channel switching", MAC2STR(addr));
  2496. return -1;
  2497. }
  2498. if (peer->chan_switch_enabled) {
  2499. wpa_printf(MSG_DEBUG, "TDLS: Peer " MACSTR
  2500. " already has channel switching enabled",
  2501. MAC2STR(addr));
  2502. return 0;
  2503. }
  2504. ret = wpa_sm_tdls_enable_channel_switch(sm, peer->addr,
  2505. oper_class, freq_params);
  2506. if (!ret)
  2507. peer->chan_switch_enabled = 1;
  2508. return ret;
  2509. }
  2510. int wpa_tdls_disable_chan_switch(struct wpa_sm *sm, const u8 *addr)
  2511. {
  2512. struct wpa_tdls_peer *peer;
  2513. if (sm->tdls_disabled || !sm->tdls_supported)
  2514. return -1;
  2515. for (peer = sm->tdls; peer; peer = peer->next) {
  2516. if (os_memcmp(peer->addr, addr, ETH_ALEN) == 0)
  2517. break;
  2518. }
  2519. if (!peer || !peer->chan_switch_enabled) {
  2520. wpa_printf(MSG_ERROR, "TDLS: Channel switching not enabled for "
  2521. MACSTR, MAC2STR(addr));
  2522. return -1;
  2523. }
  2524. /* ignore the return value */
  2525. wpa_sm_tdls_disable_channel_switch(sm, peer->addr);
  2526. peer->chan_switch_enabled = 0;
  2527. return 0;
  2528. }