p2p_supplicant.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242
  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(device_type);
  819. C(config_methods);
  820. #undef C
  821. d->p2p_group_idle = s->p2p_group_idle;
  822. d->p2p_intra_bss = s->p2p_intra_bss;
  823. }
  824. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  825. enum wpa_driver_if_type type)
  826. {
  827. char ifname[120], force_ifname[120];
  828. if (wpa_s->pending_interface_name[0]) {
  829. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  830. "- skip creation of a new one");
  831. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  832. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  833. "unknown?! ifname='%s'",
  834. wpa_s->pending_interface_name);
  835. return -1;
  836. }
  837. return 0;
  838. }
  839. os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
  840. wpa_s->p2p_group_idx);
  841. if (os_strlen(ifname) >= IFNAMSIZ &&
  842. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  843. /* Try to avoid going over the IFNAMSIZ length limit */
  844. os_snprintf(ifname, sizeof(ifname), "p2p-%d",
  845. wpa_s->p2p_group_idx);
  846. }
  847. force_ifname[0] = '\0';
  848. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  849. ifname);
  850. wpa_s->p2p_group_idx++;
  851. wpa_s->pending_interface_type = type;
  852. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  853. wpa_s->pending_interface_addr) < 0) {
  854. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  855. "interface");
  856. return -1;
  857. }
  858. if (force_ifname[0]) {
  859. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  860. force_ifname);
  861. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  862. sizeof(wpa_s->pending_interface_name));
  863. } else
  864. os_strlcpy(wpa_s->pending_interface_name, ifname,
  865. sizeof(wpa_s->pending_interface_name));
  866. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  867. MACSTR, wpa_s->pending_interface_name,
  868. MAC2STR(wpa_s->pending_interface_addr));
  869. return 0;
  870. }
  871. static void wpas_p2p_remove_pending_group_interface(
  872. struct wpa_supplicant *wpa_s)
  873. {
  874. if (!wpa_s->pending_interface_name[0] ||
  875. is_zero_ether_addr(wpa_s->pending_interface_addr))
  876. return; /* No pending virtual interface */
  877. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  878. wpa_s->pending_interface_name);
  879. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  880. wpa_s->pending_interface_name);
  881. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  882. wpa_s->pending_interface_name[0] = '\0';
  883. }
  884. static struct wpa_supplicant *
  885. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  886. {
  887. struct wpa_interface iface;
  888. struct wpa_supplicant *group_wpa_s;
  889. if (!wpa_s->pending_interface_name[0]) {
  890. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  891. if (!wpas_p2p_create_iface(wpa_s))
  892. return NULL;
  893. /*
  894. * Something has forced us to remove the pending interface; try
  895. * to create a new one and hope for the best that we will get
  896. * the same local address.
  897. */
  898. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  899. WPA_IF_P2P_CLIENT) < 0)
  900. return NULL;
  901. }
  902. os_memset(&iface, 0, sizeof(iface));
  903. iface.ifname = wpa_s->pending_interface_name;
  904. iface.driver = wpa_s->driver->name;
  905. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  906. iface.driver_param = wpa_s->conf->driver_param;
  907. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  908. if (group_wpa_s == NULL) {
  909. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  910. "wpa_supplicant interface");
  911. return NULL;
  912. }
  913. wpa_s->pending_interface_name[0] = '\0';
  914. group_wpa_s->parent = wpa_s;
  915. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  916. P2P_GROUP_INTERFACE_CLIENT;
  917. wpa_s->global->p2p_group_formation = group_wpa_s;
  918. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  919. return group_wpa_s;
  920. }
  921. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  922. void *timeout_ctx)
  923. {
  924. struct wpa_supplicant *wpa_s = eloop_ctx;
  925. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  926. if (wpa_s->global->p2p)
  927. p2p_group_formation_failed(wpa_s->global->p2p);
  928. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  929. wpa_drv_p2p_group_formation_failed(wpa_s);
  930. wpas_group_formation_completed(wpa_s, 0);
  931. }
  932. void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  933. {
  934. struct wpa_supplicant *wpa_s = ctx;
  935. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  936. wpa_drv_cancel_remain_on_channel(wpa_s);
  937. wpa_s->off_channel_freq = 0;
  938. wpa_s->roc_waiting_drv_freq = 0;
  939. }
  940. if (res->status) {
  941. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
  942. res->status);
  943. wpas_p2p_remove_pending_group_interface(wpa_s);
  944. return;
  945. }
  946. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
  947. if (wpa_s->create_p2p_iface) {
  948. struct wpa_supplicant *group_wpa_s =
  949. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  950. if (group_wpa_s == NULL) {
  951. wpas_p2p_remove_pending_group_interface(wpa_s);
  952. return;
  953. }
  954. if (group_wpa_s != wpa_s) {
  955. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  956. sizeof(group_wpa_s->p2p_pin));
  957. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  958. }
  959. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  960. wpa_s->pending_interface_name[0] = '\0';
  961. group_wpa_s->p2p_in_provisioning = 1;
  962. if (res->role_go)
  963. wpas_start_wps_go(group_wpa_s, res, 1);
  964. else
  965. wpas_start_wps_enrollee(group_wpa_s, res);
  966. } else {
  967. wpa_s->p2p_in_provisioning = 1;
  968. wpa_s->global->p2p_group_formation = wpa_s;
  969. if (res->role_go)
  970. wpas_start_wps_go(wpa_s, res, 1);
  971. else
  972. wpas_start_wps_enrollee(ctx, res);
  973. }
  974. wpa_s->p2p_long_listen = 0;
  975. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  976. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  977. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  978. (res->peer_config_timeout % 100) * 10000,
  979. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  980. }
  981. void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  982. {
  983. struct wpa_supplicant *wpa_s = ctx;
  984. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  985. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  986. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  987. }
  988. void wpas_dev_found(void *ctx, const u8 *addr,
  989. const struct p2p_peer_info *info,
  990. int new_device)
  991. {
  992. struct wpa_supplicant *wpa_s = ctx;
  993. char devtype[WPS_DEV_TYPE_BUFSIZE];
  994. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  995. " p2p_dev_addr=" MACSTR
  996. " pri_dev_type=%s name='%s' config_methods=0x%x "
  997. "dev_capab=0x%x group_capab=0x%x",
  998. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  999. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  1000. sizeof(devtype)),
  1001. info->device_name, info->config_methods,
  1002. info->dev_capab, info->group_capab);
  1003. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  1004. }
  1005. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  1006. {
  1007. struct wpa_supplicant *wpa_s = ctx;
  1008. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  1009. }
  1010. static int wpas_start_listen(void *ctx, unsigned int freq,
  1011. unsigned int duration,
  1012. const struct wpabuf *probe_resp_ie)
  1013. {
  1014. struct wpa_supplicant *wpa_s = ctx;
  1015. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
  1016. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1017. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1018. "report received Probe Request frames");
  1019. return -1;
  1020. }
  1021. wpa_s->pending_listen_freq = freq;
  1022. wpa_s->pending_listen_duration = duration;
  1023. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  1024. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1025. "to remain on channel (%u MHz) for Listen "
  1026. "state", freq);
  1027. wpa_s->pending_listen_freq = 0;
  1028. return -1;
  1029. }
  1030. wpa_s->off_channel_freq = 0;
  1031. wpa_s->roc_waiting_drv_freq = freq;
  1032. return 0;
  1033. }
  1034. static void wpas_stop_listen(void *ctx)
  1035. {
  1036. struct wpa_supplicant *wpa_s = ctx;
  1037. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1038. wpa_drv_cancel_remain_on_channel(wpa_s);
  1039. wpa_s->off_channel_freq = 0;
  1040. wpa_s->roc_waiting_drv_freq = 0;
  1041. }
  1042. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  1043. wpa_drv_probe_req_report(wpa_s, 0);
  1044. }
  1045. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  1046. {
  1047. struct wpa_supplicant *wpa_s = ctx;
  1048. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
  1049. }
  1050. static struct p2p_srv_bonjour *
  1051. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  1052. const struct wpabuf *query)
  1053. {
  1054. struct p2p_srv_bonjour *bsrv;
  1055. size_t len;
  1056. len = wpabuf_len(query);
  1057. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1058. struct p2p_srv_bonjour, list) {
  1059. if (len == wpabuf_len(bsrv->query) &&
  1060. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  1061. len) == 0)
  1062. return bsrv;
  1063. }
  1064. return NULL;
  1065. }
  1066. static struct p2p_srv_upnp *
  1067. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1068. const char *service)
  1069. {
  1070. struct p2p_srv_upnp *usrv;
  1071. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1072. struct p2p_srv_upnp, list) {
  1073. if (version == usrv->version &&
  1074. os_strcmp(service, usrv->service) == 0)
  1075. return usrv;
  1076. }
  1077. return NULL;
  1078. }
  1079. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  1080. u8 srv_trans_id)
  1081. {
  1082. u8 *len_pos;
  1083. if (wpabuf_tailroom(resp) < 5)
  1084. return;
  1085. /* Length (to be filled) */
  1086. len_pos = wpabuf_put(resp, 2);
  1087. wpabuf_put_u8(resp, srv_proto);
  1088. wpabuf_put_u8(resp, srv_trans_id);
  1089. /* Status Code */
  1090. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  1091. /* Response Data: empty */
  1092. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1093. }
  1094. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  1095. struct wpabuf *resp, u8 srv_trans_id)
  1096. {
  1097. struct p2p_srv_bonjour *bsrv;
  1098. u8 *len_pos;
  1099. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  1100. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1101. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1102. return;
  1103. }
  1104. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1105. struct p2p_srv_bonjour, list) {
  1106. if (wpabuf_tailroom(resp) <
  1107. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  1108. return;
  1109. /* Length (to be filled) */
  1110. len_pos = wpabuf_put(resp, 2);
  1111. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1112. wpabuf_put_u8(resp, srv_trans_id);
  1113. /* Status Code */
  1114. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1115. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1116. wpabuf_head(bsrv->resp),
  1117. wpabuf_len(bsrv->resp));
  1118. /* Response Data */
  1119. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1120. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1121. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1122. 2);
  1123. }
  1124. }
  1125. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  1126. struct wpabuf *resp, u8 srv_trans_id,
  1127. const u8 *query, size_t query_len)
  1128. {
  1129. struct p2p_srv_bonjour *bsrv;
  1130. struct wpabuf buf;
  1131. u8 *len_pos;
  1132. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1133. query, query_len);
  1134. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1135. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1136. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1137. srv_trans_id);
  1138. return;
  1139. }
  1140. if (query_len == 0) {
  1141. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1142. return;
  1143. }
  1144. if (wpabuf_tailroom(resp) < 5)
  1145. return;
  1146. /* Length (to be filled) */
  1147. len_pos = wpabuf_put(resp, 2);
  1148. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1149. wpabuf_put_u8(resp, srv_trans_id);
  1150. wpabuf_set(&buf, query, query_len);
  1151. bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
  1152. if (bsrv == NULL) {
  1153. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1154. "available");
  1155. /* Status Code */
  1156. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1157. /* Response Data: empty */
  1158. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1159. 2);
  1160. return;
  1161. }
  1162. /* Status Code */
  1163. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1164. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1165. wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
  1166. if (wpabuf_tailroom(resp) >=
  1167. wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
  1168. /* Response Data */
  1169. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1170. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1171. }
  1172. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1173. }
  1174. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1175. struct wpabuf *resp, u8 srv_trans_id)
  1176. {
  1177. struct p2p_srv_upnp *usrv;
  1178. u8 *len_pos;
  1179. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1180. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1181. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1182. return;
  1183. }
  1184. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1185. struct p2p_srv_upnp, list) {
  1186. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1187. return;
  1188. /* Length (to be filled) */
  1189. len_pos = wpabuf_put(resp, 2);
  1190. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1191. wpabuf_put_u8(resp, srv_trans_id);
  1192. /* Status Code */
  1193. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1194. /* Response Data */
  1195. wpabuf_put_u8(resp, usrv->version);
  1196. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1197. usrv->service);
  1198. wpabuf_put_str(resp, usrv->service);
  1199. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1200. 2);
  1201. }
  1202. }
  1203. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1204. struct wpabuf *resp, u8 srv_trans_id,
  1205. const u8 *query, size_t query_len)
  1206. {
  1207. struct p2p_srv_upnp *usrv;
  1208. u8 *len_pos;
  1209. u8 version;
  1210. char *str;
  1211. int count = 0;
  1212. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1213. query, query_len);
  1214. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1215. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1216. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1217. srv_trans_id);
  1218. return;
  1219. }
  1220. if (query_len == 0) {
  1221. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1222. return;
  1223. }
  1224. if (wpabuf_tailroom(resp) < 5)
  1225. return;
  1226. /* Length (to be filled) */
  1227. len_pos = wpabuf_put(resp, 2);
  1228. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1229. wpabuf_put_u8(resp, srv_trans_id);
  1230. version = query[0];
  1231. str = os_malloc(query_len);
  1232. if (str == NULL)
  1233. return;
  1234. os_memcpy(str, query + 1, query_len - 1);
  1235. str[query_len - 1] = '\0';
  1236. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1237. struct p2p_srv_upnp, list) {
  1238. if (version != usrv->version)
  1239. continue;
  1240. if (os_strcmp(str, "ssdp:all") != 0 &&
  1241. os_strstr(usrv->service, str) == NULL)
  1242. continue;
  1243. if (wpabuf_tailroom(resp) < 2)
  1244. break;
  1245. if (count == 0) {
  1246. /* Status Code */
  1247. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1248. /* Response Data */
  1249. wpabuf_put_u8(resp, version);
  1250. } else
  1251. wpabuf_put_u8(resp, ',');
  1252. count++;
  1253. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1254. usrv->service);
  1255. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1256. break;
  1257. wpabuf_put_str(resp, usrv->service);
  1258. }
  1259. if (count == 0) {
  1260. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1261. "available");
  1262. /* Status Code */
  1263. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1264. /* Response Data: empty */
  1265. }
  1266. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1267. }
  1268. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1269. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1270. {
  1271. struct wpa_supplicant *wpa_s = ctx;
  1272. const u8 *pos = tlvs;
  1273. const u8 *end = tlvs + tlvs_len;
  1274. const u8 *tlv_end;
  1275. u16 slen;
  1276. struct wpabuf *resp;
  1277. u8 srv_proto, srv_trans_id;
  1278. size_t buf_len;
  1279. char *buf;
  1280. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1281. tlvs, tlvs_len);
  1282. buf_len = 2 * tlvs_len + 1;
  1283. buf = os_malloc(buf_len);
  1284. if (buf) {
  1285. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1286. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1287. MACSTR " %u %u %s",
  1288. freq, MAC2STR(sa), dialog_token, update_indic,
  1289. buf);
  1290. os_free(buf);
  1291. }
  1292. if (wpa_s->p2p_sd_over_ctrl_iface)
  1293. return; /* to be processed by an external program */
  1294. resp = wpabuf_alloc(10000);
  1295. if (resp == NULL)
  1296. return;
  1297. while (pos + 1 < end) {
  1298. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1299. slen = WPA_GET_LE16(pos);
  1300. pos += 2;
  1301. if (pos + slen > end || slen < 2) {
  1302. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1303. "length");
  1304. wpabuf_free(resp);
  1305. return;
  1306. }
  1307. tlv_end = pos + slen;
  1308. srv_proto = *pos++;
  1309. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1310. srv_proto);
  1311. srv_trans_id = *pos++;
  1312. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1313. srv_trans_id);
  1314. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1315. pos, tlv_end - pos);
  1316. if (wpa_s->force_long_sd) {
  1317. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1318. "response");
  1319. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1320. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1321. goto done;
  1322. }
  1323. switch (srv_proto) {
  1324. case P2P_SERV_ALL_SERVICES:
  1325. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1326. "for all services");
  1327. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1328. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1329. wpa_printf(MSG_DEBUG, "P2P: No service "
  1330. "discovery protocols available");
  1331. wpas_sd_add_proto_not_avail(
  1332. resp, P2P_SERV_ALL_SERVICES,
  1333. srv_trans_id);
  1334. break;
  1335. }
  1336. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1337. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1338. break;
  1339. case P2P_SERV_BONJOUR:
  1340. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1341. pos, tlv_end - pos);
  1342. break;
  1343. case P2P_SERV_UPNP:
  1344. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1345. pos, tlv_end - pos);
  1346. break;
  1347. default:
  1348. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1349. "protocol %u", srv_proto);
  1350. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1351. srv_trans_id);
  1352. break;
  1353. }
  1354. pos = tlv_end;
  1355. }
  1356. done:
  1357. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1358. wpabuf_free(resp);
  1359. }
  1360. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1361. const u8 *tlvs, size_t tlvs_len)
  1362. {
  1363. struct wpa_supplicant *wpa_s = ctx;
  1364. const u8 *pos = tlvs;
  1365. const u8 *end = tlvs + tlvs_len;
  1366. const u8 *tlv_end;
  1367. u16 slen;
  1368. size_t buf_len;
  1369. char *buf;
  1370. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1371. tlvs, tlvs_len);
  1372. if (tlvs_len > 1500) {
  1373. /* TODO: better way for handling this */
  1374. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1375. P2P_EVENT_SERV_DISC_RESP MACSTR
  1376. " %u <long response: %u bytes>",
  1377. MAC2STR(sa), update_indic,
  1378. (unsigned int) tlvs_len);
  1379. } else {
  1380. buf_len = 2 * tlvs_len + 1;
  1381. buf = os_malloc(buf_len);
  1382. if (buf) {
  1383. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1384. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1385. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1386. MAC2STR(sa), update_indic, buf);
  1387. os_free(buf);
  1388. }
  1389. }
  1390. while (pos < end) {
  1391. u8 srv_proto, srv_trans_id, status;
  1392. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1393. slen = WPA_GET_LE16(pos);
  1394. pos += 2;
  1395. if (pos + slen > end || slen < 3) {
  1396. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1397. "length");
  1398. return;
  1399. }
  1400. tlv_end = pos + slen;
  1401. srv_proto = *pos++;
  1402. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1403. srv_proto);
  1404. srv_trans_id = *pos++;
  1405. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1406. srv_trans_id);
  1407. status = *pos++;
  1408. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1409. status);
  1410. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1411. pos, tlv_end - pos);
  1412. pos = tlv_end;
  1413. }
  1414. }
  1415. void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1416. const struct wpabuf *tlvs)
  1417. {
  1418. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1419. return (void *) wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
  1420. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1421. return NULL;
  1422. return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1423. }
  1424. void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1425. u8 version, const char *query)
  1426. {
  1427. struct wpabuf *tlvs;
  1428. void *ret;
  1429. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1430. if (tlvs == NULL)
  1431. return NULL;
  1432. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1433. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1434. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1435. wpabuf_put_u8(tlvs, version);
  1436. wpabuf_put_str(tlvs, query);
  1437. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1438. wpabuf_free(tlvs);
  1439. return ret;
  1440. }
  1441. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
  1442. {
  1443. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1444. return wpa_drv_p2p_sd_cancel_request(wpa_s, (u64) req);
  1445. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1446. return -1;
  1447. return p2p_sd_cancel_request(wpa_s->global->p2p, req);
  1448. }
  1449. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1450. const u8 *dst, u8 dialog_token,
  1451. const struct wpabuf *resp_tlvs)
  1452. {
  1453. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1454. wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
  1455. resp_tlvs);
  1456. return;
  1457. }
  1458. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1459. return;
  1460. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1461. resp_tlvs);
  1462. }
  1463. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1464. {
  1465. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1466. wpa_drv_p2p_service_update(wpa_s);
  1467. return;
  1468. }
  1469. if (wpa_s->global->p2p)
  1470. p2p_sd_service_update(wpa_s->global->p2p);
  1471. }
  1472. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1473. {
  1474. dl_list_del(&bsrv->list);
  1475. wpabuf_free(bsrv->query);
  1476. wpabuf_free(bsrv->resp);
  1477. os_free(bsrv);
  1478. }
  1479. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1480. {
  1481. dl_list_del(&usrv->list);
  1482. os_free(usrv->service);
  1483. os_free(usrv);
  1484. }
  1485. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1486. {
  1487. struct p2p_srv_bonjour *bsrv, *bn;
  1488. struct p2p_srv_upnp *usrv, *un;
  1489. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1490. struct p2p_srv_bonjour, list)
  1491. wpas_p2p_srv_bonjour_free(bsrv);
  1492. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1493. struct p2p_srv_upnp, list)
  1494. wpas_p2p_srv_upnp_free(usrv);
  1495. wpas_p2p_sd_service_update(wpa_s);
  1496. }
  1497. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1498. struct wpabuf *query, struct wpabuf *resp)
  1499. {
  1500. struct p2p_srv_bonjour *bsrv;
  1501. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1502. if (bsrv) {
  1503. wpabuf_free(query);
  1504. wpabuf_free(bsrv->resp);
  1505. bsrv->resp = resp;
  1506. return 0;
  1507. }
  1508. bsrv = os_zalloc(sizeof(*bsrv));
  1509. if (bsrv == NULL)
  1510. return -1;
  1511. bsrv->query = query;
  1512. bsrv->resp = resp;
  1513. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  1514. wpas_p2p_sd_service_update(wpa_s);
  1515. return 0;
  1516. }
  1517. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1518. const struct wpabuf *query)
  1519. {
  1520. struct p2p_srv_bonjour *bsrv;
  1521. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1522. if (bsrv == NULL)
  1523. return -1;
  1524. wpas_p2p_srv_bonjour_free(bsrv);
  1525. wpas_p2p_sd_service_update(wpa_s);
  1526. return 0;
  1527. }
  1528. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1529. const char *service)
  1530. {
  1531. struct p2p_srv_upnp *usrv;
  1532. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  1533. return 0; /* Already listed */
  1534. usrv = os_zalloc(sizeof(*usrv));
  1535. if (usrv == NULL)
  1536. return -1;
  1537. usrv->version = version;
  1538. usrv->service = os_strdup(service);
  1539. if (usrv->service == NULL) {
  1540. os_free(usrv);
  1541. return -1;
  1542. }
  1543. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  1544. wpas_p2p_sd_service_update(wpa_s);
  1545. return 0;
  1546. }
  1547. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1548. const char *service)
  1549. {
  1550. struct p2p_srv_upnp *usrv;
  1551. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  1552. if (usrv == NULL)
  1553. return -1;
  1554. wpas_p2p_srv_upnp_free(usrv);
  1555. wpas_p2p_sd_service_update(wpa_s);
  1556. return 0;
  1557. }
  1558. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  1559. const u8 *peer, const char *params,
  1560. unsigned int generated_pin)
  1561. {
  1562. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
  1563. MAC2STR(peer), generated_pin, params);
  1564. }
  1565. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  1566. const u8 *peer, const char *params)
  1567. {
  1568. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
  1569. MAC2STR(peer), params);
  1570. }
  1571. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  1572. const u8 *dev_addr, const u8 *pri_dev_type,
  1573. const char *dev_name, u16 supp_config_methods,
  1574. u8 dev_capab, u8 group_capab)
  1575. {
  1576. struct wpa_supplicant *wpa_s = ctx;
  1577. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1578. char params[200];
  1579. u8 empty_dev_type[8];
  1580. unsigned int generated_pin = 0;
  1581. if (pri_dev_type == NULL) {
  1582. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  1583. pri_dev_type = empty_dev_type;
  1584. }
  1585. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  1586. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1587. "dev_capab=0x%x group_capab=0x%x",
  1588. MAC2STR(dev_addr),
  1589. wps_dev_type_bin2str(pri_dev_type, devtype,
  1590. sizeof(devtype)),
  1591. dev_name, supp_config_methods, dev_capab, group_capab);
  1592. params[sizeof(params) - 1] = '\0';
  1593. if (config_methods & WPS_CONFIG_DISPLAY) {
  1594. generated_pin = wps_generate_pin();
  1595. wpas_prov_disc_local_display(wpa_s, peer, params,
  1596. generated_pin);
  1597. } else if (config_methods & WPS_CONFIG_KEYPAD)
  1598. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  1599. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1600. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
  1601. "%s", MAC2STR(peer), params);
  1602. }
  1603. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  1604. {
  1605. struct wpa_supplicant *wpa_s = ctx;
  1606. unsigned int generated_pin = 0;
  1607. if (config_methods & WPS_CONFIG_DISPLAY)
  1608. wpas_prov_disc_local_keypad(wpa_s, peer, "");
  1609. else if (config_methods & WPS_CONFIG_KEYPAD) {
  1610. generated_pin = wps_generate_pin();
  1611. wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
  1612. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1613. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
  1614. MAC2STR(peer));
  1615. if (wpa_s->pending_pd_before_join &&
  1616. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  1617. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  1618. wpa_s->pending_pd_before_join = 0;
  1619. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  1620. "join-existing-group operation");
  1621. wpas_p2p_join_start(wpa_s);
  1622. }
  1623. }
  1624. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  1625. const u8 *go_dev_addr, const u8 *ssid,
  1626. size_t ssid_len, int *go, u8 *group_bssid,
  1627. int *force_freq, int persistent_group)
  1628. {
  1629. struct wpa_supplicant *wpa_s = ctx;
  1630. struct wpa_ssid *s;
  1631. u8 cur_bssid[ETH_ALEN];
  1632. int res;
  1633. struct wpa_supplicant *grp;
  1634. if (!persistent_group) {
  1635. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  1636. " to join an active group", MAC2STR(sa));
  1637. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  1638. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  1639. == 0 ||
  1640. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  1641. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  1642. "authorized invitation");
  1643. goto accept_inv;
  1644. }
  1645. /*
  1646. * Do not accept the invitation automatically; notify user and
  1647. * request approval.
  1648. */
  1649. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  1650. }
  1651. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  1652. if (grp) {
  1653. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  1654. "running persistent group");
  1655. if (*go)
  1656. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  1657. goto accept_inv;
  1658. }
  1659. if (!wpa_s->conf->persistent_reconnect)
  1660. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  1661. for (s = wpa_s->conf->ssid; s; s = s->next) {
  1662. if (s->disabled == 2 &&
  1663. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  1664. s->ssid_len == ssid_len &&
  1665. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  1666. break;
  1667. }
  1668. if (!s) {
  1669. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  1670. " requested reinvocation of an unknown group",
  1671. MAC2STR(sa));
  1672. return P2P_SC_FAIL_UNKNOWN_GROUP;
  1673. }
  1674. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  1675. *go = 1;
  1676. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  1677. wpa_printf(MSG_DEBUG, "P2P: The only available "
  1678. "interface is already in use - reject "
  1679. "invitation");
  1680. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  1681. }
  1682. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  1683. } else if (s->mode == WPAS_MODE_P2P_GO) {
  1684. *go = 1;
  1685. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  1686. {
  1687. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  1688. "interface address for the group");
  1689. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  1690. }
  1691. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  1692. ETH_ALEN);
  1693. }
  1694. accept_inv:
  1695. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
  1696. wpa_s->assoc_freq) {
  1697. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  1698. "the channel we are already using");
  1699. *force_freq = wpa_s->assoc_freq;
  1700. }
  1701. res = wpa_drv_shared_freq(wpa_s);
  1702. if (res > 0) {
  1703. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  1704. "with the channel we are already using on a "
  1705. "shared interface");
  1706. *force_freq = res;
  1707. }
  1708. return P2P_SC_SUCCESS;
  1709. }
  1710. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  1711. const u8 *ssid, size_t ssid_len,
  1712. const u8 *go_dev_addr, u8 status,
  1713. int op_freq)
  1714. {
  1715. struct wpa_supplicant *wpa_s = ctx;
  1716. struct wpa_ssid *s;
  1717. for (s = wpa_s->conf->ssid; s; s = s->next) {
  1718. if (s->disabled == 2 &&
  1719. s->ssid_len == ssid_len &&
  1720. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  1721. break;
  1722. }
  1723. if (status == P2P_SC_SUCCESS) {
  1724. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  1725. " was accepted; op_freq=%d MHz",
  1726. MAC2STR(sa), op_freq);
  1727. if (s) {
  1728. wpas_p2p_group_add_persistent(
  1729. wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
  1730. } else if (bssid) {
  1731. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  1732. wpa_s->p2p_wps_method);
  1733. }
  1734. return;
  1735. }
  1736. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  1737. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  1738. " was rejected (status %u)", MAC2STR(sa), status);
  1739. return;
  1740. }
  1741. if (!s) {
  1742. if (bssid) {
  1743. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  1744. "sa=" MACSTR " go_dev_addr=" MACSTR
  1745. " bssid=" MACSTR " unknown-network",
  1746. MAC2STR(sa), MAC2STR(go_dev_addr),
  1747. MAC2STR(bssid));
  1748. } else {
  1749. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  1750. "sa=" MACSTR " go_dev_addr=" MACSTR
  1751. " unknown-network",
  1752. MAC2STR(sa), MAC2STR(go_dev_addr));
  1753. }
  1754. return;
  1755. }
  1756. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
  1757. " persistent=%d", MAC2STR(sa), s->id);
  1758. }
  1759. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
  1760. {
  1761. struct wpa_supplicant *wpa_s = ctx;
  1762. struct wpa_ssid *ssid;
  1763. if (bssid) {
  1764. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  1765. "status=%d " MACSTR,
  1766. status, MAC2STR(bssid));
  1767. } else {
  1768. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  1769. "status=%d ", status);
  1770. }
  1771. if (wpa_s->pending_invite_ssid_id == -1)
  1772. return; /* Invitation to active group */
  1773. if (status != P2P_SC_SUCCESS) {
  1774. wpas_p2p_remove_pending_group_interface(wpa_s);
  1775. return;
  1776. }
  1777. ssid = wpa_config_get_network(wpa_s->conf,
  1778. wpa_s->pending_invite_ssid_id);
  1779. if (ssid == NULL) {
  1780. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  1781. "data matching with invitation");
  1782. return;
  1783. }
  1784. wpas_p2p_group_add_persistent(wpa_s, ssid,
  1785. ssid->mode == WPAS_MODE_P2P_GO, 0);
  1786. }
  1787. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  1788. struct p2p_channels *chan)
  1789. {
  1790. int i, cla = 0;
  1791. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  1792. "band");
  1793. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  1794. chan->reg_class[cla].reg_class = 81;
  1795. chan->reg_class[cla].channels = 11;
  1796. for (i = 0; i < 11; i++)
  1797. chan->reg_class[cla].channel[i] = i + 1;
  1798. cla++;
  1799. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  1800. "band");
  1801. /* Operating class 115 - 5 GHz, channels 36-48 */
  1802. chan->reg_class[cla].reg_class = 115;
  1803. chan->reg_class[cla].channels = 4;
  1804. chan->reg_class[cla].channel[0] = 36;
  1805. chan->reg_class[cla].channel[1] = 40;
  1806. chan->reg_class[cla].channel[2] = 44;
  1807. chan->reg_class[cla].channel[3] = 48;
  1808. cla++;
  1809. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  1810. "band");
  1811. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  1812. chan->reg_class[cla].reg_class = 124;
  1813. chan->reg_class[cla].channels = 4;
  1814. chan->reg_class[cla].channel[0] = 149;
  1815. chan->reg_class[cla].channel[1] = 153;
  1816. chan->reg_class[cla].channel[2] = 157;
  1817. chan->reg_class[cla].channel[3] = 161;
  1818. cla++;
  1819. chan->reg_classes = cla;
  1820. return 0;
  1821. }
  1822. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  1823. u16 num_modes,
  1824. enum hostapd_hw_mode mode)
  1825. {
  1826. u16 i;
  1827. for (i = 0; i < num_modes; i++) {
  1828. if (modes[i].mode == mode)
  1829. return &modes[i];
  1830. }
  1831. return NULL;
  1832. }
  1833. static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
  1834. {
  1835. int i;
  1836. for (i = 0; i < mode->num_channels; i++) {
  1837. if (mode->channels[i].chan == chan) {
  1838. if (flags)
  1839. *flags = mode->channels[i].flag;
  1840. return !(mode->channels[i].flag &
  1841. (HOSTAPD_CHAN_DISABLED |
  1842. HOSTAPD_CHAN_PASSIVE_SCAN |
  1843. HOSTAPD_CHAN_NO_IBSS |
  1844. HOSTAPD_CHAN_RADAR));
  1845. }
  1846. }
  1847. return 0;
  1848. }
  1849. struct p2p_oper_class_map {
  1850. enum hostapd_hw_mode mode;
  1851. u8 op_class;
  1852. u8 min_chan;
  1853. u8 max_chan;
  1854. u8 inc;
  1855. enum { BW20, BW40PLUS, BW40MINUS } bw;
  1856. };
  1857. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  1858. struct p2p_channels *chan)
  1859. {
  1860. struct hostapd_hw_modes *modes, *mode;
  1861. u16 num_modes, flags;
  1862. int cla, op;
  1863. struct p2p_oper_class_map op_class[] = {
  1864. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  1865. { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
  1866. #if 0 /* Do not enable HT40 on 2 GHz for now */
  1867. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  1868. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  1869. #endif
  1870. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  1871. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  1872. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  1873. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  1874. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  1875. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  1876. { -1, 0, 0, 0, 0, BW20 }
  1877. };
  1878. modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
  1879. if (modes == NULL) {
  1880. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  1881. "of all supported channels; assume dualband "
  1882. "support");
  1883. return wpas_p2p_default_channels(wpa_s, chan);
  1884. }
  1885. cla = 0;
  1886. for (op = 0; op_class[op].op_class; op++) {
  1887. struct p2p_oper_class_map *o = &op_class[op];
  1888. u8 ch;
  1889. struct p2p_reg_class *reg = NULL;
  1890. mode = get_mode(modes, num_modes, o->mode);
  1891. if (mode == NULL)
  1892. continue;
  1893. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  1894. int flag;
  1895. if (!has_channel(mode, ch, &flag))
  1896. continue;
  1897. if (o->bw == BW40MINUS &&
  1898. (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
  1899. !has_channel(mode, ch - 4, NULL)))
  1900. continue;
  1901. if (o->bw == BW40PLUS &&
  1902. (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
  1903. !has_channel(mode, ch + 4, NULL)))
  1904. continue;
  1905. if (reg == NULL) {
  1906. wpa_printf(MSG_DEBUG, "P2P: Add operating "
  1907. "class %u", o->op_class);
  1908. reg = &chan->reg_class[cla];
  1909. cla++;
  1910. reg->reg_class = o->op_class;
  1911. }
  1912. reg->channel[reg->channels] = ch;
  1913. reg->channels++;
  1914. }
  1915. if (reg) {
  1916. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  1917. reg->channel, reg->channels);
  1918. }
  1919. }
  1920. chan->reg_classes = cla;
  1921. ieee80211_sta_free_hw_features(modes, num_modes);
  1922. return 0;
  1923. }
  1924. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  1925. size_t buf_len)
  1926. {
  1927. struct wpa_supplicant *wpa_s = ctx;
  1928. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  1929. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  1930. break;
  1931. }
  1932. if (wpa_s == NULL)
  1933. return -1;
  1934. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  1935. }
  1936. /**
  1937. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  1938. * @global: Pointer to global data from wpa_supplicant_init()
  1939. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  1940. * Returns: 0 on success, -1 on failure
  1941. */
  1942. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  1943. {
  1944. struct p2p_config p2p;
  1945. unsigned int r;
  1946. int i;
  1947. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  1948. return 0;
  1949. #ifdef CONFIG_CLIENT_MLME
  1950. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
  1951. wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
  1952. wpa_s->mlme.public_action_cb_ctx = wpa_s;
  1953. }
  1954. #endif /* CONFIG_CLIENT_MLME */
  1955. if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
  1956. wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
  1957. /* Continue anyway; this is not really a fatal error */
  1958. }
  1959. if (global->p2p)
  1960. return 0;
  1961. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1962. struct p2p_params params;
  1963. wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
  1964. os_memset(&params, 0, sizeof(params));
  1965. params.dev_name = wpa_s->conf->device_name;
  1966. if (wpa_s->conf->device_type &&
  1967. wps_dev_type_str2bin(wpa_s->conf->device_type,
  1968. params.pri_dev_type) < 0) {
  1969. wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
  1970. return -1;
  1971. }
  1972. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
  1973. if (wpa_s->conf->sec_device_type[i] == NULL)
  1974. continue;
  1975. if (wps_dev_type_str2bin(
  1976. wpa_s->conf->sec_device_type[i],
  1977. params.sec_dev_type[
  1978. params.num_sec_dev_types]) < 0) {
  1979. wpa_printf(MSG_ERROR, "P2P: Invalid "
  1980. "sec_device_type");
  1981. return -1;
  1982. }
  1983. params.num_sec_dev_types++;
  1984. if (params.num_sec_dev_types == DRV_MAX_SEC_DEV_TYPES)
  1985. break;
  1986. }
  1987. if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
  1988. return -1;
  1989. return 0;
  1990. }
  1991. os_memset(&p2p, 0, sizeof(p2p));
  1992. p2p.msg_ctx = wpa_s;
  1993. p2p.cb_ctx = wpa_s;
  1994. p2p.p2p_scan = wpas_p2p_scan;
  1995. p2p.send_action = wpas_send_action;
  1996. p2p.send_action_done = wpas_send_action_done;
  1997. p2p.go_neg_completed = wpas_go_neg_completed;
  1998. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  1999. p2p.dev_found = wpas_dev_found;
  2000. p2p.dev_lost = wpas_dev_lost;
  2001. p2p.start_listen = wpas_start_listen;
  2002. p2p.stop_listen = wpas_stop_listen;
  2003. p2p.send_probe_resp = wpas_send_probe_resp;
  2004. p2p.sd_request = wpas_sd_request;
  2005. p2p.sd_response = wpas_sd_response;
  2006. p2p.prov_disc_req = wpas_prov_disc_req;
  2007. p2p.prov_disc_resp = wpas_prov_disc_resp;
  2008. p2p.invitation_process = wpas_invitation_process;
  2009. p2p.invitation_received = wpas_invitation_received;
  2010. p2p.invitation_result = wpas_invitation_result;
  2011. p2p.get_noa = wpas_get_noa;
  2012. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  2013. os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
  2014. p2p.dev_name = wpa_s->conf->device_name;
  2015. if (wpa_s->conf->p2p_listen_reg_class &&
  2016. wpa_s->conf->p2p_listen_channel) {
  2017. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  2018. p2p.channel = wpa_s->conf->p2p_listen_channel;
  2019. } else {
  2020. p2p.reg_class = 81;
  2021. /*
  2022. * Pick one of the social channels randomly as the listen
  2023. * channel.
  2024. */
  2025. os_get_random((u8 *) &r, sizeof(r));
  2026. p2p.channel = 1 + (r % 3) * 5;
  2027. }
  2028. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  2029. if (wpa_s->conf->p2p_oper_reg_class &&
  2030. wpa_s->conf->p2p_oper_channel) {
  2031. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  2032. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  2033. p2p.cfg_op_channel = 1;
  2034. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  2035. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2036. } else {
  2037. p2p.op_reg_class = 81;
  2038. /*
  2039. * Use random operation channel from (1, 6, 11) if no other
  2040. * preference is indicated.
  2041. */
  2042. os_get_random((u8 *) &r, sizeof(r));
  2043. p2p.op_channel = 1 + (r % 3) * 5;
  2044. p2p.cfg_op_channel = 0;
  2045. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  2046. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2047. }
  2048. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2049. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  2050. p2p.country[2] = 0x04;
  2051. } else
  2052. os_memcpy(p2p.country, "XX\x04", 3);
  2053. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
  2054. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  2055. "channel list");
  2056. return -1;
  2057. }
  2058. if (wpa_s->conf->device_type &&
  2059. wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
  2060. 0) {
  2061. wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
  2062. return -1;
  2063. }
  2064. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
  2065. if (wpa_s->conf->sec_device_type[i] == NULL)
  2066. continue;
  2067. if (wps_dev_type_str2bin(
  2068. wpa_s->conf->sec_device_type[i],
  2069. p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
  2070. wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
  2071. return -1;
  2072. }
  2073. p2p.num_sec_dev_types++;
  2074. if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
  2075. break;
  2076. }
  2077. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  2078. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  2079. p2p.max_peers = 100;
  2080. if (wpa_s->conf->p2p_ssid_postfix) {
  2081. p2p.ssid_postfix_len =
  2082. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  2083. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  2084. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  2085. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  2086. p2p.ssid_postfix_len);
  2087. }
  2088. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  2089. global->p2p = p2p_init(&p2p);
  2090. if (global->p2p == NULL)
  2091. return -1;
  2092. return 0;
  2093. }
  2094. /**
  2095. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  2096. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2097. *
  2098. * This function deinitialize per-interface P2P data.
  2099. */
  2100. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  2101. {
  2102. if (wpa_s->driver && wpa_s->drv_priv)
  2103. wpa_drv_probe_req_report(wpa_s, 0);
  2104. os_free(wpa_s->go_params);
  2105. wpa_s->go_params = NULL;
  2106. wpabuf_free(wpa_s->pending_action_tx);
  2107. wpa_s->pending_action_tx = NULL;
  2108. eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
  2109. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2110. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2111. wpa_s->p2p_long_listen = 0;
  2112. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2113. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2114. wpas_p2p_remove_pending_group_interface(wpa_s);
  2115. /* TODO: remove group interface from the driver if this wpa_s instance
  2116. * is on top of a P2P group interface */
  2117. }
  2118. /**
  2119. * wpas_p2p_deinit_global - Deinitialize global P2P module
  2120. * @global: Pointer to global data from wpa_supplicant_init()
  2121. *
  2122. * This function deinitializes the global (per device) P2P module.
  2123. */
  2124. void wpas_p2p_deinit_global(struct wpa_global *global)
  2125. {
  2126. struct wpa_supplicant *wpa_s, *tmp;
  2127. char *ifname;
  2128. if (global->p2p == NULL)
  2129. return;
  2130. /* Remove remaining P2P group interfaces */
  2131. wpa_s = global->ifaces;
  2132. if (wpa_s)
  2133. wpas_p2p_service_flush(wpa_s);
  2134. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  2135. wpa_s = wpa_s->next;
  2136. while (wpa_s) {
  2137. enum wpa_driver_if_type type;
  2138. tmp = global->ifaces;
  2139. while (tmp &&
  2140. (tmp == wpa_s ||
  2141. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  2142. tmp = tmp->next;
  2143. }
  2144. if (tmp == NULL)
  2145. break;
  2146. ifname = os_strdup(tmp->ifname);
  2147. type = wpas_p2p_if_type(tmp->p2p_group_interface);
  2148. wpa_supplicant_remove_iface(global, tmp);
  2149. if (ifname)
  2150. wpa_drv_if_remove(wpa_s, type, ifname);
  2151. os_free(ifname);
  2152. }
  2153. /*
  2154. * Deinit GO data on any possibly remaining interface (if main
  2155. * interface is used as GO).
  2156. */
  2157. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2158. if (wpa_s->ap_iface)
  2159. wpas_p2p_group_deinit(wpa_s);
  2160. }
  2161. p2p_deinit(global->p2p);
  2162. global->p2p = NULL;
  2163. }
  2164. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  2165. {
  2166. if (wpa_s->drv_flags &
  2167. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  2168. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  2169. return 1; /* P2P group requires a new interface in every case
  2170. */
  2171. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  2172. return 0; /* driver does not support concurrent operations */
  2173. if (wpa_s->global->ifaces->next)
  2174. return 1; /* more that one interface already in use */
  2175. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2176. return 1; /* this interface is already in use */
  2177. return 0;
  2178. }
  2179. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  2180. const u8 *peer_addr,
  2181. enum p2p_wps_method wps_method,
  2182. int go_intent, const u8 *own_interface_addr,
  2183. unsigned int force_freq, int persistent_group)
  2184. {
  2185. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2186. return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
  2187. go_intent, own_interface_addr,
  2188. force_freq, persistent_group);
  2189. }
  2190. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  2191. go_intent, own_interface_addr, force_freq,
  2192. persistent_group);
  2193. }
  2194. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  2195. const u8 *peer_addr,
  2196. enum p2p_wps_method wps_method,
  2197. int go_intent, const u8 *own_interface_addr,
  2198. unsigned int force_freq, int persistent_group)
  2199. {
  2200. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2201. return -1;
  2202. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  2203. go_intent, own_interface_addr, force_freq,
  2204. persistent_group);
  2205. }
  2206. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  2207. {
  2208. wpa_s->p2p_join_scan_count++;
  2209. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  2210. wpa_s->p2p_join_scan_count);
  2211. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  2212. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  2213. " for join operationg - stop join attempt",
  2214. MAC2STR(wpa_s->pending_join_iface_addr));
  2215. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2216. wpa_msg(wpa_s->parent, MSG_INFO,
  2217. P2P_EVENT_GROUP_FORMATION_FAILURE);
  2218. }
  2219. }
  2220. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  2221. struct wpa_scan_results *scan_res)
  2222. {
  2223. struct wpa_bss *bss;
  2224. int freq;
  2225. u8 iface_addr[ETH_ALEN];
  2226. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2227. if (wpa_s->global->p2p_disabled)
  2228. return;
  2229. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
  2230. scan_res ? (int) scan_res->num : -1);
  2231. if (scan_res)
  2232. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  2233. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  2234. wpa_s->pending_join_iface_addr);
  2235. if (freq < 0 &&
  2236. p2p_get_interface_addr(wpa_s->global->p2p,
  2237. wpa_s->pending_join_dev_addr,
  2238. iface_addr) == 0 &&
  2239. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  2240. {
  2241. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  2242. "address for join from " MACSTR " to " MACSTR
  2243. " based on newly discovered P2P peer entry",
  2244. MAC2STR(wpa_s->pending_join_iface_addr),
  2245. MAC2STR(iface_addr));
  2246. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  2247. ETH_ALEN);
  2248. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  2249. wpa_s->pending_join_iface_addr);
  2250. }
  2251. if (freq >= 0) {
  2252. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  2253. "from P2P peer table: %d MHz", freq);
  2254. }
  2255. bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
  2256. if (bss) {
  2257. freq = bss->freq;
  2258. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  2259. "from BSS table: %d MHz", freq);
  2260. }
  2261. if (freq > 0) {
  2262. u16 method;
  2263. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  2264. "prior to joining an existing group (GO " MACSTR
  2265. " freq=%u MHz)",
  2266. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  2267. wpa_s->pending_pd_before_join = 1;
  2268. switch (wpa_s->pending_join_wps_method) {
  2269. case WPS_PIN_LABEL:
  2270. case WPS_PIN_DISPLAY:
  2271. method = WPS_CONFIG_KEYPAD;
  2272. break;
  2273. case WPS_PIN_KEYPAD:
  2274. method = WPS_CONFIG_DISPLAY;
  2275. break;
  2276. case WPS_PBC:
  2277. method = WPS_CONFIG_PUSHBUTTON;
  2278. break;
  2279. default:
  2280. method = 0;
  2281. break;
  2282. }
  2283. if (p2p_prov_disc_req(wpa_s->global->p2p,
  2284. wpa_s->pending_join_dev_addr, method, 1)
  2285. < 0) {
  2286. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  2287. "Discovery Request before joining an "
  2288. "existing group");
  2289. wpa_s->pending_pd_before_join = 0;
  2290. goto start;
  2291. }
  2292. /*
  2293. * Actual join operation will be started from the Action frame
  2294. * TX status callback.
  2295. */
  2296. return;
  2297. }
  2298. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  2299. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2300. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  2301. wpas_p2p_check_join_scan_limit(wpa_s);
  2302. return;
  2303. start:
  2304. /* Start join operation immediately */
  2305. wpas_p2p_join_start(wpa_s);
  2306. }
  2307. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  2308. {
  2309. struct wpa_supplicant *wpa_s = eloop_ctx;
  2310. int ret;
  2311. struct wpa_driver_scan_params params;
  2312. struct wpabuf *wps_ie, *ies;
  2313. os_memset(&params, 0, sizeof(params));
  2314. /* P2P Wildcard SSID */
  2315. params.num_ssids = 1;
  2316. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  2317. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  2318. wpa_s->wps->dev.p2p = 1;
  2319. wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
  2320. WPS_REQ_ENROLLEE);
  2321. if (wps_ie == NULL) {
  2322. wpas_p2p_scan_res_join(wpa_s, NULL);
  2323. return;
  2324. }
  2325. ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
  2326. if (ies == NULL) {
  2327. wpabuf_free(wps_ie);
  2328. wpas_p2p_scan_res_join(wpa_s, NULL);
  2329. return;
  2330. }
  2331. wpabuf_put_buf(ies, wps_ie);
  2332. wpabuf_free(wps_ie);
  2333. p2p_scan_ie(wpa_s->global->p2p, ies);
  2334. params.extra_ies = wpabuf_head(ies);
  2335. params.extra_ies_len = wpabuf_len(ies);
  2336. /*
  2337. * Run a scan to update BSS table and start Provision Discovery once
  2338. * the new scan results become available.
  2339. */
  2340. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  2341. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  2342. ret = ieee80211_sta_req_scan(wpa_s, &params);
  2343. else
  2344. ret = wpa_drv_scan(wpa_s, &params);
  2345. wpabuf_free(ies);
  2346. if (ret) {
  2347. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  2348. "try again later");
  2349. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2350. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  2351. wpas_p2p_check_join_scan_limit(wpa_s);
  2352. }
  2353. }
  2354. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  2355. const u8 *dev_addr, enum p2p_wps_method wps_method)
  2356. {
  2357. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  2358. MACSTR " dev " MACSTR ")",
  2359. MAC2STR(iface_addr), MAC2STR(dev_addr));
  2360. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  2361. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  2362. wpa_s->pending_join_wps_method = wps_method;
  2363. /* Make sure we are not running find during connection establishment */
  2364. wpas_p2p_stop_find(wpa_s);
  2365. wpa_s->p2p_join_scan_count = 0;
  2366. wpas_p2p_join_scan(wpa_s, NULL);
  2367. return 0;
  2368. }
  2369. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  2370. {
  2371. struct wpa_supplicant *group;
  2372. struct p2p_go_neg_results res;
  2373. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  2374. if (group == NULL)
  2375. return -1;
  2376. if (group != wpa_s) {
  2377. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  2378. sizeof(group->p2p_pin));
  2379. group->p2p_wps_method = wpa_s->p2p_wps_method;
  2380. }
  2381. group->p2p_in_provisioning = 1;
  2382. os_memset(&res, 0, sizeof(res));
  2383. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  2384. ETH_ALEN);
  2385. res.wps_method = wpa_s->pending_join_wps_method;
  2386. wpas_start_wps_enrollee(group, &res);
  2387. /*
  2388. * Allow a longer timeout for join-a-running-group than normal 15
  2389. * second group formation timeout since the GO may not have authorized
  2390. * our connection yet.
  2391. */
  2392. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2393. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  2394. wpa_s, NULL);
  2395. return 0;
  2396. }
  2397. /**
  2398. * wpas_p2p_connect - Request P2P Group Formation to be started
  2399. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2400. * @peer_addr: Address of the peer P2P Device
  2401. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  2402. * @persistent_group: Whether to create a persistent group
  2403. * @join: Whether to join an existing group (as a client) instead of starting
  2404. * Group Owner negotiation; @peer_addr is BSSID in that case
  2405. * @auth: Whether to only authorize the connection instead of doing that and
  2406. * initiating Group Owner negotiation
  2407. * @go_intent: GO Intent or -1 to use default
  2408. * @freq: Frequency for the group or 0 for auto-selection
  2409. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  2410. * failure, -2 on failure due to channel not currently available,
  2411. * -3 if forced channel is not supported
  2412. */
  2413. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2414. const char *pin, enum p2p_wps_method wps_method,
  2415. int persistent_group, int join, int auth, int go_intent,
  2416. int freq)
  2417. {
  2418. int force_freq = 0, oper_freq = 0;
  2419. u8 bssid[ETH_ALEN];
  2420. int ret = 0;
  2421. enum wpa_driver_if_type iftype;
  2422. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2423. return -1;
  2424. if (go_intent < 0)
  2425. go_intent = wpa_s->conf->p2p_go_intent;
  2426. if (!auth)
  2427. wpa_s->p2p_long_listen = 0;
  2428. wpa_s->p2p_wps_method = wps_method;
  2429. if (pin)
  2430. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  2431. else if (wps_method == WPS_PIN_DISPLAY) {
  2432. ret = wps_generate_pin();
  2433. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  2434. ret);
  2435. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  2436. wpa_s->p2p_pin);
  2437. } else
  2438. wpa_s->p2p_pin[0] = '\0';
  2439. if (join) {
  2440. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  2441. if (auth) {
  2442. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  2443. "connect a running group from " MACSTR,
  2444. MAC2STR(peer_addr));
  2445. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  2446. return ret;
  2447. }
  2448. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  2449. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  2450. iface_addr) < 0) {
  2451. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  2452. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  2453. dev_addr);
  2454. }
  2455. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
  2456. 0)
  2457. return -1;
  2458. return ret;
  2459. }
  2460. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  2461. wpa_s->assoc_freq)
  2462. oper_freq = wpa_s->assoc_freq;
  2463. else {
  2464. oper_freq = wpa_drv_shared_freq(wpa_s);
  2465. if (oper_freq < 0)
  2466. oper_freq = 0;
  2467. }
  2468. if (freq > 0) {
  2469. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2470. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  2471. "(%u MHz) is not supported for P2P uses",
  2472. freq);
  2473. return -3;
  2474. }
  2475. if (oper_freq > 0 && freq != oper_freq &&
  2476. !(wpa_s->drv_flags &
  2477. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  2478. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
  2479. "on %u MHz while connected on another "
  2480. "channel (%u MHz)", freq, oper_freq);
  2481. return -2;
  2482. }
  2483. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  2484. "requested channel (%u MHz)", freq);
  2485. force_freq = freq;
  2486. } else if (oper_freq > 0 &&
  2487. !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
  2488. if (!(wpa_s->drv_flags &
  2489. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  2490. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
  2491. "while connected on non-P2P supported "
  2492. "channel (%u MHz)", oper_freq);
  2493. return -2;
  2494. }
  2495. wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
  2496. "(%u MHz) not available for P2P - try to use "
  2497. "another channel", oper_freq);
  2498. force_freq = 0;
  2499. } else if (oper_freq > 0) {
  2500. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  2501. "channel we are already using (%u MHz) on another "
  2502. "interface", oper_freq);
  2503. force_freq = oper_freq;
  2504. }
  2505. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  2506. if (!wpa_s->create_p2p_iface) {
  2507. if (auth) {
  2508. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  2509. go_intent, wpa_s->own_addr,
  2510. force_freq, persistent_group)
  2511. < 0)
  2512. return -1;
  2513. return ret;
  2514. }
  2515. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  2516. go_intent, wpa_s->own_addr,
  2517. force_freq, persistent_group) < 0)
  2518. return -1;
  2519. return ret;
  2520. }
  2521. /* Prepare to add a new interface for the group */
  2522. iftype = WPA_IF_P2P_GROUP;
  2523. if (join)
  2524. iftype = WPA_IF_P2P_CLIENT;
  2525. else if (go_intent == 15)
  2526. iftype = WPA_IF_P2P_GO;
  2527. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  2528. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2529. "interface for the group");
  2530. return -1;
  2531. }
  2532. if (auth) {
  2533. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  2534. go_intent,
  2535. wpa_s->pending_interface_addr,
  2536. force_freq, persistent_group) < 0)
  2537. return -1;
  2538. return ret;
  2539. }
  2540. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
  2541. wpa_s->pending_interface_addr,
  2542. force_freq, persistent_group) < 0) {
  2543. wpas_p2p_remove_pending_group_interface(wpa_s);
  2544. return -1;
  2545. }
  2546. return ret;
  2547. }
  2548. /**
  2549. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  2550. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2551. * @freq: Frequency of the channel in MHz
  2552. * @duration: Duration of the stay on the channel in milliseconds
  2553. *
  2554. * This callback is called when the driver indicates that it has started the
  2555. * requested remain-on-channel duration.
  2556. */
  2557. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  2558. unsigned int freq, unsigned int duration)
  2559. {
  2560. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2561. return;
  2562. wpa_s->roc_waiting_drv_freq = 0;
  2563. wpa_s->off_channel_freq = freq;
  2564. wpas_send_action_cb(wpa_s, NULL);
  2565. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  2566. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  2567. wpa_s->pending_listen_duration);
  2568. wpa_s->pending_listen_freq = 0;
  2569. }
  2570. }
  2571. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  2572. unsigned int timeout)
  2573. {
  2574. /* Limit maximum Listen state time based on driver limitation. */
  2575. if (timeout > wpa_s->max_remain_on_chan)
  2576. timeout = wpa_s->max_remain_on_chan;
  2577. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2578. return wpa_drv_p2p_listen(wpa_s, timeout);
  2579. return p2p_listen(wpa_s->global->p2p, timeout);
  2580. }
  2581. /**
  2582. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  2583. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2584. * @freq: Frequency of the channel in MHz
  2585. *
  2586. * This callback is called when the driver indicates that a remain-on-channel
  2587. * operation has been completed, i.e., the duration on the requested channel
  2588. * has timed out.
  2589. */
  2590. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  2591. unsigned int freq)
  2592. {
  2593. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  2594. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  2595. wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
  2596. wpa_s->off_channel_freq = 0;
  2597. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2598. return;
  2599. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  2600. return; /* P2P module started a new operation */
  2601. if (wpa_s->pending_action_tx)
  2602. return;
  2603. if (wpa_s->p2p_long_listen > 0)
  2604. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  2605. if (wpa_s->p2p_long_listen > 0) {
  2606. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  2607. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  2608. }
  2609. }
  2610. /**
  2611. * wpas_p2p_group_remove - Remove a P2P group
  2612. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2613. * @ifname: Network interface name of the group interface or "*" to remove all
  2614. * groups
  2615. * Returns: 0 on success, -1 on failure
  2616. *
  2617. * This function is used to remove a P2P group. This can be used to disconnect
  2618. * from a group in which the local end is a P2P Client or to end a P2P Group in
  2619. * case the local end is the Group Owner. If a virtual network interface was
  2620. * created for this group, that interface will be removed. Otherwise, only the
  2621. * configured P2P group network will be removed from the interface.
  2622. */
  2623. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  2624. {
  2625. struct wpa_global *global = wpa_s->global;
  2626. if (os_strcmp(ifname, "*") == 0) {
  2627. struct wpa_supplicant *prev;
  2628. wpa_s = global->ifaces;
  2629. while (wpa_s) {
  2630. prev = wpa_s;
  2631. wpa_s = wpa_s->next;
  2632. wpas_p2p_disconnect(prev);
  2633. }
  2634. return 0;
  2635. }
  2636. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2637. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2638. break;
  2639. }
  2640. return wpas_p2p_disconnect(wpa_s);
  2641. }
  2642. static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  2643. struct p2p_go_neg_results *params,
  2644. int freq)
  2645. {
  2646. u8 bssid[ETH_ALEN];
  2647. int res;
  2648. os_memset(params, 0, sizeof(*params));
  2649. params->role_go = 1;
  2650. if (freq) {
  2651. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  2652. "frequency %d MHz", freq);
  2653. params->freq = freq;
  2654. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  2655. wpa_s->conf->p2p_oper_channel >= 1 &&
  2656. wpa_s->conf->p2p_oper_channel <= 11) {
  2657. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  2658. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  2659. "frequency %d MHz", params->freq);
  2660. } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
  2661. wpa_s->conf->p2p_oper_reg_class == 118) {
  2662. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  2663. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  2664. "frequency %d MHz", params->freq);
  2665. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2666. wpa_s->best_overall_freq > 0 &&
  2667. p2p_supported_freq(wpa_s->global->p2p,
  2668. wpa_s->best_overall_freq)) {
  2669. params->freq = wpa_s->best_overall_freq;
  2670. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  2671. "channel %d MHz", params->freq);
  2672. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2673. wpa_s->best_24_freq > 0 &&
  2674. p2p_supported_freq(wpa_s->global->p2p,
  2675. wpa_s->best_24_freq)) {
  2676. params->freq = wpa_s->best_24_freq;
  2677. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  2678. "channel %d MHz", params->freq);
  2679. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  2680. wpa_s->best_5_freq > 0 &&
  2681. p2p_supported_freq(wpa_s->global->p2p,
  2682. wpa_s->best_5_freq)) {
  2683. params->freq = wpa_s->best_5_freq;
  2684. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  2685. "channel %d MHz", params->freq);
  2686. } else {
  2687. params->freq = 2412;
  2688. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  2689. "known)", params->freq);
  2690. }
  2691. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  2692. wpa_s->assoc_freq && !freq) {
  2693. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  2694. "already using");
  2695. params->freq = wpa_s->assoc_freq;
  2696. }
  2697. res = wpa_drv_shared_freq(wpa_s);
  2698. if (res > 0 && !freq) {
  2699. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  2700. "already using on a shared interface");
  2701. params->freq = res;
  2702. }
  2703. }
  2704. static struct wpa_supplicant *
  2705. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  2706. int go)
  2707. {
  2708. struct wpa_supplicant *group_wpa_s;
  2709. if (!wpas_p2p_create_iface(wpa_s))
  2710. return wpa_s;
  2711. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  2712. WPA_IF_P2P_CLIENT) < 0)
  2713. return NULL;
  2714. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  2715. if (group_wpa_s == NULL) {
  2716. wpas_p2p_remove_pending_group_interface(wpa_s);
  2717. return NULL;
  2718. }
  2719. return group_wpa_s;
  2720. }
  2721. /**
  2722. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  2723. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2724. * @persistent_group: Whether to create a persistent group
  2725. * @freq: Frequency for the group or 0 to indicate no hardcoding
  2726. * Returns: 0 on success, -1 on failure
  2727. *
  2728. * This function creates a new P2P group with the local end as the Group Owner,
  2729. * i.e., without using Group Owner Negotiation.
  2730. */
  2731. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  2732. int freq)
  2733. {
  2734. struct p2p_go_neg_results params;
  2735. unsigned int r;
  2736. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2737. return -1;
  2738. if (freq == 2) {
  2739. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  2740. "band");
  2741. if (wpa_s->best_24_freq > 0 &&
  2742. p2p_supported_freq(wpa_s->global->p2p,
  2743. wpa_s->best_24_freq)) {
  2744. freq = wpa_s->best_24_freq;
  2745. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  2746. "channel: %d MHz", freq);
  2747. } else {
  2748. os_get_random((u8 *) &r, sizeof(r));
  2749. freq = 2412 + (r % 3) * 25;
  2750. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  2751. "channel: %d MHz", freq);
  2752. }
  2753. }
  2754. if (freq == 5) {
  2755. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  2756. "band");
  2757. if (wpa_s->best_5_freq > 0 &&
  2758. p2p_supported_freq(wpa_s->global->p2p,
  2759. wpa_s->best_5_freq)) {
  2760. freq = wpa_s->best_5_freq;
  2761. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  2762. "channel: %d MHz", freq);
  2763. } else {
  2764. os_get_random((u8 *) &r, sizeof(r));
  2765. freq = 5180 + (r % 4) * 20;
  2766. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2767. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  2768. "5 GHz channel for P2P group");
  2769. return -1;
  2770. }
  2771. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  2772. "channel: %d MHz", freq);
  2773. }
  2774. }
  2775. if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
  2776. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  2777. "(%u MHz) is not supported for P2P uses",
  2778. freq);
  2779. return -1;
  2780. }
  2781. wpas_p2p_init_go_params(wpa_s, &params, freq);
  2782. p2p_go_params(wpa_s->global->p2p, &params);
  2783. params.persistent_group = persistent_group;
  2784. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  2785. if (wpa_s == NULL)
  2786. return -1;
  2787. wpas_start_wps_go(wpa_s, &params, 0);
  2788. return 0;
  2789. }
  2790. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  2791. struct wpa_ssid *params, int addr_allocated)
  2792. {
  2793. struct wpa_ssid *ssid;
  2794. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  2795. if (wpa_s == NULL)
  2796. return -1;
  2797. wpa_supplicant_ap_deinit(wpa_s);
  2798. ssid = wpa_config_add_network(wpa_s->conf);
  2799. if (ssid == NULL)
  2800. return -1;
  2801. wpas_notify_network_added(wpa_s, ssid);
  2802. wpa_config_set_network_defaults(ssid);
  2803. ssid->temporary = 1;
  2804. ssid->proto = WPA_PROTO_RSN;
  2805. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  2806. ssid->group_cipher = WPA_CIPHER_CCMP;
  2807. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  2808. ssid->ssid = os_malloc(params->ssid_len);
  2809. if (ssid->ssid == NULL) {
  2810. wpas_notify_network_removed(wpa_s, ssid);
  2811. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2812. return -1;
  2813. }
  2814. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  2815. ssid->ssid_len = params->ssid_len;
  2816. ssid->p2p_group = 1;
  2817. ssid->export_keys = 1;
  2818. if (params->psk_set) {
  2819. os_memcpy(ssid->psk, params->psk, 32);
  2820. ssid->psk_set = 1;
  2821. }
  2822. if (params->passphrase)
  2823. ssid->passphrase = os_strdup(params->passphrase);
  2824. wpa_supplicant_select_network(wpa_s, ssid);
  2825. wpa_s->show_group_started = 1;
  2826. return 0;
  2827. }
  2828. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  2829. struct wpa_ssid *ssid, int addr_allocated,
  2830. int freq)
  2831. {
  2832. struct p2p_go_neg_results params;
  2833. int go = 0;
  2834. if (ssid->disabled != 2 || ssid->ssid == NULL)
  2835. return -1;
  2836. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  2837. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  2838. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  2839. "already running");
  2840. return 0;
  2841. }
  2842. /* Make sure we are not running find during connection establishment */
  2843. wpas_p2p_stop_find(wpa_s);
  2844. if (ssid->mode == WPAS_MODE_INFRA)
  2845. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  2846. if (ssid->mode != WPAS_MODE_P2P_GO)
  2847. return -1;
  2848. wpas_p2p_init_go_params(wpa_s, &params, freq);
  2849. params.role_go = 1;
  2850. if (ssid->passphrase == NULL ||
  2851. os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  2852. wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
  2853. "group");
  2854. return -1;
  2855. }
  2856. os_strlcpy(params.passphrase, ssid->passphrase,
  2857. sizeof(params.passphrase));
  2858. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  2859. params.ssid_len = ssid->ssid_len;
  2860. params.persistent_group = 1;
  2861. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  2862. if (wpa_s == NULL)
  2863. return -1;
  2864. wpas_start_wps_go(wpa_s, &params, 0);
  2865. return 0;
  2866. }
  2867. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  2868. struct wpabuf *proberesp_ies)
  2869. {
  2870. struct wpa_supplicant *wpa_s = ctx;
  2871. if (wpa_s->ap_iface) {
  2872. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  2873. if (beacon_ies) {
  2874. wpabuf_free(hapd->p2p_beacon_ie);
  2875. hapd->p2p_beacon_ie = beacon_ies;
  2876. }
  2877. wpabuf_free(hapd->p2p_probe_resp_ie);
  2878. hapd->p2p_probe_resp_ie = proberesp_ies;
  2879. } else {
  2880. wpabuf_free(beacon_ies);
  2881. wpabuf_free(proberesp_ies);
  2882. }
  2883. wpa_supplicant_ap_update_beacon(wpa_s);
  2884. }
  2885. static void wpas_p2p_idle_update(void *ctx, int idle)
  2886. {
  2887. struct wpa_supplicant *wpa_s = ctx;
  2888. if (!wpa_s->ap_iface)
  2889. return;
  2890. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  2891. if (idle)
  2892. wpas_p2p_set_group_idle_timeout(wpa_s);
  2893. else
  2894. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2895. }
  2896. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  2897. int persistent_group,
  2898. int group_formation)
  2899. {
  2900. struct p2p_group *group;
  2901. struct p2p_group_config *cfg;
  2902. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2903. return NULL;
  2904. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2905. return NULL;
  2906. cfg = os_zalloc(sizeof(*cfg));
  2907. if (cfg == NULL)
  2908. return NULL;
  2909. cfg->persistent_group = persistent_group;
  2910. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  2911. if (wpa_s->max_stations &&
  2912. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  2913. cfg->max_clients = wpa_s->max_stations;
  2914. else
  2915. cfg->max_clients = wpa_s->conf->max_num_sta;
  2916. cfg->cb_ctx = wpa_s;
  2917. cfg->ie_update = wpas_p2p_ie_update;
  2918. cfg->idle_update = wpas_p2p_idle_update;
  2919. group = p2p_group_init(wpa_s->global->p2p, cfg);
  2920. if (group == NULL)
  2921. os_free(cfg);
  2922. if (!group_formation)
  2923. p2p_group_notif_formation_done(group);
  2924. wpa_s->p2p_group = group;
  2925. return group;
  2926. }
  2927. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2928. int registrar)
  2929. {
  2930. if (!wpa_s->p2p_in_provisioning) {
  2931. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  2932. "provisioning not in progress");
  2933. return;
  2934. }
  2935. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  2936. NULL);
  2937. if (wpa_s->global->p2p)
  2938. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  2939. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2940. wpa_drv_wps_success_cb(wpa_s, peer_addr);
  2941. wpas_group_formation_completed(wpa_s, 1);
  2942. }
  2943. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  2944. const char *config_method)
  2945. {
  2946. u16 config_methods;
  2947. if (os_strcmp(config_method, "display") == 0)
  2948. config_methods = WPS_CONFIG_DISPLAY;
  2949. else if (os_strcmp(config_method, "keypad") == 0)
  2950. config_methods = WPS_CONFIG_KEYPAD;
  2951. else if (os_strcmp(config_method, "pbc") == 0 ||
  2952. os_strcmp(config_method, "pushbutton") == 0)
  2953. config_methods = WPS_CONFIG_PUSHBUTTON;
  2954. else
  2955. return -1;
  2956. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2957. return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
  2958. config_methods);
  2959. }
  2960. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  2961. return -1;
  2962. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  2963. config_methods, 0);
  2964. }
  2965. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  2966. char *end)
  2967. {
  2968. return p2p_scan_result_text(ies, ies_len, buf, end);
  2969. }
  2970. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  2971. {
  2972. if (!wpa_s->pending_action_tx)
  2973. return;
  2974. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  2975. "operation request");
  2976. wpabuf_free(wpa_s->pending_action_tx);
  2977. wpa_s->pending_action_tx = NULL;
  2978. }
  2979. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  2980. enum p2p_discovery_type type)
  2981. {
  2982. wpas_p2p_clear_pending_action_tx(wpa_s);
  2983. wpa_s->p2p_long_listen = 0;
  2984. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2985. return wpa_drv_p2p_find(wpa_s, timeout, type);
  2986. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  2987. return -1;
  2988. return p2p_find(wpa_s->global->p2p, timeout, type);
  2989. }
  2990. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  2991. {
  2992. wpas_p2p_clear_pending_action_tx(wpa_s);
  2993. wpa_s->p2p_long_listen = 0;
  2994. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2995. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2996. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2997. wpa_drv_p2p_stop_find(wpa_s);
  2998. return;
  2999. }
  3000. if (wpa_s->global->p2p)
  3001. p2p_stop_find(wpa_s->global->p2p);
  3002. wpas_p2p_remove_pending_group_interface(wpa_s);
  3003. }
  3004. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  3005. {
  3006. struct wpa_supplicant *wpa_s = eloop_ctx;
  3007. wpa_s->p2p_long_listen = 0;
  3008. }
  3009. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  3010. {
  3011. int res;
  3012. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3013. return -1;
  3014. wpas_p2p_clear_pending_action_tx(wpa_s);
  3015. if (timeout == 0) {
  3016. /*
  3017. * This is a request for unlimited Listen state. However, at
  3018. * least for now, this is mapped to a Listen state for one
  3019. * hour.
  3020. */
  3021. timeout = 3600;
  3022. }
  3023. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  3024. wpa_s->p2p_long_listen = 0;
  3025. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  3026. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  3027. wpa_s->p2p_long_listen = timeout * 1000;
  3028. eloop_register_timeout(timeout, 0,
  3029. wpas_p2p_long_listen_timeout,
  3030. wpa_s, NULL);
  3031. }
  3032. return res;
  3033. }
  3034. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  3035. u8 *buf, size_t len, int p2p_group)
  3036. {
  3037. struct wpabuf *p2p_ie;
  3038. int ret;
  3039. if (wpa_s->global->p2p_disabled)
  3040. return -1;
  3041. if (wpa_s->global->p2p == NULL)
  3042. return -1;
  3043. if (bss == NULL)
  3044. return -1;
  3045. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  3046. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  3047. p2p_group, p2p_ie);
  3048. wpabuf_free(p2p_ie);
  3049. return ret;
  3050. }
  3051. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  3052. const u8 *ie, size_t ie_len)
  3053. {
  3054. if (wpa_s->global->p2p_disabled)
  3055. return 0;
  3056. if (wpa_s->global->p2p == NULL)
  3057. return 0;
  3058. return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
  3059. }
  3060. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  3061. const u8 *sa, const u8 *bssid,
  3062. u8 category, const u8 *data, size_t len, int freq)
  3063. {
  3064. if (wpa_s->global->p2p_disabled)
  3065. return;
  3066. if (wpa_s->global->p2p == NULL)
  3067. return;
  3068. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  3069. freq);
  3070. }
  3071. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  3072. {
  3073. if (wpa_s->global->p2p_disabled)
  3074. return;
  3075. if (wpa_s->global->p2p == NULL)
  3076. return;
  3077. p2p_scan_ie(wpa_s->global->p2p, ies);
  3078. }
  3079. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  3080. {
  3081. p2p_group_deinit(wpa_s->p2p_group);
  3082. wpa_s->p2p_group = NULL;
  3083. }
  3084. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  3085. {
  3086. wpa_s->p2p_long_listen = 0;
  3087. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3088. return wpa_drv_p2p_reject(wpa_s, addr);
  3089. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3090. return -1;
  3091. return p2p_reject(wpa_s->global->p2p, addr);
  3092. }
  3093. /* Invite to reinvoke a persistent group */
  3094. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3095. struct wpa_ssid *ssid, const u8 *go_dev_addr)
  3096. {
  3097. enum p2p_invite_role role;
  3098. u8 *bssid = NULL;
  3099. if (ssid->mode == WPAS_MODE_P2P_GO) {
  3100. role = P2P_INVITE_ROLE_GO;
  3101. if (peer_addr == NULL) {
  3102. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  3103. "address in invitation command");
  3104. return -1;
  3105. }
  3106. if (wpas_p2p_create_iface(wpa_s)) {
  3107. if (wpas_p2p_add_group_interface(wpa_s,
  3108. WPA_IF_P2P_GO) < 0) {
  3109. wpa_printf(MSG_ERROR, "P2P: Failed to "
  3110. "allocate a new interface for the "
  3111. "group");
  3112. return -1;
  3113. }
  3114. bssid = wpa_s->pending_interface_addr;
  3115. } else
  3116. bssid = wpa_s->own_addr;
  3117. } else {
  3118. role = P2P_INVITE_ROLE_CLIENT;
  3119. peer_addr = ssid->bssid;
  3120. }
  3121. wpa_s->pending_invite_ssid_id = ssid->id;
  3122. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3123. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  3124. ssid->ssid, ssid->ssid_len,
  3125. go_dev_addr, 1);
  3126. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3127. return -1;
  3128. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  3129. ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
  3130. }
  3131. /* Invite to join an active group */
  3132. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  3133. const u8 *peer_addr, const u8 *go_dev_addr)
  3134. {
  3135. struct wpa_global *global = wpa_s->global;
  3136. enum p2p_invite_role role;
  3137. u8 *bssid = NULL;
  3138. struct wpa_ssid *ssid;
  3139. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3140. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3141. break;
  3142. }
  3143. if (wpa_s == NULL) {
  3144. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  3145. return -1;
  3146. }
  3147. ssid = wpa_s->current_ssid;
  3148. if (ssid == NULL) {
  3149. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  3150. "invitation");
  3151. return -1;
  3152. }
  3153. if (ssid->mode == WPAS_MODE_P2P_GO) {
  3154. role = P2P_INVITE_ROLE_ACTIVE_GO;
  3155. bssid = wpa_s->own_addr;
  3156. if (go_dev_addr == NULL)
  3157. go_dev_addr = wpa_s->parent->own_addr;
  3158. } else {
  3159. role = P2P_INVITE_ROLE_CLIENT;
  3160. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  3161. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  3162. "invite to current group");
  3163. return -1;
  3164. }
  3165. bssid = wpa_s->bssid;
  3166. if (go_dev_addr == NULL &&
  3167. !is_zero_ether_addr(wpa_s->go_dev_addr))
  3168. go_dev_addr = wpa_s->go_dev_addr;
  3169. }
  3170. wpa_s->parent->pending_invite_ssid_id = -1;
  3171. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3172. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  3173. ssid->ssid, ssid->ssid_len,
  3174. go_dev_addr, 0);
  3175. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3176. return -1;
  3177. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  3178. ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
  3179. go_dev_addr, 0);
  3180. }
  3181. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  3182. {
  3183. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3184. const char *ssid_txt;
  3185. u8 go_dev_addr[ETH_ALEN];
  3186. int persistent;
  3187. if (!wpa_s->show_group_started || !ssid)
  3188. return;
  3189. wpa_s->show_group_started = 0;
  3190. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  3191. os_memset(go_dev_addr, 0, ETH_ALEN);
  3192. if (ssid->bssid_set)
  3193. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  3194. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  3195. ssid->ssid_len);
  3196. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  3197. if (wpa_s->global->p2p_group_formation == wpa_s)
  3198. wpa_s->global->p2p_group_formation = NULL;
  3199. if (ssid->passphrase == NULL && ssid->psk_set) {
  3200. char psk[65];
  3201. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  3202. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  3203. "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  3204. MACSTR "%s",
  3205. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  3206. MAC2STR(go_dev_addr),
  3207. persistent ? " [PERSISTENT]" : "");
  3208. } else {
  3209. wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  3210. "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
  3211. "go_dev_addr=" MACSTR "%s",
  3212. wpa_s->ifname, ssid_txt, ssid->frequency,
  3213. ssid->passphrase ? ssid->passphrase : "",
  3214. MAC2STR(go_dev_addr),
  3215. persistent ? " [PERSISTENT]" : "");
  3216. }
  3217. if (persistent)
  3218. wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
  3219. go_dev_addr);
  3220. }
  3221. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  3222. u32 interval1, u32 duration2, u32 interval2)
  3223. {
  3224. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3225. return -1;
  3226. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3227. return -1;
  3228. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  3229. wpa_s->current_ssid == NULL ||
  3230. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  3231. return -1;
  3232. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  3233. wpa_s->own_addr, wpa_s->assoc_freq,
  3234. duration1, interval1, duration2, interval2);
  3235. }
  3236. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  3237. unsigned int interval)
  3238. {
  3239. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3240. return -1;
  3241. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3242. return -1;
  3243. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  3244. }
  3245. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  3246. {
  3247. struct wpa_supplicant *wpa_s = eloop_ctx;
  3248. if (wpa_s->conf->p2p_group_idle == 0) {
  3249. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  3250. "disabled");
  3251. return;
  3252. }
  3253. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  3254. "group");
  3255. wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
  3256. wpas_p2p_group_delete(wpa_s);
  3257. }
  3258. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  3259. {
  3260. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3261. if (wpa_s->conf->p2p_group_idle == 0)
  3262. return;
  3263. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  3264. return;
  3265. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  3266. wpa_s->conf->p2p_group_idle);
  3267. eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
  3268. wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3269. }
  3270. void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  3271. u16 reason_code, const u8 *ie, size_t ie_len)
  3272. {
  3273. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3274. return;
  3275. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3276. return;
  3277. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
  3278. }
  3279. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  3280. u16 reason_code, const u8 *ie, size_t ie_len)
  3281. {
  3282. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3283. return;
  3284. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3285. return;
  3286. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
  3287. }
  3288. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  3289. {
  3290. struct p2p_data *p2p = wpa_s->global->p2p;
  3291. if (p2p == NULL)
  3292. return;
  3293. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  3294. return;
  3295. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  3296. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  3297. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
  3298. u8 pri_dev_type[8];
  3299. if (wpa_s->conf->device_type) {
  3300. if (wps_dev_type_str2bin(wpa_s->conf->device_type,
  3301. pri_dev_type) < 0) {
  3302. wpa_printf(MSG_ERROR, "P2P: Invalid "
  3303. "device_type");
  3304. } else
  3305. p2p_set_pri_dev_type(p2p, pri_dev_type);
  3306. }
  3307. }
  3308. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
  3309. u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
  3310. size_t num = 0;
  3311. int i;
  3312. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
  3313. if (wpa_s->conf->sec_device_type[i] == NULL)
  3314. continue;
  3315. if (wps_dev_type_str2bin(
  3316. wpa_s->conf->sec_device_type[i],
  3317. sec_dev_type[num]) < 0) {
  3318. wpa_printf(MSG_ERROR, "P2P: Invalid "
  3319. "sec_device_type");
  3320. continue;
  3321. }
  3322. num++;
  3323. if (num == P2P_SEC_DEVICE_TYPES)
  3324. break;
  3325. }
  3326. p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
  3327. }
  3328. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  3329. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3330. char country[3];
  3331. country[0] = wpa_s->conf->country[0];
  3332. country[1] = wpa_s->conf->country[1];
  3333. country[2] = 0x04;
  3334. p2p_set_country(p2p, country);
  3335. }
  3336. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  3337. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  3338. wpa_s->conf->p2p_ssid_postfix ?
  3339. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  3340. 0);
  3341. }
  3342. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  3343. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  3344. }
  3345. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  3346. int duration)
  3347. {
  3348. if (!wpa_s->ap_iface)
  3349. return -1;
  3350. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  3351. duration);
  3352. }
  3353. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  3354. {
  3355. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3356. return -1;
  3357. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3358. return -1;
  3359. wpa_s->global->cross_connection = enabled;
  3360. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  3361. if (!enabled) {
  3362. struct wpa_supplicant *iface;
  3363. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  3364. {
  3365. if (iface->cross_connect_enabled == 0)
  3366. continue;
  3367. iface->cross_connect_enabled = 0;
  3368. iface->cross_connect_in_use = 0;
  3369. wpa_msg(iface->parent, MSG_INFO,
  3370. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  3371. iface->ifname, iface->cross_connect_uplink);
  3372. }
  3373. }
  3374. return 0;
  3375. }
  3376. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  3377. {
  3378. struct wpa_supplicant *iface;
  3379. if (!uplink->global->cross_connection)
  3380. return;
  3381. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  3382. if (!iface->cross_connect_enabled)
  3383. continue;
  3384. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  3385. 0)
  3386. continue;
  3387. if (iface->ap_iface == NULL)
  3388. continue;
  3389. if (iface->cross_connect_in_use)
  3390. continue;
  3391. iface->cross_connect_in_use = 1;
  3392. wpa_msg(iface->parent, MSG_INFO,
  3393. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  3394. iface->ifname, iface->cross_connect_uplink);
  3395. }
  3396. }
  3397. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  3398. {
  3399. struct wpa_supplicant *iface;
  3400. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  3401. if (!iface->cross_connect_enabled)
  3402. continue;
  3403. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  3404. 0)
  3405. continue;
  3406. if (!iface->cross_connect_in_use)
  3407. continue;
  3408. wpa_msg(iface->parent, MSG_INFO,
  3409. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  3410. iface->ifname, iface->cross_connect_uplink);
  3411. iface->cross_connect_in_use = 0;
  3412. }
  3413. }
  3414. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  3415. {
  3416. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  3417. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  3418. wpa_s->cross_connect_disallowed)
  3419. wpas_p2p_disable_cross_connect(wpa_s);
  3420. else
  3421. wpas_p2p_enable_cross_connect(wpa_s);
  3422. if (!wpa_s->ap_iface)
  3423. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3424. }
  3425. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  3426. {
  3427. wpas_p2p_disable_cross_connect(wpa_s);
  3428. if (!wpa_s->ap_iface)
  3429. wpas_p2p_set_group_idle_timeout(wpa_s);
  3430. }
  3431. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  3432. {
  3433. struct wpa_supplicant *iface;
  3434. if (!wpa_s->global->cross_connection)
  3435. return;
  3436. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  3437. if (iface == wpa_s)
  3438. continue;
  3439. if (iface->drv_flags &
  3440. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  3441. continue;
  3442. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  3443. continue;
  3444. wpa_s->cross_connect_enabled = 1;
  3445. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  3446. sizeof(wpa_s->cross_connect_uplink));
  3447. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  3448. "%s to %s whenever uplink is available",
  3449. wpa_s->ifname, wpa_s->cross_connect_uplink);
  3450. if (iface->ap_iface || iface->current_ssid == NULL ||
  3451. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  3452. iface->cross_connect_disallowed ||
  3453. iface->wpa_state != WPA_COMPLETED)
  3454. break;
  3455. wpa_s->cross_connect_in_use = 1;
  3456. wpa_msg(wpa_s->parent, MSG_INFO,
  3457. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  3458. wpa_s->ifname, wpa_s->cross_connect_uplink);
  3459. break;
  3460. }
  3461. }
  3462. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  3463. {
  3464. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  3465. !wpa_s->p2p_in_provisioning)
  3466. return 0; /* not P2P client operation */
  3467. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  3468. "session overlap");
  3469. if (wpa_s != wpa_s->parent)
  3470. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  3471. if (wpa_s->global->p2p)
  3472. p2p_group_formation_failed(wpa_s->global->p2p);
  3473. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  3474. wpa_s->parent, NULL);
  3475. wpas_group_formation_completed(wpa_s, 0);
  3476. return 1;
  3477. }
  3478. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  3479. {
  3480. struct p2p_channels chan;
  3481. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  3482. return;
  3483. os_memset(&chan, 0, sizeof(chan));
  3484. if (wpas_p2p_setup_channels(wpa_s, &chan)) {
  3485. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  3486. "channel list");
  3487. return;
  3488. }
  3489. p2p_update_channel_list(wpa_s->global->p2p, &chan);
  3490. }
  3491. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  3492. {
  3493. struct wpa_global *global = wpa_s->global;
  3494. int found = 0;
  3495. const u8 *peer;
  3496. if (global->p2p == NULL)
  3497. return -1;
  3498. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  3499. if (wpa_s->pending_interface_name[0] &&
  3500. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  3501. found = 1;
  3502. peer = p2p_get_go_neg_peer(global->p2p);
  3503. if (peer) {
  3504. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  3505. MACSTR, MAC2STR(peer));
  3506. p2p_unauthorize(global->p2p, peer);
  3507. }
  3508. wpas_p2p_stop_find(wpa_s);
  3509. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3510. if (wpa_s == global->p2p_group_formation &&
  3511. (wpa_s->p2p_in_provisioning ||
  3512. wpa_s->parent->pending_interface_type ==
  3513. WPA_IF_P2P_CLIENT)) {
  3514. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  3515. "formation found - cancelling",
  3516. wpa_s->ifname);
  3517. found = 1;
  3518. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  3519. wpa_s->parent, NULL);
  3520. wpas_p2p_group_delete(wpa_s);
  3521. break;
  3522. }
  3523. }
  3524. if (!found) {
  3525. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  3526. return -1;
  3527. }
  3528. return 0;
  3529. }
  3530. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  3531. {
  3532. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  3533. return;
  3534. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  3535. "being available anymore");
  3536. wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
  3537. wpas_p2p_group_delete(wpa_s);
  3538. }
  3539. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  3540. int freq_24, int freq_5, int freq_overall)
  3541. {
  3542. struct p2p_data *p2p = wpa_s->global->p2p;
  3543. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  3544. return;
  3545. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  3546. }
  3547. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  3548. {
  3549. u8 peer[ETH_ALEN];
  3550. struct p2p_data *p2p = wpa_s->global->p2p;
  3551. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  3552. return -1;
  3553. if (hwaddr_aton(addr, peer))
  3554. return -1;
  3555. return p2p_unauthorize(p2p, peer);
  3556. }
  3557. /**
  3558. * wpas_p2p_disconnect - Disconnect from a P2P Group
  3559. * @wpa_s: Pointer to wpa_supplicant data
  3560. * Returns: 0 on success, -1 on failure
  3561. *
  3562. * This can be used to disconnect from a group in which the local end is a P2P
  3563. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  3564. * virtual network interface was created for this group, that interface will be
  3565. * removed. Otherwise, only the configured P2P group network will be removed
  3566. * from the interface.
  3567. */
  3568. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  3569. {
  3570. if (wpa_s == NULL)
  3571. return -1;
  3572. wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
  3573. wpas_p2p_group_delete(wpa_s);
  3574. return 0;
  3575. }