p2p_supplicant.c 135 KB

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