p2p_supplicant.c 135 KB

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