p2p_supplicant.c 122 KB

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