p2p_supplicant.c 116 KB

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