p2p_supplicant.c 128 KB

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