p2p_supplicant.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eloop.h"
  17. #include "common/ieee802_11_common.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "common/wpa_ctrl.h"
  20. #include "wps/wps_i.h"
  21. #include "p2p/p2p.h"
  22. #include "ap/hostapd.h"
  23. #include "ap/p2p_hostapd.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "ap.h"
  27. #include "config_ssid.h"
  28. #include "config.h"
  29. #include "mlme.h"
  30. #include "notify.h"
  31. #include "scan.h"
  32. #include "bss.h"
  33. #include "wps_supplicant.h"
  34. #include "p2p_supplicant.h"
  35. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  36. static struct wpa_supplicant *
  37. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  38. int go);
  39. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  40. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  41. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  42. const u8 *dev_addr, enum p2p_wps_method wps_method);
  43. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  44. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  45. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  46. struct wpa_scan_results *scan_res)
  47. {
  48. size_t i;
  49. if (wpa_s->global->p2p_disabled)
  50. return;
  51. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  52. (int) scan_res->num);
  53. for (i = 0; i < scan_res->num; i++) {
  54. struct wpa_scan_res *bss = scan_res->res[i];
  55. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  56. bss->freq, bss->level,
  57. (const u8 *) (bss + 1),
  58. bss->ie_len) > 0)
  59. return;
  60. }
  61. p2p_scan_res_handled(wpa_s->global->p2p);
  62. }
  63. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq)
  64. {
  65. struct wpa_supplicant *wpa_s = ctx;
  66. struct wpa_driver_scan_params params;
  67. int ret;
  68. struct wpabuf *wps_ie, *ies;
  69. int social_channels[] = { 2412, 2437, 2462, 0, 0 };
  70. os_memset(&params, 0, sizeof(params));
  71. /* P2P Wildcard SSID */
  72. params.num_ssids = 1;
  73. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  74. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  75. wpa_s->wps->dev.p2p = 1;
  76. wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
  77. WPS_REQ_ENROLLEE);
  78. if (wps_ie == NULL)
  79. return -1;
  80. ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
  81. if (ies == NULL) {
  82. wpabuf_free(wps_ie);
  83. return -1;
  84. }
  85. wpabuf_put_buf(ies, wps_ie);
  86. wpabuf_free(wps_ie);
  87. p2p_scan_ie(wpa_s->global->p2p, ies);
  88. params.extra_ies = wpabuf_head(ies);
  89. params.extra_ies_len = wpabuf_len(ies);
  90. switch (type) {
  91. case P2P_SCAN_SOCIAL:
  92. params.freqs = social_channels;
  93. break;
  94. case P2P_SCAN_FULL:
  95. break;
  96. case P2P_SCAN_SPECIFIC:
  97. social_channels[0] = freq;
  98. social_channels[1] = 0;
  99. params.freqs = social_channels;
  100. break;
  101. case P2P_SCAN_SOCIAL_PLUS_ONE:
  102. social_channels[3] = freq;
  103. params.freqs = social_channels;
  104. break;
  105. }
  106. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  107. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  108. ret = ieee80211_sta_req_scan(wpa_s, &params);
  109. else
  110. ret = wpa_drv_scan(wpa_s, &params);
  111. wpabuf_free(ies);
  112. return ret;
  113. }
  114. #ifdef CONFIG_CLIENT_MLME
  115. static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq)
  116. {
  117. struct wpa_supplicant *wpa_s = ctx;
  118. const struct ieee80211_mgmt *mgmt;
  119. size_t hdr_len;
  120. if (wpa_s->global->p2p_disabled)
  121. return;
  122. mgmt = (const struct ieee80211_mgmt *) buf;
  123. hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
  124. if (hdr_len > len)
  125. return;
  126. p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
  127. mgmt->u.action.category,
  128. &mgmt->u.action.u.vs_public_action.action,
  129. len - hdr_len, freq);
  130. }
  131. #endif /* CONFIG_CLIENT_MLME */
  132. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  133. {
  134. switch (p2p_group_interface) {
  135. case P2P_GROUP_INTERFACE_PENDING:
  136. return WPA_IF_P2P_GROUP;
  137. case P2P_GROUP_INTERFACE_GO:
  138. return WPA_IF_P2P_GO;
  139. case P2P_GROUP_INTERFACE_CLIENT:
  140. return WPA_IF_P2P_CLIENT;
  141. }
  142. return WPA_IF_P2P_GROUP;
  143. }
  144. static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
  145. {
  146. struct wpa_ssid *ssid;
  147. char *gtype;
  148. ssid = wpa_s->current_ssid;
  149. if (ssid == NULL) {
  150. /*
  151. * The current SSID was not known, but there may still be a
  152. * pending P2P group interface waiting for provisioning.
  153. */
  154. ssid = wpa_s->conf->ssid;
  155. while (ssid) {
  156. if (ssid->p2p_group &&
  157. (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  158. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
  159. break;
  160. ssid = ssid->next;
  161. }
  162. }
  163. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  164. gtype = "GO";
  165. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  166. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  167. wpa_s->reassociate = 0;
  168. wpa_s->disconnected = 1;
  169. wpa_supplicant_deauthenticate(wpa_s,
  170. WLAN_REASON_DEAUTH_LEAVING);
  171. gtype = "client";
  172. } else
  173. gtype = "GO";
  174. if (wpa_s->cross_connect_in_use) {
  175. wpa_s->cross_connect_in_use = 0;
  176. wpa_msg(wpa_s->parent, MSG_INFO,
  177. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  178. wpa_s->ifname, wpa_s->cross_connect_uplink);
  179. }
  180. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s",
  181. wpa_s->ifname, gtype);
  182. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  183. struct wpa_global *global;
  184. char *ifname;
  185. enum wpa_driver_if_type type;
  186. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  187. wpa_s->ifname);
  188. global = wpa_s->global;
  189. ifname = os_strdup(wpa_s->ifname);
  190. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  191. wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
  192. wpa_s = global->ifaces;
  193. if (wpa_s && ifname)
  194. wpa_drv_if_remove(wpa_s, type, ifname);
  195. os_free(ifname);
  196. return;
  197. }
  198. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  199. if (ssid && (ssid->p2p_group ||
  200. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  201. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  202. int id = ssid->id;
  203. if (ssid == wpa_s->current_ssid)
  204. wpa_s->current_ssid = NULL;
  205. wpas_notify_network_removed(wpa_s, ssid);
  206. wpa_config_remove_network(wpa_s->conf, id);
  207. wpa_supplicant_clear_status(wpa_s);
  208. } else {
  209. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  210. "found");
  211. }
  212. wpa_supplicant_ap_deinit(wpa_s);
  213. }
  214. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  215. u8 *go_dev_addr,
  216. const u8 *ssid, size_t ssid_len)
  217. {
  218. struct wpa_bss *bss;
  219. const u8 *bssid;
  220. struct wpabuf *p2p;
  221. u8 group_capab;
  222. const u8 *addr;
  223. if (wpa_s->go_params)
  224. bssid = wpa_s->go_params->peer_interface_addr;
  225. else
  226. bssid = wpa_s->bssid;
  227. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  228. if (bss == NULL) {
  229. u8 iface_addr[ETH_ALEN];
  230. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  231. iface_addr) == 0)
  232. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  233. }
  234. if (bss == NULL) {
  235. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  236. "group is persistent - BSS " MACSTR " not found",
  237. MAC2STR(bssid));
  238. return 0;
  239. }
  240. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  241. if (p2p == NULL) {
  242. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  243. "group is persistent - BSS " MACSTR
  244. " did not include P2P IE", MAC2STR(bssid));
  245. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  246. (u8 *) (bss + 1), bss->ie_len);
  247. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  248. ((u8 *) bss + 1) + bss->ie_len,
  249. bss->beacon_ie_len);
  250. return 0;
  251. }
  252. group_capab = p2p_get_group_capab(p2p);
  253. addr = p2p_get_go_dev_addr(p2p);
  254. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  255. "group_capab=0x%x", group_capab);
  256. if (addr) {
  257. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  258. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  259. MAC2STR(addr));
  260. } else
  261. os_memset(go_dev_addr, 0, ETH_ALEN);
  262. wpabuf_free(p2p);
  263. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  264. "go_dev_addr=" MACSTR,
  265. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  266. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  267. }
  268. static void wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  269. struct wpa_ssid *ssid,
  270. const u8 *go_dev_addr)
  271. {
  272. struct wpa_ssid *s;
  273. int changed = 0;
  274. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  275. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  276. for (s = wpa_s->conf->ssid; s; s = s->next) {
  277. if (s->disabled == 2 &&
  278. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  279. s->ssid_len == ssid->ssid_len &&
  280. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  281. break;
  282. }
  283. if (s) {
  284. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  285. "entry");
  286. if (ssid->passphrase && !s->passphrase)
  287. changed = 1;
  288. else if (ssid->passphrase && s->passphrase &&
  289. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  290. changed = 1;
  291. } else {
  292. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  293. "entry");
  294. changed = 1;
  295. s = wpa_config_add_network(wpa_s->conf);
  296. if (s == NULL)
  297. return;
  298. wpa_config_set_network_defaults(s);
  299. }
  300. s->p2p_group = 1;
  301. s->p2p_persistent_group = 1;
  302. s->disabled = 2;
  303. s->bssid_set = 1;
  304. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  305. s->mode = ssid->mode;
  306. s->auth_alg = WPA_AUTH_ALG_OPEN;
  307. s->key_mgmt = WPA_KEY_MGMT_PSK;
  308. s->proto = WPA_PROTO_RSN;
  309. s->pairwise_cipher = WPA_CIPHER_CCMP;
  310. if (ssid->passphrase) {
  311. os_free(s->passphrase);
  312. s->passphrase = os_strdup(ssid->passphrase);
  313. }
  314. if (ssid->psk_set) {
  315. s->psk_set = 1;
  316. os_memcpy(s->psk, ssid->psk, 32);
  317. }
  318. if (s->passphrase && !s->psk_set)
  319. wpa_config_update_psk(s);
  320. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  321. os_free(s->ssid);
  322. s->ssid = os_malloc(ssid->ssid_len);
  323. }
  324. if (s->ssid) {
  325. s->ssid_len = ssid->ssid_len;
  326. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  327. }
  328. #ifndef CONFIG_NO_CONFIG_WRITE
  329. if (changed && wpa_s->conf->update_config &&
  330. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  331. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  332. }
  333. #endif /* CONFIG_NO_CONFIG_WRITE */
  334. }
  335. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  336. int success)
  337. {
  338. struct wpa_ssid *ssid;
  339. const char *ssid_txt;
  340. int client;
  341. int persistent;
  342. u8 go_dev_addr[ETH_ALEN];
  343. /*
  344. * This callback is likely called for the main interface. Update wpa_s
  345. * to use the group interface if a new interface was created for the
  346. * group.
  347. */
  348. if (wpa_s->global->p2p_group_formation)
  349. wpa_s = wpa_s->global->p2p_group_formation;
  350. wpa_s->p2p_in_provisioning = 0;
  351. if (!success) {
  352. wpa_msg(wpa_s->parent, MSG_INFO,
  353. P2P_EVENT_GROUP_FORMATION_FAILURE);
  354. wpas_p2p_group_delete(wpa_s);
  355. return;
  356. }
  357. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
  358. ssid = wpa_s->current_ssid;
  359. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  360. ssid->mode = WPAS_MODE_P2P_GO;
  361. p2p_group_notif_formation_done(wpa_s->p2p_group);
  362. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  363. }
  364. persistent = 0;
  365. if (ssid) {
  366. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  367. client = ssid->mode == WPAS_MODE_INFRA;
  368. if (ssid->mode == WPAS_MODE_P2P_GO) {
  369. persistent = ssid->p2p_persistent_group;
  370. os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
  371. ETH_ALEN);
  372. } else
  373. persistent = wpas_p2p_persistent_group(wpa_s,
  374. go_dev_addr,
  375. ssid->ssid,
  376. ssid->ssid_len);
  377. } else {
  378. ssid_txt = "";
  379. client = wpa_s->p2p_group_interface ==
  380. P2P_GROUP_INTERFACE_CLIENT;
  381. }
  382. wpa_s->show_group_started = 0;
  383. if (client) {
  384. /*
  385. * Indicate event only after successfully completed 4-way
  386. * handshake, i.e., when the interface is ready for data
  387. * packets.
  388. */
  389. wpa_s->show_group_started = 1;
  390. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  391. char psk[65];
  392. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  393. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  394. "%s GO ssid=\"%s\" psk=%s go_dev_addr=" MACSTR "%s",
  395. wpa_s->ifname, ssid_txt, psk, MAC2STR(go_dev_addr),
  396. persistent ? " [PERSISTENT]" : "");
  397. wpas_p2p_cross_connect_setup(wpa_s);
  398. } else {
  399. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  400. "%s GO ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
  401. MACSTR "%s",
  402. wpa_s->ifname, ssid_txt,
  403. ssid && ssid->passphrase ? ssid->passphrase : "",
  404. MAC2STR(go_dev_addr),
  405. persistent ? " [PERSISTENT]" : "");
  406. wpas_p2p_cross_connect_setup(wpa_s);
  407. }
  408. if (persistent)
  409. wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
  410. go_dev_addr);
  411. }
  412. static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
  413. {
  414. struct wpa_supplicant *wpa_s = eloop_ctx;
  415. struct wpa_supplicant *iface;
  416. int res;
  417. int without_roc;
  418. without_roc = wpa_s->pending_action_without_roc;
  419. wpa_s->pending_action_without_roc = 0;
  420. wpa_printf(MSG_DEBUG, "P2P: Send Action callback (without_roc=%d "
  421. "pending_action_tx=%p)",
  422. without_roc, wpa_s->pending_action_tx);
  423. if (wpa_s->pending_action_tx == NULL)
  424. return;
  425. if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
  426. wpa_s->pending_action_freq != 0) {
  427. wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
  428. "waiting for another freq=%u (off_channel_freq=%u)",
  429. wpa_s->pending_action_freq,
  430. wpa_s->off_channel_freq);
  431. if (without_roc && wpa_s->off_channel_freq == 0) {
  432. /*
  433. * We may get here if wpas_send_action() found us to be
  434. * on the correct channel, but remain-on-channel cancel
  435. * event was received before getting here.
  436. */
  437. wpa_printf(MSG_DEBUG, "P2P: Schedule "
  438. "remain-on-channel to send Action frame");
  439. if (wpa_drv_remain_on_channel(
  440. wpa_s, wpa_s->pending_action_freq, 200) <
  441. 0) {
  442. wpa_printf(MSG_DEBUG, "P2P: Failed to request "
  443. "driver to remain on channel (%u "
  444. "MHz) for Action Frame TX",
  445. wpa_s->pending_action_freq);
  446. } else
  447. wpa_s->roc_waiting_drv_freq =
  448. wpa_s->pending_action_freq;
  449. }
  450. return;
  451. }
  452. /*
  453. * This call is likely going to be on the P2P device instance if the
  454. * driver uses a separate interface for that purpose. However, some
  455. * Action frames are actually sent within a P2P Group and when that is
  456. * the case, we need to follow power saving (e.g., GO buffering the
  457. * frame for a client in PS mode or a client following the advertised
  458. * NoA from its GO). To make that easier for the driver, select the
  459. * correct group interface here.
  460. */
  461. if (os_memcmp(wpa_s->pending_action_src, wpa_s->own_addr, ETH_ALEN) !=
  462. 0) {
  463. /*
  464. * Try to find a group interface that matches with the source
  465. * address.
  466. */
  467. iface = wpa_s->global->ifaces;
  468. while (iface) {
  469. if (os_memcmp(wpa_s->pending_action_src,
  470. iface->own_addr, ETH_ALEN) == 0)
  471. break;
  472. iface = iface->next;
  473. }
  474. if (iface) {
  475. wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
  476. "instead of interface %s for Action TX",
  477. iface->ifname, wpa_s->ifname);
  478. } else
  479. iface = wpa_s;
  480. } else
  481. iface = wpa_s;
  482. wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
  483. MACSTR " using interface %s",
  484. MAC2STR(wpa_s->pending_action_dst), iface->ifname);
  485. res = wpa_drv_send_action(iface, wpa_s->pending_action_freq,
  486. wpa_s->pending_action_dst,
  487. wpa_s->pending_action_src,
  488. wpa_s->pending_action_bssid,
  489. wpabuf_head(wpa_s->pending_action_tx),
  490. wpabuf_len(wpa_s->pending_action_tx));
  491. if (res) {
  492. wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending "
  493. "Action frame");
  494. /*
  495. * Use fake TX status event to allow P2P state machine to
  496. * continue.
  497. */
  498. wpas_send_action_tx_status(
  499. wpa_s, wpa_s->pending_action_dst,
  500. wpabuf_head(wpa_s->pending_action_tx),
  501. wpabuf_len(wpa_s->pending_action_tx), 0);
  502. }
  503. }
  504. void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
  505. const u8 *data, size_t data_len, int ack)
  506. {
  507. if (wpa_s->global->p2p_disabled)
  508. return;
  509. if (wpa_s->pending_action_tx == NULL) {
  510. wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no "
  511. "pending operation");
  512. return;
  513. }
  514. if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
  515. wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown "
  516. "destination address");
  517. return;
  518. }
  519. wpabuf_free(wpa_s->pending_action_tx);
  520. wpa_s->pending_action_tx = NULL;
  521. p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq,
  522. wpa_s->pending_action_dst,
  523. wpa_s->pending_action_src,
  524. wpa_s->pending_action_bssid,
  525. ack);
  526. if (wpa_s->pending_pd_before_join &&
  527. (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
  528. ETH_ALEN) == 0 ||
  529. os_memcmp(wpa_s->pending_action_dst,
  530. wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  531. wpa_s->pending_pd_before_join = 0;
  532. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  533. "join-existing-group operation");
  534. wpas_p2p_join_start(wpa_s);
  535. }
  536. }
  537. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  538. const u8 *src, const u8 *bssid, const u8 *buf,
  539. size_t len, unsigned int wait_time)
  540. {
  541. struct wpa_supplicant *wpa_s = ctx;
  542. wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR
  543. " src=" MACSTR " bssid=" MACSTR " len=%d",
  544. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  545. (int) len);
  546. if (wpa_s->pending_action_tx) {
  547. wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
  548. "to " MACSTR, MAC2STR(wpa_s->pending_action_dst));
  549. wpabuf_free(wpa_s->pending_action_tx);
  550. }
  551. wpa_s->pending_action_tx = wpabuf_alloc(len);
  552. if (wpa_s->pending_action_tx == NULL) {
  553. wpa_printf(MSG_DEBUG, "P2P: Failed to allocate Action frame "
  554. "TX buffer (len=%llu)", (unsigned long long) len);
  555. return -1;
  556. }
  557. wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
  558. os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
  559. os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
  560. os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
  561. wpa_s->pending_action_freq = freq;
  562. if (wpa_s->off_channel_freq == freq || freq == 0) {
  563. wpa_printf(MSG_DEBUG, "P2P: Already on requested channel; "
  564. "send Action frame immediately");
  565. /* TODO: Would there ever be need to extend the current
  566. * duration on the channel? */
  567. wpa_s->pending_action_without_roc = 1;
  568. eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
  569. eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
  570. return 0;
  571. }
  572. wpa_s->pending_action_without_roc = 0;
  573. if (wpa_s->roc_waiting_drv_freq == freq) {
  574. wpa_printf(MSG_DEBUG, "P2P: Already waiting for driver to get "
  575. "to frequency %u MHz; continue waiting to send the "
  576. "Action frame", freq);
  577. return 0;
  578. }
  579. wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
  580. "once the driver gets to the requested channel");
  581. if (wait_time > wpa_s->max_remain_on_chan)
  582. wait_time = wpa_s->max_remain_on_chan;
  583. if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
  584. wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
  585. "to remain on channel (%u MHz) for Action "
  586. "Frame TX", freq);
  587. return -1;
  588. }
  589. wpa_s->roc_waiting_drv_freq = freq;
  590. return 0;
  591. }
  592. static void wpas_send_action_done(void *ctx)
  593. {
  594. struct wpa_supplicant *wpa_s = ctx;
  595. wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
  596. wpabuf_free(wpa_s->pending_action_tx);
  597. wpa_s->pending_action_tx = NULL;
  598. if (wpa_s->off_channel_freq) {
  599. wpa_drv_cancel_remain_on_channel(wpa_s);
  600. wpa_s->off_channel_freq = 0;
  601. wpa_s->roc_waiting_drv_freq = 0;
  602. }
  603. }
  604. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  605. struct p2p_go_neg_results *params)
  606. {
  607. if (wpa_s->go_params == NULL) {
  608. wpa_s->go_params = os_malloc(sizeof(*params));
  609. if (wpa_s->go_params == NULL)
  610. return -1;
  611. }
  612. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  613. return 0;
  614. }
  615. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  616. struct p2p_go_neg_results *res)
  617. {
  618. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  619. res->ssid, res->ssid_len);
  620. wpa_supplicant_ap_deinit(wpa_s);
  621. wpas_copy_go_neg_results(wpa_s, res);
  622. if (res->wps_method == WPS_PBC)
  623. wpas_wps_start_pbc(wpa_s, NULL /* res->peer_interface_addr */,
  624. 1);
  625. else {
  626. u16 dev_pw_id = DEV_PW_DEFAULT;
  627. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  628. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  629. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  630. wpa_s->p2p_pin, 1, dev_pw_id);
  631. }
  632. }
  633. static void p2p_go_configured(void *ctx, void *data)
  634. {
  635. struct wpa_supplicant *wpa_s = ctx;
  636. struct p2p_go_neg_results *params = data;
  637. struct wpa_ssid *ssid;
  638. ssid = wpa_s->current_ssid;
  639. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  640. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  641. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  642. "%s GO ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
  643. MACSTR "%s",
  644. wpa_s->ifname,
  645. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  646. params->passphrase ? params->passphrase : "",
  647. MAC2STR(wpa_s->parent->own_addr),
  648. params->persistent_group ? " [PERSISTENT]" : "");
  649. if (params->persistent_group)
  650. wpas_p2p_store_persistent_group(
  651. wpa_s->parent, ssid,
  652. wpa_s->parent->own_addr);
  653. wpas_p2p_cross_connect_setup(wpa_s);
  654. return;
  655. }
  656. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  657. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  658. params->peer_interface_addr)) {
  659. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  660. "filtering");
  661. return;
  662. }
  663. if (params->wps_method == WPS_PBC)
  664. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr);
  665. else if (wpa_s->p2p_pin[0])
  666. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  667. wpa_s->p2p_pin, NULL, 0);
  668. os_free(wpa_s->go_params);
  669. wpa_s->go_params = NULL;
  670. }
  671. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  672. struct p2p_go_neg_results *params,
  673. int group_formation)
  674. {
  675. struct wpa_ssid *ssid;
  676. if (wpas_copy_go_neg_results(wpa_s, params) < 0)
  677. return;
  678. ssid = wpa_config_add_network(wpa_s->conf);
  679. if (ssid == NULL)
  680. return;
  681. wpa_config_set_network_defaults(ssid);
  682. ssid->temporary = 1;
  683. ssid->p2p_group = 1;
  684. ssid->p2p_persistent_group = params->persistent_group;
  685. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  686. WPAS_MODE_P2P_GO;
  687. ssid->frequency = params->freq;
  688. ssid->ssid = os_zalloc(params->ssid_len + 1);
  689. if (ssid->ssid) {
  690. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  691. ssid->ssid_len = params->ssid_len;
  692. }
  693. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  694. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  695. ssid->proto = WPA_PROTO_RSN;
  696. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  697. ssid->passphrase = os_strdup(params->passphrase);
  698. wpa_s->ap_configured_cb = p2p_go_configured;
  699. wpa_s->ap_configured_cb_ctx = wpa_s;
  700. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  701. wpa_s->connect_without_scan = 1;
  702. wpa_s->reassociate = 1;
  703. wpa_s->disconnected = 0;
  704. wpa_supplicant_req_scan(wpa_s, 0, 0);
  705. }
  706. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  707. const struct wpa_supplicant *src)
  708. {
  709. struct wpa_config *d;
  710. const struct wpa_config *s;
  711. d = dst->conf;
  712. s = src->conf;
  713. #define C(n) if (s->n) d->n = os_strdup(s->n)
  714. C(device_name);
  715. C(manufacturer);
  716. C(model_name);
  717. C(model_number);
  718. C(serial_number);
  719. C(device_type);
  720. C(config_methods);
  721. #undef C
  722. }
  723. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  724. enum wpa_driver_if_type type)
  725. {
  726. char ifname[120], force_ifname[120];
  727. if (wpa_s->pending_interface_name[0]) {
  728. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  729. "- skip creation of a new one");
  730. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  731. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  732. "unknown?! ifname='%s'",
  733. wpa_s->pending_interface_name);
  734. return -1;
  735. }
  736. return 0;
  737. }
  738. os_snprintf(ifname, sizeof(ifname), "%s-p2p-%d", wpa_s->ifname,
  739. wpa_s->p2p_group_idx);
  740. force_ifname[0] = '\0';
  741. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  742. ifname);
  743. wpa_s->p2p_group_idx++;
  744. wpa_s->pending_interface_type = type;
  745. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  746. wpa_s->pending_interface_addr) < 0) {
  747. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  748. "interface");
  749. return -1;
  750. }
  751. if (force_ifname[0]) {
  752. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  753. force_ifname);
  754. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  755. sizeof(wpa_s->pending_interface_name));
  756. } else
  757. os_strlcpy(wpa_s->pending_interface_name, ifname,
  758. sizeof(wpa_s->pending_interface_name));
  759. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  760. MACSTR, wpa_s->pending_interface_name,
  761. MAC2STR(wpa_s->pending_interface_addr));
  762. return 0;
  763. }
  764. static void wpas_p2p_remove_pending_group_interface(
  765. struct wpa_supplicant *wpa_s)
  766. {
  767. if (!wpa_s->pending_interface_name[0] ||
  768. is_zero_ether_addr(wpa_s->pending_interface_addr))
  769. return; /* No pending virtual interface */
  770. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  771. wpa_s->pending_interface_name);
  772. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  773. wpa_s->pending_interface_name);
  774. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  775. wpa_s->pending_interface_name[0] = '\0';
  776. }
  777. static struct wpa_supplicant *
  778. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  779. {
  780. struct wpa_interface iface;
  781. struct wpa_supplicant *group_wpa_s;
  782. if (!wpa_s->pending_interface_name[0]) {
  783. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  784. return NULL;
  785. }
  786. os_memset(&iface, 0, sizeof(iface));
  787. iface.ifname = wpa_s->pending_interface_name;
  788. iface.driver = wpa_s->driver->name;
  789. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  790. iface.driver_param = wpa_s->conf->driver_param;
  791. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  792. if (group_wpa_s == NULL) {
  793. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  794. "wpa_supplicant interface");
  795. return NULL;
  796. }
  797. wpa_s->pending_interface_name[0] = '\0';
  798. group_wpa_s->parent = wpa_s;
  799. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  800. P2P_GROUP_INTERFACE_CLIENT;
  801. wpa_s->global->p2p_group_formation = group_wpa_s;
  802. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  803. return group_wpa_s;
  804. }
  805. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  806. void *timeout_ctx)
  807. {
  808. struct wpa_supplicant *wpa_s = eloop_ctx;
  809. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  810. if (wpa_s->global->p2p)
  811. p2p_group_formation_failed(wpa_s->global->p2p);
  812. wpas_group_formation_completed(wpa_s, 0);
  813. }
  814. void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  815. {
  816. struct wpa_supplicant *wpa_s = ctx;
  817. if (wpa_s->off_channel_freq) {
  818. wpa_drv_cancel_remain_on_channel(wpa_s);
  819. wpa_s->off_channel_freq = 0;
  820. wpa_s->roc_waiting_drv_freq = 0;
  821. }
  822. if (res->status) {
  823. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
  824. res->status);
  825. wpas_p2p_remove_pending_group_interface(wpa_s);
  826. return;
  827. }
  828. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
  829. if (wpa_s->create_p2p_iface) {
  830. struct wpa_supplicant *group_wpa_s =
  831. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  832. if (group_wpa_s == NULL) {
  833. wpas_p2p_remove_pending_group_interface(wpa_s);
  834. return;
  835. }
  836. if (group_wpa_s != wpa_s) {
  837. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  838. sizeof(group_wpa_s->p2p_pin));
  839. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  840. }
  841. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  842. wpa_s->pending_interface_name[0] = '\0';
  843. group_wpa_s->p2p_in_provisioning = 1;
  844. if (res->role_go)
  845. wpas_start_wps_go(group_wpa_s, res, 1);
  846. else
  847. wpas_start_wps_enrollee(group_wpa_s, res);
  848. } else {
  849. wpa_s->p2p_in_provisioning = 1;
  850. wpa_s->global->p2p_group_formation = wpa_s;
  851. if (res->role_go)
  852. wpas_start_wps_go(wpa_s, res, 1);
  853. else
  854. wpas_start_wps_enrollee(ctx, res);
  855. }
  856. wpa_s->p2p_long_listen = 0;
  857. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  858. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  859. /* TODO: add peer Config Timeout */
  860. eloop_register_timeout(15, 0, wpas_p2p_group_formation_timeout, wpa_s,
  861. NULL);
  862. }
  863. void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  864. {
  865. struct wpa_supplicant *wpa_s = ctx;
  866. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  867. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  868. }
  869. void wpas_dev_found(void *ctx, const u8 *addr, const u8 *dev_addr,
  870. const u8 *pri_dev_type, const char *dev_name,
  871. u16 config_methods, u8 dev_capab, u8 group_capab)
  872. {
  873. struct wpa_supplicant *wpa_s = ctx;
  874. char devtype[WPS_DEV_TYPE_BUFSIZE];
  875. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  876. " p2p_dev_addr=" MACSTR
  877. " pri_dev_type=%s name='%s' config_methods=0x%x "
  878. "dev_capab=0x%x group_capab=0x%x",
  879. MAC2STR(addr), MAC2STR(dev_addr),
  880. wps_dev_type_bin2str(pri_dev_type, devtype, sizeof(devtype)),
  881. dev_name, config_methods, dev_capab, group_capab);
  882. }
  883. static int wpas_start_listen(void *ctx, unsigned int freq,
  884. unsigned int duration,
  885. const struct wpabuf *probe_resp_ie)
  886. {
  887. struct wpa_supplicant *wpa_s = ctx;
  888. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie);
  889. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  890. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  891. "report received Probe Request frames");
  892. return -1;
  893. }
  894. wpa_s->pending_listen_freq = freq;
  895. wpa_s->pending_listen_duration = duration;
  896. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  897. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  898. "to remain on channel (%u MHz) for Listen "
  899. "state", freq);
  900. wpa_s->pending_listen_freq = 0;
  901. return -1;
  902. }
  903. wpa_s->roc_waiting_drv_freq = freq;
  904. return 0;
  905. }
  906. static void wpas_stop_listen(void *ctx)
  907. {
  908. struct wpa_supplicant *wpa_s = ctx;
  909. if (wpa_s->off_channel_freq) {
  910. wpa_drv_cancel_remain_on_channel(wpa_s);
  911. wpa_s->off_channel_freq = 0;
  912. wpa_s->roc_waiting_drv_freq = 0;
  913. }
  914. wpa_drv_probe_req_report(wpa_s, 0);
  915. }
  916. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  917. {
  918. struct wpa_supplicant *wpa_s = ctx;
  919. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
  920. }
  921. static struct p2p_srv_bonjour *
  922. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  923. const struct wpabuf *query)
  924. {
  925. struct p2p_srv_bonjour *bsrv;
  926. size_t len;
  927. len = wpabuf_len(query);
  928. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  929. struct p2p_srv_bonjour, list) {
  930. if (len == wpabuf_len(bsrv->query) &&
  931. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  932. len) == 0)
  933. return bsrv;
  934. }
  935. return NULL;
  936. }
  937. static struct p2p_srv_upnp *
  938. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  939. const char *service)
  940. {
  941. struct p2p_srv_upnp *usrv;
  942. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  943. struct p2p_srv_upnp, list) {
  944. if (version == usrv->version &&
  945. os_strcmp(service, usrv->service) == 0)
  946. return usrv;
  947. }
  948. return NULL;
  949. }
  950. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  951. u8 srv_trans_id)
  952. {
  953. u8 *len_pos;
  954. if (wpabuf_tailroom(resp) < 5)
  955. return;
  956. /* Length (to be filled) */
  957. len_pos = wpabuf_put(resp, 2);
  958. wpabuf_put_u8(resp, srv_proto);
  959. wpabuf_put_u8(resp, srv_trans_id);
  960. /* Status Code */
  961. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  962. /* Response Data: empty */
  963. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  964. }
  965. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  966. struct wpabuf *resp, u8 srv_trans_id)
  967. {
  968. struct p2p_srv_bonjour *bsrv;
  969. u8 *len_pos;
  970. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  971. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  972. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  973. return;
  974. }
  975. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  976. struct p2p_srv_bonjour, list) {
  977. if (wpabuf_tailroom(resp) <
  978. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  979. return;
  980. /* Length (to be filled) */
  981. len_pos = wpabuf_put(resp, 2);
  982. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  983. wpabuf_put_u8(resp, srv_trans_id);
  984. /* Status Code */
  985. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  986. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  987. wpabuf_head(bsrv->resp),
  988. wpabuf_len(bsrv->resp));
  989. /* Response Data */
  990. wpabuf_put_buf(resp, bsrv->query); /* Key */
  991. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  992. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  993. 2);
  994. }
  995. }
  996. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  997. struct wpabuf *resp, u8 srv_trans_id,
  998. const u8 *query, size_t query_len)
  999. {
  1000. struct p2p_srv_bonjour *bsrv;
  1001. struct wpabuf buf;
  1002. u8 *len_pos;
  1003. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1004. query, query_len);
  1005. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1006. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1007. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1008. srv_trans_id);
  1009. return;
  1010. }
  1011. if (query_len == 0) {
  1012. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1013. return;
  1014. }
  1015. if (wpabuf_tailroom(resp) < 5)
  1016. return;
  1017. /* Length (to be filled) */
  1018. len_pos = wpabuf_put(resp, 2);
  1019. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1020. wpabuf_put_u8(resp, srv_trans_id);
  1021. wpabuf_set(&buf, query, query_len);
  1022. bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
  1023. if (bsrv == NULL) {
  1024. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1025. "available");
  1026. /* Status Code */
  1027. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1028. /* Response Data: empty */
  1029. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1030. 2);
  1031. return;
  1032. }
  1033. /* Status Code */
  1034. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1035. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1036. wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
  1037. if (wpabuf_tailroom(resp) >=
  1038. wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
  1039. /* Response Data */
  1040. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1041. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1042. }
  1043. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1044. }
  1045. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1046. struct wpabuf *resp, u8 srv_trans_id)
  1047. {
  1048. struct p2p_srv_upnp *usrv;
  1049. u8 *len_pos;
  1050. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1051. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1052. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1053. return;
  1054. }
  1055. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1056. struct p2p_srv_upnp, list) {
  1057. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1058. return;
  1059. /* Length (to be filled) */
  1060. len_pos = wpabuf_put(resp, 2);
  1061. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1062. wpabuf_put_u8(resp, srv_trans_id);
  1063. /* Status Code */
  1064. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1065. /* Response Data */
  1066. wpabuf_put_u8(resp, usrv->version);
  1067. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1068. usrv->service);
  1069. wpabuf_put_str(resp, usrv->service);
  1070. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1071. 2);
  1072. }
  1073. }
  1074. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1075. struct wpabuf *resp, u8 srv_trans_id,
  1076. const u8 *query, size_t query_len)
  1077. {
  1078. struct p2p_srv_upnp *usrv;
  1079. u8 *len_pos;
  1080. u8 version;
  1081. char *str;
  1082. int count = 0;
  1083. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1084. query, query_len);
  1085. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1086. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1087. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1088. srv_trans_id);
  1089. return;
  1090. }
  1091. if (query_len == 0) {
  1092. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1093. return;
  1094. }
  1095. version = query[0];
  1096. str = os_malloc(query_len);
  1097. if (str == NULL)
  1098. return;
  1099. os_memcpy(str, query + 1, query_len - 1);
  1100. str[query_len - 1] = '\0';
  1101. if (wpabuf_tailroom(resp) < 5)
  1102. return;
  1103. /* Length (to be filled) */
  1104. len_pos = wpabuf_put(resp, 2);
  1105. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1106. wpabuf_put_u8(resp, srv_trans_id);
  1107. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1108. struct p2p_srv_upnp, list) {
  1109. if (version != usrv->version)
  1110. continue;
  1111. if (os_strcmp(str, "ssdp:all") != 0 &&
  1112. os_strstr(usrv->service, str) == NULL)
  1113. continue;
  1114. if (wpabuf_tailroom(resp) < 2)
  1115. break;
  1116. if (count == 0) {
  1117. /* Status Code */
  1118. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1119. /* Response Data */
  1120. wpabuf_put_u8(resp, version);
  1121. } else
  1122. wpabuf_put_u8(resp, ',');
  1123. count++;
  1124. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1125. usrv->service);
  1126. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1127. break;
  1128. wpabuf_put_str(resp, usrv->service);
  1129. }
  1130. if (count == 0) {
  1131. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1132. "available");
  1133. /* Status Code */
  1134. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1135. /* Response Data: empty */
  1136. }
  1137. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1138. }
  1139. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1140. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1141. {
  1142. struct wpa_supplicant *wpa_s = ctx;
  1143. const u8 *pos = tlvs;
  1144. const u8 *end = tlvs + tlvs_len;
  1145. const u8 *tlv_end;
  1146. u16 slen;
  1147. struct wpabuf *resp;
  1148. u8 srv_proto, srv_trans_id;
  1149. size_t buf_len;
  1150. char *buf;
  1151. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1152. tlvs, tlvs_len);
  1153. buf_len = 2 * tlvs_len + 1;
  1154. buf = os_malloc(buf_len);
  1155. if (buf) {
  1156. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1157. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1158. MACSTR " %u %u %s",
  1159. freq, MAC2STR(sa), dialog_token, update_indic,
  1160. buf);
  1161. os_free(buf);
  1162. }
  1163. if (wpa_s->p2p_sd_over_ctrl_iface)
  1164. return; /* to be processed by an external program */
  1165. resp = wpabuf_alloc(10000);
  1166. if (resp == NULL)
  1167. return;
  1168. while (pos + 1 < end) {
  1169. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1170. slen = WPA_GET_LE16(pos);
  1171. pos += 2;
  1172. if (pos + slen > end || slen < 2) {
  1173. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1174. "length");
  1175. wpabuf_free(resp);
  1176. return;
  1177. }
  1178. tlv_end = pos + slen;
  1179. srv_proto = *pos++;
  1180. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1181. srv_proto);
  1182. srv_trans_id = *pos++;
  1183. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1184. srv_trans_id);
  1185. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1186. pos, tlv_end - pos);
  1187. if (wpa_s->force_long_sd) {
  1188. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1189. "response");
  1190. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1191. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1192. goto done;
  1193. }
  1194. switch (srv_proto) {
  1195. case P2P_SERV_ALL_SERVICES:
  1196. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1197. "for all services");
  1198. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1199. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1200. wpa_printf(MSG_DEBUG, "P2P: No service "
  1201. "discovery protocols available");
  1202. wpas_sd_add_proto_not_avail(
  1203. resp, P2P_SERV_ALL_SERVICES,
  1204. srv_trans_id);
  1205. break;
  1206. }
  1207. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1208. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1209. break;
  1210. case P2P_SERV_BONJOUR:
  1211. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1212. pos, tlv_end - pos);
  1213. break;
  1214. case P2P_SERV_UPNP:
  1215. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1216. pos, tlv_end - pos);
  1217. break;
  1218. default:
  1219. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1220. "protocol %u", srv_proto);
  1221. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1222. srv_trans_id);
  1223. break;
  1224. }
  1225. pos = tlv_end;
  1226. }
  1227. done:
  1228. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1229. wpabuf_free(resp);
  1230. }
  1231. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1232. const u8 *tlvs, size_t tlvs_len)
  1233. {
  1234. struct wpa_supplicant *wpa_s = ctx;
  1235. const u8 *pos = tlvs;
  1236. const u8 *end = tlvs + tlvs_len;
  1237. const u8 *tlv_end;
  1238. u16 slen;
  1239. size_t buf_len;
  1240. char *buf;
  1241. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1242. tlvs, tlvs_len);
  1243. if (tlvs_len > 1500) {
  1244. /* TODO: better way for handling this */
  1245. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1246. P2P_EVENT_SERV_DISC_RESP MACSTR
  1247. " %u <long response: %u bytes>",
  1248. MAC2STR(sa), update_indic,
  1249. (unsigned int) tlvs_len);
  1250. } else {
  1251. buf_len = 2 * tlvs_len + 1;
  1252. buf = os_malloc(buf_len);
  1253. if (buf) {
  1254. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1255. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1256. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1257. MAC2STR(sa), update_indic, buf);
  1258. os_free(buf);
  1259. }
  1260. }
  1261. while (pos < end) {
  1262. u8 srv_proto, srv_trans_id, status;
  1263. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1264. slen = WPA_GET_LE16(pos);
  1265. pos += 2;
  1266. if (pos + slen > end || slen < 3) {
  1267. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1268. "length");
  1269. return;
  1270. }
  1271. tlv_end = pos + slen;
  1272. srv_proto = *pos++;
  1273. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1274. srv_proto);
  1275. srv_trans_id = *pos++;
  1276. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1277. srv_trans_id);
  1278. status = *pos++;
  1279. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1280. status);
  1281. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1282. pos, tlv_end - pos);
  1283. pos = tlv_end;
  1284. }
  1285. }
  1286. void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1287. const struct wpabuf *tlvs)
  1288. {
  1289. return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1290. }
  1291. void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1292. u8 version, const char *query)
  1293. {
  1294. struct wpabuf *tlvs;
  1295. void *ret;
  1296. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1297. if (tlvs == NULL)
  1298. return NULL;
  1299. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1300. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1301. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1302. wpabuf_put_u8(tlvs, version);
  1303. wpabuf_put_str(tlvs, query);
  1304. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1305. wpabuf_free(tlvs);
  1306. return ret;
  1307. }
  1308. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
  1309. {
  1310. return p2p_sd_cancel_request(wpa_s->global->p2p, req);
  1311. }
  1312. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1313. const u8 *dst, u8 dialog_token,
  1314. const struct wpabuf *resp_tlvs)
  1315. {
  1316. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1317. resp_tlvs);
  1318. }
  1319. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1320. {
  1321. p2p_sd_service_update(wpa_s->global->p2p);
  1322. }
  1323. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1324. {
  1325. dl_list_del(&bsrv->list);
  1326. wpabuf_free(bsrv->query);
  1327. wpabuf_free(bsrv->resp);
  1328. os_free(bsrv);
  1329. }
  1330. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1331. {
  1332. dl_list_del(&usrv->list);
  1333. os_free(usrv->service);
  1334. os_free(usrv);
  1335. }
  1336. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1337. {
  1338. struct p2p_srv_bonjour *bsrv, *bn;
  1339. struct p2p_srv_upnp *usrv, *un;
  1340. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1341. struct p2p_srv_bonjour, list)
  1342. wpas_p2p_srv_bonjour_free(bsrv);
  1343. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1344. struct p2p_srv_upnp, list)
  1345. wpas_p2p_srv_upnp_free(usrv);
  1346. wpas_p2p_sd_service_update(wpa_s);
  1347. }
  1348. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1349. struct wpabuf *query, struct wpabuf *resp)
  1350. {
  1351. struct p2p_srv_bonjour *bsrv;
  1352. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1353. if (bsrv) {
  1354. wpabuf_free(query);
  1355. wpabuf_free(bsrv->resp);
  1356. bsrv->resp = resp;
  1357. return 0;
  1358. }
  1359. bsrv = os_zalloc(sizeof(*bsrv));
  1360. if (bsrv == NULL)
  1361. return -1;
  1362. bsrv->query = query;
  1363. bsrv->resp = resp;
  1364. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  1365. wpas_p2p_sd_service_update(wpa_s);
  1366. return 0;
  1367. }
  1368. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1369. const struct wpabuf *query)
  1370. {
  1371. struct p2p_srv_bonjour *bsrv;
  1372. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1373. if (bsrv == NULL)
  1374. return -1;
  1375. wpas_p2p_srv_bonjour_free(bsrv);
  1376. wpas_p2p_sd_service_update(wpa_s);
  1377. return 0;
  1378. }
  1379. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1380. const char *service)
  1381. {
  1382. struct p2p_srv_upnp *usrv;
  1383. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  1384. return 0; /* Already listed */
  1385. usrv = os_zalloc(sizeof(*usrv));
  1386. if (usrv == NULL)
  1387. return -1;
  1388. usrv->version = version;
  1389. usrv->service = os_strdup(service);
  1390. if (usrv->service == NULL) {
  1391. os_free(usrv);
  1392. return -1;
  1393. }
  1394. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  1395. wpas_p2p_sd_service_update(wpa_s);
  1396. return 0;
  1397. }
  1398. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1399. const char *service)
  1400. {
  1401. struct p2p_srv_upnp *usrv;
  1402. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  1403. if (usrv == NULL)
  1404. return -1;
  1405. wpas_p2p_srv_upnp_free(usrv);
  1406. wpas_p2p_sd_service_update(wpa_s);
  1407. return 0;
  1408. }
  1409. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  1410. const u8 *peer, const char *params)
  1411. {
  1412. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
  1413. MAC2STR(peer), wps_generate_pin(), params);
  1414. }
  1415. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  1416. const u8 *peer, const char *params)
  1417. {
  1418. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
  1419. MAC2STR(peer), params);
  1420. }
  1421. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  1422. const u8 *dev_addr, const u8 *pri_dev_type,
  1423. const char *dev_name, u16 supp_config_methods,
  1424. u8 dev_capab, u8 group_capab)
  1425. {
  1426. struct wpa_supplicant *wpa_s = ctx;
  1427. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1428. char params[200];
  1429. u8 empty_dev_type[8];
  1430. if (pri_dev_type == NULL) {
  1431. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  1432. pri_dev_type = empty_dev_type;
  1433. }
  1434. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  1435. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1436. "dev_capab=0x%x group_capab=0x%x",
  1437. MAC2STR(dev_addr),
  1438. wps_dev_type_bin2str(pri_dev_type, devtype,
  1439. sizeof(devtype)),
  1440. dev_name, supp_config_methods, dev_capab, group_capab);
  1441. params[sizeof(params) - 1] = '\0';
  1442. if (config_methods & WPS_CONFIG_DISPLAY)
  1443. wpas_prov_disc_local_display(wpa_s, peer, params);
  1444. else if (config_methods & WPS_CONFIG_KEYPAD)
  1445. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  1446. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1447. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
  1448. "%s", MAC2STR(peer), params);
  1449. }
  1450. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  1451. {
  1452. struct wpa_supplicant *wpa_s = ctx;
  1453. if (config_methods & WPS_CONFIG_DISPLAY)
  1454. wpas_prov_disc_local_keypad(wpa_s, peer, "");
  1455. else if (config_methods & WPS_CONFIG_KEYPAD)
  1456. wpas_prov_disc_local_display(wpa_s, peer, "");
  1457. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1458. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
  1459. MAC2STR(peer));
  1460. if (wpa_s->pending_pd_before_join &&
  1461. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  1462. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  1463. wpa_s->pending_pd_before_join = 0;
  1464. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  1465. "join-existing-group operation");
  1466. wpas_p2p_join_start(wpa_s);
  1467. }
  1468. }
  1469. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  1470. const u8 *go_dev_addr, const u8 *ssid,
  1471. size_t ssid_len, int *go, u8 *group_bssid,
  1472. int *force_freq, int persistent_group)
  1473. {
  1474. struct wpa_supplicant *wpa_s = ctx;
  1475. struct wpa_ssid *s;
  1476. u8 cur_bssid[ETH_ALEN];
  1477. int res;
  1478. if (!persistent_group) {
  1479. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  1480. " to join an active group", MAC2STR(sa));
  1481. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  1482. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  1483. == 0 ||
  1484. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  1485. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  1486. "authorized invitation");
  1487. goto accept_inv;
  1488. }
  1489. /*
  1490. * Do not accept the invitation automatically; notify user and
  1491. * request approval.
  1492. */
  1493. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  1494. }
  1495. if (!wpa_s->conf->persistent_reconnect)
  1496. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  1497. for (s = wpa_s->conf->ssid; s; s = s->next) {
  1498. if (s->disabled == 2 &&
  1499. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  1500. s->ssid_len == ssid_len &&
  1501. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  1502. break;
  1503. }
  1504. if (!s) {
  1505. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  1506. " requested reinvocation of an unknown group",
  1507. MAC2STR(sa));
  1508. return P2P_SC_FAIL_UNKNOWN_GROUP;
  1509. }
  1510. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  1511. *go = 1;
  1512. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  1513. wpa_printf(MSG_DEBUG, "P2P: The only available "
  1514. "interface is already in use - reject "
  1515. "invitation");
  1516. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  1517. }
  1518. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  1519. } else if (s->mode == WPAS_MODE_P2P_GO) {
  1520. *go = 1;
  1521. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  1522. {
  1523. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  1524. "interface address for the group");
  1525. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  1526. }
  1527. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  1528. ETH_ALEN);
  1529. }
  1530. accept_inv:
  1531. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
  1532. wpa_s->assoc_freq) {
  1533. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  1534. "the channel we are already using");
  1535. *force_freq = wpa_s->assoc_freq;
  1536. }
  1537. res = wpa_drv_shared_freq(wpa_s);
  1538. if (res > 0) {
  1539. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  1540. "with the channel we are already using on a "
  1541. "shared interface");
  1542. *force_freq = res;
  1543. }
  1544. return P2P_SC_SUCCESS;
  1545. }
  1546. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  1547. const u8 *ssid, size_t ssid_len,
  1548. const u8 *go_dev_addr, u8 status,
  1549. int op_freq)
  1550. {
  1551. struct wpa_supplicant *wpa_s = ctx;
  1552. struct wpa_ssid *s;
  1553. for (s = wpa_s->conf->ssid; s; s = s->next) {
  1554. if (s->disabled == 2 &&
  1555. s->ssid_len == ssid_len &&
  1556. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  1557. break;
  1558. }
  1559. if (status == P2P_SC_SUCCESS) {
  1560. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  1561. " was accepted; op_freq=%d MHz",
  1562. MAC2STR(sa), op_freq);
  1563. if (s) {
  1564. wpas_p2p_group_add_persistent(
  1565. wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
  1566. } else {
  1567. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  1568. wpa_s->p2p_wps_method);
  1569. }
  1570. return;
  1571. }
  1572. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  1573. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  1574. " was rejected (status %u)", MAC2STR(sa), status);
  1575. return;
  1576. }
  1577. if (!s) {
  1578. if (bssid) {
  1579. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  1580. "sa=" MACSTR " go_dev_addr=" MACSTR
  1581. " bssid=" MACSTR " unknown-network",
  1582. MAC2STR(sa), MAC2STR(go_dev_addr),
  1583. MAC2STR(bssid));
  1584. } else {
  1585. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  1586. "sa=" MACSTR " go_dev_addr=" MACSTR
  1587. " unknown-network",
  1588. MAC2STR(sa), MAC2STR(go_dev_addr));
  1589. }
  1590. return;
  1591. }
  1592. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
  1593. " persistent=%d", MAC2STR(sa), s->id);
  1594. }
  1595. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
  1596. {
  1597. struct wpa_supplicant *wpa_s = ctx;
  1598. struct wpa_ssid *ssid;
  1599. if (bssid) {
  1600. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  1601. "status=%d " MACSTR,
  1602. status, MAC2STR(bssid));
  1603. } else {
  1604. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  1605. "status=%d ", status);
  1606. }
  1607. if (wpa_s->pending_invite_ssid_id == -1)
  1608. return; /* Invitation to active group */
  1609. if (status != P2P_SC_SUCCESS) {
  1610. wpas_p2p_remove_pending_group_interface(wpa_s);
  1611. return;
  1612. }
  1613. ssid = wpa_config_get_network(wpa_s->conf,
  1614. wpa_s->pending_invite_ssid_id);
  1615. if (ssid == NULL) {
  1616. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  1617. "data matching with invitation");
  1618. return;
  1619. }
  1620. wpas_p2p_group_add_persistent(wpa_s, ssid,
  1621. ssid->mode == WPAS_MODE_P2P_GO, 0);
  1622. }
  1623. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  1624. struct p2p_config *p2p)
  1625. {
  1626. struct hostapd_hw_modes *modes;
  1627. u16 num_modes, flags;
  1628. int i, cla;
  1629. int band24 = 0, band5_low = 0, band5_high = 0;
  1630. /* TODO: more detailed selection of channels within reg_class based on
  1631. * driver capabilities */
  1632. modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
  1633. if (modes == NULL) {
  1634. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  1635. "of all supported channels; assume dualband "
  1636. "support");
  1637. band24 = band5_low = band5_high = 1;
  1638. } else {
  1639. for (i = 0; i < num_modes; i++) {
  1640. struct hostapd_hw_modes *mode;
  1641. mode = &modes[i];
  1642. if (mode->mode == HOSTAPD_MODE_IEEE80211G) {
  1643. band24 = 1;
  1644. } else if (mode->mode == HOSTAPD_MODE_IEEE80211A) {
  1645. int j;
  1646. for (j = 0; j < mode->num_channels; j++) {
  1647. struct hostapd_channel_data *ch;
  1648. ch = &mode->channels[j];
  1649. if (ch->chan == 36)
  1650. band5_low = 1;
  1651. else if (ch->chan == 157)
  1652. band5_high = 1;
  1653. }
  1654. }
  1655. }
  1656. }
  1657. cla = 0;
  1658. if (band24) {
  1659. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
  1660. "2.4 GHz band");
  1661. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  1662. p2p->channels.reg_class[cla].reg_class = 81;
  1663. #if 0
  1664. p2p->channels.reg_class[cla].channels = 13;
  1665. for (i = 0; i < 13; i++)
  1666. p2p->channels.reg_class[cla].channel[i] = i + 1;
  1667. #else
  1668. p2p->channels.reg_class[cla].channels = 11;
  1669. for (i = 0; i < 11; i++)
  1670. p2p->channels.reg_class[cla].channel[i] = i + 1;
  1671. #endif
  1672. cla++;
  1673. #if 0
  1674. /* Operating class 82 - 2.4 GHz band channel 14 */
  1675. p2p->channels.reg_class[cla].reg_class = 82;
  1676. p2p->channels.reg_class[cla].channels = 1;
  1677. p2p->channels.reg_class[cla].channel[0] = 14;
  1678. cla++;
  1679. #endif
  1680. #if 0
  1681. /* Operating class 83 - 2.4 GHz band channels 1..9; 40 MHz */
  1682. p2p->channels.reg_class[cla].reg_class = 83;
  1683. p2p->channels.reg_class[cla].channels = 9;
  1684. for (i = 0; i < 9; i++)
  1685. p2p->channels.reg_class[cla].channel[i] = i + 1;
  1686. cla++;
  1687. /* Operating class 84 - 2.4 GHz band channels 5..13; 40 MHz */
  1688. p2p->channels.reg_class[cla].reg_class = 84;
  1689. p2p->channels.reg_class[cla].channels = 9;
  1690. for (i = 0; i < 9; i++)
  1691. p2p->channels.reg_class[cla].channel[i] = i + 5;
  1692. cla++;
  1693. #endif
  1694. }
  1695. if (band5_low) {
  1696. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
  1697. "lower 5 GHz band");
  1698. /* Operating class 115 - 5 GHz, channels 36-48 */
  1699. p2p->channels.reg_class[cla].reg_class = 115;
  1700. p2p->channels.reg_class[cla].channels = 4;
  1701. p2p->channels.reg_class[cla].channel[0] = 36;
  1702. p2p->channels.reg_class[cla].channel[1] = 40;
  1703. p2p->channels.reg_class[cla].channel[2] = 44;
  1704. p2p->channels.reg_class[cla].channel[3] = 48;
  1705. cla++;
  1706. #if 0
  1707. /* Operating class 116 - 5 GHz, channels 36,44; 40 MHz */
  1708. p2p->channels.reg_class[cla].reg_class = 116;
  1709. p2p->channels.reg_class[cla].channels = 2;
  1710. p2p->channels.reg_class[cla].channel[0] = 36;
  1711. p2p->channels.reg_class[cla].channel[1] = 44;
  1712. cla++;
  1713. /* Operating class 117 - 5 GHz, channels 40,48; 40 MHz */
  1714. p2p->channels.reg_class[cla].reg_class = 117;
  1715. p2p->channels.reg_class[cla].channels = 2;
  1716. p2p->channels.reg_class[cla].channel[0] = 40;
  1717. p2p->channels.reg_class[cla].channel[1] = 48;
  1718. cla++;
  1719. #endif
  1720. }
  1721. if (band5_high) {
  1722. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
  1723. "higher 5 GHz band");
  1724. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  1725. p2p->channels.reg_class[cla].reg_class = 124;
  1726. p2p->channels.reg_class[cla].channels = 4;
  1727. p2p->channels.reg_class[cla].channel[0] = 149;
  1728. p2p->channels.reg_class[cla].channel[1] = 153;
  1729. p2p->channels.reg_class[cla].channel[2] = 157;
  1730. p2p->channels.reg_class[cla].channel[3] = 161;
  1731. cla++;
  1732. #if 0
  1733. /* Operating class 126 - 5 GHz, channels 149,157; 40 MHz */
  1734. p2p->channels.reg_class[cla].reg_class = 126;
  1735. p2p->channels.reg_class[cla].channels = 2;
  1736. p2p->channels.reg_class[cla].channel[0] = 149;
  1737. p2p->channels.reg_class[cla].channel[1] = 157;
  1738. cla++;
  1739. /* Operating class 127 - 5 GHz, channels 153,161; 40 MHz */
  1740. p2p->channels.reg_class[cla].reg_class = 127;
  1741. p2p->channels.reg_class[cla].channels = 2;
  1742. p2p->channels.reg_class[cla].channel[0] = 153;
  1743. p2p->channels.reg_class[cla].channel[1] = 161;
  1744. cla++;
  1745. #endif
  1746. }
  1747. p2p->channels.reg_classes = cla;
  1748. if (modes)
  1749. ieee80211_sta_free_hw_features(modes, num_modes);
  1750. return 0;
  1751. }
  1752. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  1753. size_t buf_len)
  1754. {
  1755. struct wpa_supplicant *wpa_s = ctx;
  1756. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  1757. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  1758. break;
  1759. }
  1760. if (wpa_s == NULL)
  1761. return -1;
  1762. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  1763. }
  1764. /**
  1765. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  1766. * @global: Pointer to global data from wpa_supplicant_init()
  1767. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  1768. * Returns: 0 on success, -1 on failure
  1769. */
  1770. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  1771. {
  1772. struct p2p_config p2p;
  1773. unsigned int r;
  1774. int i;
  1775. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  1776. return 0;
  1777. #ifdef CONFIG_CLIENT_MLME
  1778. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
  1779. wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
  1780. wpa_s->mlme.public_action_cb_ctx = wpa_s;
  1781. }
  1782. #endif /* CONFIG_CLIENT_MLME */
  1783. if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
  1784. wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
  1785. /* Continue anyway; this is not really a fatal error */
  1786. }
  1787. if (global->p2p)
  1788. return 0;
  1789. os_memset(&p2p, 0, sizeof(p2p));
  1790. p2p.msg_ctx = wpa_s;
  1791. p2p.cb_ctx = wpa_s;
  1792. p2p.p2p_scan = wpas_p2p_scan;
  1793. p2p.send_action = wpas_send_action;
  1794. p2p.send_action_done = wpas_send_action_done;
  1795. p2p.go_neg_completed = wpas_go_neg_completed;
  1796. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  1797. p2p.dev_found = wpas_dev_found;
  1798. p2p.start_listen = wpas_start_listen;
  1799. p2p.stop_listen = wpas_stop_listen;
  1800. p2p.send_probe_resp = wpas_send_probe_resp;
  1801. p2p.sd_request = wpas_sd_request;
  1802. p2p.sd_response = wpas_sd_response;
  1803. p2p.prov_disc_req = wpas_prov_disc_req;
  1804. p2p.prov_disc_resp = wpas_prov_disc_resp;
  1805. p2p.invitation_process = wpas_invitation_process;
  1806. p2p.invitation_received = wpas_invitation_received;
  1807. p2p.invitation_result = wpas_invitation_result;
  1808. p2p.get_noa = wpas_get_noa;
  1809. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  1810. os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
  1811. p2p.dev_name = wpa_s->conf->device_name;
  1812. if (wpa_s->conf->p2p_listen_reg_class &&
  1813. wpa_s->conf->p2p_listen_channel) {
  1814. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  1815. p2p.channel = wpa_s->conf->p2p_listen_channel;
  1816. } else {
  1817. p2p.reg_class = 81;
  1818. /*
  1819. * Pick one of the social channels randomly as the listen
  1820. * channel.
  1821. */
  1822. os_get_random((u8 *) &r, sizeof(r));
  1823. p2p.channel = 1 + (r % 3) * 5;
  1824. }
  1825. if (wpa_s->conf->p2p_oper_reg_class &&
  1826. wpa_s->conf->p2p_oper_channel) {
  1827. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  1828. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  1829. } else {
  1830. p2p.op_reg_class = 81;
  1831. /*
  1832. * For initial tests, pick the operation channel randomly.
  1833. * TODO: Use scan results (etc.) to select the best channel.
  1834. */
  1835. p2p.op_channel = 1 + r % 11;
  1836. }
  1837. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d "
  1838. "Own preferred operation channel: %d",
  1839. p2p.channel, p2p.op_channel);
  1840. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  1841. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  1842. p2p.country[2] = 0x04;
  1843. } else
  1844. os_memcpy(p2p.country, "US\x04", 3);
  1845. if (wpas_p2p_setup_channels(wpa_s, &p2p)) {
  1846. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  1847. "channel list");
  1848. return -1;
  1849. }
  1850. if (wpa_s->conf->device_type &&
  1851. wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
  1852. 0) {
  1853. wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
  1854. return -1;
  1855. }
  1856. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
  1857. if (wpa_s->conf->sec_device_type[i] == NULL)
  1858. continue;
  1859. if (wps_dev_type_str2bin(
  1860. wpa_s->conf->sec_device_type[i],
  1861. p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
  1862. wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
  1863. return -1;
  1864. }
  1865. p2p.num_sec_dev_types++;
  1866. if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
  1867. break;
  1868. }
  1869. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  1870. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  1871. p2p.max_peers = wpa_s->max_stations ? wpa_s->max_stations : 100;
  1872. if (wpa_s->conf->p2p_ssid_postfix) {
  1873. p2p.ssid_postfix_len =
  1874. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  1875. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  1876. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  1877. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  1878. p2p.ssid_postfix_len);
  1879. }
  1880. global->p2p = p2p_init(&p2p);
  1881. if (global->p2p == NULL)
  1882. return -1;
  1883. return 0;
  1884. }
  1885. /**
  1886. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  1887. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  1888. *
  1889. * This function deinitialize per-interface P2P data.
  1890. */
  1891. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  1892. {
  1893. if (wpa_s->driver && wpa_s->drv_priv)
  1894. wpa_drv_probe_req_report(wpa_s, 0);
  1895. os_free(wpa_s->go_params);
  1896. wpa_s->go_params = NULL;
  1897. wpabuf_free(wpa_s->pending_action_tx);
  1898. wpa_s->pending_action_tx = NULL;
  1899. eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
  1900. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1901. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  1902. wpa_s->p2p_long_listen = 0;
  1903. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1904. wpas_p2p_remove_pending_group_interface(wpa_s);
  1905. /* TODO: remove group interface from the driver if this wpa_s instance
  1906. * is on top of a P2P group interface */
  1907. }
  1908. /**
  1909. * wpas_p2p_deinit_global - Deinitialize global P2P module
  1910. * @global: Pointer to global data from wpa_supplicant_init()
  1911. *
  1912. * This function deinitializes the global (per device) P2P module.
  1913. */
  1914. void wpas_p2p_deinit_global(struct wpa_global *global)
  1915. {
  1916. struct wpa_supplicant *wpa_s, *tmp;
  1917. char *ifname;
  1918. if (global->p2p == NULL)
  1919. return;
  1920. /* Remove remaining P2P group interfaces */
  1921. wpa_s = global->ifaces;
  1922. if (wpa_s)
  1923. wpas_p2p_service_flush(wpa_s);
  1924. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  1925. wpa_s = wpa_s->next;
  1926. while (wpa_s) {
  1927. enum wpa_driver_if_type type;
  1928. tmp = global->ifaces;
  1929. while (tmp &&
  1930. (tmp == wpa_s ||
  1931. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  1932. tmp = tmp->next;
  1933. }
  1934. if (tmp == NULL)
  1935. break;
  1936. ifname = os_strdup(tmp->ifname);
  1937. type = wpas_p2p_if_type(tmp->p2p_group_interface);
  1938. wpa_supplicant_remove_iface(global, tmp);
  1939. if (ifname)
  1940. wpa_drv_if_remove(wpa_s, type, ifname);
  1941. os_free(ifname);
  1942. }
  1943. /*
  1944. * Deinit GO data on any possibly remaining interface (if main
  1945. * interface is used as GO).
  1946. */
  1947. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  1948. if (wpa_s->ap_iface)
  1949. wpas_p2p_group_deinit(wpa_s);
  1950. }
  1951. p2p_deinit(global->p2p);
  1952. global->p2p = NULL;
  1953. }
  1954. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  1955. {
  1956. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  1957. return 1; /* P2P group requires a new interface in every case
  1958. */
  1959. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  1960. return 0; /* driver does not support concurrent operations */
  1961. if (wpa_s->global->ifaces->next)
  1962. return 1; /* more that one interface already in use */
  1963. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  1964. return 1; /* this interface is already in use */
  1965. return 0;
  1966. }
  1967. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  1968. const u8 *peer_addr,
  1969. enum p2p_wps_method wps_method,
  1970. int go_intent, const u8 *own_interface_addr,
  1971. unsigned int force_freq, int persistent_group)
  1972. {
  1973. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  1974. go_intent, own_interface_addr, force_freq,
  1975. persistent_group);
  1976. }
  1977. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  1978. const u8 *peer_addr,
  1979. enum p2p_wps_method wps_method,
  1980. int go_intent, const u8 *own_interface_addr,
  1981. unsigned int force_freq, int persistent_group)
  1982. {
  1983. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  1984. go_intent, own_interface_addr, force_freq,
  1985. persistent_group);
  1986. }
  1987. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  1988. struct wpa_scan_results *scan_res)
  1989. {
  1990. struct wpa_bss *bss;
  1991. int freq;
  1992. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  1993. if (wpa_s->global->p2p_disabled)
  1994. return;
  1995. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
  1996. scan_res ? (int) scan_res->num : -1);
  1997. if (scan_res)
  1998. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  1999. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  2000. wpa_s->pending_join_iface_addr);
  2001. if (freq >= 0) {
  2002. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  2003. "from P2P peer table: %d MHz", freq);
  2004. }
  2005. bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
  2006. if (bss) {
  2007. freq = bss->freq;
  2008. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  2009. "from BSS table: %d MHz", freq);
  2010. }
  2011. if (freq > 0) {
  2012. u16 method;
  2013. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  2014. "prior to joining an existing group (GO " MACSTR
  2015. " freq=%u MHz)",
  2016. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  2017. wpa_s->pending_pd_before_join = 1;
  2018. switch (wpa_s->pending_join_wps_method) {
  2019. case WPS_PIN_LABEL:
  2020. case WPS_PIN_DISPLAY:
  2021. method = WPS_CONFIG_KEYPAD;
  2022. break;
  2023. case WPS_PIN_KEYPAD:
  2024. method = WPS_CONFIG_DISPLAY;
  2025. break;
  2026. case WPS_PBC:
  2027. method = WPS_CONFIG_PUSHBUTTON;
  2028. break;
  2029. default:
  2030. method = 0;
  2031. break;
  2032. }
  2033. if (p2p_prov_disc_req(wpa_s->global->p2p,
  2034. wpa_s->pending_join_dev_addr, method, 1)
  2035. < 0) {
  2036. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  2037. "Discovery Request before joining an "
  2038. "existing group");
  2039. wpa_s->pending_pd_before_join = 0;
  2040. goto start;
  2041. }
  2042. /*
  2043. * Actual join operation will be started from the Action frame
  2044. * TX status callback.
  2045. */
  2046. return;
  2047. }
  2048. wpa_printf(MSG_DEBUG, "P2P: Target BSS/GO not yet in BSS table - "
  2049. "cannot send Provision Discovery Request");
  2050. start:
  2051. /* Start join operation immediately */
  2052. wpas_p2p_join_start(wpa_s);
  2053. }
  2054. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  2055. {
  2056. struct wpa_supplicant *wpa_s = eloop_ctx;
  2057. int ret;
  2058. struct wpa_driver_scan_params params;
  2059. struct wpabuf *wps_ie, *ies;
  2060. os_memset(&params, 0, sizeof(params));
  2061. /* P2P Wildcard SSID */
  2062. params.num_ssids = 1;
  2063. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  2064. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  2065. wpa_s->wps->dev.p2p = 1;
  2066. wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
  2067. WPS_REQ_ENROLLEE);
  2068. if (wps_ie == NULL) {
  2069. wpas_p2p_scan_res_join(wpa_s, NULL);
  2070. return;
  2071. }
  2072. ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
  2073. if (ies == NULL) {
  2074. wpabuf_free(wps_ie);
  2075. wpas_p2p_scan_res_join(wpa_s, NULL);
  2076. return;
  2077. }
  2078. wpabuf_put_buf(ies, wps_ie);
  2079. wpabuf_free(wps_ie);
  2080. p2p_scan_ie(wpa_s->global->p2p, ies);
  2081. params.extra_ies = wpabuf_head(ies);
  2082. params.extra_ies_len = wpabuf_len(ies);
  2083. /*
  2084. * Run a scan to update BSS table and start Provision Discovery once
  2085. * the new scan results become available.
  2086. */
  2087. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  2088. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  2089. ret = ieee80211_sta_req_scan(wpa_s, &params);
  2090. else
  2091. ret = wpa_drv_scan(wpa_s, &params);
  2092. wpabuf_free(ies);
  2093. if (ret) {
  2094. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  2095. "try again later");
  2096. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2097. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  2098. }
  2099. }
  2100. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  2101. const u8 *dev_addr, enum p2p_wps_method wps_method)
  2102. {
  2103. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  2104. MACSTR " dev " MACSTR ")",
  2105. MAC2STR(iface_addr), MAC2STR(dev_addr));
  2106. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  2107. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  2108. wpa_s->pending_join_wps_method = wps_method;
  2109. /* Make sure we are not running find during connection establishment */
  2110. wpas_p2p_stop_find(wpa_s);
  2111. wpas_p2p_join_scan(wpa_s, NULL);
  2112. return 0;
  2113. }
  2114. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  2115. {
  2116. struct wpa_supplicant *group;
  2117. struct p2p_go_neg_results res;
  2118. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  2119. if (group == NULL)
  2120. return -1;
  2121. if (group != wpa_s) {
  2122. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  2123. sizeof(group->p2p_pin));
  2124. group->p2p_wps_method = wpa_s->p2p_wps_method;
  2125. }
  2126. group->p2p_in_provisioning = 1;
  2127. os_memset(&res, 0, sizeof(res));
  2128. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  2129. ETH_ALEN);
  2130. res.wps_method = wpa_s->pending_join_wps_method;
  2131. wpas_start_wps_enrollee(group, &res);
  2132. return 0;
  2133. }
  2134. /**
  2135. * wpas_p2p_connect - Request P2P Group Formation to be started
  2136. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2137. * @peer_addr: Address of the peer P2P Device
  2138. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  2139. * @persistent_group: Whether to create a persistent group
  2140. * @join: Whether to join an existing group (as a client) instead of starting
  2141. * Group Owner negotiation; @peer_addr is BSSID in that case
  2142. * @auth: Whether to only authorize the connection instead of doing that and
  2143. * initiating Group Owner negotiation
  2144. * @go_intent: GO Intent or -1 to use default
  2145. * @freq: Frequency for the group or 0 for auto-selection
  2146. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on failure
  2147. */
  2148. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2149. const char *pin, enum p2p_wps_method wps_method,
  2150. int persistent_group, int join, int auth, int go_intent,
  2151. int freq)
  2152. {
  2153. int force_freq = 0;
  2154. u8 bssid[ETH_ALEN];
  2155. int ret = 0;
  2156. enum wpa_driver_if_type iftype;
  2157. if (go_intent < 0)
  2158. go_intent = wpa_s->conf->p2p_go_intent;
  2159. if (!auth)
  2160. wpa_s->p2p_long_listen = 0;
  2161. wpa_s->p2p_wps_method = wps_method;
  2162. if (pin)
  2163. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  2164. else if (wps_method == WPS_PIN_DISPLAY) {
  2165. ret = wps_generate_pin();
  2166. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  2167. ret);
  2168. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  2169. wpa_s->p2p_pin);
  2170. } else
  2171. wpa_s->p2p_pin[0] = '\0';
  2172. if (join) {
  2173. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  2174. if (auth) {
  2175. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  2176. "connect a running group from " MACSTR,
  2177. MAC2STR(peer_addr));
  2178. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  2179. return ret;
  2180. }
  2181. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  2182. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  2183. iface_addr) < 0) {
  2184. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  2185. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  2186. dev_addr);
  2187. }
  2188. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
  2189. 0)
  2190. return -1;
  2191. return ret;
  2192. }
  2193. if (freq > 0)
  2194. force_freq = freq;
  2195. else if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  2196. wpa_s->assoc_freq)
  2197. force_freq = wpa_s->assoc_freq;
  2198. else {
  2199. force_freq = wpa_drv_shared_freq(wpa_s);
  2200. if (force_freq < 0)
  2201. force_freq = 0;
  2202. }
  2203. if (force_freq > 0) {
  2204. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  2205. "channel we are already using (%u MHz) on another "
  2206. "interface", force_freq);
  2207. }
  2208. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  2209. if (!wpa_s->create_p2p_iface) {
  2210. if (auth) {
  2211. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  2212. go_intent, wpa_s->own_addr,
  2213. force_freq, persistent_group)
  2214. < 0)
  2215. return -1;
  2216. return ret;
  2217. }
  2218. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  2219. go_intent, wpa_s->own_addr,
  2220. force_freq, persistent_group) < 0)
  2221. return -1;
  2222. return ret;
  2223. }
  2224. /* Prepare to add a new interface for the group */
  2225. iftype = WPA_IF_P2P_GROUP;
  2226. if (join)
  2227. iftype = WPA_IF_P2P_CLIENT;
  2228. else if (go_intent == 15)
  2229. iftype = WPA_IF_P2P_GO;
  2230. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  2231. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2232. "interface for the group");
  2233. return -1;
  2234. }
  2235. if (auth) {
  2236. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  2237. go_intent,
  2238. wpa_s->pending_interface_addr,
  2239. force_freq, persistent_group) < 0)
  2240. return -1;
  2241. return ret;
  2242. }
  2243. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
  2244. wpa_s->pending_interface_addr,
  2245. force_freq, persistent_group) < 0) {
  2246. wpas_p2p_remove_pending_group_interface(wpa_s);
  2247. return -1;
  2248. }
  2249. return ret;
  2250. }
  2251. /**
  2252. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  2253. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2254. * @freq: Frequency of the channel in MHz
  2255. * @duration: Duration of the stay on the channel in milliseconds
  2256. *
  2257. * This callback is called when the driver indicates that it has started the
  2258. * requested remain-on-channel duration.
  2259. */
  2260. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  2261. unsigned int freq, unsigned int duration)
  2262. {
  2263. wpa_s->roc_waiting_drv_freq = 0;
  2264. wpa_s->off_channel_freq = freq;
  2265. wpas_send_action_cb(wpa_s, NULL);
  2266. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  2267. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  2268. wpa_s->pending_listen_duration);
  2269. wpa_s->pending_listen_freq = 0;
  2270. }
  2271. }
  2272. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  2273. unsigned int timeout)
  2274. {
  2275. /* Limit maximum Listen state time based on driver limitation. */
  2276. if (timeout > wpa_s->max_remain_on_chan)
  2277. timeout = wpa_s->max_remain_on_chan;
  2278. return p2p_listen(wpa_s->global->p2p, timeout);
  2279. }
  2280. /**
  2281. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  2282. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2283. * @freq: Frequency of the channel in MHz
  2284. *
  2285. * This callback is called when the driver indicates that a remain-on-channel
  2286. * operation has been completed, i.e., the duration on the requested channel
  2287. * has timed out.
  2288. */
  2289. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  2290. unsigned int freq)
  2291. {
  2292. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  2293. "(p2p_long_listen=%d pending_action_tx=%p)",
  2294. wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
  2295. wpa_s->off_channel_freq = 0;
  2296. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  2297. return; /* P2P module started a new operation */
  2298. if (wpa_s->pending_action_tx)
  2299. return;
  2300. if (wpa_s->p2p_long_listen > 0)
  2301. wpa_s->p2p_long_listen -= 5;
  2302. if (wpa_s->p2p_long_listen > 0) {
  2303. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  2304. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen * 1000);
  2305. }
  2306. }
  2307. /**
  2308. * wpas_p2p_group_remove - Remove a P2P group
  2309. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2310. * @ifname: Network interface name of the group interface or "*" to remove all
  2311. * groups
  2312. * Returns: 0 on success, -1 on failure
  2313. *
  2314. * This function is used to remove a P2P group. This can be used to disconnect
  2315. * from a group in which the local end is a P2P Client or to end a P2P Group in
  2316. * case the local end is the Group Owner. If a virtual network interface was
  2317. * created for this group, that interface will be removed. Otherwise, only the
  2318. * configured P2P group network will be removed from the interface.
  2319. */
  2320. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  2321. {
  2322. struct wpa_global *global = wpa_s->global;
  2323. if (os_strcmp(ifname, "*") == 0) {
  2324. struct wpa_supplicant *prev;
  2325. wpa_s = global->ifaces;
  2326. while (wpa_s) {
  2327. prev = wpa_s;
  2328. wpa_s = wpa_s->next;
  2329. wpas_p2p_group_delete(prev);
  2330. }
  2331. return 0;
  2332. }
  2333. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2334. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2335. break;
  2336. }
  2337. if (wpa_s == NULL)
  2338. return -1;
  2339. wpas_p2p_group_delete(wpa_s);
  2340. return 0;
  2341. }
  2342. static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  2343. struct p2p_go_neg_results *params,
  2344. int freq)
  2345. {
  2346. u8 bssid[ETH_ALEN];
  2347. int res;
  2348. os_memset(params, 0, sizeof(*params));
  2349. params->role_go = 1;
  2350. params->freq = 2412;
  2351. if (freq)
  2352. params->freq = freq;
  2353. else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  2354. wpa_s->conf->p2p_oper_channel >= 1 &&
  2355. wpa_s->conf->p2p_oper_channel <= 11)
  2356. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  2357. else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
  2358. wpa_s->conf->p2p_oper_reg_class == 118)
  2359. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  2360. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  2361. wpa_s->assoc_freq && !freq) {
  2362. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  2363. "already using");
  2364. params->freq = wpa_s->assoc_freq;
  2365. }
  2366. res = wpa_drv_shared_freq(wpa_s);
  2367. if (res > 0 && !freq) {
  2368. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  2369. "already using on a shared interface");
  2370. params->freq = res;
  2371. }
  2372. }
  2373. static struct wpa_supplicant *
  2374. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  2375. int go)
  2376. {
  2377. struct wpa_supplicant *group_wpa_s;
  2378. if (!wpas_p2p_create_iface(wpa_s))
  2379. return wpa_s;
  2380. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  2381. WPA_IF_P2P_CLIENT) < 0)
  2382. return NULL;
  2383. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  2384. if (group_wpa_s == NULL) {
  2385. wpas_p2p_remove_pending_group_interface(wpa_s);
  2386. return NULL;
  2387. }
  2388. return group_wpa_s;
  2389. }
  2390. /**
  2391. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  2392. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2393. * @persistent_group: Whether to create a persistent group
  2394. * @freq: Frequency for the group or 0 to indicate no hardcoding
  2395. * Returns: 0 on success, -1 on failure
  2396. *
  2397. * This function creates a new P2P group with the local end as the Group Owner,
  2398. * i.e., without using Group Owner Negotiation.
  2399. */
  2400. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  2401. int freq)
  2402. {
  2403. struct p2p_go_neg_results params;
  2404. wpas_p2p_init_go_params(wpa_s, &params, freq);
  2405. p2p_go_params(wpa_s->global->p2p, &params);
  2406. params.persistent_group = persistent_group;
  2407. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  2408. if (wpa_s == NULL)
  2409. return -1;
  2410. wpas_start_wps_go(wpa_s, &params, 0);
  2411. return 0;
  2412. }
  2413. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  2414. struct wpa_ssid *params, int addr_allocated)
  2415. {
  2416. struct wpa_ssid *ssid;
  2417. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  2418. if (wpa_s == NULL)
  2419. return -1;
  2420. wpa_supplicant_ap_deinit(wpa_s);
  2421. ssid = wpa_config_add_network(wpa_s->conf);
  2422. if (ssid == NULL)
  2423. return -1;
  2424. wpas_notify_network_added(wpa_s, ssid);
  2425. wpa_config_set_network_defaults(ssid);
  2426. ssid->temporary = 1;
  2427. ssid->proto = WPA_PROTO_RSN;
  2428. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  2429. ssid->group_cipher = WPA_CIPHER_CCMP;
  2430. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  2431. ssid->ssid = os_malloc(params->ssid_len);
  2432. if (ssid->ssid == NULL) {
  2433. wpas_notify_network_removed(wpa_s, ssid);
  2434. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2435. return -1;
  2436. }
  2437. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  2438. ssid->ssid_len = params->ssid_len;
  2439. ssid->p2p_group = 1;
  2440. if (params->psk_set) {
  2441. os_memcpy(ssid->psk, params->psk, 32);
  2442. ssid->psk_set = 1;
  2443. }
  2444. if (params->passphrase)
  2445. ssid->passphrase = os_strdup(params->passphrase);
  2446. wpa_supplicant_select_network(wpa_s, ssid);
  2447. wpa_s->show_group_started = 1;
  2448. return 0;
  2449. }
  2450. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  2451. struct wpa_ssid *ssid, int addr_allocated,
  2452. int freq)
  2453. {
  2454. struct p2p_go_neg_results params;
  2455. if (ssid->disabled != 2 || ssid->ssid == NULL)
  2456. return -1;
  2457. wpa_s->p2p_long_listen = 0;
  2458. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2459. if (ssid->mode == WPAS_MODE_INFRA)
  2460. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  2461. if (ssid->mode != WPAS_MODE_P2P_GO)
  2462. return -1;
  2463. wpas_p2p_init_go_params(wpa_s, &params, freq);
  2464. params.role_go = 1;
  2465. if (ssid->passphrase == NULL ||
  2466. os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  2467. wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
  2468. "group");
  2469. return -1;
  2470. }
  2471. os_strlcpy(params.passphrase, ssid->passphrase,
  2472. sizeof(params.passphrase));
  2473. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  2474. params.ssid_len = ssid->ssid_len;
  2475. params.persistent_group = 1;
  2476. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  2477. if (wpa_s == NULL)
  2478. return -1;
  2479. wpas_start_wps_go(wpa_s, &params, 0);
  2480. return 0;
  2481. }
  2482. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  2483. struct wpabuf *proberesp_ies)
  2484. {
  2485. struct wpa_supplicant *wpa_s = ctx;
  2486. if (wpa_s->ap_iface) {
  2487. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  2488. if (beacon_ies) {
  2489. wpabuf_free(hapd->p2p_beacon_ie);
  2490. hapd->p2p_beacon_ie = beacon_ies;
  2491. }
  2492. wpabuf_free(hapd->p2p_probe_resp_ie);
  2493. hapd->p2p_probe_resp_ie = proberesp_ies;
  2494. } else {
  2495. wpabuf_free(beacon_ies);
  2496. wpabuf_free(proberesp_ies);
  2497. }
  2498. wpa_supplicant_ap_update_beacon(wpa_s);
  2499. }
  2500. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  2501. int persistent_group,
  2502. int group_formation)
  2503. {
  2504. struct p2p_group *group;
  2505. struct p2p_group_config *cfg;
  2506. cfg = os_zalloc(sizeof(*cfg));
  2507. if (cfg == NULL)
  2508. return NULL;
  2509. cfg->persistent_group = persistent_group;
  2510. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  2511. cfg->cb_ctx = wpa_s;
  2512. cfg->ie_update = wpas_p2p_ie_update;
  2513. group = p2p_group_init(wpa_s->global->p2p, cfg);
  2514. if (group == NULL)
  2515. os_free(cfg);
  2516. if (!group_formation)
  2517. p2p_group_notif_formation_done(group);
  2518. wpa_s->p2p_group = group;
  2519. return group;
  2520. }
  2521. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2522. int registrar)
  2523. {
  2524. if (!wpa_s->p2p_in_provisioning) {
  2525. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  2526. "provisioning not in progress");
  2527. return;
  2528. }
  2529. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  2530. NULL);
  2531. if (wpa_s->global->p2p)
  2532. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  2533. wpas_group_formation_completed(wpa_s, 1);
  2534. }
  2535. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2536. const char *config_method)
  2537. {
  2538. u16 config_methods;
  2539. if (os_strcmp(config_method, "display") == 0)
  2540. config_methods = WPS_CONFIG_DISPLAY;
  2541. else if (os_strcmp(config_method, "keypad") == 0)
  2542. config_methods = WPS_CONFIG_KEYPAD;
  2543. else if (os_strcmp(config_method, "pbc") == 0 ||
  2544. os_strcmp(config_method, "pushbutton") == 0)
  2545. config_methods = WPS_CONFIG_PUSHBUTTON;
  2546. else
  2547. return -1;
  2548. if (wpa_s->global->p2p == NULL)
  2549. return -1;
  2550. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  2551. config_methods, 0);
  2552. }
  2553. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  2554. char *end)
  2555. {
  2556. return p2p_scan_result_text(ies, ies_len, buf, end);
  2557. }
  2558. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  2559. {
  2560. if (!wpa_s->pending_action_tx)
  2561. return;
  2562. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  2563. "operation request");
  2564. wpabuf_free(wpa_s->pending_action_tx);
  2565. wpa_s->pending_action_tx = NULL;
  2566. }
  2567. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  2568. enum p2p_discovery_type type)
  2569. {
  2570. wpas_p2p_clear_pending_action_tx(wpa_s);
  2571. wpa_s->p2p_long_listen = 0;
  2572. return p2p_find(wpa_s->global->p2p, timeout, type);
  2573. }
  2574. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  2575. {
  2576. wpas_p2p_clear_pending_action_tx(wpa_s);
  2577. wpa_s->p2p_long_listen = 0;
  2578. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2579. p2p_stop_find(wpa_s->global->p2p);
  2580. wpas_p2p_remove_pending_group_interface(wpa_s);
  2581. }
  2582. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  2583. {
  2584. struct wpa_supplicant *wpa_s = eloop_ctx;
  2585. wpa_s->p2p_long_listen = 0;
  2586. }
  2587. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  2588. {
  2589. int res;
  2590. wpas_p2p_clear_pending_action_tx(wpa_s);
  2591. if (timeout == 0) {
  2592. /*
  2593. * This is a request for unlimited Listen state. However, at
  2594. * least for now, this is mapped to a Listen state for one
  2595. * hour.
  2596. */
  2597. timeout = 3600;
  2598. }
  2599. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2600. wpa_s->p2p_long_listen = 0;
  2601. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  2602. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  2603. wpa_s->p2p_long_listen = timeout;
  2604. eloop_register_timeout(timeout, 0,
  2605. wpas_p2p_long_listen_timeout,
  2606. wpa_s, NULL);
  2607. }
  2608. return res;
  2609. }
  2610. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2611. u8 *buf, size_t len, int p2p_group)
  2612. {
  2613. struct wpabuf *p2p_ie;
  2614. int ret;
  2615. if (wpa_s->global->p2p_disabled)
  2616. return -1;
  2617. if (wpa_s->global->p2p == NULL)
  2618. return -1;
  2619. if (bss == NULL)
  2620. return -1;
  2621. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  2622. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  2623. p2p_group, p2p_ie);
  2624. wpabuf_free(p2p_ie);
  2625. return ret;
  2626. }
  2627. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  2628. const u8 *ie, size_t ie_len)
  2629. {
  2630. if (wpa_s->global->p2p_disabled)
  2631. return 0;
  2632. if (wpa_s->global->p2p == NULL)
  2633. return 0;
  2634. return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
  2635. }
  2636. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  2637. const u8 *sa, const u8 *bssid,
  2638. u8 category, const u8 *data, size_t len, int freq)
  2639. {
  2640. if (wpa_s->global->p2p_disabled)
  2641. return;
  2642. if (wpa_s->global->p2p == NULL)
  2643. return;
  2644. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  2645. freq);
  2646. }
  2647. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  2648. {
  2649. if (wpa_s->global->p2p_disabled)
  2650. return;
  2651. if (wpa_s->global->p2p == NULL)
  2652. return;
  2653. p2p_scan_ie(wpa_s->global->p2p, ies);
  2654. }
  2655. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  2656. {
  2657. p2p_group_deinit(wpa_s->p2p_group);
  2658. wpa_s->p2p_group = NULL;
  2659. }
  2660. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  2661. {
  2662. wpa_s->p2p_long_listen = 0;
  2663. return p2p_reject(wpa_s->global->p2p, addr);
  2664. }
  2665. /* Invite to reinvoke a persistent group */
  2666. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2667. struct wpa_ssid *ssid, const u8 *go_dev_addr)
  2668. {
  2669. enum p2p_invite_role role;
  2670. u8 *bssid = NULL;
  2671. if (ssid->mode == WPAS_MODE_P2P_GO) {
  2672. role = P2P_INVITE_ROLE_GO;
  2673. if (peer_addr == NULL) {
  2674. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  2675. "address in invitation command");
  2676. return -1;
  2677. }
  2678. if (wpas_p2p_create_iface(wpa_s)) {
  2679. if (wpas_p2p_add_group_interface(wpa_s,
  2680. WPA_IF_P2P_GO) < 0) {
  2681. wpa_printf(MSG_ERROR, "P2P: Failed to "
  2682. "allocate a new interface for the "
  2683. "group");
  2684. return -1;
  2685. }
  2686. bssid = wpa_s->pending_interface_addr;
  2687. } else
  2688. bssid = wpa_s->own_addr;
  2689. } else {
  2690. role = P2P_INVITE_ROLE_CLIENT;
  2691. peer_addr = ssid->bssid;
  2692. }
  2693. wpa_s->pending_invite_ssid_id = ssid->id;
  2694. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  2695. ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
  2696. }
  2697. /* Invite to join an active group */
  2698. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  2699. const u8 *peer_addr, const u8 *go_dev_addr)
  2700. {
  2701. struct wpa_global *global = wpa_s->global;
  2702. enum p2p_invite_role role;
  2703. u8 *bssid = NULL;
  2704. struct wpa_ssid *ssid;
  2705. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2706. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2707. break;
  2708. }
  2709. if (wpa_s == NULL) {
  2710. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  2711. return -1;
  2712. }
  2713. ssid = wpa_s->current_ssid;
  2714. if (ssid == NULL) {
  2715. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  2716. "invitation");
  2717. return -1;
  2718. }
  2719. if (ssid->mode == WPAS_MODE_P2P_GO) {
  2720. role = P2P_INVITE_ROLE_ACTIVE_GO;
  2721. bssid = wpa_s->own_addr;
  2722. if (go_dev_addr == NULL)
  2723. go_dev_addr = wpa_s->parent->own_addr;
  2724. } else {
  2725. role = P2P_INVITE_ROLE_CLIENT;
  2726. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  2727. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  2728. "invite to current group");
  2729. return -1;
  2730. }
  2731. bssid = wpa_s->bssid;
  2732. if (go_dev_addr == NULL &&
  2733. !is_zero_ether_addr(wpa_s->go_dev_addr))
  2734. go_dev_addr = wpa_s->go_dev_addr;
  2735. }
  2736. wpa_s->pending_invite_ssid_id = -1;
  2737. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  2738. ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
  2739. go_dev_addr, 0);
  2740. }
  2741. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  2742. {
  2743. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2744. const char *ssid_txt;
  2745. u8 go_dev_addr[ETH_ALEN];
  2746. int persistent;
  2747. if (!wpa_s->show_group_started || !ssid)
  2748. return;
  2749. wpa_s->show_group_started = 0;
  2750. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  2751. os_memset(go_dev_addr, 0, ETH_ALEN);
  2752. if (ssid->bssid_set)
  2753. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  2754. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  2755. ssid->ssid_len);
  2756. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  2757. if (ssid->passphrase == NULL && ssid->psk_set) {
  2758. char psk[65];
  2759. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  2760. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  2761. "%s client ssid=\"%s\" psk=%s go_dev_addr=" MACSTR
  2762. "%s",
  2763. wpa_s->ifname, ssid_txt, psk, MAC2STR(go_dev_addr),
  2764. persistent ? " [PERSISTENT]" : "");
  2765. } else {
  2766. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  2767. "%s client ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
  2768. MACSTR "%s",
  2769. wpa_s->ifname, ssid_txt,
  2770. ssid->passphrase ? ssid->passphrase : "",
  2771. MAC2STR(go_dev_addr),
  2772. persistent ? " [PERSISTENT]" : "");
  2773. }
  2774. if (persistent)
  2775. wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
  2776. go_dev_addr);
  2777. }
  2778. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  2779. u32 interval1, u32 duration2, u32 interval2)
  2780. {
  2781. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  2782. wpa_s->current_ssid == NULL ||
  2783. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  2784. return -1;
  2785. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  2786. wpa_s->own_addr, wpa_s->assoc_freq,
  2787. duration1, interval1, duration2, interval2);
  2788. }
  2789. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  2790. unsigned int interval)
  2791. {
  2792. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  2793. }
  2794. void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  2795. u16 reason_code, const u8 *ie, size_t ie_len)
  2796. {
  2797. if (wpa_s->global->p2p_disabled)
  2798. return;
  2799. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
  2800. }
  2801. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  2802. u16 reason_code, const u8 *ie, size_t ie_len)
  2803. {
  2804. if (wpa_s->global->p2p_disabled)
  2805. return;
  2806. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
  2807. }
  2808. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  2809. {
  2810. struct p2p_data *p2p = wpa_s->global->p2p;
  2811. if (p2p == NULL)
  2812. return;
  2813. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  2814. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  2815. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
  2816. u8 pri_dev_type[8];
  2817. if (wpa_s->conf->device_type) {
  2818. if (wps_dev_type_str2bin(wpa_s->conf->device_type,
  2819. pri_dev_type) < 0) {
  2820. wpa_printf(MSG_ERROR, "P2P: Invalid "
  2821. "device_type");
  2822. } else
  2823. p2p_set_pri_dev_type(p2p, pri_dev_type);
  2824. }
  2825. }
  2826. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
  2827. u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
  2828. size_t num = 0;
  2829. int i;
  2830. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
  2831. if (wpa_s->conf->sec_device_type[i] == NULL)
  2832. continue;
  2833. if (wps_dev_type_str2bin(
  2834. wpa_s->conf->sec_device_type[i],
  2835. sec_dev_type[num]) < 0) {
  2836. wpa_printf(MSG_ERROR, "P2P: Invalid "
  2837. "sec_device_type");
  2838. continue;
  2839. }
  2840. num++;
  2841. if (num == P2P_SEC_DEVICE_TYPES)
  2842. break;
  2843. }
  2844. p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
  2845. }
  2846. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  2847. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2848. char country[3];
  2849. country[0] = wpa_s->conf->country[0];
  2850. country[1] = wpa_s->conf->country[1];
  2851. country[2] = 0x04;
  2852. p2p_set_country(p2p, country);
  2853. }
  2854. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  2855. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  2856. wpa_s->conf->p2p_ssid_postfix ?
  2857. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  2858. 0);
  2859. }
  2860. }
  2861. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  2862. int duration)
  2863. {
  2864. if (!wpa_s->ap_iface)
  2865. return -1;
  2866. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  2867. duration);
  2868. }
  2869. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  2870. {
  2871. if (wpa_s->global->p2p_disabled)
  2872. return -1;
  2873. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2874. return -1;
  2875. wpa_s->global->cross_connection = enabled;
  2876. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  2877. if (!enabled) {
  2878. struct wpa_supplicant *iface;
  2879. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  2880. {
  2881. if (iface->cross_connect_enabled == 0)
  2882. continue;
  2883. iface->cross_connect_enabled = 0;
  2884. iface->cross_connect_in_use = 0;
  2885. wpa_msg(iface->parent, MSG_INFO,
  2886. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  2887. iface->ifname, iface->cross_connect_uplink);
  2888. }
  2889. }
  2890. return 0;
  2891. }
  2892. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  2893. {
  2894. struct wpa_supplicant *iface;
  2895. if (!uplink->global->cross_connection)
  2896. return;
  2897. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  2898. if (!iface->cross_connect_enabled)
  2899. continue;
  2900. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  2901. 0)
  2902. continue;
  2903. if (iface->ap_iface == NULL)
  2904. continue;
  2905. if (iface->cross_connect_in_use)
  2906. continue;
  2907. iface->cross_connect_in_use = 1;
  2908. wpa_msg(iface->parent, MSG_INFO,
  2909. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  2910. iface->ifname, iface->cross_connect_uplink);
  2911. }
  2912. }
  2913. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  2914. {
  2915. struct wpa_supplicant *iface;
  2916. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  2917. if (!iface->cross_connect_enabled)
  2918. continue;
  2919. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  2920. 0)
  2921. continue;
  2922. if (!iface->cross_connect_in_use)
  2923. continue;
  2924. wpa_msg(iface->parent, MSG_INFO,
  2925. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  2926. iface->ifname, iface->cross_connect_uplink);
  2927. iface->cross_connect_in_use = 0;
  2928. }
  2929. }
  2930. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  2931. {
  2932. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  2933. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  2934. wpa_s->cross_connect_disallowed)
  2935. wpas_p2p_disable_cross_connect(wpa_s);
  2936. else
  2937. wpas_p2p_enable_cross_connect(wpa_s);
  2938. }
  2939. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  2940. {
  2941. wpas_p2p_disable_cross_connect(wpa_s);
  2942. }
  2943. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  2944. {
  2945. struct wpa_supplicant *iface;
  2946. if (!wpa_s->global->cross_connection)
  2947. return;
  2948. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  2949. if (iface == wpa_s)
  2950. continue;
  2951. if (iface->drv_flags &
  2952. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  2953. continue;
  2954. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  2955. continue;
  2956. wpa_s->cross_connect_enabled = 1;
  2957. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  2958. sizeof(wpa_s->cross_connect_uplink));
  2959. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  2960. "%s to %s whenever uplink is available",
  2961. wpa_s->ifname, wpa_s->cross_connect_uplink);
  2962. if (iface->ap_iface || iface->current_ssid == NULL ||
  2963. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  2964. iface->cross_connect_disallowed ||
  2965. iface->wpa_state != WPA_COMPLETED)
  2966. break;
  2967. wpa_s->cross_connect_in_use = 1;
  2968. wpa_msg(wpa_s->parent, MSG_INFO,
  2969. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  2970. wpa_s->ifname, wpa_s->cross_connect_uplink);
  2971. break;
  2972. }
  2973. }