tdls.c 77 KB

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