p2p_supplicant.c 113 KB

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