p2p_supplicant.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277
  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/ap_config.h"
  24. #include "ap/p2p_hostapd.h"
  25. #include "eapol_supp/eapol_supp_sm.h"
  26. #include "rsn_supp/wpa.h"
  27. #include "wpa_supplicant_i.h"
  28. #include "driver_i.h"
  29. #include "ap.h"
  30. #include "config_ssid.h"
  31. #include "config.h"
  32. #include "notify.h"
  33. #include "scan.h"
  34. #include "bss.h"
  35. #include "offchannel.h"
  36. #include "wps_supplicant.h"
  37. #include "p2p_supplicant.h"
  38. /*
  39. * How many times to try to scan to find the GO before giving up on join
  40. * request.
  41. */
  42. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  43. #ifndef P2P_MAX_CLIENT_IDLE
  44. /*
  45. * How many seconds to try to reconnect to the GO when connection in P2P client
  46. * role has been lost.
  47. */
  48. #define P2P_MAX_CLIENT_IDLE 10
  49. #endif /* P2P_MAX_CLIENT_IDLE */
  50. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  51. static struct wpa_supplicant *
  52. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  53. int go);
  54. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  55. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  56. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  57. const u8 *dev_addr, enum p2p_wps_method wps_method);
  58. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  59. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  60. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  61. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  62. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  63. struct wpa_scan_results *scan_res)
  64. {
  65. size_t i;
  66. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  67. return;
  68. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  69. (int) scan_res->num);
  70. for (i = 0; i < scan_res->num; i++) {
  71. struct wpa_scan_res *bss = scan_res->res[i];
  72. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  73. bss->freq, bss->level,
  74. (const u8 *) (bss + 1),
  75. bss->ie_len) > 0)
  76. break;
  77. }
  78. p2p_scan_res_handled(wpa_s->global->p2p);
  79. }
  80. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  81. unsigned int num_req_dev_types,
  82. const u8 *req_dev_types)
  83. {
  84. struct wpa_supplicant *wpa_s = ctx;
  85. struct wpa_driver_scan_params params;
  86. int ret;
  87. struct wpabuf *wps_ie, *ies;
  88. int social_channels[] = { 2412, 2437, 2462, 0, 0 };
  89. size_t ielen;
  90. int was_in_p2p_scan;
  91. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  92. return -1;
  93. os_memset(&params, 0, sizeof(params));
  94. /* P2P Wildcard SSID */
  95. params.num_ssids = 1;
  96. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  97. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  98. wpa_s->wps->dev.p2p = 1;
  99. wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
  100. WPS_REQ_ENROLLEE,
  101. num_req_dev_types, req_dev_types);
  102. if (wps_ie == NULL)
  103. return -1;
  104. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  105. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  106. if (ies == NULL) {
  107. wpabuf_free(wps_ie);
  108. return -1;
  109. }
  110. wpabuf_put_buf(ies, wps_ie);
  111. wpabuf_free(wps_ie);
  112. p2p_scan_ie(wpa_s->global->p2p, ies);
  113. params.p2p_probe = 1;
  114. params.extra_ies = wpabuf_head(ies);
  115. params.extra_ies_len = wpabuf_len(ies);
  116. switch (type) {
  117. case P2P_SCAN_SOCIAL:
  118. params.freqs = social_channels;
  119. break;
  120. case P2P_SCAN_FULL:
  121. break;
  122. case P2P_SCAN_SPECIFIC:
  123. social_channels[0] = freq;
  124. social_channels[1] = 0;
  125. params.freqs = social_channels;
  126. break;
  127. case P2P_SCAN_SOCIAL_PLUS_ONE:
  128. social_channels[3] = freq;
  129. params.freqs = social_channels;
  130. break;
  131. }
  132. was_in_p2p_scan = wpa_s->scan_res_handler == wpas_p2p_scan_res_handler;
  133. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  134. ret = wpa_drv_scan(wpa_s, &params);
  135. wpabuf_free(ies);
  136. if (ret) {
  137. wpa_s->scan_res_handler = NULL;
  138. if (wpa_s->scanning || was_in_p2p_scan) {
  139. wpa_s->p2p_cb_on_scan_complete = 1;
  140. ret = 1;
  141. }
  142. }
  143. return ret;
  144. }
  145. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  146. {
  147. switch (p2p_group_interface) {
  148. case P2P_GROUP_INTERFACE_PENDING:
  149. return WPA_IF_P2P_GROUP;
  150. case P2P_GROUP_INTERFACE_GO:
  151. return WPA_IF_P2P_GO;
  152. case P2P_GROUP_INTERFACE_CLIENT:
  153. return WPA_IF_P2P_CLIENT;
  154. }
  155. return WPA_IF_P2P_GROUP;
  156. }
  157. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  158. const u8 *ssid,
  159. size_t ssid_len, int *go)
  160. {
  161. struct wpa_ssid *s;
  162. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  163. for (s = wpa_s->conf->ssid; s; s = s->next) {
  164. if (s->disabled != 0 || !s->p2p_group ||
  165. s->ssid_len != ssid_len ||
  166. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  167. continue;
  168. if (s->mode == WPAS_MODE_P2P_GO &&
  169. s != wpa_s->current_ssid)
  170. continue;
  171. if (go)
  172. *go = s->mode == WPAS_MODE_P2P_GO;
  173. return wpa_s;
  174. }
  175. }
  176. return NULL;
  177. }
  178. static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
  179. {
  180. struct wpa_ssid *ssid;
  181. char *gtype;
  182. const char *reason;
  183. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  184. ssid = wpa_s->current_ssid;
  185. if (ssid == NULL) {
  186. /*
  187. * The current SSID was not known, but there may still be a
  188. * pending P2P group interface waiting for provisioning.
  189. */
  190. ssid = wpa_s->conf->ssid;
  191. while (ssid) {
  192. if (ssid->p2p_group &&
  193. (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  194. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
  195. break;
  196. ssid = ssid->next;
  197. }
  198. }
  199. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  200. gtype = "GO";
  201. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  202. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  203. wpa_s->reassociate = 0;
  204. wpa_s->disconnected = 1;
  205. wpa_supplicant_deauthenticate(wpa_s,
  206. WLAN_REASON_DEAUTH_LEAVING);
  207. gtype = "client";
  208. } else
  209. gtype = "GO";
  210. if (wpa_s->cross_connect_in_use) {
  211. wpa_s->cross_connect_in_use = 0;
  212. wpa_msg(wpa_s->parent, MSG_INFO,
  213. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  214. wpa_s->ifname, wpa_s->cross_connect_uplink);
  215. }
  216. switch (wpa_s->removal_reason) {
  217. case P2P_GROUP_REMOVAL_REQUESTED:
  218. reason = " reason=REQUESTED";
  219. break;
  220. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  221. reason = " reason=IDLE";
  222. break;
  223. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  224. reason = " reason=UNAVAILABLE";
  225. break;
  226. default:
  227. reason = "";
  228. break;
  229. }
  230. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
  231. wpa_s->ifname, gtype, reason);
  232. if (ssid)
  233. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  234. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  235. struct wpa_global *global;
  236. char *ifname;
  237. enum wpa_driver_if_type type;
  238. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  239. wpa_s->ifname);
  240. global = wpa_s->global;
  241. ifname = os_strdup(wpa_s->ifname);
  242. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  243. wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
  244. wpa_s = global->ifaces;
  245. if (wpa_s && ifname)
  246. wpa_drv_if_remove(wpa_s, type, ifname);
  247. os_free(ifname);
  248. return;
  249. }
  250. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  251. if (ssid && (ssid->p2p_group ||
  252. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  253. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  254. int id = ssid->id;
  255. if (ssid == wpa_s->current_ssid) {
  256. wpa_sm_set_config(wpa_s->wpa, NULL);
  257. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  258. wpa_s->current_ssid = NULL;
  259. }
  260. /*
  261. * Networks objects created during any P2P activities are not
  262. * exposed out as they might/will confuse certain non-P2P aware
  263. * applications since these network objects won't behave like
  264. * regular ones.
  265. *
  266. * Likewise, we don't send out network removed signals for such
  267. * network objects.
  268. */
  269. wpa_config_remove_network(wpa_s->conf, id);
  270. wpa_supplicant_clear_status(wpa_s);
  271. } else {
  272. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  273. "found");
  274. }
  275. wpa_supplicant_ap_deinit(wpa_s);
  276. }
  277. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  278. u8 *go_dev_addr,
  279. const u8 *ssid, size_t ssid_len)
  280. {
  281. struct wpa_bss *bss;
  282. const u8 *bssid;
  283. struct wpabuf *p2p;
  284. u8 group_capab;
  285. const u8 *addr;
  286. if (wpa_s->go_params)
  287. bssid = wpa_s->go_params->peer_interface_addr;
  288. else
  289. bssid = wpa_s->bssid;
  290. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  291. if (bss == NULL) {
  292. u8 iface_addr[ETH_ALEN];
  293. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  294. iface_addr) == 0)
  295. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  296. }
  297. if (bss == NULL) {
  298. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  299. "group is persistent - BSS " MACSTR " not found",
  300. MAC2STR(bssid));
  301. return 0;
  302. }
  303. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  304. if (p2p == NULL) {
  305. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  306. "group is persistent - BSS " MACSTR
  307. " did not include P2P IE", MAC2STR(bssid));
  308. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  309. (u8 *) (bss + 1), bss->ie_len);
  310. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  311. ((u8 *) bss + 1) + bss->ie_len,
  312. bss->beacon_ie_len);
  313. return 0;
  314. }
  315. group_capab = p2p_get_group_capab(p2p);
  316. addr = p2p_get_go_dev_addr(p2p);
  317. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  318. "group_capab=0x%x", group_capab);
  319. if (addr) {
  320. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  321. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  322. MAC2STR(addr));
  323. } else
  324. os_memset(go_dev_addr, 0, ETH_ALEN);
  325. wpabuf_free(p2p);
  326. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  327. "go_dev_addr=" MACSTR,
  328. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  329. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  330. }
  331. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  332. struct wpa_ssid *ssid,
  333. const u8 *go_dev_addr)
  334. {
  335. struct wpa_ssid *s;
  336. int changed = 0;
  337. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  338. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  339. for (s = wpa_s->conf->ssid; s; s = s->next) {
  340. if (s->disabled == 2 &&
  341. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  342. s->ssid_len == ssid->ssid_len &&
  343. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  344. break;
  345. }
  346. if (s) {
  347. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  348. "entry");
  349. if (ssid->passphrase && !s->passphrase)
  350. changed = 1;
  351. else if (ssid->passphrase && s->passphrase &&
  352. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  353. changed = 1;
  354. } else {
  355. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  356. "entry");
  357. changed = 1;
  358. s = wpa_config_add_network(wpa_s->conf);
  359. if (s == NULL)
  360. return -1;
  361. /*
  362. * Instead of network_added we emit persistent_group_added
  363. * notification. Also to keep the defense checks in
  364. * persistent_group obj registration method, we set the
  365. * relevant flags in s to designate it as a persistent group.
  366. */
  367. s->p2p_group = 1;
  368. s->p2p_persistent_group = 1;
  369. wpas_notify_persistent_group_added(wpa_s, s);
  370. wpa_config_set_network_defaults(s);
  371. }
  372. s->p2p_group = 1;
  373. s->p2p_persistent_group = 1;
  374. s->disabled = 2;
  375. s->bssid_set = 1;
  376. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  377. s->mode = ssid->mode;
  378. s->auth_alg = WPA_AUTH_ALG_OPEN;
  379. s->key_mgmt = WPA_KEY_MGMT_PSK;
  380. s->proto = WPA_PROTO_RSN;
  381. s->pairwise_cipher = WPA_CIPHER_CCMP;
  382. s->export_keys = 1;
  383. if (ssid->passphrase) {
  384. os_free(s->passphrase);
  385. s->passphrase = os_strdup(ssid->passphrase);
  386. }
  387. if (ssid->psk_set) {
  388. s->psk_set = 1;
  389. os_memcpy(s->psk, ssid->psk, 32);
  390. }
  391. if (s->passphrase && !s->psk_set)
  392. wpa_config_update_psk(s);
  393. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  394. os_free(s->ssid);
  395. s->ssid = os_malloc(ssid->ssid_len);
  396. }
  397. if (s->ssid) {
  398. s->ssid_len = ssid->ssid_len;
  399. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  400. }
  401. #ifndef CONFIG_NO_CONFIG_WRITE
  402. if (changed && wpa_s->conf->update_config &&
  403. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  404. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  405. }
  406. #endif /* CONFIG_NO_CONFIG_WRITE */
  407. return s->id;
  408. }
  409. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  410. int success)
  411. {
  412. struct wpa_ssid *ssid;
  413. const char *ssid_txt;
  414. int client;
  415. int persistent;
  416. u8 go_dev_addr[ETH_ALEN];
  417. int network_id = -1;
  418. /*
  419. * This callback is likely called for the main interface. Update wpa_s
  420. * to use the group interface if a new interface was created for the
  421. * group.
  422. */
  423. if (wpa_s->global->p2p_group_formation)
  424. wpa_s = wpa_s->global->p2p_group_formation;
  425. wpa_s->global->p2p_group_formation = NULL;
  426. wpa_s->p2p_in_provisioning = 0;
  427. if (!success) {
  428. wpa_msg(wpa_s->parent, MSG_INFO,
  429. P2P_EVENT_GROUP_FORMATION_FAILURE);
  430. wpas_p2p_group_delete(wpa_s);
  431. return;
  432. }
  433. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
  434. ssid = wpa_s->current_ssid;
  435. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  436. ssid->mode = WPAS_MODE_P2P_GO;
  437. p2p_group_notif_formation_done(wpa_s->p2p_group);
  438. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  439. }
  440. persistent = 0;
  441. if (ssid) {
  442. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  443. client = ssid->mode == WPAS_MODE_INFRA;
  444. if (ssid->mode == WPAS_MODE_P2P_GO) {
  445. persistent = ssid->p2p_persistent_group;
  446. os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
  447. ETH_ALEN);
  448. } else
  449. persistent = wpas_p2p_persistent_group(wpa_s,
  450. go_dev_addr,
  451. ssid->ssid,
  452. ssid->ssid_len);
  453. } else {
  454. ssid_txt = "";
  455. client = wpa_s->p2p_group_interface ==
  456. P2P_GROUP_INTERFACE_CLIENT;
  457. os_memset(go_dev_addr, 0, ETH_ALEN);
  458. }
  459. wpa_s->show_group_started = 0;
  460. if (client) {
  461. /*
  462. * Indicate event only after successfully completed 4-way
  463. * handshake, i.e., when the interface is ready for data
  464. * packets.
  465. */
  466. wpa_s->show_group_started = 1;
  467. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  468. char psk[65];
  469. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  470. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  471. "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
  472. "%s",
  473. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  474. MAC2STR(go_dev_addr),
  475. persistent ? " [PERSISTENT]" : "");
  476. wpas_p2p_cross_connect_setup(wpa_s);
  477. wpas_p2p_set_group_idle_timeout(wpa_s);
  478. } else {
  479. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  480. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  481. "go_dev_addr=" MACSTR "%s",
  482. wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
  483. ssid && ssid->passphrase ? ssid->passphrase : "",
  484. MAC2STR(go_dev_addr),
  485. persistent ? " [PERSISTENT]" : "");
  486. wpas_p2p_cross_connect_setup(wpa_s);
  487. wpas_p2p_set_group_idle_timeout(wpa_s);
  488. }
  489. if (persistent)
  490. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  491. ssid, go_dev_addr);
  492. if (network_id < 0 && ssid)
  493. network_id = ssid->id;
  494. if (!client)
  495. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  496. }
  497. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  498. unsigned int freq,
  499. const u8 *dst, const u8 *src,
  500. const u8 *bssid,
  501. const u8 *data, size_t data_len,
  502. enum offchannel_send_action_result
  503. result)
  504. {
  505. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  506. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  507. return;
  508. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  509. return;
  510. switch (result) {
  511. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  512. res = P2P_SEND_ACTION_SUCCESS;
  513. break;
  514. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  515. res = P2P_SEND_ACTION_NO_ACK;
  516. break;
  517. case OFFCHANNEL_SEND_ACTION_FAILED:
  518. res = P2P_SEND_ACTION_FAILED;
  519. break;
  520. }
  521. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  522. if (wpa_s->pending_pd_before_join &&
  523. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  524. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  525. wpa_s->pending_pd_before_join = 0;
  526. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  527. "join-existing-group operation");
  528. wpas_p2p_join_start(wpa_s);
  529. }
  530. }
  531. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  532. const u8 *src, const u8 *bssid, const u8 *buf,
  533. size_t len, unsigned int wait_time)
  534. {
  535. struct wpa_supplicant *wpa_s = ctx;
  536. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  537. wait_time,
  538. wpas_p2p_send_action_tx_status, 1);
  539. }
  540. static void wpas_send_action_done(void *ctx)
  541. {
  542. struct wpa_supplicant *wpa_s = ctx;
  543. offchannel_send_action_done(wpa_s);
  544. }
  545. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  546. struct p2p_go_neg_results *params)
  547. {
  548. if (wpa_s->go_params == NULL) {
  549. wpa_s->go_params = os_malloc(sizeof(*params));
  550. if (wpa_s->go_params == NULL)
  551. return -1;
  552. }
  553. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  554. return 0;
  555. }
  556. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  557. struct p2p_go_neg_results *res)
  558. {
  559. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
  560. MAC2STR(res->peer_interface_addr));
  561. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  562. res->ssid, res->ssid_len);
  563. wpa_supplicant_ap_deinit(wpa_s);
  564. wpas_copy_go_neg_results(wpa_s, res);
  565. if (res->wps_method == WPS_PBC)
  566. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  567. else {
  568. u16 dev_pw_id = DEV_PW_DEFAULT;
  569. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  570. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  571. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  572. wpa_s->p2p_pin, 1, dev_pw_id);
  573. }
  574. }
  575. static void p2p_go_configured(void *ctx, void *data)
  576. {
  577. struct wpa_supplicant *wpa_s = ctx;
  578. struct p2p_go_neg_results *params = data;
  579. struct wpa_ssid *ssid;
  580. int network_id = -1;
  581. ssid = wpa_s->current_ssid;
  582. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  583. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  584. if (wpa_s->global->p2p_group_formation == wpa_s)
  585. wpa_s->global->p2p_group_formation = NULL;
  586. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  587. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  588. "go_dev_addr=" MACSTR "%s",
  589. wpa_s->ifname,
  590. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  591. ssid->frequency,
  592. params->passphrase ? params->passphrase : "",
  593. MAC2STR(wpa_s->parent->own_addr),
  594. params->persistent_group ? " [PERSISTENT]" : "");
  595. if (params->persistent_group)
  596. network_id = wpas_p2p_store_persistent_group(
  597. wpa_s->parent, ssid,
  598. wpa_s->parent->own_addr);
  599. if (network_id < 0)
  600. network_id = ssid->id;
  601. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  602. wpas_p2p_cross_connect_setup(wpa_s);
  603. wpas_p2p_set_group_idle_timeout(wpa_s);
  604. return;
  605. }
  606. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  607. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  608. params->peer_interface_addr)) {
  609. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  610. "filtering");
  611. return;
  612. }
  613. if (params->wps_method == WPS_PBC)
  614. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  615. params->peer_device_addr);
  616. else if (wpa_s->p2p_pin[0])
  617. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  618. wpa_s->p2p_pin, NULL, 0);
  619. os_free(wpa_s->go_params);
  620. wpa_s->go_params = NULL;
  621. }
  622. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  623. struct p2p_go_neg_results *params,
  624. int group_formation)
  625. {
  626. struct wpa_ssid *ssid;
  627. if (wpas_copy_go_neg_results(wpa_s, params) < 0)
  628. return;
  629. ssid = wpa_config_add_network(wpa_s->conf);
  630. if (ssid == NULL)
  631. return;
  632. wpa_s->show_group_started = 0;
  633. wpa_config_set_network_defaults(ssid);
  634. ssid->temporary = 1;
  635. ssid->p2p_group = 1;
  636. ssid->p2p_persistent_group = params->persistent_group;
  637. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  638. WPAS_MODE_P2P_GO;
  639. ssid->frequency = params->freq;
  640. ssid->ssid = os_zalloc(params->ssid_len + 1);
  641. if (ssid->ssid) {
  642. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  643. ssid->ssid_len = params->ssid_len;
  644. }
  645. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  646. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  647. ssid->proto = WPA_PROTO_RSN;
  648. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  649. ssid->passphrase = os_strdup(params->passphrase);
  650. wpa_s->ap_configured_cb = p2p_go_configured;
  651. wpa_s->ap_configured_cb_ctx = wpa_s;
  652. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  653. wpa_s->connect_without_scan = ssid;
  654. wpa_s->reassociate = 1;
  655. wpa_s->disconnected = 0;
  656. wpa_supplicant_req_scan(wpa_s, 0, 0);
  657. }
  658. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  659. const struct wpa_supplicant *src)
  660. {
  661. struct wpa_config *d;
  662. const struct wpa_config *s;
  663. d = dst->conf;
  664. s = src->conf;
  665. #define C(n) if (s->n) d->n = os_strdup(s->n)
  666. C(device_name);
  667. C(manufacturer);
  668. C(model_name);
  669. C(model_number);
  670. C(serial_number);
  671. C(config_methods);
  672. #undef C
  673. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  674. os_memcpy(d->sec_device_type, s->sec_device_type,
  675. sizeof(d->sec_device_type));
  676. d->num_sec_device_types = s->num_sec_device_types;
  677. d->p2p_group_idle = s->p2p_group_idle;
  678. d->p2p_intra_bss = s->p2p_intra_bss;
  679. d->persistent_reconnect = s->persistent_reconnect;
  680. }
  681. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  682. enum wpa_driver_if_type type)
  683. {
  684. char ifname[120], force_ifname[120];
  685. if (wpa_s->pending_interface_name[0]) {
  686. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  687. "- skip creation of a new one");
  688. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  689. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  690. "unknown?! ifname='%s'",
  691. wpa_s->pending_interface_name);
  692. return -1;
  693. }
  694. return 0;
  695. }
  696. os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
  697. wpa_s->p2p_group_idx);
  698. if (os_strlen(ifname) >= IFNAMSIZ &&
  699. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  700. /* Try to avoid going over the IFNAMSIZ length limit */
  701. os_snprintf(ifname, sizeof(ifname), "p2p-%d",
  702. wpa_s->p2p_group_idx);
  703. }
  704. force_ifname[0] = '\0';
  705. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  706. ifname);
  707. wpa_s->p2p_group_idx++;
  708. wpa_s->pending_interface_type = type;
  709. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  710. wpa_s->pending_interface_addr, NULL) < 0) {
  711. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  712. "interface");
  713. return -1;
  714. }
  715. if (force_ifname[0]) {
  716. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  717. force_ifname);
  718. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  719. sizeof(wpa_s->pending_interface_name));
  720. } else
  721. os_strlcpy(wpa_s->pending_interface_name, ifname,
  722. sizeof(wpa_s->pending_interface_name));
  723. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  724. MACSTR, wpa_s->pending_interface_name,
  725. MAC2STR(wpa_s->pending_interface_addr));
  726. return 0;
  727. }
  728. static void wpas_p2p_remove_pending_group_interface(
  729. struct wpa_supplicant *wpa_s)
  730. {
  731. if (!wpa_s->pending_interface_name[0] ||
  732. is_zero_ether_addr(wpa_s->pending_interface_addr))
  733. return; /* No pending virtual interface */
  734. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  735. wpa_s->pending_interface_name);
  736. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  737. wpa_s->pending_interface_name);
  738. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  739. wpa_s->pending_interface_name[0] = '\0';
  740. }
  741. static struct wpa_supplicant *
  742. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  743. {
  744. struct wpa_interface iface;
  745. struct wpa_supplicant *group_wpa_s;
  746. if (!wpa_s->pending_interface_name[0]) {
  747. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  748. if (!wpas_p2p_create_iface(wpa_s))
  749. return NULL;
  750. /*
  751. * Something has forced us to remove the pending interface; try
  752. * to create a new one and hope for the best that we will get
  753. * the same local address.
  754. */
  755. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  756. WPA_IF_P2P_CLIENT) < 0)
  757. return NULL;
  758. }
  759. os_memset(&iface, 0, sizeof(iface));
  760. iface.ifname = wpa_s->pending_interface_name;
  761. iface.driver = wpa_s->driver->name;
  762. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  763. iface.driver_param = wpa_s->conf->driver_param;
  764. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  765. if (group_wpa_s == NULL) {
  766. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  767. "wpa_supplicant interface");
  768. return NULL;
  769. }
  770. wpa_s->pending_interface_name[0] = '\0';
  771. group_wpa_s->parent = wpa_s;
  772. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  773. P2P_GROUP_INTERFACE_CLIENT;
  774. wpa_s->global->p2p_group_formation = group_wpa_s;
  775. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  776. return group_wpa_s;
  777. }
  778. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  779. void *timeout_ctx)
  780. {
  781. struct wpa_supplicant *wpa_s = eloop_ctx;
  782. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  783. if (wpa_s->global->p2p)
  784. p2p_group_formation_failed(wpa_s->global->p2p);
  785. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  786. wpa_drv_p2p_group_formation_failed(wpa_s);
  787. wpas_group_formation_completed(wpa_s, 0);
  788. }
  789. void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  790. {
  791. struct wpa_supplicant *wpa_s = ctx;
  792. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  793. wpa_drv_cancel_remain_on_channel(wpa_s);
  794. wpa_s->off_channel_freq = 0;
  795. wpa_s->roc_waiting_drv_freq = 0;
  796. }
  797. if (res->status) {
  798. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
  799. res->status);
  800. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  801. wpas_p2p_remove_pending_group_interface(wpa_s);
  802. return;
  803. }
  804. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
  805. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  806. if (wpa_s->create_p2p_iface) {
  807. struct wpa_supplicant *group_wpa_s =
  808. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  809. if (group_wpa_s == NULL) {
  810. wpas_p2p_remove_pending_group_interface(wpa_s);
  811. return;
  812. }
  813. if (group_wpa_s != wpa_s) {
  814. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  815. sizeof(group_wpa_s->p2p_pin));
  816. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  817. }
  818. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  819. wpa_s->pending_interface_name[0] = '\0';
  820. group_wpa_s->p2p_in_provisioning = 1;
  821. if (res->role_go)
  822. wpas_start_wps_go(group_wpa_s, res, 1);
  823. else
  824. wpas_start_wps_enrollee(group_wpa_s, res);
  825. } else {
  826. wpa_s->p2p_in_provisioning = 1;
  827. wpa_s->global->p2p_group_formation = wpa_s;
  828. if (res->role_go)
  829. wpas_start_wps_go(wpa_s, res, 1);
  830. else
  831. wpas_start_wps_enrollee(ctx, res);
  832. }
  833. wpa_s->p2p_long_listen = 0;
  834. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  835. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  836. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  837. (res->peer_config_timeout % 100) * 10000,
  838. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  839. }
  840. void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  841. {
  842. struct wpa_supplicant *wpa_s = ctx;
  843. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  844. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  845. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  846. }
  847. void wpas_dev_found(void *ctx, const u8 *addr,
  848. const struct p2p_peer_info *info,
  849. int new_device)
  850. {
  851. #ifndef CONFIG_NO_STDOUT_DEBUG
  852. struct wpa_supplicant *wpa_s = ctx;
  853. char devtype[WPS_DEV_TYPE_BUFSIZE];
  854. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  855. " p2p_dev_addr=" MACSTR
  856. " pri_dev_type=%s name='%s' config_methods=0x%x "
  857. "dev_capab=0x%x group_capab=0x%x",
  858. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  859. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  860. sizeof(devtype)),
  861. info->device_name, info->config_methods,
  862. info->dev_capab, info->group_capab);
  863. #endif /* CONFIG_NO_STDOUT_DEBUG */
  864. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  865. }
  866. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  867. {
  868. struct wpa_supplicant *wpa_s = ctx;
  869. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  870. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  871. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  872. }
  873. static int wpas_start_listen(void *ctx, unsigned int freq,
  874. unsigned int duration,
  875. const struct wpabuf *probe_resp_ie)
  876. {
  877. struct wpa_supplicant *wpa_s = ctx;
  878. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
  879. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  880. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  881. "report received Probe Request frames");
  882. return -1;
  883. }
  884. wpa_s->pending_listen_freq = freq;
  885. wpa_s->pending_listen_duration = duration;
  886. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  887. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  888. "to remain on channel (%u MHz) for Listen "
  889. "state", freq);
  890. wpa_s->pending_listen_freq = 0;
  891. return -1;
  892. }
  893. wpa_s->off_channel_freq = 0;
  894. wpa_s->roc_waiting_drv_freq = freq;
  895. return 0;
  896. }
  897. static void wpas_stop_listen(void *ctx)
  898. {
  899. struct wpa_supplicant *wpa_s = ctx;
  900. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  901. wpa_drv_cancel_remain_on_channel(wpa_s);
  902. wpa_s->off_channel_freq = 0;
  903. wpa_s->roc_waiting_drv_freq = 0;
  904. }
  905. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  906. wpa_drv_probe_req_report(wpa_s, 0);
  907. }
  908. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  909. {
  910. struct wpa_supplicant *wpa_s = ctx;
  911. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
  912. }
  913. static struct p2p_srv_bonjour *
  914. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  915. const struct wpabuf *query)
  916. {
  917. struct p2p_srv_bonjour *bsrv;
  918. size_t len;
  919. len = wpabuf_len(query);
  920. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  921. struct p2p_srv_bonjour, list) {
  922. if (len == wpabuf_len(bsrv->query) &&
  923. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  924. len) == 0)
  925. return bsrv;
  926. }
  927. return NULL;
  928. }
  929. static struct p2p_srv_upnp *
  930. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  931. const char *service)
  932. {
  933. struct p2p_srv_upnp *usrv;
  934. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  935. struct p2p_srv_upnp, list) {
  936. if (version == usrv->version &&
  937. os_strcmp(service, usrv->service) == 0)
  938. return usrv;
  939. }
  940. return NULL;
  941. }
  942. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  943. u8 srv_trans_id)
  944. {
  945. u8 *len_pos;
  946. if (wpabuf_tailroom(resp) < 5)
  947. return;
  948. /* Length (to be filled) */
  949. len_pos = wpabuf_put(resp, 2);
  950. wpabuf_put_u8(resp, srv_proto);
  951. wpabuf_put_u8(resp, srv_trans_id);
  952. /* Status Code */
  953. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  954. /* Response Data: empty */
  955. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  956. }
  957. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  958. struct wpabuf *resp, u8 srv_trans_id)
  959. {
  960. struct p2p_srv_bonjour *bsrv;
  961. u8 *len_pos;
  962. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  963. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  964. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  965. return;
  966. }
  967. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  968. struct p2p_srv_bonjour, list) {
  969. if (wpabuf_tailroom(resp) <
  970. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  971. return;
  972. /* Length (to be filled) */
  973. len_pos = wpabuf_put(resp, 2);
  974. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  975. wpabuf_put_u8(resp, srv_trans_id);
  976. /* Status Code */
  977. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  978. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  979. wpabuf_head(bsrv->resp),
  980. wpabuf_len(bsrv->resp));
  981. /* Response Data */
  982. wpabuf_put_buf(resp, bsrv->query); /* Key */
  983. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  984. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  985. 2);
  986. }
  987. }
  988. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  989. struct wpabuf *resp, u8 srv_trans_id,
  990. const u8 *query, size_t query_len)
  991. {
  992. struct p2p_srv_bonjour *bsrv;
  993. struct wpabuf buf;
  994. u8 *len_pos;
  995. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  996. query, query_len);
  997. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  998. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  999. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1000. srv_trans_id);
  1001. return;
  1002. }
  1003. if (query_len == 0) {
  1004. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1005. return;
  1006. }
  1007. if (wpabuf_tailroom(resp) < 5)
  1008. return;
  1009. /* Length (to be filled) */
  1010. len_pos = wpabuf_put(resp, 2);
  1011. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1012. wpabuf_put_u8(resp, srv_trans_id);
  1013. wpabuf_set(&buf, query, query_len);
  1014. bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
  1015. if (bsrv == NULL) {
  1016. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1017. "available");
  1018. /* Status Code */
  1019. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1020. /* Response Data: empty */
  1021. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1022. 2);
  1023. return;
  1024. }
  1025. /* Status Code */
  1026. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1027. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1028. wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
  1029. if (wpabuf_tailroom(resp) >=
  1030. wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
  1031. /* Response Data */
  1032. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1033. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1034. }
  1035. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1036. }
  1037. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1038. struct wpabuf *resp, u8 srv_trans_id)
  1039. {
  1040. struct p2p_srv_upnp *usrv;
  1041. u8 *len_pos;
  1042. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1043. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1044. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1045. return;
  1046. }
  1047. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1048. struct p2p_srv_upnp, list) {
  1049. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1050. return;
  1051. /* Length (to be filled) */
  1052. len_pos = wpabuf_put(resp, 2);
  1053. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1054. wpabuf_put_u8(resp, srv_trans_id);
  1055. /* Status Code */
  1056. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1057. /* Response Data */
  1058. wpabuf_put_u8(resp, usrv->version);
  1059. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1060. usrv->service);
  1061. wpabuf_put_str(resp, usrv->service);
  1062. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1063. 2);
  1064. }
  1065. }
  1066. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1067. struct wpabuf *resp, u8 srv_trans_id,
  1068. const u8 *query, size_t query_len)
  1069. {
  1070. struct p2p_srv_upnp *usrv;
  1071. u8 *len_pos;
  1072. u8 version;
  1073. char *str;
  1074. int count = 0;
  1075. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1076. query, query_len);
  1077. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1078. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1079. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1080. srv_trans_id);
  1081. return;
  1082. }
  1083. if (query_len == 0) {
  1084. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1085. return;
  1086. }
  1087. if (wpabuf_tailroom(resp) < 5)
  1088. return;
  1089. /* Length (to be filled) */
  1090. len_pos = wpabuf_put(resp, 2);
  1091. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1092. wpabuf_put_u8(resp, srv_trans_id);
  1093. version = query[0];
  1094. str = os_malloc(query_len);
  1095. if (str == NULL)
  1096. return;
  1097. os_memcpy(str, query + 1, query_len - 1);
  1098. str[query_len - 1] = '\0';
  1099. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1100. struct p2p_srv_upnp, list) {
  1101. if (version != usrv->version)
  1102. continue;
  1103. if (os_strcmp(str, "ssdp:all") != 0 &&
  1104. os_strstr(usrv->service, str) == NULL)
  1105. continue;
  1106. if (wpabuf_tailroom(resp) < 2)
  1107. break;
  1108. if (count == 0) {
  1109. /* Status Code */
  1110. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1111. /* Response Data */
  1112. wpabuf_put_u8(resp, version);
  1113. } else
  1114. wpabuf_put_u8(resp, ',');
  1115. count++;
  1116. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1117. usrv->service);
  1118. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1119. break;
  1120. wpabuf_put_str(resp, usrv->service);
  1121. }
  1122. os_free(str);
  1123. if (count == 0) {
  1124. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1125. "available");
  1126. /* Status Code */
  1127. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1128. /* Response Data: empty */
  1129. }
  1130. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1131. }
  1132. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1133. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1134. {
  1135. struct wpa_supplicant *wpa_s = ctx;
  1136. const u8 *pos = tlvs;
  1137. const u8 *end = tlvs + tlvs_len;
  1138. const u8 *tlv_end;
  1139. u16 slen;
  1140. struct wpabuf *resp;
  1141. u8 srv_proto, srv_trans_id;
  1142. size_t buf_len;
  1143. char *buf;
  1144. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1145. tlvs, tlvs_len);
  1146. buf_len = 2 * tlvs_len + 1;
  1147. buf = os_malloc(buf_len);
  1148. if (buf) {
  1149. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1150. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1151. MACSTR " %u %u %s",
  1152. freq, MAC2STR(sa), dialog_token, update_indic,
  1153. buf);
  1154. os_free(buf);
  1155. }
  1156. if (wpa_s->p2p_sd_over_ctrl_iface) {
  1157. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1158. update_indic, tlvs, tlvs_len);
  1159. return; /* to be processed by an external program */
  1160. }
  1161. resp = wpabuf_alloc(10000);
  1162. if (resp == NULL)
  1163. return;
  1164. while (pos + 1 < end) {
  1165. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1166. slen = WPA_GET_LE16(pos);
  1167. pos += 2;
  1168. if (pos + slen > end || slen < 2) {
  1169. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1170. "length");
  1171. wpabuf_free(resp);
  1172. return;
  1173. }
  1174. tlv_end = pos + slen;
  1175. srv_proto = *pos++;
  1176. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1177. srv_proto);
  1178. srv_trans_id = *pos++;
  1179. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1180. srv_trans_id);
  1181. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1182. pos, tlv_end - pos);
  1183. if (wpa_s->force_long_sd) {
  1184. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1185. "response");
  1186. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1187. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1188. goto done;
  1189. }
  1190. switch (srv_proto) {
  1191. case P2P_SERV_ALL_SERVICES:
  1192. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1193. "for all services");
  1194. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1195. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1196. wpa_printf(MSG_DEBUG, "P2P: No service "
  1197. "discovery protocols available");
  1198. wpas_sd_add_proto_not_avail(
  1199. resp, P2P_SERV_ALL_SERVICES,
  1200. srv_trans_id);
  1201. break;
  1202. }
  1203. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1204. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1205. break;
  1206. case P2P_SERV_BONJOUR:
  1207. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1208. pos, tlv_end - pos);
  1209. break;
  1210. case P2P_SERV_UPNP:
  1211. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1212. pos, tlv_end - pos);
  1213. break;
  1214. default:
  1215. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1216. "protocol %u", srv_proto);
  1217. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1218. srv_trans_id);
  1219. break;
  1220. }
  1221. pos = tlv_end;
  1222. }
  1223. done:
  1224. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1225. update_indic, tlvs, tlvs_len);
  1226. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1227. wpabuf_free(resp);
  1228. }
  1229. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1230. const u8 *tlvs, size_t tlvs_len)
  1231. {
  1232. struct wpa_supplicant *wpa_s = ctx;
  1233. const u8 *pos = tlvs;
  1234. const u8 *end = tlvs + tlvs_len;
  1235. const u8 *tlv_end;
  1236. u16 slen;
  1237. size_t buf_len;
  1238. char *buf;
  1239. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1240. tlvs, tlvs_len);
  1241. if (tlvs_len > 1500) {
  1242. /* TODO: better way for handling this */
  1243. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1244. P2P_EVENT_SERV_DISC_RESP MACSTR
  1245. " %u <long response: %u bytes>",
  1246. MAC2STR(sa), update_indic,
  1247. (unsigned int) tlvs_len);
  1248. } else {
  1249. buf_len = 2 * tlvs_len + 1;
  1250. buf = os_malloc(buf_len);
  1251. if (buf) {
  1252. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1253. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1254. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1255. MAC2STR(sa), update_indic, buf);
  1256. os_free(buf);
  1257. }
  1258. }
  1259. while (pos < end) {
  1260. u8 srv_proto, srv_trans_id, status;
  1261. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1262. slen = WPA_GET_LE16(pos);
  1263. pos += 2;
  1264. if (pos + slen > end || slen < 3) {
  1265. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1266. "length");
  1267. return;
  1268. }
  1269. tlv_end = pos + slen;
  1270. srv_proto = *pos++;
  1271. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1272. srv_proto);
  1273. srv_trans_id = *pos++;
  1274. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1275. srv_trans_id);
  1276. status = *pos++;
  1277. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1278. status);
  1279. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1280. pos, tlv_end - pos);
  1281. pos = tlv_end;
  1282. }
  1283. wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
  1284. }
  1285. u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1286. const struct wpabuf *tlvs)
  1287. {
  1288. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1289. return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
  1290. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1291. return 0;
  1292. return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1293. }
  1294. u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1295. u8 version, const char *query)
  1296. {
  1297. struct wpabuf *tlvs;
  1298. u64 ret;
  1299. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1300. if (tlvs == NULL)
  1301. return 0;
  1302. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1303. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1304. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1305. wpabuf_put_u8(tlvs, version);
  1306. wpabuf_put_str(tlvs, query);
  1307. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1308. wpabuf_free(tlvs);
  1309. return ret;
  1310. }
  1311. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
  1312. {
  1313. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1314. return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
  1315. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1316. return -1;
  1317. return p2p_sd_cancel_request(wpa_s->global->p2p,
  1318. (void *) (uintptr_t) req);
  1319. }
  1320. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1321. const u8 *dst, u8 dialog_token,
  1322. const struct wpabuf *resp_tlvs)
  1323. {
  1324. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1325. wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
  1326. resp_tlvs);
  1327. return;
  1328. }
  1329. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1330. return;
  1331. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1332. resp_tlvs);
  1333. }
  1334. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1335. {
  1336. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1337. wpa_drv_p2p_service_update(wpa_s);
  1338. return;
  1339. }
  1340. if (wpa_s->global->p2p)
  1341. p2p_sd_service_update(wpa_s->global->p2p);
  1342. }
  1343. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1344. {
  1345. dl_list_del(&bsrv->list);
  1346. wpabuf_free(bsrv->query);
  1347. wpabuf_free(bsrv->resp);
  1348. os_free(bsrv);
  1349. }
  1350. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1351. {
  1352. dl_list_del(&usrv->list);
  1353. os_free(usrv->service);
  1354. os_free(usrv);
  1355. }
  1356. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1357. {
  1358. struct p2p_srv_bonjour *bsrv, *bn;
  1359. struct p2p_srv_upnp *usrv, *un;
  1360. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1361. struct p2p_srv_bonjour, list)
  1362. wpas_p2p_srv_bonjour_free(bsrv);
  1363. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1364. struct p2p_srv_upnp, list)
  1365. wpas_p2p_srv_upnp_free(usrv);
  1366. wpas_p2p_sd_service_update(wpa_s);
  1367. }
  1368. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1369. struct wpabuf *query, struct wpabuf *resp)
  1370. {
  1371. struct p2p_srv_bonjour *bsrv;
  1372. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1373. if (bsrv) {
  1374. wpabuf_free(query);
  1375. wpabuf_free(bsrv->resp);
  1376. bsrv->resp = resp;
  1377. return 0;
  1378. }
  1379. bsrv = os_zalloc(sizeof(*bsrv));
  1380. if (bsrv == NULL)
  1381. return -1;
  1382. bsrv->query = query;
  1383. bsrv->resp = resp;
  1384. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  1385. wpas_p2p_sd_service_update(wpa_s);
  1386. return 0;
  1387. }
  1388. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1389. const struct wpabuf *query)
  1390. {
  1391. struct p2p_srv_bonjour *bsrv;
  1392. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1393. if (bsrv == NULL)
  1394. return -1;
  1395. wpas_p2p_srv_bonjour_free(bsrv);
  1396. wpas_p2p_sd_service_update(wpa_s);
  1397. return 0;
  1398. }
  1399. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1400. const char *service)
  1401. {
  1402. struct p2p_srv_upnp *usrv;
  1403. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  1404. return 0; /* Already listed */
  1405. usrv = os_zalloc(sizeof(*usrv));
  1406. if (usrv == NULL)
  1407. return -1;
  1408. usrv->version = version;
  1409. usrv->service = os_strdup(service);
  1410. if (usrv->service == NULL) {
  1411. os_free(usrv);
  1412. return -1;
  1413. }
  1414. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  1415. wpas_p2p_sd_service_update(wpa_s);
  1416. return 0;
  1417. }
  1418. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1419. const char *service)
  1420. {
  1421. struct p2p_srv_upnp *usrv;
  1422. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  1423. if (usrv == NULL)
  1424. return -1;
  1425. wpas_p2p_srv_upnp_free(usrv);
  1426. wpas_p2p_sd_service_update(wpa_s);
  1427. return 0;
  1428. }
  1429. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  1430. const u8 *peer, const char *params,
  1431. unsigned int generated_pin)
  1432. {
  1433. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
  1434. MAC2STR(peer), generated_pin, params);
  1435. }
  1436. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  1437. const u8 *peer, const char *params)
  1438. {
  1439. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
  1440. MAC2STR(peer), params);
  1441. }
  1442. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  1443. const u8 *dev_addr, const u8 *pri_dev_type,
  1444. const char *dev_name, u16 supp_config_methods,
  1445. u8 dev_capab, u8 group_capab, const u8 *group_id,
  1446. size_t group_id_len)
  1447. {
  1448. struct wpa_supplicant *wpa_s = ctx;
  1449. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1450. char params[300];
  1451. u8 empty_dev_type[8];
  1452. unsigned int generated_pin = 0;
  1453. struct wpa_supplicant *group = NULL;
  1454. if (group_id) {
  1455. for (group = wpa_s->global->ifaces; group; group = group->next)
  1456. {
  1457. struct wpa_ssid *s = group->current_ssid;
  1458. if (s != NULL &&
  1459. s->mode == WPAS_MODE_P2P_GO &&
  1460. group_id_len - ETH_ALEN == s->ssid_len &&
  1461. os_memcmp(group_id + ETH_ALEN, s->ssid,
  1462. s->ssid_len) == 0)
  1463. break;
  1464. }
  1465. }
  1466. if (pri_dev_type == NULL) {
  1467. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  1468. pri_dev_type = empty_dev_type;
  1469. }
  1470. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  1471. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1472. "dev_capab=0x%x group_capab=0x%x%s%s",
  1473. MAC2STR(dev_addr),
  1474. wps_dev_type_bin2str(pri_dev_type, devtype,
  1475. sizeof(devtype)),
  1476. dev_name, supp_config_methods, dev_capab, group_capab,
  1477. group ? " group=" : "",
  1478. group ? group->ifname : "");
  1479. params[sizeof(params) - 1] = '\0';
  1480. if (config_methods & WPS_CONFIG_DISPLAY) {
  1481. generated_pin = wps_generate_pin();
  1482. wpas_prov_disc_local_display(wpa_s, peer, params,
  1483. generated_pin);
  1484. } else if (config_methods & WPS_CONFIG_KEYPAD)
  1485. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  1486. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1487. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
  1488. "%s", MAC2STR(peer), params);
  1489. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  1490. P2P_PROV_DISC_SUCCESS,
  1491. config_methods, generated_pin);
  1492. }
  1493. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  1494. {
  1495. struct wpa_supplicant *wpa_s = ctx;
  1496. unsigned int generated_pin = 0;
  1497. if (config_methods & WPS_CONFIG_DISPLAY)
  1498. wpas_prov_disc_local_keypad(wpa_s, peer, "");
  1499. else if (config_methods & WPS_CONFIG_KEYPAD) {
  1500. generated_pin = wps_generate_pin();
  1501. wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
  1502. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1503. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
  1504. MAC2STR(peer));
  1505. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  1506. P2P_PROV_DISC_SUCCESS,
  1507. config_methods, generated_pin);
  1508. if (wpa_s->pending_pd_before_join &&
  1509. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  1510. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  1511. wpa_s->pending_pd_before_join = 0;
  1512. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  1513. "join-existing-group operation");
  1514. wpas_p2p_join_start(wpa_s);
  1515. }
  1516. }
  1517. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  1518. enum p2p_prov_disc_status status)
  1519. {
  1520. struct wpa_supplicant *wpa_s = ctx;
  1521. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  1522. status, 0, 0);
  1523. }
  1524. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  1525. const u8 *go_dev_addr, const u8 *ssid,
  1526. size_t ssid_len, int *go, u8 *group_bssid,
  1527. int *force_freq, int persistent_group)
  1528. {
  1529. struct wpa_supplicant *wpa_s = ctx;
  1530. struct wpa_ssid *s;
  1531. u8 cur_bssid[ETH_ALEN];
  1532. int res;
  1533. struct wpa_supplicant *grp;
  1534. if (!persistent_group) {
  1535. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  1536. " to join an active group", MAC2STR(sa));
  1537. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  1538. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  1539. == 0 ||
  1540. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  1541. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  1542. "authorized invitation");
  1543. goto accept_inv;
  1544. }
  1545. /*
  1546. * Do not accept the invitation automatically; notify user and
  1547. * request approval.
  1548. */
  1549. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  1550. }
  1551. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  1552. if (grp) {
  1553. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  1554. "running persistent group");
  1555. if (*go)
  1556. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  1557. goto accept_inv;
  1558. }
  1559. if (!wpa_s->conf->persistent_reconnect)
  1560. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  1561. for (s = wpa_s->conf->ssid; s; s = s->next) {
  1562. if (s->disabled == 2 &&
  1563. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  1564. s->ssid_len == ssid_len &&
  1565. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  1566. break;
  1567. }
  1568. if (!s) {
  1569. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  1570. " requested reinvocation of an unknown group",
  1571. MAC2STR(sa));
  1572. return P2P_SC_FAIL_UNKNOWN_GROUP;
  1573. }
  1574. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  1575. *go = 1;
  1576. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  1577. wpa_printf(MSG_DEBUG, "P2P: The only available "
  1578. "interface is already in use - reject "
  1579. "invitation");
  1580. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  1581. }
  1582. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  1583. } else if (s->mode == WPAS_MODE_P2P_GO) {
  1584. *go = 1;
  1585. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  1586. {
  1587. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  1588. "interface address for the group");
  1589. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  1590. }
  1591. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  1592. ETH_ALEN);
  1593. }
  1594. accept_inv:
  1595. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
  1596. wpa_s->assoc_freq) {
  1597. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  1598. "the channel we are already using");
  1599. *force_freq = wpa_s->assoc_freq;
  1600. }
  1601. res = wpa_drv_shared_freq(wpa_s);
  1602. if (res > 0) {
  1603. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  1604. "with the channel we are already using on a "
  1605. "shared interface");
  1606. *force_freq = res;
  1607. }
  1608. return P2P_SC_SUCCESS;
  1609. }
  1610. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  1611. const u8 *ssid, size_t ssid_len,
  1612. const u8 *go_dev_addr, u8 status,
  1613. int op_freq)
  1614. {
  1615. struct wpa_supplicant *wpa_s = ctx;
  1616. struct wpa_ssid *s;
  1617. for (s = wpa_s->conf->ssid; s; s = s->next) {
  1618. if (s->disabled == 2 &&
  1619. s->ssid_len == ssid_len &&
  1620. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  1621. break;
  1622. }
  1623. if (status == P2P_SC_SUCCESS) {
  1624. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  1625. " was accepted; op_freq=%d MHz",
  1626. MAC2STR(sa), op_freq);
  1627. if (s) {
  1628. wpas_p2p_group_add_persistent(
  1629. wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
  1630. } else if (bssid) {
  1631. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  1632. wpa_s->p2p_wps_method);
  1633. }
  1634. return;
  1635. }
  1636. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  1637. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  1638. " was rejected (status %u)", MAC2STR(sa), status);
  1639. return;
  1640. }
  1641. if (!s) {
  1642. if (bssid) {
  1643. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  1644. "sa=" MACSTR " go_dev_addr=" MACSTR
  1645. " bssid=" MACSTR " unknown-network",
  1646. MAC2STR(sa), MAC2STR(go_dev_addr),
  1647. MAC2STR(bssid));
  1648. } else {
  1649. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  1650. "sa=" MACSTR " go_dev_addr=" MACSTR
  1651. " unknown-network",
  1652. MAC2STR(sa), MAC2STR(go_dev_addr));
  1653. }
  1654. return;
  1655. }
  1656. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
  1657. " persistent=%d", MAC2STR(sa), s->id);
  1658. }
  1659. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
  1660. {
  1661. struct wpa_supplicant *wpa_s = ctx;
  1662. struct wpa_ssid *ssid;
  1663. if (bssid) {
  1664. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  1665. "status=%d " MACSTR,
  1666. status, MAC2STR(bssid));
  1667. } else {
  1668. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  1669. "status=%d ", status);
  1670. }
  1671. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  1672. if (wpa_s->pending_invite_ssid_id == -1)
  1673. return; /* Invitation to active group */
  1674. if (status != P2P_SC_SUCCESS) {
  1675. wpas_p2p_remove_pending_group_interface(wpa_s);
  1676. return;
  1677. }
  1678. ssid = wpa_config_get_network(wpa_s->conf,
  1679. wpa_s->pending_invite_ssid_id);
  1680. if (ssid == NULL) {
  1681. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  1682. "data matching with invitation");
  1683. return;
  1684. }
  1685. wpas_p2p_group_add_persistent(wpa_s, ssid,
  1686. ssid->mode == WPAS_MODE_P2P_GO, 0);
  1687. }
  1688. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  1689. struct p2p_channels *chan)
  1690. {
  1691. int i, cla = 0;
  1692. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  1693. "band");
  1694. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  1695. chan->reg_class[cla].reg_class = 81;
  1696. chan->reg_class[cla].channels = 11;
  1697. for (i = 0; i < 11; i++)
  1698. chan->reg_class[cla].channel[i] = i + 1;
  1699. cla++;
  1700. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  1701. "band");
  1702. /* Operating class 115 - 5 GHz, channels 36-48 */
  1703. chan->reg_class[cla].reg_class = 115;
  1704. chan->reg_class[cla].channels = 4;
  1705. chan->reg_class[cla].channel[0] = 36;
  1706. chan->reg_class[cla].channel[1] = 40;
  1707. chan->reg_class[cla].channel[2] = 44;
  1708. chan->reg_class[cla].channel[3] = 48;
  1709. cla++;
  1710. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  1711. "band");
  1712. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  1713. chan->reg_class[cla].reg_class = 124;
  1714. chan->reg_class[cla].channels = 4;
  1715. chan->reg_class[cla].channel[0] = 149;
  1716. chan->reg_class[cla].channel[1] = 153;
  1717. chan->reg_class[cla].channel[2] = 157;
  1718. chan->reg_class[cla].channel[3] = 161;
  1719. cla++;
  1720. chan->reg_classes = cla;
  1721. return 0;
  1722. }
  1723. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  1724. u16 num_modes,
  1725. enum hostapd_hw_mode mode)
  1726. {
  1727. u16 i;
  1728. for (i = 0; i < num_modes; i++) {
  1729. if (modes[i].mode == mode)
  1730. return &modes[i];
  1731. }
  1732. return NULL;
  1733. }
  1734. static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
  1735. {
  1736. int i;
  1737. for (i = 0; i < mode->num_channels; i++) {
  1738. if (mode->channels[i].chan == chan) {
  1739. if (flags)
  1740. *flags = mode->channels[i].flag;
  1741. return !(mode->channels[i].flag &
  1742. (HOSTAPD_CHAN_DISABLED |
  1743. HOSTAPD_CHAN_PASSIVE_SCAN |
  1744. HOSTAPD_CHAN_NO_IBSS |
  1745. HOSTAPD_CHAN_RADAR));
  1746. }
  1747. }
  1748. return 0;
  1749. }
  1750. struct p2p_oper_class_map {
  1751. enum hostapd_hw_mode mode;
  1752. u8 op_class;
  1753. u8 min_chan;
  1754. u8 max_chan;
  1755. u8 inc;
  1756. enum { BW20, BW40PLUS, BW40MINUS } bw;
  1757. };
  1758. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  1759. struct p2p_channels *chan)
  1760. {
  1761. struct hostapd_hw_modes *mode;
  1762. int cla, op;
  1763. struct p2p_oper_class_map op_class[] = {
  1764. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  1765. { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
  1766. #if 0 /* Do not enable HT40 on 2 GHz for now */
  1767. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  1768. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  1769. #endif
  1770. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  1771. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  1772. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  1773. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  1774. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  1775. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  1776. { -1, 0, 0, 0, 0, BW20 }
  1777. };
  1778. if (wpa_s->hw.modes == NULL) {
  1779. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  1780. "of all supported channels; assume dualband "
  1781. "support");
  1782. return wpas_p2p_default_channels(wpa_s, chan);
  1783. }
  1784. cla = 0;
  1785. for (op = 0; op_class[op].op_class; op++) {
  1786. struct p2p_oper_class_map *o = &op_class[op];
  1787. u8 ch;
  1788. struct p2p_reg_class *reg = NULL;
  1789. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  1790. if (mode == NULL)
  1791. continue;
  1792. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  1793. int flag;
  1794. if (!has_channel(mode, ch, &flag))
  1795. continue;
  1796. if (o->bw == BW40MINUS &&
  1797. (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
  1798. !has_channel(mode, ch - 4, NULL)))
  1799. continue;
  1800. if (o->bw == BW40PLUS &&
  1801. (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
  1802. !has_channel(mode, ch + 4, NULL)))
  1803. continue;
  1804. if (reg == NULL) {
  1805. wpa_printf(MSG_DEBUG, "P2P: Add operating "
  1806. "class %u", o->op_class);
  1807. reg = &chan->reg_class[cla];
  1808. cla++;
  1809. reg->reg_class = o->op_class;
  1810. }
  1811. reg->channel[reg->channels] = ch;
  1812. reg->channels++;
  1813. }
  1814. if (reg) {
  1815. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  1816. reg->channel, reg->channels);
  1817. }
  1818. }
  1819. chan->reg_classes = cla;
  1820. return 0;
  1821. }
  1822. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  1823. size_t buf_len)
  1824. {
  1825. struct wpa_supplicant *wpa_s = ctx;
  1826. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  1827. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  1828. break;
  1829. }
  1830. if (wpa_s == NULL)
  1831. return -1;
  1832. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  1833. }
  1834. /**
  1835. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  1836. * @global: Pointer to global data from wpa_supplicant_init()
  1837. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  1838. * Returns: 0 on success, -1 on failure
  1839. */
  1840. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  1841. {
  1842. struct p2p_config p2p;
  1843. unsigned int r;
  1844. int i;
  1845. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  1846. return 0;
  1847. if (global->p2p)
  1848. return 0;
  1849. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1850. struct p2p_params params;
  1851. wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
  1852. os_memset(&params, 0, sizeof(params));
  1853. params.dev_name = wpa_s->conf->device_name;
  1854. os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
  1855. WPS_DEV_TYPE_LEN);
  1856. params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1857. os_memcpy(params.sec_dev_type,
  1858. wpa_s->conf->sec_device_type,
  1859. params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  1860. if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
  1861. return -1;
  1862. return 0;
  1863. }
  1864. os_memset(&p2p, 0, sizeof(p2p));
  1865. p2p.msg_ctx = wpa_s;
  1866. p2p.cb_ctx = wpa_s;
  1867. p2p.p2p_scan = wpas_p2p_scan;
  1868. p2p.send_action = wpas_send_action;
  1869. p2p.send_action_done = wpas_send_action_done;
  1870. p2p.go_neg_completed = wpas_go_neg_completed;
  1871. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  1872. p2p.dev_found = wpas_dev_found;
  1873. p2p.dev_lost = wpas_dev_lost;
  1874. p2p.start_listen = wpas_start_listen;
  1875. p2p.stop_listen = wpas_stop_listen;
  1876. p2p.send_probe_resp = wpas_send_probe_resp;
  1877. p2p.sd_request = wpas_sd_request;
  1878. p2p.sd_response = wpas_sd_response;
  1879. p2p.prov_disc_req = wpas_prov_disc_req;
  1880. p2p.prov_disc_resp = wpas_prov_disc_resp;
  1881. p2p.prov_disc_fail = wpas_prov_disc_fail;
  1882. p2p.invitation_process = wpas_invitation_process;
  1883. p2p.invitation_received = wpas_invitation_received;
  1884. p2p.invitation_result = wpas_invitation_result;
  1885. p2p.get_noa = wpas_get_noa;
  1886. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  1887. os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
  1888. p2p.dev_name = wpa_s->conf->device_name;
  1889. p2p.manufacturer = wpa_s->conf->manufacturer;
  1890. p2p.model_name = wpa_s->conf->model_name;
  1891. p2p.model_number = wpa_s->conf->model_number;
  1892. p2p.serial_number = wpa_s->conf->serial_number;
  1893. if (wpa_s->wps) {
  1894. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  1895. p2p.config_methods = wpa_s->wps->config_methods;
  1896. }
  1897. if (wpa_s->conf->p2p_listen_reg_class &&
  1898. wpa_s->conf->p2p_listen_channel) {
  1899. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  1900. p2p.channel = wpa_s->conf->p2p_listen_channel;
  1901. } else {
  1902. p2p.reg_class = 81;
  1903. /*
  1904. * Pick one of the social channels randomly as the listen
  1905. * channel.
  1906. */
  1907. os_get_random((u8 *) &r, sizeof(r));
  1908. p2p.channel = 1 + (r % 3) * 5;
  1909. }
  1910. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  1911. if (wpa_s->conf->p2p_oper_reg_class &&
  1912. wpa_s->conf->p2p_oper_channel) {
  1913. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  1914. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  1915. p2p.cfg_op_channel = 1;
  1916. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  1917. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  1918. } else {
  1919. p2p.op_reg_class = 81;
  1920. /*
  1921. * Use random operation channel from (1, 6, 11) if no other
  1922. * preference is indicated.
  1923. */
  1924. os_get_random((u8 *) &r, sizeof(r));
  1925. p2p.op_channel = 1 + (r % 3) * 5;
  1926. p2p.cfg_op_channel = 0;
  1927. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  1928. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  1929. }
  1930. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  1931. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  1932. p2p.country[2] = 0x04;
  1933. } else
  1934. os_memcpy(p2p.country, "XX\x04", 3);
  1935. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
  1936. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  1937. "channel list");
  1938. return -1;
  1939. }
  1940. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  1941. WPS_DEV_TYPE_LEN);
  1942. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1943. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  1944. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  1945. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  1946. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  1947. p2p.max_peers = 100;
  1948. if (wpa_s->conf->p2p_ssid_postfix) {
  1949. p2p.ssid_postfix_len =
  1950. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  1951. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  1952. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  1953. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  1954. p2p.ssid_postfix_len);
  1955. }
  1956. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  1957. global->p2p = p2p_init(&p2p);
  1958. if (global->p2p == NULL)
  1959. return -1;
  1960. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  1961. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  1962. continue;
  1963. p2p_add_wps_vendor_extension(
  1964. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  1965. }
  1966. return 0;
  1967. }
  1968. /**
  1969. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  1970. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  1971. *
  1972. * This function deinitialize per-interface P2P data.
  1973. */
  1974. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  1975. {
  1976. if (wpa_s->driver && wpa_s->drv_priv)
  1977. wpa_drv_probe_req_report(wpa_s, 0);
  1978. if (wpa_s->go_params) {
  1979. /* Clear any stored provisioning info */
  1980. p2p_clear_provisioning_info(
  1981. wpa_s->global->p2p,
  1982. wpa_s->go_params->peer_interface_addr);
  1983. }
  1984. os_free(wpa_s->go_params);
  1985. wpa_s->go_params = NULL;
  1986. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1987. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  1988. wpa_s->p2p_long_listen = 0;
  1989. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1990. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  1991. wpas_p2p_remove_pending_group_interface(wpa_s);
  1992. /* TODO: remove group interface from the driver if this wpa_s instance
  1993. * is on top of a P2P group interface */
  1994. }
  1995. /**
  1996. * wpas_p2p_deinit_global - Deinitialize global P2P module
  1997. * @global: Pointer to global data from wpa_supplicant_init()
  1998. *
  1999. * This function deinitializes the global (per device) P2P module.
  2000. */
  2001. void wpas_p2p_deinit_global(struct wpa_global *global)
  2002. {
  2003. struct wpa_supplicant *wpa_s, *tmp;
  2004. char *ifname;
  2005. if (global->p2p == NULL)
  2006. return;
  2007. /* Remove remaining P2P group interfaces */
  2008. wpa_s = global->ifaces;
  2009. if (wpa_s)
  2010. wpas_p2p_service_flush(wpa_s);
  2011. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  2012. wpa_s = wpa_s->next;
  2013. while (wpa_s) {
  2014. enum wpa_driver_if_type type;
  2015. tmp = global->ifaces;
  2016. while (tmp &&
  2017. (tmp == wpa_s ||
  2018. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  2019. tmp = tmp->next;
  2020. }
  2021. if (tmp == NULL)
  2022. break;
  2023. ifname = os_strdup(tmp->ifname);
  2024. type = wpas_p2p_if_type(tmp->p2p_group_interface);
  2025. wpa_supplicant_remove_iface(global, tmp);
  2026. if (ifname)
  2027. wpa_drv_if_remove(wpa_s, type, ifname);
  2028. os_free(ifname);
  2029. }
  2030. /*
  2031. * Deinit GO data on any possibly remaining interface (if main
  2032. * interface is used as GO).
  2033. */
  2034. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2035. if (wpa_s->ap_iface)
  2036. wpas_p2p_group_deinit(wpa_s);
  2037. }
  2038. p2p_deinit(global->p2p);
  2039. global->p2p = NULL;
  2040. }
  2041. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  2042. {
  2043. if (wpa_s->drv_flags &
  2044. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  2045. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  2046. return 1; /* P2P group requires a new interface in every case
  2047. */
  2048. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  2049. return 0; /* driver does not support concurrent operations */
  2050. if (wpa_s->global->ifaces->next)
  2051. return 1; /* more that one interface already in use */
  2052. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2053. return 1; /* this interface is already in use */
  2054. return 0;
  2055. }
  2056. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  2057. const u8 *peer_addr,
  2058. enum p2p_wps_method wps_method,
  2059. int go_intent, const u8 *own_interface_addr,
  2060. unsigned int force_freq, int persistent_group)
  2061. {
  2062. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2063. persistent_group = 2;
  2064. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2065. return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
  2066. go_intent, own_interface_addr,
  2067. force_freq, persistent_group);
  2068. }
  2069. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  2070. go_intent, own_interface_addr, force_freq,
  2071. persistent_group);
  2072. }
  2073. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  2074. const u8 *peer_addr,
  2075. enum p2p_wps_method wps_method,
  2076. int go_intent, const u8 *own_interface_addr,
  2077. unsigned int force_freq, int persistent_group)
  2078. {
  2079. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2080. persistent_group = 2;
  2081. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2082. return -1;
  2083. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  2084. go_intent, own_interface_addr, force_freq,
  2085. persistent_group);
  2086. }
  2087. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  2088. {
  2089. wpa_s->p2p_join_scan_count++;
  2090. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  2091. wpa_s->p2p_join_scan_count);
  2092. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  2093. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  2094. " for join operationg - stop join attempt",
  2095. MAC2STR(wpa_s->pending_join_iface_addr));
  2096. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2097. wpa_msg(wpa_s->parent, MSG_INFO,
  2098. P2P_EVENT_GROUP_FORMATION_FAILURE);
  2099. }
  2100. }
  2101. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  2102. struct wpa_scan_results *scan_res)
  2103. {
  2104. struct wpa_bss *bss;
  2105. int freq;
  2106. u8 iface_addr[ETH_ALEN];
  2107. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2108. if (wpa_s->global->p2p_disabled)
  2109. return;
  2110. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
  2111. scan_res ? (int) scan_res->num : -1);
  2112. if (scan_res)
  2113. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  2114. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  2115. wpa_s->pending_join_iface_addr);
  2116. if (freq < 0 &&
  2117. p2p_get_interface_addr(wpa_s->global->p2p,
  2118. wpa_s->pending_join_dev_addr,
  2119. iface_addr) == 0 &&
  2120. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  2121. {
  2122. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  2123. "address for join from " MACSTR " to " MACSTR
  2124. " based on newly discovered P2P peer entry",
  2125. MAC2STR(wpa_s->pending_join_iface_addr),
  2126. MAC2STR(iface_addr));
  2127. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  2128. ETH_ALEN);
  2129. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  2130. wpa_s->pending_join_iface_addr);
  2131. }
  2132. if (freq >= 0) {
  2133. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  2134. "from P2P peer table: %d MHz", freq);
  2135. }
  2136. bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
  2137. if (bss) {
  2138. freq = bss->freq;
  2139. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  2140. "from BSS table: %d MHz", freq);
  2141. }
  2142. if (freq > 0) {
  2143. u16 method;
  2144. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  2145. "prior to joining an existing group (GO " MACSTR
  2146. " freq=%u MHz)",
  2147. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  2148. wpa_s->pending_pd_before_join = 1;
  2149. switch (wpa_s->pending_join_wps_method) {
  2150. case WPS_PIN_DISPLAY:
  2151. method = WPS_CONFIG_KEYPAD;
  2152. break;
  2153. case WPS_PIN_KEYPAD:
  2154. method = WPS_CONFIG_DISPLAY;
  2155. break;
  2156. case WPS_PBC:
  2157. method = WPS_CONFIG_PUSHBUTTON;
  2158. break;
  2159. default:
  2160. method = 0;
  2161. break;
  2162. }
  2163. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  2164. wpa_s->pending_join_dev_addr) ==
  2165. method)) {
  2166. /*
  2167. * We have already performed provision discovery for
  2168. * joining the group. Proceed directly to join
  2169. * operation without duplicated provision discovery. */
  2170. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  2171. "with " MACSTR " already done - proceed to "
  2172. "join",
  2173. MAC2STR(wpa_s->pending_join_dev_addr));
  2174. wpa_s->pending_pd_before_join = 0;
  2175. goto start;
  2176. }
  2177. if (p2p_prov_disc_req(wpa_s->global->p2p,
  2178. wpa_s->pending_join_dev_addr, method, 1,
  2179. freq) < 0) {
  2180. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  2181. "Discovery Request before joining an "
  2182. "existing group");
  2183. wpa_s->pending_pd_before_join = 0;
  2184. goto start;
  2185. }
  2186. /*
  2187. * Actual join operation will be started from the Action frame
  2188. * TX status callback.
  2189. */
  2190. return;
  2191. }
  2192. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  2193. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2194. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  2195. wpas_p2p_check_join_scan_limit(wpa_s);
  2196. return;
  2197. start:
  2198. /* Start join operation immediately */
  2199. wpas_p2p_join_start(wpa_s);
  2200. }
  2201. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  2202. {
  2203. struct wpa_supplicant *wpa_s = eloop_ctx;
  2204. int ret;
  2205. struct wpa_driver_scan_params params;
  2206. struct wpabuf *wps_ie, *ies;
  2207. size_t ielen;
  2208. os_memset(&params, 0, sizeof(params));
  2209. /* P2P Wildcard SSID */
  2210. params.num_ssids = 1;
  2211. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  2212. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  2213. wpa_s->wps->dev.p2p = 1;
  2214. wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
  2215. WPS_REQ_ENROLLEE, 0, NULL);
  2216. if (wps_ie == NULL) {
  2217. wpas_p2p_scan_res_join(wpa_s, NULL);
  2218. return;
  2219. }
  2220. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  2221. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  2222. if (ies == NULL) {
  2223. wpabuf_free(wps_ie);
  2224. wpas_p2p_scan_res_join(wpa_s, NULL);
  2225. return;
  2226. }
  2227. wpabuf_put_buf(ies, wps_ie);
  2228. wpabuf_free(wps_ie);
  2229. p2p_scan_ie(wpa_s->global->p2p, ies);
  2230. params.p2p_probe = 1;
  2231. params.extra_ies = wpabuf_head(ies);
  2232. params.extra_ies_len = wpabuf_len(ies);
  2233. /*
  2234. * Run a scan to update BSS table and start Provision Discovery once
  2235. * the new scan results become available.
  2236. */
  2237. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  2238. ret = wpa_drv_scan(wpa_s, &params);
  2239. wpabuf_free(ies);
  2240. if (ret) {
  2241. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  2242. "try again later");
  2243. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2244. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  2245. wpas_p2p_check_join_scan_limit(wpa_s);
  2246. }
  2247. }
  2248. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  2249. const u8 *dev_addr, enum p2p_wps_method wps_method)
  2250. {
  2251. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  2252. MACSTR " dev " MACSTR ")",
  2253. MAC2STR(iface_addr), MAC2STR(dev_addr));
  2254. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  2255. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  2256. wpa_s->pending_join_wps_method = wps_method;
  2257. /* Make sure we are not running find during connection establishment */
  2258. wpas_p2p_stop_find(wpa_s);
  2259. wpa_s->p2p_join_scan_count = 0;
  2260. wpas_p2p_join_scan(wpa_s, NULL);
  2261. return 0;
  2262. }
  2263. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  2264. {
  2265. struct wpa_supplicant *group;
  2266. struct p2p_go_neg_results res;
  2267. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  2268. if (group == NULL)
  2269. return -1;
  2270. if (group != wpa_s) {
  2271. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  2272. sizeof(group->p2p_pin));
  2273. group->p2p_wps_method = wpa_s->p2p_wps_method;
  2274. }
  2275. group->p2p_in_provisioning = 1;
  2276. os_memset(&res, 0, sizeof(res));
  2277. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  2278. ETH_ALEN);
  2279. res.wps_method = wpa_s->pending_join_wps_method;
  2280. wpas_start_wps_enrollee(group, &res);
  2281. /*
  2282. * Allow a longer timeout for join-a-running-group than normal 15
  2283. * second group formation timeout since the GO may not have authorized
  2284. * our connection yet.
  2285. */
  2286. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2287. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  2288. wpa_s, NULL);
  2289. return 0;
  2290. }
  2291. /**
  2292. * wpas_p2p_connect - Request P2P Group Formation to be started
  2293. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2294. * @peer_addr: Address of the peer P2P Device
  2295. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  2296. * @persistent_group: Whether to create a persistent group
  2297. * @join: Whether to join an existing group (as a client) instead of starting
  2298. * Group Owner negotiation; @peer_addr is BSSID in that case
  2299. * @auth: Whether to only authorize the connection instead of doing that and
  2300. * initiating Group Owner negotiation
  2301. * @go_intent: GO Intent or -1 to use default
  2302. * @freq: Frequency for the group or 0 for auto-selection
  2303. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  2304. * failure, -2 on failure due to channel not currently available,
  2305. * -3 if forced channel is not supported
  2306. */
  2307. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2308. const char *pin, enum p2p_wps_method wps_method,
  2309. int persistent_group, int join, int auth, int go_intent,
  2310. int freq)
  2311. {
  2312. int force_freq = 0, oper_freq = 0;
  2313. u8 bssid[ETH_ALEN];
  2314. int ret = 0;
  2315. enum wpa_driver_if_type iftype;
  2316. const u8 *if_addr;
  2317. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2318. return -1;
  2319. if (go_intent < 0)
  2320. go_intent = wpa_s->conf->p2p_go_intent;
  2321. if (!auth)
  2322. wpa_s->p2p_long_listen = 0;
  2323. wpa_s->p2p_wps_method = wps_method;
  2324. if (pin)
  2325. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  2326. else if (wps_method == WPS_PIN_DISPLAY) {
  2327. ret = wps_generate_pin();
  2328. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  2329. ret);
  2330. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  2331. wpa_s->p2p_pin);
  2332. } else
  2333. wpa_s->p2p_pin[0] = '\0';
  2334. if (join) {
  2335. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  2336. if (auth) {
  2337. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  2338. "connect a running group from " MACSTR,
  2339. MAC2STR(peer_addr));
  2340. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  2341. return ret;
  2342. }
  2343. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  2344. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  2345. iface_addr) < 0) {
  2346. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  2347. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  2348. dev_addr);
  2349. }
  2350. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
  2351. 0)
  2352. return -1;
  2353. return ret;
  2354. }
  2355. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  2356. wpa_s->assoc_freq)
  2357. oper_freq = wpa_s->assoc_freq;
  2358. else {
  2359. oper_freq = wpa_drv_shared_freq(wpa_s);
  2360. if (oper_freq < 0)
  2361. oper_freq = 0;
  2362. }
  2363. if (freq > 0) {
  2364. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2365. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  2366. "(%u MHz) is not supported for P2P uses",
  2367. freq);
  2368. return -3;
  2369. }
  2370. if (oper_freq > 0 && freq != oper_freq &&
  2371. !(wpa_s->drv_flags &
  2372. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  2373. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
  2374. "on %u MHz while connected on another "
  2375. "channel (%u MHz)", freq, oper_freq);
  2376. return -2;
  2377. }
  2378. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  2379. "requested channel (%u MHz)", freq);
  2380. force_freq = freq;
  2381. } else if (oper_freq > 0 &&
  2382. !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
  2383. if (!(wpa_s->drv_flags &
  2384. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  2385. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
  2386. "while connected on non-P2P supported "
  2387. "channel (%u MHz)", oper_freq);
  2388. return -2;
  2389. }
  2390. wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
  2391. "(%u MHz) not available for P2P - try to use "
  2392. "another channel", oper_freq);
  2393. force_freq = 0;
  2394. } else if (oper_freq > 0) {
  2395. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  2396. "channel we are already using (%u MHz) on another "
  2397. "interface", oper_freq);
  2398. force_freq = oper_freq;
  2399. }
  2400. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  2401. if (wpa_s->create_p2p_iface) {
  2402. /* Prepare to add a new interface for the group */
  2403. iftype = WPA_IF_P2P_GROUP;
  2404. if (go_intent == 15)
  2405. iftype = WPA_IF_P2P_GO;
  2406. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  2407. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2408. "interface for the group");
  2409. return -1;
  2410. }
  2411. if_addr = wpa_s->pending_interface_addr;
  2412. } else
  2413. if_addr = wpa_s->own_addr;
  2414. if (auth) {
  2415. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  2416. go_intent, if_addr,
  2417. force_freq, persistent_group) < 0)
  2418. return -1;
  2419. return ret;
  2420. }
  2421. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  2422. go_intent, if_addr, force_freq,
  2423. persistent_group) < 0) {
  2424. if (wpa_s->create_p2p_iface)
  2425. wpas_p2p_remove_pending_group_interface(wpa_s);
  2426. return -1;
  2427. }
  2428. return ret;
  2429. }
  2430. /**
  2431. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  2432. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2433. * @freq: Frequency of the channel in MHz
  2434. * @duration: Duration of the stay on the channel in milliseconds
  2435. *
  2436. * This callback is called when the driver indicates that it has started the
  2437. * requested remain-on-channel duration.
  2438. */
  2439. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  2440. unsigned int freq, unsigned int duration)
  2441. {
  2442. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2443. return;
  2444. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  2445. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  2446. wpa_s->pending_listen_duration);
  2447. wpa_s->pending_listen_freq = 0;
  2448. }
  2449. }
  2450. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  2451. unsigned int timeout)
  2452. {
  2453. /* Limit maximum Listen state time based on driver limitation. */
  2454. if (timeout > wpa_s->max_remain_on_chan)
  2455. timeout = wpa_s->max_remain_on_chan;
  2456. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2457. return wpa_drv_p2p_listen(wpa_s, timeout);
  2458. return p2p_listen(wpa_s->global->p2p, timeout);
  2459. }
  2460. /**
  2461. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  2462. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2463. * @freq: Frequency of the channel in MHz
  2464. *
  2465. * This callback is called when the driver indicates that a remain-on-channel
  2466. * operation has been completed, i.e., the duration on the requested channel
  2467. * has timed out.
  2468. */
  2469. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  2470. unsigned int freq)
  2471. {
  2472. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  2473. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  2474. wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
  2475. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2476. return;
  2477. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  2478. return; /* P2P module started a new operation */
  2479. if (wpa_s->pending_action_tx)
  2480. return;
  2481. if (wpa_s->p2p_long_listen > 0)
  2482. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  2483. if (wpa_s->p2p_long_listen > 0) {
  2484. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  2485. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  2486. }
  2487. }
  2488. /**
  2489. * wpas_p2p_group_remove - Remove a P2P group
  2490. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2491. * @ifname: Network interface name of the group interface or "*" to remove all
  2492. * groups
  2493. * Returns: 0 on success, -1 on failure
  2494. *
  2495. * This function is used to remove a P2P group. This can be used to disconnect
  2496. * from a group in which the local end is a P2P Client or to end a P2P Group in
  2497. * case the local end is the Group Owner. If a virtual network interface was
  2498. * created for this group, that interface will be removed. Otherwise, only the
  2499. * configured P2P group network will be removed from the interface.
  2500. */
  2501. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  2502. {
  2503. struct wpa_global *global = wpa_s->global;
  2504. if (os_strcmp(ifname, "*") == 0) {
  2505. struct wpa_supplicant *prev;
  2506. wpa_s = global->ifaces;
  2507. while (wpa_s) {
  2508. prev = wpa_s;
  2509. wpa_s = wpa_s->next;
  2510. wpas_p2p_disconnect(prev);
  2511. }
  2512. return 0;
  2513. }
  2514. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2515. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2516. break;
  2517. }
  2518. return wpas_p2p_disconnect(wpa_s);
  2519. }
  2520. static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  2521. struct p2p_go_neg_results *params,
  2522. int freq)
  2523. {
  2524. u8 bssid[ETH_ALEN];
  2525. int res;
  2526. os_memset(params, 0, sizeof(*params));
  2527. params->role_go = 1;
  2528. if (freq) {
  2529. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  2530. "frequency %d MHz", freq);
  2531. params->freq = freq;
  2532. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  2533. wpa_s->conf->p2p_oper_channel >= 1 &&
  2534. wpa_s->conf->p2p_oper_channel <= 11) {
  2535. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  2536. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  2537. "frequency %d MHz", params->freq);
  2538. } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
  2539. wpa_s->conf->p2p_oper_reg_class == 124) {
  2540. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  2541. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  2542. "frequency %d MHz", params->freq);
  2543. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2544. wpa_s->best_overall_freq > 0 &&
  2545. p2p_supported_freq(wpa_s->global->p2p,
  2546. wpa_s->best_overall_freq)) {
  2547. params->freq = wpa_s->best_overall_freq;
  2548. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  2549. "channel %d MHz", params->freq);
  2550. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2551. wpa_s->best_24_freq > 0 &&
  2552. p2p_supported_freq(wpa_s->global->p2p,
  2553. wpa_s->best_24_freq)) {
  2554. params->freq = wpa_s->best_24_freq;
  2555. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  2556. "channel %d MHz", params->freq);
  2557. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2558. wpa_s->best_5_freq > 0 &&
  2559. p2p_supported_freq(wpa_s->global->p2p,
  2560. wpa_s->best_5_freq)) {
  2561. params->freq = wpa_s->best_5_freq;
  2562. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  2563. "channel %d MHz", params->freq);
  2564. } else {
  2565. params->freq = 2412;
  2566. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  2567. "known)", params->freq);
  2568. }
  2569. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  2570. wpa_s->assoc_freq && !freq) {
  2571. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  2572. "already using");
  2573. params->freq = wpa_s->assoc_freq;
  2574. }
  2575. res = wpa_drv_shared_freq(wpa_s);
  2576. if (res > 0 && !freq) {
  2577. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  2578. "already using on a shared interface");
  2579. params->freq = res;
  2580. }
  2581. }
  2582. static struct wpa_supplicant *
  2583. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  2584. int go)
  2585. {
  2586. struct wpa_supplicant *group_wpa_s;
  2587. if (!wpas_p2p_create_iface(wpa_s))
  2588. return wpa_s;
  2589. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  2590. WPA_IF_P2P_CLIENT) < 0)
  2591. return NULL;
  2592. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  2593. if (group_wpa_s == NULL) {
  2594. wpas_p2p_remove_pending_group_interface(wpa_s);
  2595. return NULL;
  2596. }
  2597. return group_wpa_s;
  2598. }
  2599. /**
  2600. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  2601. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2602. * @persistent_group: Whether to create a persistent group
  2603. * @freq: Frequency for the group or 0 to indicate no hardcoding
  2604. * Returns: 0 on success, -1 on failure
  2605. *
  2606. * This function creates a new P2P group with the local end as the Group Owner,
  2607. * i.e., without using Group Owner Negotiation.
  2608. */
  2609. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  2610. int freq)
  2611. {
  2612. struct p2p_go_neg_results params;
  2613. unsigned int r;
  2614. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2615. return -1;
  2616. /* Make sure we are not running find during connection establishment */
  2617. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  2618. wpas_p2p_stop_find(wpa_s);
  2619. if (freq == 2) {
  2620. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  2621. "band");
  2622. if (wpa_s->best_24_freq > 0 &&
  2623. p2p_supported_freq(wpa_s->global->p2p,
  2624. wpa_s->best_24_freq)) {
  2625. freq = wpa_s->best_24_freq;
  2626. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  2627. "channel: %d MHz", freq);
  2628. } else {
  2629. os_get_random((u8 *) &r, sizeof(r));
  2630. freq = 2412 + (r % 3) * 25;
  2631. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  2632. "channel: %d MHz", freq);
  2633. }
  2634. }
  2635. if (freq == 5) {
  2636. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  2637. "band");
  2638. if (wpa_s->best_5_freq > 0 &&
  2639. p2p_supported_freq(wpa_s->global->p2p,
  2640. wpa_s->best_5_freq)) {
  2641. freq = wpa_s->best_5_freq;
  2642. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  2643. "channel: %d MHz", freq);
  2644. } else {
  2645. os_get_random((u8 *) &r, sizeof(r));
  2646. freq = 5180 + (r % 4) * 20;
  2647. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2648. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  2649. "5 GHz channel for P2P group");
  2650. return -1;
  2651. }
  2652. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  2653. "channel: %d MHz", freq);
  2654. }
  2655. }
  2656. if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2657. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  2658. "(%u MHz) is not supported for P2P uses",
  2659. freq);
  2660. return -1;
  2661. }
  2662. wpas_p2p_init_go_params(wpa_s, &params, freq);
  2663. p2p_go_params(wpa_s->global->p2p, &params);
  2664. params.persistent_group = persistent_group;
  2665. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  2666. if (wpa_s == NULL)
  2667. return -1;
  2668. wpas_start_wps_go(wpa_s, &params, 0);
  2669. return 0;
  2670. }
  2671. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  2672. struct wpa_ssid *params, int addr_allocated)
  2673. {
  2674. struct wpa_ssid *ssid;
  2675. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  2676. if (wpa_s == NULL)
  2677. return -1;
  2678. wpa_supplicant_ap_deinit(wpa_s);
  2679. ssid = wpa_config_add_network(wpa_s->conf);
  2680. if (ssid == NULL)
  2681. return -1;
  2682. wpa_config_set_network_defaults(ssid);
  2683. ssid->temporary = 1;
  2684. ssid->proto = WPA_PROTO_RSN;
  2685. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  2686. ssid->group_cipher = WPA_CIPHER_CCMP;
  2687. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  2688. ssid->ssid = os_malloc(params->ssid_len);
  2689. if (ssid->ssid == NULL) {
  2690. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2691. return -1;
  2692. }
  2693. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  2694. ssid->ssid_len = params->ssid_len;
  2695. ssid->p2p_group = 1;
  2696. ssid->export_keys = 1;
  2697. if (params->psk_set) {
  2698. os_memcpy(ssid->psk, params->psk, 32);
  2699. ssid->psk_set = 1;
  2700. }
  2701. if (params->passphrase)
  2702. ssid->passphrase = os_strdup(params->passphrase);
  2703. wpa_supplicant_select_network(wpa_s, ssid);
  2704. wpa_s->show_group_started = 1;
  2705. return 0;
  2706. }
  2707. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  2708. struct wpa_ssid *ssid, int addr_allocated,
  2709. int freq)
  2710. {
  2711. struct p2p_go_neg_results params;
  2712. int go = 0;
  2713. if (ssid->disabled != 2 || ssid->ssid == NULL)
  2714. return -1;
  2715. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  2716. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  2717. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  2718. "already running");
  2719. return 0;
  2720. }
  2721. /* Make sure we are not running find during connection establishment */
  2722. wpas_p2p_stop_find(wpa_s);
  2723. if (ssid->mode == WPAS_MODE_INFRA)
  2724. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  2725. if (ssid->mode != WPAS_MODE_P2P_GO)
  2726. return -1;
  2727. wpas_p2p_init_go_params(wpa_s, &params, freq);
  2728. params.role_go = 1;
  2729. if (ssid->passphrase == NULL ||
  2730. os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  2731. wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
  2732. "group");
  2733. return -1;
  2734. }
  2735. os_strlcpy(params.passphrase, ssid->passphrase,
  2736. sizeof(params.passphrase));
  2737. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  2738. params.ssid_len = ssid->ssid_len;
  2739. params.persistent_group = 1;
  2740. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  2741. if (wpa_s == NULL)
  2742. return -1;
  2743. wpas_start_wps_go(wpa_s, &params, 0);
  2744. return 0;
  2745. }
  2746. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  2747. struct wpabuf *proberesp_ies)
  2748. {
  2749. struct wpa_supplicant *wpa_s = ctx;
  2750. if (wpa_s->ap_iface) {
  2751. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  2752. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  2753. wpabuf_free(beacon_ies);
  2754. wpabuf_free(proberesp_ies);
  2755. return;
  2756. }
  2757. if (beacon_ies) {
  2758. wpabuf_free(hapd->p2p_beacon_ie);
  2759. hapd->p2p_beacon_ie = beacon_ies;
  2760. }
  2761. wpabuf_free(hapd->p2p_probe_resp_ie);
  2762. hapd->p2p_probe_resp_ie = proberesp_ies;
  2763. } else {
  2764. wpabuf_free(beacon_ies);
  2765. wpabuf_free(proberesp_ies);
  2766. }
  2767. wpa_supplicant_ap_update_beacon(wpa_s);
  2768. }
  2769. static void wpas_p2p_idle_update(void *ctx, int idle)
  2770. {
  2771. struct wpa_supplicant *wpa_s = ctx;
  2772. if (!wpa_s->ap_iface)
  2773. return;
  2774. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  2775. if (idle)
  2776. wpas_p2p_set_group_idle_timeout(wpa_s);
  2777. else
  2778. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2779. }
  2780. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  2781. int persistent_group,
  2782. int group_formation)
  2783. {
  2784. struct p2p_group *group;
  2785. struct p2p_group_config *cfg;
  2786. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2787. return NULL;
  2788. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2789. return NULL;
  2790. cfg = os_zalloc(sizeof(*cfg));
  2791. if (cfg == NULL)
  2792. return NULL;
  2793. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2794. cfg->persistent_group = 2;
  2795. else if (persistent_group)
  2796. cfg->persistent_group = 1;
  2797. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  2798. if (wpa_s->max_stations &&
  2799. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  2800. cfg->max_clients = wpa_s->max_stations;
  2801. else
  2802. cfg->max_clients = wpa_s->conf->max_num_sta;
  2803. cfg->cb_ctx = wpa_s;
  2804. cfg->ie_update = wpas_p2p_ie_update;
  2805. cfg->idle_update = wpas_p2p_idle_update;
  2806. group = p2p_group_init(wpa_s->global->p2p, cfg);
  2807. if (group == NULL)
  2808. os_free(cfg);
  2809. if (!group_formation)
  2810. p2p_group_notif_formation_done(group);
  2811. wpa_s->p2p_group = group;
  2812. return group;
  2813. }
  2814. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2815. int registrar)
  2816. {
  2817. if (!wpa_s->p2p_in_provisioning) {
  2818. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  2819. "provisioning not in progress");
  2820. return;
  2821. }
  2822. /* Clear any stored provisioning info */
  2823. p2p_clear_provisioning_info(wpa_s->global->p2p, peer_addr);
  2824. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  2825. NULL);
  2826. if (wpa_s->global->p2p)
  2827. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  2828. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2829. wpa_drv_wps_success_cb(wpa_s, peer_addr);
  2830. wpas_group_formation_completed(wpa_s, 1);
  2831. }
  2832. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  2833. struct wps_event_fail *fail)
  2834. {
  2835. if (!wpa_s->p2p_in_provisioning) {
  2836. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  2837. "provisioning not in progress");
  2838. return;
  2839. }
  2840. if (wpa_s->go_params) {
  2841. p2p_clear_provisioning_info(
  2842. wpa_s->global->p2p,
  2843. wpa_s->go_params->peer_interface_addr);
  2844. }
  2845. wpas_notify_p2p_wps_failed(wpa_s, fail);
  2846. }
  2847. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2848. const char *config_method, int join)
  2849. {
  2850. u16 config_methods;
  2851. if (os_strncmp(config_method, "display", 7) == 0)
  2852. config_methods = WPS_CONFIG_DISPLAY;
  2853. else if (os_strncmp(config_method, "keypad", 6) == 0)
  2854. config_methods = WPS_CONFIG_KEYPAD;
  2855. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  2856. os_strncmp(config_method, "pushbutton", 10) == 0)
  2857. config_methods = WPS_CONFIG_PUSHBUTTON;
  2858. else {
  2859. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  2860. return -1;
  2861. }
  2862. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2863. return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
  2864. config_methods, join);
  2865. }
  2866. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  2867. return -1;
  2868. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  2869. config_methods, join, 0);
  2870. }
  2871. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  2872. char *end)
  2873. {
  2874. return p2p_scan_result_text(ies, ies_len, buf, end);
  2875. }
  2876. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  2877. {
  2878. if (!wpa_s->pending_action_tx)
  2879. return;
  2880. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  2881. "operation request");
  2882. wpabuf_free(wpa_s->pending_action_tx);
  2883. wpa_s->pending_action_tx = NULL;
  2884. }
  2885. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  2886. enum p2p_discovery_type type,
  2887. unsigned int num_req_dev_types, const u8 *req_dev_types)
  2888. {
  2889. wpas_p2p_clear_pending_action_tx(wpa_s);
  2890. wpa_s->p2p_long_listen = 0;
  2891. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2892. return wpa_drv_p2p_find(wpa_s, timeout, type);
  2893. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2894. return -1;
  2895. return p2p_find(wpa_s->global->p2p, timeout, type,
  2896. num_req_dev_types, req_dev_types);
  2897. }
  2898. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  2899. {
  2900. wpas_p2p_clear_pending_action_tx(wpa_s);
  2901. wpa_s->p2p_long_listen = 0;
  2902. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2903. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2904. wpa_s->p2p_cb_on_scan_complete = 0;
  2905. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2906. wpa_drv_p2p_stop_find(wpa_s);
  2907. return;
  2908. }
  2909. if (wpa_s->global->p2p)
  2910. p2p_stop_find(wpa_s->global->p2p);
  2911. wpas_p2p_remove_pending_group_interface(wpa_s);
  2912. }
  2913. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  2914. {
  2915. struct wpa_supplicant *wpa_s = eloop_ctx;
  2916. wpa_s->p2p_long_listen = 0;
  2917. }
  2918. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  2919. {
  2920. int res;
  2921. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2922. return -1;
  2923. wpas_p2p_clear_pending_action_tx(wpa_s);
  2924. if (timeout == 0) {
  2925. /*
  2926. * This is a request for unlimited Listen state. However, at
  2927. * least for now, this is mapped to a Listen state for one
  2928. * hour.
  2929. */
  2930. timeout = 3600;
  2931. }
  2932. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2933. wpa_s->p2p_long_listen = 0;
  2934. /*
  2935. * Stop previous find/listen operation to avoid trying to request a new
  2936. * remain-on-channel operation while the driver is still running the
  2937. * previous one.
  2938. */
  2939. if (wpa_s->global->p2p)
  2940. p2p_stop_find(wpa_s->global->p2p);
  2941. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  2942. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  2943. wpa_s->p2p_long_listen = timeout * 1000;
  2944. eloop_register_timeout(timeout, 0,
  2945. wpas_p2p_long_listen_timeout,
  2946. wpa_s, NULL);
  2947. }
  2948. return res;
  2949. }
  2950. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2951. u8 *buf, size_t len, int p2p_group)
  2952. {
  2953. struct wpabuf *p2p_ie;
  2954. int ret;
  2955. if (wpa_s->global->p2p_disabled)
  2956. return -1;
  2957. if (wpa_s->global->p2p == NULL)
  2958. return -1;
  2959. if (bss == NULL)
  2960. return -1;
  2961. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  2962. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  2963. p2p_group, p2p_ie);
  2964. wpabuf_free(p2p_ie);
  2965. return ret;
  2966. }
  2967. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  2968. const u8 *dst, const u8 *bssid,
  2969. const u8 *ie, size_t ie_len)
  2970. {
  2971. if (wpa_s->global->p2p_disabled)
  2972. return 0;
  2973. if (wpa_s->global->p2p == NULL)
  2974. return 0;
  2975. return p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  2976. ie, ie_len);
  2977. }
  2978. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  2979. const u8 *sa, const u8 *bssid,
  2980. u8 category, const u8 *data, size_t len, int freq)
  2981. {
  2982. if (wpa_s->global->p2p_disabled)
  2983. return;
  2984. if (wpa_s->global->p2p == NULL)
  2985. return;
  2986. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  2987. freq);
  2988. }
  2989. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  2990. {
  2991. if (wpa_s->global->p2p_disabled)
  2992. return;
  2993. if (wpa_s->global->p2p == NULL)
  2994. return;
  2995. p2p_scan_ie(wpa_s->global->p2p, ies);
  2996. }
  2997. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  2998. {
  2999. p2p_group_deinit(wpa_s->p2p_group);
  3000. wpa_s->p2p_group = NULL;
  3001. wpa_s->ap_configured_cb = NULL;
  3002. wpa_s->ap_configured_cb_ctx = NULL;
  3003. wpa_s->ap_configured_cb_data = NULL;
  3004. wpa_s->connect_without_scan = NULL;
  3005. }
  3006. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  3007. {
  3008. wpa_s->p2p_long_listen = 0;
  3009. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3010. return wpa_drv_p2p_reject(wpa_s, addr);
  3011. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3012. return -1;
  3013. return p2p_reject(wpa_s->global->p2p, addr);
  3014. }
  3015. /* Invite to reinvoke a persistent group */
  3016. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3017. struct wpa_ssid *ssid, const u8 *go_dev_addr)
  3018. {
  3019. enum p2p_invite_role role;
  3020. u8 *bssid = NULL;
  3021. if (ssid->mode == WPAS_MODE_P2P_GO) {
  3022. role = P2P_INVITE_ROLE_GO;
  3023. if (peer_addr == NULL) {
  3024. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  3025. "address in invitation command");
  3026. return -1;
  3027. }
  3028. if (wpas_p2p_create_iface(wpa_s)) {
  3029. if (wpas_p2p_add_group_interface(wpa_s,
  3030. WPA_IF_P2P_GO) < 0) {
  3031. wpa_printf(MSG_ERROR, "P2P: Failed to "
  3032. "allocate a new interface for the "
  3033. "group");
  3034. return -1;
  3035. }
  3036. bssid = wpa_s->pending_interface_addr;
  3037. } else
  3038. bssid = wpa_s->own_addr;
  3039. } else {
  3040. role = P2P_INVITE_ROLE_CLIENT;
  3041. peer_addr = ssid->bssid;
  3042. }
  3043. wpa_s->pending_invite_ssid_id = ssid->id;
  3044. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3045. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  3046. ssid->ssid, ssid->ssid_len,
  3047. go_dev_addr, 1);
  3048. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3049. return -1;
  3050. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  3051. ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
  3052. }
  3053. /* Invite to join an active group */
  3054. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  3055. const u8 *peer_addr, const u8 *go_dev_addr)
  3056. {
  3057. struct wpa_global *global = wpa_s->global;
  3058. enum p2p_invite_role role;
  3059. u8 *bssid = NULL;
  3060. struct wpa_ssid *ssid;
  3061. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3062. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3063. break;
  3064. }
  3065. if (wpa_s == NULL) {
  3066. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  3067. return -1;
  3068. }
  3069. ssid = wpa_s->current_ssid;
  3070. if (ssid == NULL) {
  3071. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  3072. "invitation");
  3073. return -1;
  3074. }
  3075. if (ssid->mode == WPAS_MODE_P2P_GO) {
  3076. role = P2P_INVITE_ROLE_ACTIVE_GO;
  3077. bssid = wpa_s->own_addr;
  3078. if (go_dev_addr == NULL)
  3079. go_dev_addr = wpa_s->parent->own_addr;
  3080. } else {
  3081. role = P2P_INVITE_ROLE_CLIENT;
  3082. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  3083. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  3084. "invite to current group");
  3085. return -1;
  3086. }
  3087. bssid = wpa_s->bssid;
  3088. if (go_dev_addr == NULL &&
  3089. !is_zero_ether_addr(wpa_s->go_dev_addr))
  3090. go_dev_addr = wpa_s->go_dev_addr;
  3091. }
  3092. wpa_s->parent->pending_invite_ssid_id = -1;
  3093. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3094. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  3095. ssid->ssid, ssid->ssid_len,
  3096. go_dev_addr, 0);
  3097. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3098. return -1;
  3099. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  3100. ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
  3101. go_dev_addr, 0);
  3102. }
  3103. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  3104. {
  3105. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3106. const char *ssid_txt;
  3107. u8 go_dev_addr[ETH_ALEN];
  3108. int network_id = -1;
  3109. int persistent;
  3110. int freq;
  3111. if (!wpa_s->show_group_started || !ssid)
  3112. return;
  3113. wpa_s->show_group_started = 0;
  3114. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  3115. os_memset(go_dev_addr, 0, ETH_ALEN);
  3116. if (ssid->bssid_set)
  3117. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  3118. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  3119. ssid->ssid_len);
  3120. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  3121. if (wpa_s->global->p2p_group_formation == wpa_s)
  3122. wpa_s->global->p2p_group_formation = NULL;
  3123. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  3124. (int) wpa_s->assoc_freq;
  3125. if (ssid->passphrase == NULL && ssid->psk_set) {
  3126. char psk[65];
  3127. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  3128. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  3129. "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  3130. MACSTR "%s",
  3131. wpa_s->ifname, ssid_txt, freq, psk,
  3132. MAC2STR(go_dev_addr),
  3133. persistent ? " [PERSISTENT]" : "");
  3134. } else {
  3135. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  3136. "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
  3137. "go_dev_addr=" MACSTR "%s",
  3138. wpa_s->ifname, ssid_txt, freq,
  3139. ssid->passphrase ? ssid->passphrase : "",
  3140. MAC2STR(go_dev_addr),
  3141. persistent ? " [PERSISTENT]" : "");
  3142. }
  3143. if (persistent)
  3144. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  3145. ssid, go_dev_addr);
  3146. if (network_id < 0)
  3147. network_id = ssid->id;
  3148. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  3149. }
  3150. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  3151. u32 interval1, u32 duration2, u32 interval2)
  3152. {
  3153. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3154. return -1;
  3155. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3156. return -1;
  3157. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  3158. wpa_s->current_ssid == NULL ||
  3159. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  3160. return -1;
  3161. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  3162. wpa_s->own_addr, wpa_s->assoc_freq,
  3163. duration1, interval1, duration2, interval2);
  3164. }
  3165. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  3166. unsigned int interval)
  3167. {
  3168. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3169. return -1;
  3170. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3171. return -1;
  3172. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  3173. }
  3174. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  3175. {
  3176. return wpa_s->current_ssid != NULL &&
  3177. wpa_s->current_ssid->p2p_group &&
  3178. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  3179. }
  3180. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  3181. {
  3182. struct wpa_supplicant *wpa_s = eloop_ctx;
  3183. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  3184. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  3185. "disabled");
  3186. return;
  3187. }
  3188. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  3189. "group");
  3190. wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
  3191. wpas_p2p_group_delete(wpa_s);
  3192. }
  3193. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  3194. {
  3195. unsigned int timeout;
  3196. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3197. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  3198. return;
  3199. timeout = wpa_s->conf->p2p_group_idle;
  3200. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  3201. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  3202. timeout = P2P_MAX_CLIENT_IDLE;
  3203. if (timeout == 0)
  3204. return;
  3205. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  3206. timeout);
  3207. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  3208. wpa_s, NULL);
  3209. }
  3210. void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  3211. u16 reason_code, const u8 *ie, size_t ie_len)
  3212. {
  3213. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3214. return;
  3215. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3216. return;
  3217. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
  3218. }
  3219. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  3220. u16 reason_code, const u8 *ie, size_t ie_len)
  3221. {
  3222. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3223. return;
  3224. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3225. return;
  3226. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
  3227. }
  3228. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  3229. {
  3230. struct p2p_data *p2p = wpa_s->global->p2p;
  3231. if (p2p == NULL)
  3232. return;
  3233. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  3234. return;
  3235. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  3236. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  3237. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  3238. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  3239. if (wpa_s->wps &&
  3240. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  3241. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  3242. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  3243. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  3244. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  3245. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  3246. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  3247. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  3248. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  3249. }
  3250. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  3251. p2p_set_sec_dev_types(p2p,
  3252. (void *) wpa_s->conf->sec_device_type,
  3253. wpa_s->conf->num_sec_device_types);
  3254. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  3255. int i;
  3256. p2p_remove_wps_vendor_extensions(p2p);
  3257. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  3258. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  3259. continue;
  3260. p2p_add_wps_vendor_extension(
  3261. p2p, wpa_s->conf->wps_vendor_ext[i]);
  3262. }
  3263. }
  3264. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  3265. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3266. char country[3];
  3267. country[0] = wpa_s->conf->country[0];
  3268. country[1] = wpa_s->conf->country[1];
  3269. country[2] = 0x04;
  3270. p2p_set_country(p2p, country);
  3271. }
  3272. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  3273. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  3274. wpa_s->conf->p2p_ssid_postfix ?
  3275. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  3276. 0);
  3277. }
  3278. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  3279. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  3280. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  3281. u8 reg_class, channel;
  3282. int ret;
  3283. unsigned int r;
  3284. if (wpa_s->conf->p2p_listen_reg_class &&
  3285. wpa_s->conf->p2p_listen_channel) {
  3286. reg_class = wpa_s->conf->p2p_listen_reg_class;
  3287. channel = wpa_s->conf->p2p_listen_channel;
  3288. } else {
  3289. reg_class = 81;
  3290. /*
  3291. * Pick one of the social channels randomly as the
  3292. * listen channel.
  3293. */
  3294. os_get_random((u8 *) &r, sizeof(r));
  3295. channel = 1 + (r % 3) * 5;
  3296. }
  3297. ret = p2p_set_listen_channel(p2p, reg_class, channel);
  3298. if (ret)
  3299. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  3300. "failed: %d", ret);
  3301. }
  3302. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  3303. u8 op_reg_class, op_channel, cfg_op_channel;
  3304. int ret = 0;
  3305. unsigned int r;
  3306. if (wpa_s->conf->p2p_oper_reg_class &&
  3307. wpa_s->conf->p2p_oper_channel) {
  3308. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  3309. op_channel = wpa_s->conf->p2p_oper_channel;
  3310. cfg_op_channel = 1;
  3311. } else {
  3312. op_reg_class = 81;
  3313. /*
  3314. * Use random operation channel from (1, 6, 11)
  3315. *if no other preference is indicated.
  3316. */
  3317. os_get_random((u8 *) &r, sizeof(r));
  3318. op_channel = 1 + (r % 3) * 5;
  3319. cfg_op_channel = 0;
  3320. }
  3321. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  3322. cfg_op_channel);
  3323. if (ret)
  3324. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  3325. "failed: %d", ret);
  3326. }
  3327. }
  3328. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  3329. int duration)
  3330. {
  3331. if (!wpa_s->ap_iface)
  3332. return -1;
  3333. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  3334. duration);
  3335. }
  3336. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  3337. {
  3338. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3339. return -1;
  3340. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3341. return -1;
  3342. wpa_s->global->cross_connection = enabled;
  3343. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  3344. if (!enabled) {
  3345. struct wpa_supplicant *iface;
  3346. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  3347. {
  3348. if (iface->cross_connect_enabled == 0)
  3349. continue;
  3350. iface->cross_connect_enabled = 0;
  3351. iface->cross_connect_in_use = 0;
  3352. wpa_msg(iface->parent, MSG_INFO,
  3353. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  3354. iface->ifname, iface->cross_connect_uplink);
  3355. }
  3356. }
  3357. return 0;
  3358. }
  3359. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  3360. {
  3361. struct wpa_supplicant *iface;
  3362. if (!uplink->global->cross_connection)
  3363. return;
  3364. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  3365. if (!iface->cross_connect_enabled)
  3366. continue;
  3367. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  3368. 0)
  3369. continue;
  3370. if (iface->ap_iface == NULL)
  3371. continue;
  3372. if (iface->cross_connect_in_use)
  3373. continue;
  3374. iface->cross_connect_in_use = 1;
  3375. wpa_msg(iface->parent, MSG_INFO,
  3376. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  3377. iface->ifname, iface->cross_connect_uplink);
  3378. }
  3379. }
  3380. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  3381. {
  3382. struct wpa_supplicant *iface;
  3383. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  3384. if (!iface->cross_connect_enabled)
  3385. continue;
  3386. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  3387. 0)
  3388. continue;
  3389. if (!iface->cross_connect_in_use)
  3390. continue;
  3391. wpa_msg(iface->parent, MSG_INFO,
  3392. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  3393. iface->ifname, iface->cross_connect_uplink);
  3394. iface->cross_connect_in_use = 0;
  3395. }
  3396. }
  3397. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  3398. {
  3399. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  3400. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  3401. wpa_s->cross_connect_disallowed)
  3402. wpas_p2p_disable_cross_connect(wpa_s);
  3403. else
  3404. wpas_p2p_enable_cross_connect(wpa_s);
  3405. if (!wpa_s->ap_iface)
  3406. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3407. }
  3408. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  3409. {
  3410. wpas_p2p_disable_cross_connect(wpa_s);
  3411. if (!wpa_s->ap_iface &&
  3412. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  3413. wpa_s, NULL))
  3414. wpas_p2p_set_group_idle_timeout(wpa_s);
  3415. }
  3416. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  3417. {
  3418. struct wpa_supplicant *iface;
  3419. if (!wpa_s->global->cross_connection)
  3420. return;
  3421. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  3422. if (iface == wpa_s)
  3423. continue;
  3424. if (iface->drv_flags &
  3425. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  3426. continue;
  3427. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  3428. continue;
  3429. wpa_s->cross_connect_enabled = 1;
  3430. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  3431. sizeof(wpa_s->cross_connect_uplink));
  3432. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  3433. "%s to %s whenever uplink is available",
  3434. wpa_s->ifname, wpa_s->cross_connect_uplink);
  3435. if (iface->ap_iface || iface->current_ssid == NULL ||
  3436. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  3437. iface->cross_connect_disallowed ||
  3438. iface->wpa_state != WPA_COMPLETED)
  3439. break;
  3440. wpa_s->cross_connect_in_use = 1;
  3441. wpa_msg(wpa_s->parent, MSG_INFO,
  3442. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  3443. wpa_s->ifname, wpa_s->cross_connect_uplink);
  3444. break;
  3445. }
  3446. }
  3447. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  3448. {
  3449. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  3450. !wpa_s->p2p_in_provisioning)
  3451. return 0; /* not P2P client operation */
  3452. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  3453. "session overlap");
  3454. if (wpa_s != wpa_s->parent)
  3455. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  3456. if (wpa_s->global->p2p)
  3457. p2p_group_formation_failed(wpa_s->global->p2p);
  3458. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  3459. wpa_s->parent, NULL);
  3460. wpas_group_formation_completed(wpa_s, 0);
  3461. return 1;
  3462. }
  3463. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  3464. {
  3465. struct p2p_channels chan;
  3466. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  3467. return;
  3468. os_memset(&chan, 0, sizeof(chan));
  3469. if (wpas_p2p_setup_channels(wpa_s, &chan)) {
  3470. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  3471. "channel list");
  3472. return;
  3473. }
  3474. p2p_update_channel_list(wpa_s->global->p2p, &chan);
  3475. }
  3476. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  3477. {
  3478. struct wpa_global *global = wpa_s->global;
  3479. int found = 0;
  3480. const u8 *peer;
  3481. if (global->p2p == NULL)
  3482. return -1;
  3483. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  3484. if (wpa_s->pending_interface_name[0] &&
  3485. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  3486. found = 1;
  3487. peer = p2p_get_go_neg_peer(global->p2p);
  3488. if (peer) {
  3489. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  3490. MACSTR, MAC2STR(peer));
  3491. p2p_unauthorize(global->p2p, peer);
  3492. }
  3493. wpas_p2p_stop_find(wpa_s);
  3494. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3495. if (wpa_s == global->p2p_group_formation &&
  3496. (wpa_s->p2p_in_provisioning ||
  3497. wpa_s->parent->pending_interface_type ==
  3498. WPA_IF_P2P_CLIENT)) {
  3499. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  3500. "formation found - cancelling",
  3501. wpa_s->ifname);
  3502. found = 1;
  3503. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  3504. wpa_s->parent, NULL);
  3505. wpas_p2p_group_delete(wpa_s);
  3506. break;
  3507. }
  3508. }
  3509. if (!found) {
  3510. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  3511. return -1;
  3512. }
  3513. return 0;
  3514. }
  3515. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  3516. {
  3517. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  3518. return;
  3519. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  3520. "being available anymore");
  3521. wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
  3522. wpas_p2p_group_delete(wpa_s);
  3523. }
  3524. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  3525. int freq_24, int freq_5, int freq_overall)
  3526. {
  3527. struct p2p_data *p2p = wpa_s->global->p2p;
  3528. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  3529. return;
  3530. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  3531. }
  3532. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  3533. {
  3534. u8 peer[ETH_ALEN];
  3535. struct p2p_data *p2p = wpa_s->global->p2p;
  3536. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  3537. return -1;
  3538. if (hwaddr_aton(addr, peer))
  3539. return -1;
  3540. return p2p_unauthorize(p2p, peer);
  3541. }
  3542. /**
  3543. * wpas_p2p_disconnect - Disconnect from a P2P Group
  3544. * @wpa_s: Pointer to wpa_supplicant data
  3545. * Returns: 0 on success, -1 on failure
  3546. *
  3547. * This can be used to disconnect from a group in which the local end is a P2P
  3548. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  3549. * virtual network interface was created for this group, that interface will be
  3550. * removed. Otherwise, only the configured P2P group network will be removed
  3551. * from the interface.
  3552. */
  3553. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  3554. {
  3555. if (wpa_s == NULL)
  3556. return -1;
  3557. wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
  3558. wpas_p2p_group_delete(wpa_s);
  3559. return 0;
  3560. }
  3561. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  3562. {
  3563. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3564. return 0;
  3565. return p2p_in_progress(wpa_s->global->p2p);
  3566. }
  3567. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  3568. struct wpa_ssid *ssid)
  3569. {
  3570. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  3571. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  3572. wpa_s->parent, NULL) > 0) {
  3573. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  3574. "P2P group network getting removed");
  3575. wpas_p2p_group_formation_timeout(wpa_s->parent, NULL);
  3576. }
  3577. }
  3578. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  3579. const u8 *addr)
  3580. {
  3581. struct wpa_ssid *s;
  3582. for (s = wpa_s->conf->ssid; s; s = s->next) {
  3583. if (s->disabled == 2 &&
  3584. os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  3585. return s;
  3586. }
  3587. return NULL;
  3588. }