p2p_supplicant.c 116 KB

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