p2p_supplicant.c 115 KB

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