p2p_supplicant.c 150 KB

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