p2p_supplicant.c 124 KB

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