p2p.c 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544
  1. /*
  2. * Wi-Fi Direct - P2P module
  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/defs.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/ieee802_11_common.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "crypto/sha256.h"
  16. #include "crypto/crypto.h"
  17. #include "wps/wps_i.h"
  18. #include "p2p_i.h"
  19. #include "p2p.h"
  20. static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
  21. static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
  22. static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
  23. const u8 *sa, const u8 *data, size_t len,
  24. int rx_freq);
  25. static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
  26. const u8 *sa, const u8 *data,
  27. size_t len);
  28. static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  29. static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
  30. /*
  31. * p2p_scan recovery timeout
  32. *
  33. * Many drivers are using 30 second timeout on scan results. Allow a bit larger
  34. * timeout for this to avoid hitting P2P timeout unnecessarily.
  35. */
  36. #define P2P_SCAN_TIMEOUT 35
  37. /**
  38. * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
  39. * entries will be removed
  40. */
  41. #ifndef P2P_PEER_EXPIRATION_AGE
  42. #define P2P_PEER_EXPIRATION_AGE 60
  43. #endif /* P2P_PEER_EXPIRATION_AGE */
  44. void p2p_expire_peers(struct p2p_data *p2p)
  45. {
  46. struct p2p_device *dev, *n;
  47. struct os_reltime now;
  48. size_t i;
  49. os_get_reltime(&now);
  50. dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
  51. if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
  52. continue;
  53. if (dev == p2p->go_neg_peer) {
  54. /*
  55. * GO Negotiation is in progress with the peer, so
  56. * don't expire the peer entry until GO Negotiation
  57. * fails or times out.
  58. */
  59. continue;
  60. }
  61. if (p2p->cfg->go_connected &&
  62. p2p->cfg->go_connected(p2p->cfg->cb_ctx,
  63. dev->info.p2p_device_addr)) {
  64. /*
  65. * We are connected as a client to a group in which the
  66. * peer is the GO, so do not expire the peer entry.
  67. */
  68. os_get_reltime(&dev->last_seen);
  69. continue;
  70. }
  71. for (i = 0; i < p2p->num_groups; i++) {
  72. if (p2p_group_is_client_connected(
  73. p2p->groups[i], dev->info.p2p_device_addr))
  74. break;
  75. }
  76. if (i < p2p->num_groups) {
  77. /*
  78. * The peer is connected as a client in a group where
  79. * we are the GO, so do not expire the peer entry.
  80. */
  81. os_get_reltime(&dev->last_seen);
  82. continue;
  83. }
  84. p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
  85. MAC2STR(dev->info.p2p_device_addr));
  86. dl_list_del(&dev->list);
  87. p2p_device_free(p2p, dev);
  88. }
  89. }
  90. static const char * p2p_state_txt(int state)
  91. {
  92. switch (state) {
  93. case P2P_IDLE:
  94. return "IDLE";
  95. case P2P_SEARCH:
  96. return "SEARCH";
  97. case P2P_CONNECT:
  98. return "CONNECT";
  99. case P2P_CONNECT_LISTEN:
  100. return "CONNECT_LISTEN";
  101. case P2P_GO_NEG:
  102. return "GO_NEG";
  103. case P2P_LISTEN_ONLY:
  104. return "LISTEN_ONLY";
  105. case P2P_WAIT_PEER_CONNECT:
  106. return "WAIT_PEER_CONNECT";
  107. case P2P_WAIT_PEER_IDLE:
  108. return "WAIT_PEER_IDLE";
  109. case P2P_SD_DURING_FIND:
  110. return "SD_DURING_FIND";
  111. case P2P_PROVISIONING:
  112. return "PROVISIONING";
  113. case P2P_PD_DURING_FIND:
  114. return "PD_DURING_FIND";
  115. case P2P_INVITE:
  116. return "INVITE";
  117. case P2P_INVITE_LISTEN:
  118. return "INVITE_LISTEN";
  119. default:
  120. return "?";
  121. }
  122. }
  123. const char * p2p_get_state_txt(struct p2p_data *p2p)
  124. {
  125. return p2p_state_txt(p2p->state);
  126. }
  127. struct p2ps_advertisement * p2p_get_p2ps_adv_list(struct p2p_data *p2p)
  128. {
  129. return p2p ? p2p->p2ps_adv_list : NULL;
  130. }
  131. void p2p_set_intended_addr(struct p2p_data *p2p, const u8 *intended_addr)
  132. {
  133. if (p2p && intended_addr)
  134. os_memcpy(p2p->intended_addr, intended_addr, ETH_ALEN);
  135. }
  136. u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
  137. {
  138. struct p2p_device *dev = NULL;
  139. if (!addr || !p2p)
  140. return 0;
  141. dev = p2p_get_device(p2p, addr);
  142. if (dev)
  143. return dev->wps_prov_info;
  144. else
  145. return 0;
  146. }
  147. void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
  148. {
  149. struct p2p_device *dev = NULL;
  150. if (!addr || !p2p)
  151. return;
  152. dev = p2p_get_device(p2p, addr);
  153. if (dev)
  154. dev->wps_prov_info = 0;
  155. }
  156. void p2p_set_state(struct p2p_data *p2p, int new_state)
  157. {
  158. p2p_dbg(p2p, "State %s -> %s",
  159. p2p_state_txt(p2p->state), p2p_state_txt(new_state));
  160. p2p->state = new_state;
  161. if (new_state == P2P_IDLE && p2p->pending_channel) {
  162. p2p_dbg(p2p, "Apply change in listen channel");
  163. p2p->cfg->reg_class = p2p->pending_reg_class;
  164. p2p->cfg->channel = p2p->pending_channel;
  165. p2p->pending_reg_class = 0;
  166. p2p->pending_channel = 0;
  167. }
  168. }
  169. void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
  170. {
  171. p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
  172. p2p_state_txt(p2p->state), sec, usec);
  173. eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
  174. eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
  175. }
  176. void p2p_clear_timeout(struct p2p_data *p2p)
  177. {
  178. p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
  179. eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
  180. }
  181. void p2p_go_neg_failed(struct p2p_data *p2p, int status)
  182. {
  183. struct p2p_go_neg_results res;
  184. struct p2p_device *peer = p2p->go_neg_peer;
  185. if (!peer)
  186. return;
  187. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  188. if (p2p->state != P2P_SEARCH) {
  189. /*
  190. * Clear timeouts related to GO Negotiation if no new p2p_find
  191. * has been started.
  192. */
  193. p2p_clear_timeout(p2p);
  194. p2p_set_state(p2p, P2P_IDLE);
  195. }
  196. peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
  197. peer->wps_method = WPS_NOT_READY;
  198. peer->oob_pw_id = 0;
  199. wpabuf_free(peer->go_neg_conf);
  200. peer->go_neg_conf = NULL;
  201. p2p->go_neg_peer = NULL;
  202. os_memset(&res, 0, sizeof(res));
  203. res.status = status;
  204. os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
  205. os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
  206. p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
  207. }
  208. static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
  209. {
  210. unsigned int r, tu;
  211. int freq;
  212. struct wpabuf *ies;
  213. p2p_dbg(p2p, "Starting short listen state (state=%s)",
  214. p2p_state_txt(p2p->state));
  215. if (p2p->pending_listen_freq) {
  216. /* We have a pending p2p_listen request */
  217. p2p_dbg(p2p, "p2p_listen command pending already");
  218. return;
  219. }
  220. freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
  221. if (freq < 0) {
  222. p2p_dbg(p2p, "Unknown regulatory class/channel");
  223. return;
  224. }
  225. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  226. r = 0;
  227. tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
  228. p2p->min_disc_int) * 100;
  229. if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
  230. tu = p2p->max_disc_tu;
  231. if (!dev_disc && tu < 100)
  232. tu = 100; /* Need to wait in non-device discovery use cases */
  233. if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
  234. tu = p2p->cfg->max_listen * 1000 / 1024;
  235. if (tu == 0) {
  236. p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
  237. p2p_set_timeout(p2p, 0, 0);
  238. return;
  239. }
  240. ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
  241. if (ies == NULL)
  242. return;
  243. p2p->pending_listen_freq = freq;
  244. p2p->pending_listen_sec = 0;
  245. p2p->pending_listen_usec = 1024 * tu;
  246. if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
  247. ies) < 0) {
  248. p2p_dbg(p2p, "Failed to start listen mode");
  249. p2p->pending_listen_freq = 0;
  250. }
  251. wpabuf_free(ies);
  252. }
  253. int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
  254. {
  255. int freq;
  256. struct wpabuf *ies;
  257. p2p_dbg(p2p, "Going to listen(only) state");
  258. if (p2p->pending_listen_freq) {
  259. /* We have a pending p2p_listen request */
  260. p2p_dbg(p2p, "p2p_listen command pending already");
  261. return -1;
  262. }
  263. freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
  264. if (freq < 0) {
  265. p2p_dbg(p2p, "Unknown regulatory class/channel");
  266. return -1;
  267. }
  268. p2p->pending_listen_sec = timeout / 1000;
  269. p2p->pending_listen_usec = (timeout % 1000) * 1000;
  270. if (p2p->p2p_scan_running) {
  271. if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
  272. p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
  273. return 0;
  274. }
  275. p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
  276. p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
  277. return 0;
  278. }
  279. ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
  280. if (ies == NULL)
  281. return -1;
  282. p2p->pending_listen_freq = freq;
  283. if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
  284. p2p_dbg(p2p, "Failed to start listen mode");
  285. p2p->pending_listen_freq = 0;
  286. wpabuf_free(ies);
  287. return -1;
  288. }
  289. wpabuf_free(ies);
  290. p2p_set_state(p2p, P2P_LISTEN_ONLY);
  291. return 0;
  292. }
  293. static void p2p_device_clear_reported(struct p2p_data *p2p)
  294. {
  295. struct p2p_device *dev;
  296. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  297. dev->flags &= ~P2P_DEV_REPORTED;
  298. dev->sd_reqs = 0;
  299. }
  300. }
  301. /**
  302. * p2p_get_device - Fetch a peer entry
  303. * @p2p: P2P module context from p2p_init()
  304. * @addr: P2P Device Address of the peer
  305. * Returns: Pointer to the device entry or %NULL if not found
  306. */
  307. struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
  308. {
  309. struct p2p_device *dev;
  310. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  311. if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
  312. return dev;
  313. }
  314. return NULL;
  315. }
  316. /**
  317. * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
  318. * @p2p: P2P module context from p2p_init()
  319. * @addr: P2P Interface Address of the peer
  320. * Returns: Pointer to the device entry or %NULL if not found
  321. */
  322. struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
  323. const u8 *addr)
  324. {
  325. struct p2p_device *dev;
  326. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  327. if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
  328. return dev;
  329. }
  330. return NULL;
  331. }
  332. /**
  333. * p2p_create_device - Create a peer entry
  334. * @p2p: P2P module context from p2p_init()
  335. * @addr: P2P Device Address of the peer
  336. * Returns: Pointer to the device entry or %NULL on failure
  337. *
  338. * If there is already an entry for the peer, it will be returned instead of
  339. * creating a new one.
  340. */
  341. static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
  342. const u8 *addr)
  343. {
  344. struct p2p_device *dev, *oldest = NULL;
  345. size_t count = 0;
  346. dev = p2p_get_device(p2p, addr);
  347. if (dev)
  348. return dev;
  349. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  350. count++;
  351. if (oldest == NULL ||
  352. os_reltime_before(&dev->last_seen, &oldest->last_seen))
  353. oldest = dev;
  354. }
  355. if (count + 1 > p2p->cfg->max_peers && oldest) {
  356. p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer");
  357. dl_list_del(&oldest->list);
  358. p2p_device_free(p2p, oldest);
  359. }
  360. dev = os_zalloc(sizeof(*dev));
  361. if (dev == NULL)
  362. return NULL;
  363. dl_list_add(&p2p->devices, &dev->list);
  364. os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
  365. return dev;
  366. }
  367. static void p2p_copy_client_info(struct p2p_device *dev,
  368. struct p2p_client_info *cli)
  369. {
  370. p2p_copy_filter_devname(dev->info.device_name,
  371. sizeof(dev->info.device_name),
  372. cli->dev_name, cli->dev_name_len);
  373. dev->info.dev_capab = cli->dev_capab;
  374. dev->info.config_methods = cli->config_methods;
  375. os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
  376. dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
  377. os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
  378. dev->info.wps_sec_dev_type_list_len);
  379. }
  380. static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
  381. const u8 *go_interface_addr, int freq,
  382. const u8 *gi, size_t gi_len,
  383. struct os_reltime *rx_time)
  384. {
  385. struct p2p_group_info info;
  386. size_t c;
  387. struct p2p_device *dev;
  388. if (gi == NULL)
  389. return 0;
  390. if (p2p_group_info_parse(gi, gi_len, &info) < 0)
  391. return -1;
  392. /*
  393. * Clear old data for this group; if the devices are still in the
  394. * group, the information will be restored in the loop following this.
  395. */
  396. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  397. if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
  398. ETH_ALEN) == 0) {
  399. os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
  400. os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
  401. }
  402. }
  403. for (c = 0; c < info.num_clients; c++) {
  404. struct p2p_client_info *cli = &info.client[c];
  405. if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
  406. ETH_ALEN) == 0)
  407. continue; /* ignore our own entry */
  408. dev = p2p_get_device(p2p, cli->p2p_device_addr);
  409. if (dev) {
  410. if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
  411. P2P_DEV_PROBE_REQ_ONLY)) {
  412. /*
  413. * Update information since we have not
  414. * received this directly from the client.
  415. */
  416. p2p_copy_client_info(dev, cli);
  417. } else {
  418. /*
  419. * Need to update P2P Client Discoverability
  420. * flag since it is valid only in P2P Group
  421. * Info attribute.
  422. */
  423. dev->info.dev_capab &=
  424. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  425. dev->info.dev_capab |=
  426. cli->dev_capab &
  427. P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  428. }
  429. if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
  430. dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
  431. }
  432. } else {
  433. dev = p2p_create_device(p2p, cli->p2p_device_addr);
  434. if (dev == NULL)
  435. continue;
  436. dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
  437. p2p_copy_client_info(dev, cli);
  438. dev->oper_freq = freq;
  439. p2p->cfg->dev_found(p2p->cfg->cb_ctx,
  440. dev->info.p2p_device_addr,
  441. &dev->info, 1);
  442. dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
  443. }
  444. os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
  445. ETH_ALEN);
  446. os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
  447. os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
  448. os_memcpy(dev->member_in_go_iface, go_interface_addr,
  449. ETH_ALEN);
  450. dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT;
  451. }
  452. return 0;
  453. }
  454. static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
  455. int probe_req, const struct p2p_message *msg)
  456. {
  457. os_memcpy(dev->info.device_name, msg->device_name,
  458. sizeof(dev->info.device_name));
  459. if (msg->manufacturer &&
  460. msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
  461. os_memset(dev->info.manufacturer, 0,
  462. sizeof(dev->info.manufacturer));
  463. os_memcpy(dev->info.manufacturer, msg->manufacturer,
  464. msg->manufacturer_len);
  465. }
  466. if (msg->model_name &&
  467. msg->model_name_len < sizeof(dev->info.model_name)) {
  468. os_memset(dev->info.model_name, 0,
  469. sizeof(dev->info.model_name));
  470. os_memcpy(dev->info.model_name, msg->model_name,
  471. msg->model_name_len);
  472. }
  473. if (msg->model_number &&
  474. msg->model_number_len < sizeof(dev->info.model_number)) {
  475. os_memset(dev->info.model_number, 0,
  476. sizeof(dev->info.model_number));
  477. os_memcpy(dev->info.model_number, msg->model_number,
  478. msg->model_number_len);
  479. }
  480. if (msg->serial_number &&
  481. msg->serial_number_len < sizeof(dev->info.serial_number)) {
  482. os_memset(dev->info.serial_number, 0,
  483. sizeof(dev->info.serial_number));
  484. os_memcpy(dev->info.serial_number, msg->serial_number,
  485. msg->serial_number_len);
  486. }
  487. if (msg->pri_dev_type)
  488. os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
  489. sizeof(dev->info.pri_dev_type));
  490. else if (msg->wps_pri_dev_type)
  491. os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
  492. sizeof(dev->info.pri_dev_type));
  493. if (msg->wps_sec_dev_type_list) {
  494. os_memcpy(dev->info.wps_sec_dev_type_list,
  495. msg->wps_sec_dev_type_list,
  496. msg->wps_sec_dev_type_list_len);
  497. dev->info.wps_sec_dev_type_list_len =
  498. msg->wps_sec_dev_type_list_len;
  499. }
  500. if (msg->capability) {
  501. /*
  502. * P2P Client Discoverability bit is reserved in all frames
  503. * that use this function, so do not change its value here.
  504. */
  505. dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  506. dev->info.dev_capab |= msg->capability[0] &
  507. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  508. dev->info.group_capab = msg->capability[1];
  509. }
  510. if (msg->ext_listen_timing) {
  511. dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
  512. dev->ext_listen_interval =
  513. WPA_GET_LE16(msg->ext_listen_timing + 2);
  514. }
  515. if (!probe_req) {
  516. u16 new_config_methods;
  517. new_config_methods = msg->config_methods ?
  518. msg->config_methods : msg->wps_config_methods;
  519. if (new_config_methods &&
  520. dev->info.config_methods != new_config_methods) {
  521. p2p_dbg(p2p, "Update peer " MACSTR
  522. " config_methods 0x%x -> 0x%x",
  523. MAC2STR(dev->info.p2p_device_addr),
  524. dev->info.config_methods,
  525. new_config_methods);
  526. dev->info.config_methods = new_config_methods;
  527. }
  528. }
  529. }
  530. static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
  531. size_t ies_len)
  532. {
  533. const u8 *pos, *end;
  534. u8 id, len;
  535. wpabuf_free(dev->info.vendor_elems);
  536. dev->info.vendor_elems = NULL;
  537. end = ies + ies_len;
  538. for (pos = ies; end - pos > 1; pos += len) {
  539. id = *pos++;
  540. len = *pos++;
  541. if (len > end - pos)
  542. break;
  543. if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
  544. continue;
  545. if (len >= 4) {
  546. u32 type = WPA_GET_BE32(pos);
  547. if (type == WPA_IE_VENDOR_TYPE ||
  548. type == WMM_IE_VENDOR_TYPE ||
  549. type == WPS_IE_VENDOR_TYPE ||
  550. type == P2P_IE_VENDOR_TYPE ||
  551. type == WFD_IE_VENDOR_TYPE)
  552. continue;
  553. }
  554. /* Unknown vendor element - make raw IE data available */
  555. if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
  556. break;
  557. wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
  558. }
  559. }
  560. static int p2p_compare_wfd_info(struct p2p_device *dev,
  561. const struct p2p_message *msg)
  562. {
  563. if (dev->info.wfd_subelems && msg->wfd_subelems) {
  564. if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
  565. return 1;
  566. return os_memcmp(dev->info.wfd_subelems->buf,
  567. msg->wfd_subelems->buf,
  568. dev->info.wfd_subelems->used);
  569. }
  570. if (dev->info.wfd_subelems || msg->wfd_subelems)
  571. return 1;
  572. return 0;
  573. }
  574. /**
  575. * p2p_add_device - Add peer entries based on scan results or P2P frames
  576. * @p2p: P2P module context from p2p_init()
  577. * @addr: Source address of Beacon or Probe Response frame (may be either
  578. * P2P Device Address or P2P Interface Address)
  579. * @level: Signal level (signal strength of the received frame from the peer)
  580. * @freq: Frequency on which the Beacon or Probe Response frame was received
  581. * @rx_time: Time when the result was received
  582. * @ies: IEs from the Beacon or Probe Response frame
  583. * @ies_len: Length of ies buffer in octets
  584. * @scan_res: Whether this was based on scan results
  585. * Returns: 0 on success, -1 on failure
  586. *
  587. * If the scan result is for a GO, the clients in the group will also be added
  588. * to the peer table. This function can also be used with some other frames
  589. * like Provision Discovery Request that contains P2P Capability and P2P Device
  590. * Info attributes.
  591. */
  592. int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
  593. struct os_reltime *rx_time, int level, const u8 *ies,
  594. size_t ies_len, int scan_res)
  595. {
  596. struct p2p_device *dev;
  597. struct p2p_message msg;
  598. const u8 *p2p_dev_addr;
  599. int wfd_changed;
  600. int i;
  601. struct os_reltime time_now;
  602. os_memset(&msg, 0, sizeof(msg));
  603. if (p2p_parse_ies(ies, ies_len, &msg)) {
  604. p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
  605. p2p_parse_free(&msg);
  606. return -1;
  607. }
  608. if (msg.p2p_device_addr)
  609. p2p_dev_addr = msg.p2p_device_addr;
  610. else if (msg.device_id)
  611. p2p_dev_addr = msg.device_id;
  612. else {
  613. p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
  614. p2p_parse_free(&msg);
  615. return -1;
  616. }
  617. if (!is_zero_ether_addr(p2p->peer_filter) &&
  618. os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
  619. p2p_dbg(p2p, "Do not add peer filter for " MACSTR
  620. " due to peer filter", MAC2STR(p2p_dev_addr));
  621. p2p_parse_free(&msg);
  622. return 0;
  623. }
  624. dev = p2p_create_device(p2p, p2p_dev_addr);
  625. if (dev == NULL) {
  626. p2p_parse_free(&msg);
  627. return -1;
  628. }
  629. if (rx_time == NULL) {
  630. os_get_reltime(&time_now);
  631. rx_time = &time_now;
  632. }
  633. /*
  634. * Update the device entry only if the new peer
  635. * entry is newer than the one previously stored, or if
  636. * the device was previously seen as a P2P Client in a group
  637. * and the new entry isn't older than a threshold.
  638. */
  639. if (dev->last_seen.sec > 0 &&
  640. os_reltime_before(rx_time, &dev->last_seen) &&
  641. (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) ||
  642. os_reltime_expired(&dev->last_seen, rx_time,
  643. P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) {
  644. p2p_dbg(p2p,
  645. "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u flags=0x%x)",
  646. (unsigned int) rx_time->sec,
  647. (unsigned int) rx_time->usec,
  648. (unsigned int) dev->last_seen.sec,
  649. (unsigned int) dev->last_seen.usec,
  650. dev->flags);
  651. p2p_parse_free(&msg);
  652. return -1;
  653. }
  654. os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
  655. dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY |
  656. P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT);
  657. if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
  658. os_memcpy(dev->interface_addr, addr, ETH_ALEN);
  659. if (msg.ssid &&
  660. msg.ssid[1] <= sizeof(dev->oper_ssid) &&
  661. (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
  662. os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
  663. != 0)) {
  664. os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
  665. dev->oper_ssid_len = msg.ssid[1];
  666. }
  667. wpabuf_free(dev->info.p2ps_instance);
  668. dev->info.p2ps_instance = NULL;
  669. if (msg.adv_service_instance && msg.adv_service_instance_len)
  670. dev->info.p2ps_instance = wpabuf_alloc_copy(
  671. msg.adv_service_instance, msg.adv_service_instance_len);
  672. if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
  673. *msg.ds_params >= 1 && *msg.ds_params <= 14) {
  674. int ds_freq;
  675. if (*msg.ds_params == 14)
  676. ds_freq = 2484;
  677. else
  678. ds_freq = 2407 + *msg.ds_params * 5;
  679. if (freq != ds_freq) {
  680. p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
  681. freq, ds_freq);
  682. freq = ds_freq;
  683. }
  684. }
  685. if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
  686. p2p_dbg(p2p, "Update Listen frequency based on scan results ("
  687. MACSTR " %d -> %d MHz (DS param %d)",
  688. MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
  689. freq, msg.ds_params ? *msg.ds_params : -1);
  690. }
  691. if (scan_res) {
  692. dev->listen_freq = freq;
  693. if (msg.group_info)
  694. dev->oper_freq = freq;
  695. }
  696. dev->info.level = level;
  697. p2p_copy_wps_info(p2p, dev, 0, &msg);
  698. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  699. wpabuf_free(dev->info.wps_vendor_ext[i]);
  700. dev->info.wps_vendor_ext[i] = NULL;
  701. }
  702. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  703. if (msg.wps_vendor_ext[i] == NULL)
  704. break;
  705. dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
  706. msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
  707. if (dev->info.wps_vendor_ext[i] == NULL)
  708. break;
  709. }
  710. wfd_changed = p2p_compare_wfd_info(dev, &msg);
  711. if (msg.wfd_subelems) {
  712. wpabuf_free(dev->info.wfd_subelems);
  713. dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
  714. }
  715. if (scan_res) {
  716. p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
  717. msg.group_info, msg.group_info_len,
  718. rx_time);
  719. }
  720. p2p_parse_free(&msg);
  721. p2p_update_peer_vendor_elems(dev, ies, ies_len);
  722. if (dev->flags & P2P_DEV_REPORTED && !wfd_changed &&
  723. (!msg.adv_service_instance ||
  724. (dev->flags & P2P_DEV_P2PS_REPORTED)))
  725. return 0;
  726. p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
  727. freq, (unsigned int) rx_time->sec,
  728. (unsigned int) rx_time->usec);
  729. if (dev->flags & P2P_DEV_USER_REJECTED) {
  730. p2p_dbg(p2p, "Do not report rejected device");
  731. return 0;
  732. }
  733. if (dev->info.config_methods == 0 &&
  734. (freq == 2412 || freq == 2437 || freq == 2462)) {
  735. /*
  736. * If we have only seen a Beacon frame from a GO, we do not yet
  737. * know what WPS config methods it supports. Since some
  738. * applications use config_methods value from P2P-DEVICE-FOUND
  739. * events, postpone reporting this peer until we've fully
  740. * discovered its capabilities.
  741. *
  742. * At least for now, do this only if the peer was detected on
  743. * one of the social channels since that peer can be easily be
  744. * found again and there are no limitations of having to use
  745. * passive scan on this channels, so this can be done through
  746. * Probe Response frame that includes the config_methods
  747. * information.
  748. */
  749. p2p_dbg(p2p, "Do not report peer " MACSTR
  750. " with unknown config methods", MAC2STR(addr));
  751. return 0;
  752. }
  753. p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
  754. !(dev->flags & P2P_DEV_REPORTED_ONCE));
  755. dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
  756. if (msg.adv_service_instance)
  757. dev->flags |= P2P_DEV_P2PS_REPORTED;
  758. return 0;
  759. }
  760. static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
  761. {
  762. int i;
  763. if (p2p->go_neg_peer == dev) {
  764. /*
  765. * If GO Negotiation is in progress, report that it has failed.
  766. */
  767. p2p_go_neg_failed(p2p, -1);
  768. }
  769. if (p2p->invite_peer == dev)
  770. p2p->invite_peer = NULL;
  771. if (p2p->sd_peer == dev)
  772. p2p->sd_peer = NULL;
  773. if (p2p->pending_client_disc_go == dev)
  774. p2p->pending_client_disc_go = NULL;
  775. /* dev_lost() device, but only if it was previously dev_found() */
  776. if (dev->flags & P2P_DEV_REPORTED_ONCE)
  777. p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
  778. dev->info.p2p_device_addr);
  779. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  780. wpabuf_free(dev->info.wps_vendor_ext[i]);
  781. dev->info.wps_vendor_ext[i] = NULL;
  782. }
  783. wpabuf_free(dev->info.wfd_subelems);
  784. wpabuf_free(dev->info.vendor_elems);
  785. wpabuf_free(dev->go_neg_conf);
  786. wpabuf_free(dev->info.p2ps_instance);
  787. os_free(dev);
  788. }
  789. static int p2p_get_next_prog_freq(struct p2p_data *p2p)
  790. {
  791. struct p2p_channels *c;
  792. struct p2p_reg_class *cla;
  793. size_t cl, ch;
  794. int found = 0;
  795. u8 reg_class;
  796. u8 channel;
  797. int freq;
  798. c = &p2p->cfg->channels;
  799. for (cl = 0; cl < c->reg_classes; cl++) {
  800. cla = &c->reg_class[cl];
  801. if (cla->reg_class != p2p->last_prog_scan_class)
  802. continue;
  803. for (ch = 0; ch < cla->channels; ch++) {
  804. if (cla->channel[ch] == p2p->last_prog_scan_chan) {
  805. found = 1;
  806. break;
  807. }
  808. }
  809. if (found)
  810. break;
  811. }
  812. if (!found) {
  813. /* Start from beginning */
  814. reg_class = c->reg_class[0].reg_class;
  815. channel = c->reg_class[0].channel[0];
  816. } else {
  817. /* Pick the next channel */
  818. ch++;
  819. if (ch == cla->channels) {
  820. cl++;
  821. if (cl == c->reg_classes)
  822. cl = 0;
  823. ch = 0;
  824. }
  825. reg_class = c->reg_class[cl].reg_class;
  826. channel = c->reg_class[cl].channel[ch];
  827. }
  828. freq = p2p_channel_to_freq(reg_class, channel);
  829. p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
  830. reg_class, channel, freq);
  831. p2p->last_prog_scan_class = reg_class;
  832. p2p->last_prog_scan_chan = channel;
  833. if (freq == 2412 || freq == 2437 || freq == 2462)
  834. return 0; /* No need to add social channels */
  835. return freq;
  836. }
  837. static void p2p_search(struct p2p_data *p2p)
  838. {
  839. int freq = 0;
  840. enum p2p_scan_type type;
  841. u16 pw_id = DEV_PW_DEFAULT;
  842. int res;
  843. if (p2p->drv_in_listen) {
  844. p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
  845. return;
  846. }
  847. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  848. if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
  849. (freq = p2p_get_next_prog_freq(p2p)) > 0) {
  850. type = P2P_SCAN_SOCIAL_PLUS_ONE;
  851. p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
  852. } else {
  853. type = P2P_SCAN_SOCIAL;
  854. p2p_dbg(p2p, "Starting search");
  855. }
  856. res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
  857. p2p->num_req_dev_types, p2p->req_dev_types,
  858. p2p->find_dev_id, pw_id);
  859. if (res < 0) {
  860. p2p_dbg(p2p, "Scan request schedule failed");
  861. p2p_continue_find(p2p);
  862. }
  863. }
  864. static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
  865. {
  866. struct p2p_data *p2p = eloop_ctx;
  867. p2p_dbg(p2p, "Find timeout -> stop");
  868. p2p_stop_find(p2p);
  869. }
  870. void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
  871. {
  872. if (status != 0) {
  873. p2p_dbg(p2p, "Scan request failed");
  874. /* Do continue find even for the first p2p_find_scan */
  875. p2p_continue_find(p2p);
  876. } else {
  877. p2p_dbg(p2p, "Running p2p_scan");
  878. p2p->p2p_scan_running = 1;
  879. eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
  880. eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
  881. p2p, NULL);
  882. }
  883. }
  884. static int p2p_run_after_scan(struct p2p_data *p2p)
  885. {
  886. struct p2p_device *dev;
  887. enum p2p_after_scan op;
  888. if (p2p->after_scan_tx) {
  889. p2p->after_scan_tx_in_progress = 1;
  890. p2p_dbg(p2p, "Send pending Action frame at p2p_scan completion");
  891. p2p->cfg->send_action(p2p->cfg->cb_ctx,
  892. p2p->after_scan_tx->freq,
  893. p2p->after_scan_tx->dst,
  894. p2p->after_scan_tx->src,
  895. p2p->after_scan_tx->bssid,
  896. (u8 *) (p2p->after_scan_tx + 1),
  897. p2p->after_scan_tx->len,
  898. p2p->after_scan_tx->wait_time);
  899. os_free(p2p->after_scan_tx);
  900. p2p->after_scan_tx = NULL;
  901. return 1;
  902. }
  903. op = p2p->start_after_scan;
  904. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  905. switch (op) {
  906. case P2P_AFTER_SCAN_NOTHING:
  907. break;
  908. case P2P_AFTER_SCAN_LISTEN:
  909. p2p_dbg(p2p, "Start previously requested Listen state");
  910. p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
  911. p2p->pending_listen_usec / 1000);
  912. return 1;
  913. case P2P_AFTER_SCAN_CONNECT:
  914. p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
  915. MAC2STR(p2p->after_scan_peer));
  916. dev = p2p_get_device(p2p, p2p->after_scan_peer);
  917. if (dev == NULL) {
  918. p2p_dbg(p2p, "Peer not known anymore");
  919. break;
  920. }
  921. p2p_connect_send(p2p, dev);
  922. return 1;
  923. }
  924. return 0;
  925. }
  926. static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  927. {
  928. struct p2p_data *p2p = eloop_ctx;
  929. int running;
  930. p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
  931. running = p2p->p2p_scan_running;
  932. /* Make sure we recover from missed scan results callback */
  933. p2p->p2p_scan_running = 0;
  934. if (running)
  935. p2p_run_after_scan(p2p);
  936. }
  937. static void p2p_free_req_dev_types(struct p2p_data *p2p)
  938. {
  939. p2p->num_req_dev_types = 0;
  940. os_free(p2p->req_dev_types);
  941. p2p->req_dev_types = NULL;
  942. }
  943. static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
  944. {
  945. u8 buf[SHA256_MAC_LEN];
  946. char str_buf[256];
  947. const u8 *adv_array;
  948. size_t i, adv_len;
  949. if (!str || !hash)
  950. return 0;
  951. if (!str[0]) {
  952. os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
  953. return 1;
  954. }
  955. adv_array = (u8 *) str_buf;
  956. adv_len = os_strlen(str);
  957. if (adv_len >= sizeof(str_buf))
  958. return 0;
  959. for (i = 0; i < adv_len; i++) {
  960. if (str[i] >= 'A' && str[i] <= 'Z')
  961. str_buf[i] = str[i] - 'A' + 'a';
  962. else
  963. str_buf[i] = str[i];
  964. }
  965. if (sha256_vector(1, &adv_array, &adv_len, buf))
  966. return 0;
  967. os_memcpy(hash, buf, P2PS_HASH_LEN);
  968. return 1;
  969. }
  970. int p2p_find(struct p2p_data *p2p, unsigned int timeout,
  971. enum p2p_discovery_type type,
  972. unsigned int num_req_dev_types, const u8 *req_dev_types,
  973. const u8 *dev_id, unsigned int search_delay,
  974. u8 seek_count, const char **seek, int freq)
  975. {
  976. int res;
  977. p2p_dbg(p2p, "Starting find (type=%d)", type);
  978. os_get_reltime(&p2p->find_start);
  979. if (p2p->p2p_scan_running) {
  980. p2p_dbg(p2p, "p2p_scan is already running");
  981. }
  982. p2p_free_req_dev_types(p2p);
  983. if (req_dev_types && num_req_dev_types) {
  984. p2p->req_dev_types = os_malloc(num_req_dev_types *
  985. WPS_DEV_TYPE_LEN);
  986. if (p2p->req_dev_types == NULL)
  987. return -1;
  988. os_memcpy(p2p->req_dev_types, req_dev_types,
  989. num_req_dev_types * WPS_DEV_TYPE_LEN);
  990. p2p->num_req_dev_types = num_req_dev_types;
  991. }
  992. if (dev_id) {
  993. os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
  994. p2p->find_dev_id = p2p->find_dev_id_buf;
  995. } else
  996. p2p->find_dev_id = NULL;
  997. if (seek_count == 0 || !seek) {
  998. /* Not an ASP search */
  999. p2p->p2ps_seek = 0;
  1000. } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
  1001. /*
  1002. * An empty seek string means no hash values, but still an ASP
  1003. * search.
  1004. */
  1005. p2p_dbg(p2p, "ASP search");
  1006. p2p->p2ps_seek_count = 0;
  1007. p2p->p2ps_seek = 1;
  1008. } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
  1009. u8 buf[P2PS_HASH_LEN];
  1010. int i, count = 0;
  1011. for (i = 0; i < seek_count; i++) {
  1012. if (!p2ps_gen_hash(p2p, seek[i], buf))
  1013. continue;
  1014. p2p_dbg(p2p, "Seek service %s hash " MACSTR,
  1015. seek[i], MAC2STR(buf));
  1016. os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
  1017. buf, P2PS_HASH_LEN);
  1018. count++;
  1019. }
  1020. p2p->p2ps_seek_count = count;
  1021. p2p->p2ps_seek = 1;
  1022. } else {
  1023. p2p->p2ps_seek_count = 0;
  1024. p2p->p2ps_seek = 1;
  1025. }
  1026. /* Special case to perform wildcard search */
  1027. if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
  1028. p2p->p2ps_seek_count = 1;
  1029. os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
  1030. P2PS_HASH_LEN);
  1031. }
  1032. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  1033. p2p_clear_timeout(p2p);
  1034. if (p2p->pending_listen_freq) {
  1035. p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
  1036. p2p->pending_listen_freq = 0;
  1037. }
  1038. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  1039. p2p->find_type = type;
  1040. p2p_device_clear_reported(p2p);
  1041. os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
  1042. p2p_set_state(p2p, P2P_SEARCH);
  1043. p2p->search_delay = search_delay;
  1044. p2p->in_search_delay = 0;
  1045. eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
  1046. p2p->last_p2p_find_timeout = timeout;
  1047. if (timeout)
  1048. eloop_register_timeout(timeout, 0, p2p_find_timeout,
  1049. p2p, NULL);
  1050. switch (type) {
  1051. case P2P_FIND_START_WITH_FULL:
  1052. if (freq > 0) {
  1053. /*
  1054. * Start with the specified channel and then move to
  1055. * social channels only scans.
  1056. */
  1057. res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
  1058. P2P_SCAN_SPECIFIC, freq,
  1059. p2p->num_req_dev_types,
  1060. p2p->req_dev_types, dev_id,
  1061. DEV_PW_DEFAULT);
  1062. break;
  1063. }
  1064. /* fall through */
  1065. case P2P_FIND_PROGRESSIVE:
  1066. res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
  1067. p2p->num_req_dev_types,
  1068. p2p->req_dev_types, dev_id,
  1069. DEV_PW_DEFAULT);
  1070. break;
  1071. case P2P_FIND_ONLY_SOCIAL:
  1072. res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
  1073. p2p->num_req_dev_types,
  1074. p2p->req_dev_types, dev_id,
  1075. DEV_PW_DEFAULT);
  1076. break;
  1077. default:
  1078. return -1;
  1079. }
  1080. if (res != 0 && p2p->p2p_scan_running) {
  1081. p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
  1082. /* wait for the previous p2p_scan to complete */
  1083. res = 0; /* do not report failure */
  1084. } else if (res != 0) {
  1085. p2p_dbg(p2p, "Failed to start p2p_scan");
  1086. p2p_set_state(p2p, P2P_IDLE);
  1087. eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
  1088. }
  1089. return res;
  1090. }
  1091. void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
  1092. {
  1093. p2p_dbg(p2p, "Stopping find");
  1094. eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
  1095. p2p_clear_timeout(p2p);
  1096. if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
  1097. p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
  1098. p2p->p2ps_seek_count = 0;
  1099. p2p_set_state(p2p, P2P_IDLE);
  1100. p2p_free_req_dev_types(p2p);
  1101. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  1102. if (p2p->go_neg_peer)
  1103. p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
  1104. p2p->go_neg_peer = NULL;
  1105. p2p->sd_peer = NULL;
  1106. p2p->invite_peer = NULL;
  1107. p2p_stop_listen_for_freq(p2p, freq);
  1108. p2p->send_action_in_progress = 0;
  1109. }
  1110. void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
  1111. {
  1112. if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
  1113. p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
  1114. return;
  1115. }
  1116. if (p2p->in_listen) {
  1117. p2p->in_listen = 0;
  1118. p2p_clear_timeout(p2p);
  1119. }
  1120. if (p2p->drv_in_listen) {
  1121. /*
  1122. * The driver may not deliver callback to p2p_listen_end()
  1123. * when the operation gets canceled, so clear the internal
  1124. * variable that is tracking driver state.
  1125. */
  1126. p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
  1127. p2p->drv_in_listen = 0;
  1128. }
  1129. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  1130. }
  1131. void p2p_stop_listen(struct p2p_data *p2p)
  1132. {
  1133. if (p2p->state != P2P_LISTEN_ONLY) {
  1134. p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
  1135. return;
  1136. }
  1137. p2p_stop_listen_for_freq(p2p, 0);
  1138. p2p_set_state(p2p, P2P_IDLE);
  1139. }
  1140. void p2p_stop_find(struct p2p_data *p2p)
  1141. {
  1142. p2p->pending_listen_freq = 0;
  1143. p2p_stop_find_for_freq(p2p, 0);
  1144. }
  1145. static int p2p_prepare_channel_pref(struct p2p_data *p2p,
  1146. unsigned int force_freq,
  1147. unsigned int pref_freq, int go)
  1148. {
  1149. u8 op_class, op_channel;
  1150. unsigned int freq = force_freq ? force_freq : pref_freq;
  1151. p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
  1152. force_freq, pref_freq, go);
  1153. if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
  1154. p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
  1155. return -1;
  1156. }
  1157. if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
  1158. (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
  1159. op_channel))) {
  1160. p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
  1161. freq, op_class, op_channel);
  1162. return -1;
  1163. }
  1164. p2p->op_reg_class = op_class;
  1165. p2p->op_channel = op_channel;
  1166. if (force_freq) {
  1167. p2p->channels.reg_classes = 1;
  1168. p2p->channels.reg_class[0].channels = 1;
  1169. p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
  1170. p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
  1171. } else {
  1172. os_memcpy(&p2p->channels, &p2p->cfg->channels,
  1173. sizeof(struct p2p_channels));
  1174. }
  1175. return 0;
  1176. }
  1177. static void p2p_prepare_channel_best(struct p2p_data *p2p)
  1178. {
  1179. u8 op_class, op_channel;
  1180. const int op_classes_5ghz[] = { 124, 125, 115, 0 };
  1181. const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
  1182. const int op_classes_vht[] = { 128, 0 };
  1183. p2p_dbg(p2p, "Prepare channel best");
  1184. if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
  1185. p2p_supported_freq(p2p, p2p->best_freq_overall) &&
  1186. p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
  1187. == 0) {
  1188. p2p_dbg(p2p, "Select best overall channel as operating channel preference");
  1189. p2p->op_reg_class = op_class;
  1190. p2p->op_channel = op_channel;
  1191. } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
  1192. p2p_supported_freq(p2p, p2p->best_freq_5) &&
  1193. p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
  1194. == 0) {
  1195. p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
  1196. p2p->op_reg_class = op_class;
  1197. p2p->op_channel = op_channel;
  1198. } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
  1199. p2p_supported_freq(p2p, p2p->best_freq_24) &&
  1200. p2p_freq_to_channel(p2p->best_freq_24, &op_class,
  1201. &op_channel) == 0) {
  1202. p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
  1203. p2p->op_reg_class = op_class;
  1204. p2p->op_channel = op_channel;
  1205. } else if (p2p->cfg->num_pref_chan > 0 &&
  1206. p2p_channels_includes(&p2p->cfg->channels,
  1207. p2p->cfg->pref_chan[0].op_class,
  1208. p2p->cfg->pref_chan[0].chan)) {
  1209. p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
  1210. p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
  1211. p2p->op_channel = p2p->cfg->pref_chan[0].chan;
  1212. } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
  1213. &p2p->op_reg_class, &p2p->op_channel) ==
  1214. 0) {
  1215. p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
  1216. p2p->op_reg_class, p2p->op_channel);
  1217. } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
  1218. &p2p->op_reg_class, &p2p->op_channel) ==
  1219. 0) {
  1220. p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
  1221. p2p->op_reg_class, p2p->op_channel);
  1222. } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
  1223. &p2p->op_reg_class, &p2p->op_channel) ==
  1224. 0) {
  1225. p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
  1226. p2p->op_reg_class, p2p->op_channel);
  1227. } else if (p2p_channels_includes(&p2p->cfg->channels,
  1228. p2p->cfg->op_reg_class,
  1229. p2p->cfg->op_channel)) {
  1230. p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
  1231. p2p->op_reg_class = p2p->cfg->op_reg_class;
  1232. p2p->op_channel = p2p->cfg->op_channel;
  1233. } else if (p2p_channel_random_social(&p2p->cfg->channels,
  1234. &p2p->op_reg_class,
  1235. &p2p->op_channel) == 0) {
  1236. p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
  1237. p2p->op_reg_class, p2p->op_channel);
  1238. } else {
  1239. /* Select any random available channel from the first available
  1240. * operating class */
  1241. p2p_channel_select(&p2p->cfg->channels, NULL,
  1242. &p2p->op_reg_class,
  1243. &p2p->op_channel);
  1244. p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
  1245. p2p->op_channel, p2p->op_reg_class);
  1246. }
  1247. os_memcpy(&p2p->channels, &p2p->cfg->channels,
  1248. sizeof(struct p2p_channels));
  1249. }
  1250. /**
  1251. * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
  1252. * @p2p: P2P module context from p2p_init()
  1253. * @dev: Selected peer device
  1254. * @force_freq: Forced frequency in MHz or 0 if not forced
  1255. * @pref_freq: Preferred frequency in MHz or 0 if no preference
  1256. * @go: Whether the local end will be forced to be GO
  1257. * Returns: 0 on success, -1 on failure (channel not supported for P2P)
  1258. *
  1259. * This function is used to do initial operating channel selection for GO
  1260. * Negotiation prior to having received peer information or for P2PS PD
  1261. * signalling. The selected channel may be further optimized in
  1262. * p2p_reselect_channel() once the peer information is available.
  1263. */
  1264. int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
  1265. unsigned int force_freq, unsigned int pref_freq, int go)
  1266. {
  1267. p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
  1268. force_freq, pref_freq, go);
  1269. if (force_freq || pref_freq) {
  1270. if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
  1271. 0)
  1272. return -1;
  1273. } else {
  1274. p2p_prepare_channel_best(p2p);
  1275. }
  1276. p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
  1277. if (go)
  1278. p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
  1279. else if (!force_freq)
  1280. p2p_channels_union_inplace(&p2p->channels,
  1281. &p2p->cfg->cli_channels);
  1282. p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
  1283. p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
  1284. p2p->op_reg_class, p2p->op_channel,
  1285. force_freq ? " (forced)" : "");
  1286. if (force_freq)
  1287. dev->flags |= P2P_DEV_FORCE_FREQ;
  1288. else
  1289. dev->flags &= ~P2P_DEV_FORCE_FREQ;
  1290. return 0;
  1291. }
  1292. static void p2p_set_dev_persistent(struct p2p_device *dev,
  1293. int persistent_group)
  1294. {
  1295. switch (persistent_group) {
  1296. case 0:
  1297. dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
  1298. P2P_DEV_PREFER_PERSISTENT_RECONN);
  1299. break;
  1300. case 1:
  1301. dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
  1302. dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
  1303. break;
  1304. case 2:
  1305. dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
  1306. P2P_DEV_PREFER_PERSISTENT_RECONN;
  1307. break;
  1308. }
  1309. }
  1310. int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
  1311. enum p2p_wps_method wps_method,
  1312. int go_intent, const u8 *own_interface_addr,
  1313. unsigned int force_freq, int persistent_group,
  1314. const u8 *force_ssid, size_t force_ssid_len,
  1315. int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
  1316. {
  1317. struct p2p_device *dev;
  1318. p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
  1319. " GO Intent=%d Intended Interface Address=" MACSTR
  1320. " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
  1321. "oob_pw_id=%u",
  1322. MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
  1323. wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
  1324. dev = p2p_get_device(p2p, peer_addr);
  1325. if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
  1326. p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
  1327. MAC2STR(peer_addr));
  1328. return -1;
  1329. }
  1330. if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
  1331. go_intent == 15) < 0)
  1332. return -1;
  1333. if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
  1334. if (!(dev->info.dev_capab &
  1335. P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
  1336. p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
  1337. " that is in a group and is not discoverable",
  1338. MAC2STR(peer_addr));
  1339. return -1;
  1340. }
  1341. if (dev->oper_freq <= 0) {
  1342. p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
  1343. " with incomplete information",
  1344. MAC2STR(peer_addr));
  1345. return -1;
  1346. }
  1347. /*
  1348. * First, try to connect directly. If the peer does not
  1349. * acknowledge frames, assume it is sleeping and use device
  1350. * discoverability via the GO at that point.
  1351. */
  1352. }
  1353. p2p->ssid_set = 0;
  1354. if (force_ssid) {
  1355. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
  1356. force_ssid, force_ssid_len);
  1357. os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
  1358. p2p->ssid_len = force_ssid_len;
  1359. p2p->ssid_set = 1;
  1360. }
  1361. dev->flags &= ~P2P_DEV_NOT_YET_READY;
  1362. dev->flags &= ~P2P_DEV_USER_REJECTED;
  1363. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
  1364. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
  1365. if (pd_before_go_neg)
  1366. dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
  1367. else {
  1368. dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
  1369. /*
  1370. * Assign dialog token and tie breaker here to use the same
  1371. * values in each retry within the same GO Negotiation exchange.
  1372. */
  1373. dev->dialog_token++;
  1374. if (dev->dialog_token == 0)
  1375. dev->dialog_token = 1;
  1376. dev->tie_breaker = p2p->next_tie_breaker;
  1377. p2p->next_tie_breaker = !p2p->next_tie_breaker;
  1378. }
  1379. dev->connect_reqs = 0;
  1380. dev->go_neg_req_sent = 0;
  1381. dev->go_state = UNKNOWN_GO;
  1382. p2p_set_dev_persistent(dev, persistent_group);
  1383. p2p->go_intent = go_intent;
  1384. os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
  1385. if (p2p->state != P2P_IDLE)
  1386. p2p_stop_find(p2p);
  1387. if (p2p->after_scan_tx) {
  1388. /*
  1389. * We need to drop the pending frame to avoid issues with the
  1390. * new GO Negotiation, e.g., when the pending frame was from a
  1391. * previous attempt at starting a GO Negotiation.
  1392. */
  1393. p2p_dbg(p2p, "Dropped previous pending Action frame TX that was waiting for p2p_scan completion");
  1394. os_free(p2p->after_scan_tx);
  1395. p2p->after_scan_tx = NULL;
  1396. }
  1397. dev->wps_method = wps_method;
  1398. dev->oob_pw_id = oob_pw_id;
  1399. dev->status = P2P_SC_SUCCESS;
  1400. if (p2p->p2p_scan_running) {
  1401. p2p_dbg(p2p, "p2p_scan running - delay connect send");
  1402. p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
  1403. os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
  1404. return 0;
  1405. }
  1406. p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
  1407. return p2p_connect_send(p2p, dev);
  1408. }
  1409. int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
  1410. enum p2p_wps_method wps_method,
  1411. int go_intent, const u8 *own_interface_addr,
  1412. unsigned int force_freq, int persistent_group,
  1413. const u8 *force_ssid, size_t force_ssid_len,
  1414. unsigned int pref_freq, u16 oob_pw_id)
  1415. {
  1416. struct p2p_device *dev;
  1417. p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
  1418. " GO Intent=%d Intended Interface Address=" MACSTR
  1419. " wps_method=%d persistent_group=%d oob_pw_id=%u",
  1420. MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
  1421. wps_method, persistent_group, oob_pw_id);
  1422. dev = p2p_get_device(p2p, peer_addr);
  1423. if (dev == NULL) {
  1424. p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
  1425. MAC2STR(peer_addr));
  1426. return -1;
  1427. }
  1428. if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
  1429. 15) < 0)
  1430. return -1;
  1431. p2p->ssid_set = 0;
  1432. if (force_ssid) {
  1433. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
  1434. force_ssid, force_ssid_len);
  1435. os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
  1436. p2p->ssid_len = force_ssid_len;
  1437. p2p->ssid_set = 1;
  1438. }
  1439. dev->flags &= ~P2P_DEV_NOT_YET_READY;
  1440. dev->flags &= ~P2P_DEV_USER_REJECTED;
  1441. dev->go_neg_req_sent = 0;
  1442. dev->go_state = UNKNOWN_GO;
  1443. p2p_set_dev_persistent(dev, persistent_group);
  1444. p2p->go_intent = go_intent;
  1445. os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
  1446. dev->wps_method = wps_method;
  1447. dev->oob_pw_id = oob_pw_id;
  1448. dev->status = P2P_SC_SUCCESS;
  1449. return 0;
  1450. }
  1451. void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
  1452. struct p2p_device *dev, struct p2p_message *msg)
  1453. {
  1454. os_get_reltime(&dev->last_seen);
  1455. p2p_copy_wps_info(p2p, dev, 0, msg);
  1456. if (msg->listen_channel) {
  1457. int freq;
  1458. freq = p2p_channel_to_freq(msg->listen_channel[3],
  1459. msg->listen_channel[4]);
  1460. if (freq < 0) {
  1461. p2p_dbg(p2p, "Unknown peer Listen channel: "
  1462. "country=%c%c(0x%02x) reg_class=%u channel=%u",
  1463. msg->listen_channel[0],
  1464. msg->listen_channel[1],
  1465. msg->listen_channel[2],
  1466. msg->listen_channel[3],
  1467. msg->listen_channel[4]);
  1468. } else {
  1469. p2p_dbg(p2p, "Update peer " MACSTR
  1470. " Listen channel: %u -> %u MHz",
  1471. MAC2STR(dev->info.p2p_device_addr),
  1472. dev->listen_freq, freq);
  1473. dev->listen_freq = freq;
  1474. }
  1475. }
  1476. if (msg->wfd_subelems) {
  1477. wpabuf_free(dev->info.wfd_subelems);
  1478. dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
  1479. }
  1480. if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
  1481. dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
  1482. p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
  1483. } else {
  1484. p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
  1485. MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
  1486. "listen_freq=%d",
  1487. MAC2STR(dev->info.p2p_device_addr),
  1488. dev->info.dev_capab, dev->info.group_capab,
  1489. dev->info.device_name, dev->listen_freq);
  1490. }
  1491. dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
  1492. if (dev->flags & P2P_DEV_USER_REJECTED) {
  1493. p2p_dbg(p2p, "Do not report rejected device");
  1494. return;
  1495. }
  1496. p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
  1497. !(dev->flags & P2P_DEV_REPORTED_ONCE));
  1498. dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
  1499. }
  1500. void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
  1501. {
  1502. os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  1503. p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
  1504. os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
  1505. p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
  1506. *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
  1507. }
  1508. int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
  1509. {
  1510. if (p2p->ssid_set) {
  1511. os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
  1512. params->ssid_len = p2p->ssid_len;
  1513. } else {
  1514. p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
  1515. }
  1516. p2p->ssid_set = 0;
  1517. p2p_random(params->passphrase, p2p->cfg->passphrase_len);
  1518. return 0;
  1519. }
  1520. void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
  1521. {
  1522. struct p2p_go_neg_results res;
  1523. int go = peer->go_state == LOCAL_GO;
  1524. struct p2p_channels intersection;
  1525. p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
  1526. MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
  1527. os_memset(&res, 0, sizeof(res));
  1528. res.role_go = go;
  1529. os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
  1530. os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
  1531. res.wps_method = peer->wps_method;
  1532. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
  1533. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
  1534. res.persistent_group = 2;
  1535. else
  1536. res.persistent_group = 1;
  1537. }
  1538. if (go) {
  1539. /* Setup AP mode for WPS provisioning */
  1540. res.freq = p2p_channel_to_freq(p2p->op_reg_class,
  1541. p2p->op_channel);
  1542. os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
  1543. res.ssid_len = p2p->ssid_len;
  1544. p2p_random(res.passphrase, p2p->cfg->passphrase_len);
  1545. } else {
  1546. res.freq = peer->oper_freq;
  1547. if (p2p->ssid_len) {
  1548. os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
  1549. res.ssid_len = p2p->ssid_len;
  1550. }
  1551. }
  1552. p2p_channels_dump(p2p, "own channels", &p2p->channels);
  1553. p2p_channels_dump(p2p, "peer channels", &peer->channels);
  1554. p2p_channels_intersect(&p2p->channels, &peer->channels,
  1555. &intersection);
  1556. if (go) {
  1557. p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
  1558. p2p_channels_dump(p2p, "intersection after no-GO removal",
  1559. &intersection);
  1560. }
  1561. p2p_channels_to_freqs(&intersection, res.freq_list,
  1562. P2P_MAX_CHANNELS);
  1563. res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
  1564. p2p_clear_timeout(p2p);
  1565. p2p->ssid_set = 0;
  1566. peer->go_neg_req_sent = 0;
  1567. peer->wps_method = WPS_NOT_READY;
  1568. peer->oob_pw_id = 0;
  1569. wpabuf_free(peer->go_neg_conf);
  1570. peer->go_neg_conf = NULL;
  1571. p2p_set_state(p2p, P2P_PROVISIONING);
  1572. p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
  1573. }
  1574. static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
  1575. const u8 *data, size_t len, int rx_freq)
  1576. {
  1577. p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
  1578. wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
  1579. if (len < 1)
  1580. return;
  1581. switch (data[0]) {
  1582. case P2P_GO_NEG_REQ:
  1583. p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
  1584. break;
  1585. case P2P_GO_NEG_RESP:
  1586. p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
  1587. break;
  1588. case P2P_GO_NEG_CONF:
  1589. p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
  1590. break;
  1591. case P2P_INVITATION_REQ:
  1592. p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
  1593. rx_freq);
  1594. break;
  1595. case P2P_INVITATION_RESP:
  1596. p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
  1597. break;
  1598. case P2P_PROV_DISC_REQ:
  1599. p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
  1600. break;
  1601. case P2P_PROV_DISC_RESP:
  1602. p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
  1603. break;
  1604. case P2P_DEV_DISC_REQ:
  1605. p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
  1606. break;
  1607. case P2P_DEV_DISC_RESP:
  1608. p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
  1609. break;
  1610. default:
  1611. p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
  1612. data[0]);
  1613. break;
  1614. }
  1615. }
  1616. static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
  1617. const u8 *sa, const u8 *bssid, const u8 *data,
  1618. size_t len, int freq)
  1619. {
  1620. if (len < 1)
  1621. return;
  1622. switch (data[0]) {
  1623. case WLAN_PA_VENDOR_SPECIFIC:
  1624. data++;
  1625. len--;
  1626. if (len < 4)
  1627. return;
  1628. if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
  1629. return;
  1630. data += 4;
  1631. len -= 4;
  1632. p2p_rx_p2p_action(p2p, sa, data, len, freq);
  1633. break;
  1634. case WLAN_PA_GAS_INITIAL_REQ:
  1635. p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
  1636. break;
  1637. case WLAN_PA_GAS_INITIAL_RESP:
  1638. p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
  1639. break;
  1640. case WLAN_PA_GAS_COMEBACK_REQ:
  1641. p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
  1642. break;
  1643. case WLAN_PA_GAS_COMEBACK_RESP:
  1644. p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
  1645. break;
  1646. }
  1647. }
  1648. void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
  1649. const u8 *bssid, u8 category,
  1650. const u8 *data, size_t len, int freq)
  1651. {
  1652. if (category == WLAN_ACTION_PUBLIC) {
  1653. p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
  1654. return;
  1655. }
  1656. if (category != WLAN_ACTION_VENDOR_SPECIFIC)
  1657. return;
  1658. if (len < 4)
  1659. return;
  1660. if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
  1661. return;
  1662. data += 4;
  1663. len -= 4;
  1664. /* P2P action frame */
  1665. p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
  1666. wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
  1667. if (len < 1)
  1668. return;
  1669. switch (data[0]) {
  1670. case P2P_NOA:
  1671. p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
  1672. /* TODO */
  1673. break;
  1674. case P2P_PRESENCE_REQ:
  1675. p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
  1676. break;
  1677. case P2P_PRESENCE_RESP:
  1678. p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
  1679. break;
  1680. case P2P_GO_DISC_REQ:
  1681. p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
  1682. break;
  1683. default:
  1684. p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
  1685. break;
  1686. }
  1687. }
  1688. static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
  1689. {
  1690. struct p2p_data *p2p = eloop_ctx;
  1691. if (p2p->go_neg_peer == NULL)
  1692. return;
  1693. if (p2p->pending_listen_freq) {
  1694. p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
  1695. p2p->pending_listen_freq = 0;
  1696. }
  1697. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  1698. p2p->go_neg_peer->status = P2P_SC_SUCCESS;
  1699. /*
  1700. * Set new timeout to make sure a previously set one does not expire
  1701. * too quickly while waiting for the GO Negotiation to complete.
  1702. */
  1703. p2p_set_timeout(p2p, 0, 500000);
  1704. p2p_connect_send(p2p, p2p->go_neg_peer);
  1705. }
  1706. static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
  1707. {
  1708. struct p2p_data *p2p = eloop_ctx;
  1709. if (p2p->invite_peer == NULL)
  1710. return;
  1711. if (p2p->pending_listen_freq) {
  1712. p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
  1713. p2p->pending_listen_freq = 0;
  1714. }
  1715. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  1716. p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
  1717. p2p->invite_dev_pw_id);
  1718. }
  1719. static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
  1720. const u8 *ie, size_t ie_len)
  1721. {
  1722. struct p2p_message msg;
  1723. struct p2p_device *dev;
  1724. os_memset(&msg, 0, sizeof(msg));
  1725. if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
  1726. {
  1727. p2p_parse_free(&msg);
  1728. return; /* not a P2P probe */
  1729. }
  1730. if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
  1731. os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
  1732. != 0) {
  1733. /* The Probe Request is not part of P2P Device Discovery. It is
  1734. * not known whether the source address of the frame is the P2P
  1735. * Device Address or P2P Interface Address. Do not add a new
  1736. * peer entry based on this frames.
  1737. */
  1738. p2p_parse_free(&msg);
  1739. return;
  1740. }
  1741. dev = p2p_get_device(p2p, addr);
  1742. if (dev) {
  1743. if (msg.listen_channel) {
  1744. int freq;
  1745. if (dev->country[0] == 0)
  1746. os_memcpy(dev->country, msg.listen_channel, 3);
  1747. freq = p2p_channel_to_freq(msg.listen_channel[3],
  1748. msg.listen_channel[4]);
  1749. if (freq > 0 && dev->listen_freq != freq) {
  1750. p2p_dbg(p2p,
  1751. "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
  1752. MAC2STR(addr), dev->listen_freq, freq);
  1753. dev->listen_freq = freq;
  1754. }
  1755. }
  1756. os_get_reltime(&dev->last_seen);
  1757. p2p_parse_free(&msg);
  1758. return; /* already known */
  1759. }
  1760. dev = p2p_create_device(p2p, addr);
  1761. if (dev == NULL) {
  1762. p2p_parse_free(&msg);
  1763. return;
  1764. }
  1765. os_get_reltime(&dev->last_seen);
  1766. dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
  1767. if (msg.listen_channel) {
  1768. os_memcpy(dev->country, msg.listen_channel, 3);
  1769. dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
  1770. msg.listen_channel[4]);
  1771. }
  1772. p2p_copy_wps_info(p2p, dev, 1, &msg);
  1773. if (msg.wfd_subelems) {
  1774. wpabuf_free(dev->info.wfd_subelems);
  1775. dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
  1776. }
  1777. p2p_parse_free(&msg);
  1778. p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
  1779. " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
  1780. MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
  1781. dev->info.group_capab, dev->info.device_name,
  1782. dev->listen_freq);
  1783. }
  1784. struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
  1785. const u8 *addr,
  1786. struct p2p_message *msg)
  1787. {
  1788. struct p2p_device *dev;
  1789. dev = p2p_get_device(p2p, addr);
  1790. if (dev) {
  1791. os_get_reltime(&dev->last_seen);
  1792. return dev; /* already known */
  1793. }
  1794. dev = p2p_create_device(p2p, addr);
  1795. if (dev == NULL)
  1796. return NULL;
  1797. p2p_add_dev_info(p2p, addr, dev, msg);
  1798. return dev;
  1799. }
  1800. static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
  1801. {
  1802. if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
  1803. return 1;
  1804. if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
  1805. WPA_GET_BE32(&req_dev_type[2]) == 0 &&
  1806. WPA_GET_BE16(&req_dev_type[6]) == 0)
  1807. return 1; /* Category match with wildcard OUI/sub-category */
  1808. return 0;
  1809. }
  1810. int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
  1811. size_t num_req_dev_type)
  1812. {
  1813. size_t i;
  1814. for (i = 0; i < num_req_dev_type; i++) {
  1815. if (dev_type_match(dev_type, req_dev_type[i]))
  1816. return 1;
  1817. }
  1818. return 0;
  1819. }
  1820. /**
  1821. * p2p_match_dev_type - Match local device type with requested type
  1822. * @p2p: P2P module context from p2p_init()
  1823. * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
  1824. * Returns: 1 on match, 0 on mismatch
  1825. *
  1826. * This function can be used to match the Requested Device Type attribute in
  1827. * WPS IE with the local device types for deciding whether to reply to a Probe
  1828. * Request frame.
  1829. */
  1830. int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
  1831. {
  1832. struct wps_parse_attr attr;
  1833. size_t i;
  1834. if (wps_parse_msg(wps, &attr))
  1835. return 1; /* assume no Requested Device Type attributes */
  1836. if (attr.num_req_dev_type == 0)
  1837. return 1; /* no Requested Device Type attributes -> match */
  1838. if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
  1839. attr.num_req_dev_type))
  1840. return 1; /* Own Primary Device Type matches */
  1841. for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
  1842. if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
  1843. attr.req_dev_type,
  1844. attr.num_req_dev_type))
  1845. return 1; /* Own Secondary Device Type matches */
  1846. }
  1847. /* No matching device type found */
  1848. return 0;
  1849. }
  1850. struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
  1851. const u8 *query_hash,
  1852. u8 query_count)
  1853. {
  1854. struct wpabuf *buf;
  1855. u8 *len;
  1856. int pw_id = -1;
  1857. size_t extra = 0;
  1858. #ifdef CONFIG_WIFI_DISPLAY
  1859. if (p2p->wfd_ie_probe_resp)
  1860. extra = wpabuf_len(p2p->wfd_ie_probe_resp);
  1861. #endif /* CONFIG_WIFI_DISPLAY */
  1862. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
  1863. extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
  1864. if (query_count)
  1865. extra += MAX_SVC_ADV_IE_LEN;
  1866. buf = wpabuf_alloc(1000 + extra);
  1867. if (buf == NULL)
  1868. return NULL;
  1869. if (p2p->go_neg_peer) {
  1870. /* Advertise immediate availability of WPS credential */
  1871. pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
  1872. }
  1873. if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
  1874. p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
  1875. wpabuf_free(buf);
  1876. return NULL;
  1877. }
  1878. #ifdef CONFIG_WIFI_DISPLAY
  1879. if (p2p->wfd_ie_probe_resp)
  1880. wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
  1881. #endif /* CONFIG_WIFI_DISPLAY */
  1882. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
  1883. wpabuf_put_buf(buf,
  1884. p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
  1885. /* P2P IE */
  1886. len = p2p_buf_add_ie_hdr(buf);
  1887. p2p_buf_add_capability(buf, p2p->dev_capab &
  1888. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
  1889. if (p2p->ext_listen_interval)
  1890. p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
  1891. p2p->ext_listen_interval);
  1892. p2p_buf_add_device_info(buf, p2p, NULL);
  1893. p2p_buf_update_ie_hdr(buf, len);
  1894. if (query_count) {
  1895. p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
  1896. p2p->p2ps_adv_list);
  1897. }
  1898. return buf;
  1899. }
  1900. static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
  1901. struct wpabuf *ies,
  1902. const u8 *addr, int rx_freq)
  1903. {
  1904. struct ieee80211_mgmt *resp;
  1905. u8 channel, op_class;
  1906. resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
  1907. u.probe_resp.variable));
  1908. resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
  1909. (WLAN_FC_STYPE_PROBE_RESP << 4));
  1910. os_memcpy(resp->da, addr, ETH_ALEN);
  1911. os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
  1912. os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
  1913. resp->u.probe_resp.beacon_int = host_to_le16(100);
  1914. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  1915. resp->u.probe_resp.capab_info =
  1916. host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
  1917. WLAN_CAPABILITY_PRIVACY |
  1918. WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1919. wpabuf_put_u8(buf, WLAN_EID_SSID);
  1920. wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
  1921. wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  1922. wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
  1923. wpabuf_put_u8(buf, 8);
  1924. wpabuf_put_u8(buf, (60 / 5) | 0x80);
  1925. wpabuf_put_u8(buf, 90 / 5);
  1926. wpabuf_put_u8(buf, (120 / 5) | 0x80);
  1927. wpabuf_put_u8(buf, 180 / 5);
  1928. wpabuf_put_u8(buf, (240 / 5) | 0x80);
  1929. wpabuf_put_u8(buf, 360 / 5);
  1930. wpabuf_put_u8(buf, 480 / 5);
  1931. wpabuf_put_u8(buf, 540 / 5);
  1932. if (!rx_freq) {
  1933. channel = p2p->cfg->channel;
  1934. } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
  1935. p2p_err(p2p, "Failed to convert freq to channel");
  1936. return -1;
  1937. }
  1938. wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
  1939. wpabuf_put_u8(buf, 1);
  1940. wpabuf_put_u8(buf, channel);
  1941. wpabuf_put_buf(buf, ies);
  1942. return 0;
  1943. }
  1944. static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
  1945. {
  1946. struct p2ps_advertisement *adv_data;
  1947. int any_wfa;
  1948. p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
  1949. /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
  1950. any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
  1951. adv_data = p2p->p2ps_adv_list;
  1952. while (adv_data) {
  1953. if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
  1954. return 1; /* exact hash match */
  1955. if (any_wfa &&
  1956. os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
  1957. os_strlen(P2PS_WILD_HASH_STR)) == 0)
  1958. return 1; /* WFA service match */
  1959. adv_data = adv_data->next;
  1960. }
  1961. return 0;
  1962. }
  1963. static enum p2p_probe_req_status
  1964. p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
  1965. const u8 *bssid, const u8 *ie, size_t ie_len,
  1966. unsigned int rx_freq)
  1967. {
  1968. struct ieee802_11_elems elems;
  1969. struct wpabuf *buf;
  1970. struct p2p_message msg;
  1971. struct wpabuf *ies;
  1972. if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
  1973. ParseFailed) {
  1974. /* Ignore invalid Probe Request frames */
  1975. p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
  1976. return P2P_PREQ_MALFORMED;
  1977. }
  1978. if (elems.p2p == NULL) {
  1979. /* not a P2P probe - ignore it */
  1980. p2p_dbg(p2p, "Not a P2P probe - ignore it");
  1981. return P2P_PREQ_NOT_P2P;
  1982. }
  1983. if (dst && !is_broadcast_ether_addr(dst) &&
  1984. os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
  1985. /* Not sent to the broadcast address or our P2P Device Address
  1986. */
  1987. p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
  1988. MAC2STR(dst));
  1989. return P2P_PREQ_NOT_PROCESSED;
  1990. }
  1991. if (bssid && !is_broadcast_ether_addr(bssid)) {
  1992. /* Not sent to the Wildcard BSSID */
  1993. p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
  1994. MAC2STR(bssid));
  1995. return P2P_PREQ_NOT_PROCESSED;
  1996. }
  1997. if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
  1998. os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
  1999. 0) {
  2000. /* not using P2P Wildcard SSID - ignore */
  2001. p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
  2002. return P2P_PREQ_NOT_PROCESSED;
  2003. }
  2004. if (supp_rates_11b_only(&elems)) {
  2005. /* Indicates support for 11b rates only */
  2006. p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
  2007. return P2P_PREQ_NOT_P2P;
  2008. }
  2009. os_memset(&msg, 0, sizeof(msg));
  2010. if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
  2011. /* Could not parse P2P attributes */
  2012. p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
  2013. return P2P_PREQ_NOT_P2P;
  2014. }
  2015. if (msg.service_hash && msg.service_hash_count) {
  2016. const u8 *hash = msg.service_hash;
  2017. u8 i;
  2018. int p2ps_svc_found = 0;
  2019. p2p_dbg(p2p, "in_listen=%d drv_in_listen=%d when received P2PS Probe Request at %u MHz; own Listen channel %u, pending listen freq %u MHz",
  2020. p2p->in_listen, p2p->drv_in_listen, rx_freq,
  2021. p2p->cfg->channel, p2p->pending_listen_freq);
  2022. if (!p2p->in_listen && !p2p->drv_in_listen &&
  2023. p2p->pending_listen_freq && rx_freq &&
  2024. rx_freq != p2p->pending_listen_freq) {
  2025. p2p_dbg(p2p, "Do not reply to Probe Request frame that was received on %u MHz while waiting to start Listen state on %u MHz",
  2026. rx_freq, p2p->pending_listen_freq);
  2027. p2p_parse_free(&msg);
  2028. return P2P_PREQ_NOT_LISTEN;
  2029. }
  2030. for (i = 0; i < msg.service_hash_count; i++) {
  2031. if (p2p_service_find_asp(p2p, hash)) {
  2032. p2p_dbg(p2p, "Service Hash match found: "
  2033. MACSTR, MAC2STR(hash));
  2034. p2ps_svc_found = 1;
  2035. break;
  2036. }
  2037. hash += P2PS_HASH_LEN;
  2038. }
  2039. /* Probed hash unknown */
  2040. if (!p2ps_svc_found) {
  2041. p2p_dbg(p2p, "No Service Hash match found");
  2042. p2p_parse_free(&msg);
  2043. return P2P_PREQ_NOT_PROCESSED;
  2044. }
  2045. } else {
  2046. /* This is not a P2PS Probe Request */
  2047. p2p_dbg(p2p, "No P2PS Hash in Probe Request");
  2048. if (!p2p->in_listen || !p2p->drv_in_listen) {
  2049. /* not in Listen state - ignore Probe Request */
  2050. p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
  2051. p2p->in_listen, p2p->drv_in_listen);
  2052. p2p_parse_free(&msg);
  2053. return P2P_PREQ_NOT_LISTEN;
  2054. }
  2055. }
  2056. if (msg.device_id &&
  2057. os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
  2058. /* Device ID did not match */
  2059. p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
  2060. MAC2STR(msg.device_id));
  2061. p2p_parse_free(&msg);
  2062. return P2P_PREQ_NOT_PROCESSED;
  2063. }
  2064. /* Check Requested Device Type match */
  2065. if (msg.wps_attributes &&
  2066. !p2p_match_dev_type(p2p, msg.wps_attributes)) {
  2067. /* No match with Requested Device Type */
  2068. p2p_dbg(p2p, "Probe Req requestred Device Type did not match - ignore it");
  2069. p2p_parse_free(&msg);
  2070. return P2P_PREQ_NOT_PROCESSED;
  2071. }
  2072. if (!p2p->cfg->send_probe_resp) {
  2073. /* Response generated elsewhere */
  2074. p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
  2075. p2p_parse_free(&msg);
  2076. return P2P_PREQ_NOT_PROCESSED;
  2077. }
  2078. p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
  2079. /*
  2080. * We do not really have a specific BSS that this frame is advertising,
  2081. * so build a frame that has some information in valid format. This is
  2082. * really only used for discovery purposes, not to learn exact BSS
  2083. * parameters.
  2084. */
  2085. ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
  2086. msg.service_hash_count);
  2087. p2p_parse_free(&msg);
  2088. if (ies == NULL)
  2089. return P2P_PREQ_NOT_PROCESSED;
  2090. buf = wpabuf_alloc(200 + wpabuf_len(ies));
  2091. if (buf == NULL) {
  2092. wpabuf_free(ies);
  2093. return P2P_PREQ_NOT_PROCESSED;
  2094. }
  2095. if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
  2096. wpabuf_free(ies);
  2097. wpabuf_free(buf);
  2098. return P2P_PREQ_NOT_PROCESSED;
  2099. }
  2100. wpabuf_free(ies);
  2101. p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
  2102. wpabuf_free(buf);
  2103. return P2P_PREQ_PROCESSED;
  2104. }
  2105. enum p2p_probe_req_status
  2106. p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
  2107. const u8 *bssid, const u8 *ie, size_t ie_len,
  2108. unsigned int rx_freq, int p2p_lo_started)
  2109. {
  2110. enum p2p_probe_req_status res;
  2111. p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
  2112. if (p2p_lo_started) {
  2113. p2p_dbg(p2p,
  2114. "Probe Response is offloaded, do not reply Probe Request");
  2115. return P2P_PREQ_PROCESSED;
  2116. }
  2117. res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
  2118. if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
  2119. return res;
  2120. /*
  2121. * Activate a pending GO Negotiation/Invite flow if a received Probe
  2122. * Request frame is from an expected peer. Some devices may share the
  2123. * same address for P2P and non-P2P STA running simultaneously. The
  2124. * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
  2125. * return values verified above ensure we are handling a Probe Request
  2126. * frame from a P2P peer.
  2127. */
  2128. if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
  2129. p2p->go_neg_peer &&
  2130. os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
  2131. == 0 &&
  2132. !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
  2133. /* Received a Probe Request from GO Negotiation peer */
  2134. p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
  2135. eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
  2136. eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
  2137. return res;
  2138. }
  2139. if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
  2140. p2p->invite_peer &&
  2141. (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
  2142. os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
  2143. == 0) {
  2144. /* Received a Probe Request from Invite peer */
  2145. p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
  2146. eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
  2147. eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
  2148. return res;
  2149. }
  2150. return res;
  2151. }
  2152. static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
  2153. u8 *buf, size_t len, struct wpabuf *p2p_ie)
  2154. {
  2155. struct wpabuf *tmp;
  2156. u8 *lpos;
  2157. size_t tmplen;
  2158. int res;
  2159. u8 group_capab;
  2160. struct p2p_message msg;
  2161. if (p2p_ie == NULL)
  2162. return 0; /* WLAN AP is not a P2P manager */
  2163. os_memset(&msg, 0, sizeof(msg));
  2164. if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
  2165. return 0;
  2166. p2p_dbg(p2p, "BSS P2P manageability %s",
  2167. msg.manageability ? "enabled" : "disabled");
  2168. if (!msg.manageability)
  2169. return 0;
  2170. /*
  2171. * (Re)Association Request - P2P IE
  2172. * P2P Capability attribute (shall be present)
  2173. * P2P Interface attribute (present if concurrent device and
  2174. * P2P Management is enabled)
  2175. */
  2176. tmp = wpabuf_alloc(200);
  2177. if (tmp == NULL)
  2178. return -1;
  2179. lpos = p2p_buf_add_ie_hdr(tmp);
  2180. group_capab = 0;
  2181. if (p2p->num_groups > 0) {
  2182. group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
  2183. if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
  2184. (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
  2185. p2p->cross_connect)
  2186. group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
  2187. }
  2188. p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
  2189. if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
  2190. (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
  2191. p2p_buf_add_p2p_interface(tmp, p2p);
  2192. p2p_buf_update_ie_hdr(tmp, lpos);
  2193. tmplen = wpabuf_len(tmp);
  2194. if (tmplen > len)
  2195. res = -1;
  2196. else {
  2197. os_memcpy(buf, wpabuf_head(tmp), tmplen);
  2198. res = tmplen;
  2199. }
  2200. wpabuf_free(tmp);
  2201. return res;
  2202. }
  2203. int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
  2204. size_t len, int p2p_group, struct wpabuf *p2p_ie)
  2205. {
  2206. struct wpabuf *tmp;
  2207. u8 *lpos;
  2208. struct p2p_device *peer;
  2209. size_t tmplen;
  2210. int res;
  2211. size_t extra = 0;
  2212. if (!p2p_group)
  2213. return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
  2214. #ifdef CONFIG_WIFI_DISPLAY
  2215. if (p2p->wfd_ie_assoc_req)
  2216. extra = wpabuf_len(p2p->wfd_ie_assoc_req);
  2217. #endif /* CONFIG_WIFI_DISPLAY */
  2218. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
  2219. extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
  2220. /*
  2221. * (Re)Association Request - P2P IE
  2222. * P2P Capability attribute (shall be present)
  2223. * Extended Listen Timing (may be present)
  2224. * P2P Device Info attribute (shall be present)
  2225. */
  2226. tmp = wpabuf_alloc(200 + extra);
  2227. if (tmp == NULL)
  2228. return -1;
  2229. #ifdef CONFIG_WIFI_DISPLAY
  2230. if (p2p->wfd_ie_assoc_req)
  2231. wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
  2232. #endif /* CONFIG_WIFI_DISPLAY */
  2233. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
  2234. wpabuf_put_buf(tmp,
  2235. p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
  2236. peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
  2237. lpos = p2p_buf_add_ie_hdr(tmp);
  2238. p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
  2239. if (p2p->ext_listen_interval)
  2240. p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
  2241. p2p->ext_listen_interval);
  2242. p2p_buf_add_device_info(tmp, p2p, peer);
  2243. p2p_buf_update_ie_hdr(tmp, lpos);
  2244. tmplen = wpabuf_len(tmp);
  2245. if (tmplen > len)
  2246. res = -1;
  2247. else {
  2248. os_memcpy(buf, wpabuf_head(tmp), tmplen);
  2249. res = tmplen;
  2250. }
  2251. wpabuf_free(tmp);
  2252. return res;
  2253. }
  2254. int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
  2255. {
  2256. struct wpabuf *p2p_ie;
  2257. int ret;
  2258. p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
  2259. if (p2p_ie == NULL)
  2260. return 0;
  2261. ret = p2p_attr_text(p2p_ie, buf, end);
  2262. wpabuf_free(p2p_ie);
  2263. return ret;
  2264. }
  2265. struct p2ps_advertisement *
  2266. p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
  2267. {
  2268. struct p2ps_advertisement *adv_data;
  2269. if (!p2p)
  2270. return NULL;
  2271. adv_data = p2p->p2ps_adv_list;
  2272. while (adv_data) {
  2273. if (adv_data->id == adv_id)
  2274. return adv_data;
  2275. adv_data = adv_data->next;
  2276. }
  2277. return NULL;
  2278. }
  2279. int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
  2280. {
  2281. struct p2ps_advertisement *adv_data;
  2282. struct p2ps_advertisement **prior;
  2283. if (!p2p)
  2284. return -1;
  2285. adv_data = p2p->p2ps_adv_list;
  2286. prior = &p2p->p2ps_adv_list;
  2287. while (adv_data) {
  2288. if (adv_data->id == adv_id) {
  2289. p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
  2290. *prior = adv_data->next;
  2291. os_free(adv_data);
  2292. return 0;
  2293. }
  2294. prior = &adv_data->next;
  2295. adv_data = adv_data->next;
  2296. }
  2297. return -1;
  2298. }
  2299. int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
  2300. const char *adv_str, u8 svc_state, u16 config_methods,
  2301. const char *svc_info, const u8 *cpt_priority)
  2302. {
  2303. struct p2ps_advertisement *adv_data, *tmp, **prev;
  2304. u8 buf[P2PS_HASH_LEN];
  2305. size_t adv_data_len, adv_len, info_len = 0;
  2306. int i;
  2307. if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
  2308. return -1;
  2309. if (!(config_methods & p2p->cfg->config_methods)) {
  2310. p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
  2311. config_methods, p2p->cfg->config_methods);
  2312. return -1;
  2313. }
  2314. if (!p2ps_gen_hash(p2p, adv_str, buf))
  2315. return -1;
  2316. if (svc_info)
  2317. info_len = os_strlen(svc_info);
  2318. adv_len = os_strlen(adv_str);
  2319. adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
  2320. info_len + 1;
  2321. adv_data = os_zalloc(adv_data_len);
  2322. if (!adv_data)
  2323. return -1;
  2324. os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
  2325. adv_data->id = adv_id;
  2326. adv_data->state = svc_state;
  2327. adv_data->config_methods = config_methods & p2p->cfg->config_methods;
  2328. adv_data->auto_accept = (u8) auto_accept;
  2329. os_memcpy(adv_data->svc_name, adv_str, adv_len);
  2330. for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
  2331. adv_data->cpt_priority[i] = cpt_priority[i];
  2332. adv_data->cpt_mask |= cpt_priority[i];
  2333. }
  2334. if (svc_info && info_len) {
  2335. adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
  2336. os_memcpy(adv_data->svc_info, svc_info, info_len);
  2337. }
  2338. /*
  2339. * Group Advertisements by service string. They do not need to be
  2340. * sorted, but groups allow easier Probe Response instance grouping
  2341. */
  2342. tmp = p2p->p2ps_adv_list;
  2343. prev = &p2p->p2ps_adv_list;
  2344. while (tmp) {
  2345. if (tmp->id == adv_data->id) {
  2346. if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
  2347. os_free(adv_data);
  2348. return -1;
  2349. }
  2350. adv_data->next = tmp->next;
  2351. *prev = adv_data;
  2352. os_free(tmp);
  2353. goto inserted;
  2354. } else {
  2355. if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
  2356. adv_data->next = tmp->next;
  2357. tmp->next = adv_data;
  2358. goto inserted;
  2359. }
  2360. }
  2361. prev = &tmp->next;
  2362. tmp = tmp->next;
  2363. }
  2364. /* No svc_name match found */
  2365. adv_data->next = p2p->p2ps_adv_list;
  2366. p2p->p2ps_adv_list = adv_data;
  2367. inserted:
  2368. p2p_dbg(p2p,
  2369. "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
  2370. adv_id, adv_data->config_methods, svc_state, adv_str,
  2371. adv_data->cpt_mask);
  2372. return 0;
  2373. }
  2374. void p2p_service_flush_asp(struct p2p_data *p2p)
  2375. {
  2376. struct p2ps_advertisement *adv, *prev;
  2377. if (!p2p)
  2378. return;
  2379. adv = p2p->p2ps_adv_list;
  2380. while (adv) {
  2381. prev = adv;
  2382. adv = adv->next;
  2383. os_free(prev);
  2384. }
  2385. p2p->p2ps_adv_list = NULL;
  2386. p2ps_prov_free(p2p);
  2387. p2p_dbg(p2p, "All ASP advertisements flushed");
  2388. }
  2389. int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
  2390. {
  2391. struct p2p_message msg;
  2392. os_memset(&msg, 0, sizeof(msg));
  2393. if (p2p_parse_p2p_ie(p2p_ie, &msg))
  2394. return -1;
  2395. if (msg.p2p_device_addr) {
  2396. os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
  2397. return 0;
  2398. } else if (msg.device_id) {
  2399. os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
  2400. return 0;
  2401. }
  2402. return -1;
  2403. }
  2404. int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
  2405. {
  2406. struct wpabuf *p2p_ie;
  2407. int ret;
  2408. p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
  2409. P2P_IE_VENDOR_TYPE);
  2410. if (p2p_ie == NULL)
  2411. return -1;
  2412. ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
  2413. wpabuf_free(p2p_ie);
  2414. return ret;
  2415. }
  2416. static void p2p_clear_go_neg(struct p2p_data *p2p)
  2417. {
  2418. p2p->go_neg_peer = NULL;
  2419. p2p_clear_timeout(p2p);
  2420. p2p_set_state(p2p, P2P_IDLE);
  2421. }
  2422. void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
  2423. {
  2424. if (p2p->go_neg_peer == NULL) {
  2425. p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
  2426. return; /* No pending Group Formation */
  2427. }
  2428. if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
  2429. 0) {
  2430. p2p_dbg(p2p, "Ignore WPS registration success notification for "
  2431. MACSTR " (GO Negotiation peer " MACSTR ")",
  2432. MAC2STR(mac_addr),
  2433. MAC2STR(p2p->go_neg_peer->intended_addr));
  2434. return; /* Ignore unexpected peer address */
  2435. }
  2436. p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
  2437. MAC2STR(mac_addr));
  2438. p2p_clear_go_neg(p2p);
  2439. }
  2440. void p2p_group_formation_failed(struct p2p_data *p2p)
  2441. {
  2442. if (p2p->go_neg_peer == NULL) {
  2443. p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
  2444. return; /* No pending Group Formation */
  2445. }
  2446. p2p_dbg(p2p, "Group Formation failed with " MACSTR,
  2447. MAC2STR(p2p->go_neg_peer->intended_addr));
  2448. p2p_clear_go_neg(p2p);
  2449. }
  2450. struct p2p_data * p2p_init(const struct p2p_config *cfg)
  2451. {
  2452. struct p2p_data *p2p;
  2453. if (cfg->max_peers < 1 ||
  2454. cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
  2455. return NULL;
  2456. p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
  2457. if (p2p == NULL)
  2458. return NULL;
  2459. p2p->cfg = (struct p2p_config *) (p2p + 1);
  2460. os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
  2461. if (cfg->dev_name)
  2462. p2p->cfg->dev_name = os_strdup(cfg->dev_name);
  2463. if (cfg->manufacturer)
  2464. p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
  2465. if (cfg->model_name)
  2466. p2p->cfg->model_name = os_strdup(cfg->model_name);
  2467. if (cfg->model_number)
  2468. p2p->cfg->model_number = os_strdup(cfg->model_number);
  2469. if (cfg->serial_number)
  2470. p2p->cfg->serial_number = os_strdup(cfg->serial_number);
  2471. if (cfg->pref_chan) {
  2472. p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
  2473. sizeof(struct p2p_channel));
  2474. if (p2p->cfg->pref_chan) {
  2475. os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
  2476. cfg->num_pref_chan *
  2477. sizeof(struct p2p_channel));
  2478. } else
  2479. p2p->cfg->num_pref_chan = 0;
  2480. }
  2481. p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
  2482. p2p->min_disc_int = 1;
  2483. p2p->max_disc_int = 3;
  2484. p2p->max_disc_tu = -1;
  2485. if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
  2486. p2p->next_tie_breaker = 0;
  2487. p2p->next_tie_breaker &= 0x01;
  2488. if (cfg->sd_request)
  2489. p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
  2490. p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
  2491. if (cfg->concurrent_operations)
  2492. p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
  2493. p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  2494. dl_list_init(&p2p->devices);
  2495. p2p->go_timeout = 100;
  2496. p2p->client_timeout = 20;
  2497. p2p->num_p2p_sd_queries = 0;
  2498. p2p_dbg(p2p, "initialized");
  2499. p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
  2500. p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
  2501. return p2p;
  2502. }
  2503. void p2p_deinit(struct p2p_data *p2p)
  2504. {
  2505. #ifdef CONFIG_WIFI_DISPLAY
  2506. wpabuf_free(p2p->wfd_ie_beacon);
  2507. wpabuf_free(p2p->wfd_ie_probe_req);
  2508. wpabuf_free(p2p->wfd_ie_probe_resp);
  2509. wpabuf_free(p2p->wfd_ie_assoc_req);
  2510. wpabuf_free(p2p->wfd_ie_invitation);
  2511. wpabuf_free(p2p->wfd_ie_prov_disc_req);
  2512. wpabuf_free(p2p->wfd_ie_prov_disc_resp);
  2513. wpabuf_free(p2p->wfd_ie_go_neg);
  2514. wpabuf_free(p2p->wfd_dev_info);
  2515. wpabuf_free(p2p->wfd_assoc_bssid);
  2516. wpabuf_free(p2p->wfd_coupled_sink_info);
  2517. #endif /* CONFIG_WIFI_DISPLAY */
  2518. eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
  2519. eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
  2520. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  2521. p2p_flush(p2p);
  2522. p2p_free_req_dev_types(p2p);
  2523. os_free(p2p->cfg->dev_name);
  2524. os_free(p2p->cfg->manufacturer);
  2525. os_free(p2p->cfg->model_name);
  2526. os_free(p2p->cfg->model_number);
  2527. os_free(p2p->cfg->serial_number);
  2528. os_free(p2p->cfg->pref_chan);
  2529. os_free(p2p->groups);
  2530. p2ps_prov_free(p2p);
  2531. wpabuf_free(p2p->sd_resp);
  2532. p2p_remove_wps_vendor_extensions(p2p);
  2533. os_free(p2p->no_go_freq.range);
  2534. p2p_service_flush_asp(p2p);
  2535. os_free(p2p);
  2536. }
  2537. void p2p_flush(struct p2p_data *p2p)
  2538. {
  2539. struct p2p_device *dev, *prev;
  2540. p2p_ext_listen(p2p, 0, 0);
  2541. p2p_stop_find(p2p);
  2542. dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
  2543. list) {
  2544. dl_list_del(&dev->list);
  2545. p2p_device_free(p2p, dev);
  2546. }
  2547. p2p_free_sd_queries(p2p);
  2548. os_free(p2p->after_scan_tx);
  2549. p2p->after_scan_tx = NULL;
  2550. p2p->ssid_set = 0;
  2551. p2ps_prov_free(p2p);
  2552. p2p_reset_pending_pd(p2p);
  2553. }
  2554. int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
  2555. {
  2556. struct p2p_device *dev;
  2557. dev = p2p_get_device(p2p, addr);
  2558. if (dev == NULL)
  2559. return -1;
  2560. p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
  2561. if (p2p->go_neg_peer == dev) {
  2562. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  2563. p2p->go_neg_peer = NULL;
  2564. }
  2565. dev->wps_method = WPS_NOT_READY;
  2566. dev->oob_pw_id = 0;
  2567. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
  2568. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
  2569. /* Check if after_scan_tx is for this peer. If so free it */
  2570. if (p2p->after_scan_tx &&
  2571. os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
  2572. os_free(p2p->after_scan_tx);
  2573. p2p->after_scan_tx = NULL;
  2574. }
  2575. return 0;
  2576. }
  2577. int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
  2578. {
  2579. os_free(p2p->cfg->dev_name);
  2580. if (dev_name) {
  2581. p2p->cfg->dev_name = os_strdup(dev_name);
  2582. if (p2p->cfg->dev_name == NULL)
  2583. return -1;
  2584. } else
  2585. p2p->cfg->dev_name = NULL;
  2586. return 0;
  2587. }
  2588. int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
  2589. {
  2590. os_free(p2p->cfg->manufacturer);
  2591. p2p->cfg->manufacturer = NULL;
  2592. if (manufacturer) {
  2593. p2p->cfg->manufacturer = os_strdup(manufacturer);
  2594. if (p2p->cfg->manufacturer == NULL)
  2595. return -1;
  2596. }
  2597. return 0;
  2598. }
  2599. int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
  2600. {
  2601. os_free(p2p->cfg->model_name);
  2602. p2p->cfg->model_name = NULL;
  2603. if (model_name) {
  2604. p2p->cfg->model_name = os_strdup(model_name);
  2605. if (p2p->cfg->model_name == NULL)
  2606. return -1;
  2607. }
  2608. return 0;
  2609. }
  2610. int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
  2611. {
  2612. os_free(p2p->cfg->model_number);
  2613. p2p->cfg->model_number = NULL;
  2614. if (model_number) {
  2615. p2p->cfg->model_number = os_strdup(model_number);
  2616. if (p2p->cfg->model_number == NULL)
  2617. return -1;
  2618. }
  2619. return 0;
  2620. }
  2621. int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
  2622. {
  2623. os_free(p2p->cfg->serial_number);
  2624. p2p->cfg->serial_number = NULL;
  2625. if (serial_number) {
  2626. p2p->cfg->serial_number = os_strdup(serial_number);
  2627. if (p2p->cfg->serial_number == NULL)
  2628. return -1;
  2629. }
  2630. return 0;
  2631. }
  2632. void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
  2633. {
  2634. p2p->cfg->config_methods = config_methods;
  2635. }
  2636. void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
  2637. {
  2638. os_memcpy(p2p->cfg->uuid, uuid, 16);
  2639. }
  2640. int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
  2641. {
  2642. os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
  2643. return 0;
  2644. }
  2645. int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
  2646. size_t num_dev_types)
  2647. {
  2648. if (num_dev_types > P2P_SEC_DEVICE_TYPES)
  2649. num_dev_types = P2P_SEC_DEVICE_TYPES;
  2650. p2p->cfg->num_sec_dev_types = num_dev_types;
  2651. os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
  2652. return 0;
  2653. }
  2654. void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
  2655. {
  2656. int i;
  2657. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  2658. wpabuf_free(p2p->wps_vendor_ext[i]);
  2659. p2p->wps_vendor_ext[i] = NULL;
  2660. }
  2661. }
  2662. int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
  2663. const struct wpabuf *vendor_ext)
  2664. {
  2665. int i;
  2666. if (vendor_ext == NULL)
  2667. return -1;
  2668. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  2669. if (p2p->wps_vendor_ext[i] == NULL)
  2670. break;
  2671. }
  2672. if (i >= P2P_MAX_WPS_VENDOR_EXT)
  2673. return -1;
  2674. p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
  2675. if (p2p->wps_vendor_ext[i] == NULL)
  2676. return -1;
  2677. return 0;
  2678. }
  2679. int p2p_set_country(struct p2p_data *p2p, const char *country)
  2680. {
  2681. os_memcpy(p2p->cfg->country, country, 3);
  2682. return 0;
  2683. }
  2684. static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
  2685. {
  2686. int res;
  2687. if (dev->sd_pending_bcast_queries == 0) {
  2688. /* Initialize with total number of registered broadcast
  2689. * SD queries. */
  2690. dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
  2691. }
  2692. res = p2p_start_sd(p2p, dev);
  2693. if (res == -2)
  2694. return -2;
  2695. if (res == 0)
  2696. return 1;
  2697. if (dev->req_config_methods &&
  2698. !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
  2699. p2p_dbg(p2p, "Send pending Provision Discovery Request to "
  2700. MACSTR " (config methods 0x%x)",
  2701. MAC2STR(dev->info.p2p_device_addr),
  2702. dev->req_config_methods);
  2703. if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
  2704. return 1;
  2705. }
  2706. return 0;
  2707. }
  2708. void p2p_continue_find(struct p2p_data *p2p)
  2709. {
  2710. struct p2p_device *dev;
  2711. int found, res;
  2712. p2p_set_state(p2p, P2P_SEARCH);
  2713. /* Continue from the device following the last iteration */
  2714. found = 0;
  2715. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  2716. if (dev == p2p->last_p2p_find_oper) {
  2717. found = 1;
  2718. continue;
  2719. }
  2720. if (!found)
  2721. continue;
  2722. res = p2p_pre_find_operation(p2p, dev);
  2723. if (res > 0) {
  2724. p2p->last_p2p_find_oper = dev;
  2725. return;
  2726. }
  2727. if (res == -2)
  2728. goto skip_sd;
  2729. }
  2730. /*
  2731. * Wrap around to the beginning of the list and continue until the last
  2732. * iteration device.
  2733. */
  2734. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  2735. res = p2p_pre_find_operation(p2p, dev);
  2736. if (res > 0) {
  2737. p2p->last_p2p_find_oper = dev;
  2738. return;
  2739. }
  2740. if (res == -2)
  2741. goto skip_sd;
  2742. if (dev == p2p->last_p2p_find_oper)
  2743. break;
  2744. }
  2745. skip_sd:
  2746. os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
  2747. p2p_listen_in_find(p2p, 1);
  2748. }
  2749. static void p2p_sd_cb(struct p2p_data *p2p, int success)
  2750. {
  2751. p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
  2752. success);
  2753. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2754. if (!success) {
  2755. if (p2p->sd_peer) {
  2756. if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
  2757. os_memcpy(p2p->sd_query_no_ack,
  2758. p2p->sd_peer->info.p2p_device_addr,
  2759. ETH_ALEN);
  2760. p2p_dbg(p2p,
  2761. "First SD Query no-ACK in this search iteration: "
  2762. MACSTR, MAC2STR(p2p->sd_query_no_ack));
  2763. }
  2764. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2765. }
  2766. p2p->sd_peer = NULL;
  2767. if (p2p->state != P2P_IDLE)
  2768. p2p_continue_find(p2p);
  2769. return;
  2770. }
  2771. if (p2p->sd_peer == NULL) {
  2772. p2p_dbg(p2p, "No SD peer entry known");
  2773. if (p2p->state != P2P_IDLE)
  2774. p2p_continue_find(p2p);
  2775. return;
  2776. }
  2777. if (p2p->sd_query && p2p->sd_query->for_all_peers) {
  2778. /* Update the pending broadcast SD query count for this device
  2779. */
  2780. p2p->sd_peer->sd_pending_bcast_queries--;
  2781. /*
  2782. * If there are no pending broadcast queries for this device,
  2783. * mark it as done (-1).
  2784. */
  2785. if (p2p->sd_peer->sd_pending_bcast_queries == 0)
  2786. p2p->sd_peer->sd_pending_bcast_queries = -1;
  2787. }
  2788. /* Wait for response from the peer */
  2789. p2p_set_state(p2p, P2P_SD_DURING_FIND);
  2790. p2p_set_timeout(p2p, 0, 200000);
  2791. }
  2792. /**
  2793. * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
  2794. * @p2p: P2P module context from p2p_init()
  2795. */
  2796. static void p2p_retry_pd(struct p2p_data *p2p)
  2797. {
  2798. struct p2p_device *dev;
  2799. /*
  2800. * Retry the prov disc req attempt only for the peer that the user had
  2801. * requested.
  2802. */
  2803. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  2804. if (os_memcmp(p2p->pending_pd_devaddr,
  2805. dev->info.p2p_device_addr, ETH_ALEN) != 0)
  2806. continue;
  2807. if (!dev->req_config_methods)
  2808. continue;
  2809. p2p_dbg(p2p, "Send pending Provision Discovery Request to "
  2810. MACSTR " (config methods 0x%x)",
  2811. MAC2STR(dev->info.p2p_device_addr),
  2812. dev->req_config_methods);
  2813. p2p_send_prov_disc_req(p2p, dev,
  2814. dev->flags & P2P_DEV_PD_FOR_JOIN,
  2815. p2p->pd_force_freq);
  2816. return;
  2817. }
  2818. }
  2819. static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
  2820. {
  2821. p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
  2822. success);
  2823. /*
  2824. * Postpone resetting the pending action state till after we actually
  2825. * time out. This allows us to take some action like notifying any
  2826. * interested parties about no response to the request.
  2827. *
  2828. * When the timer (below) goes off we check in IDLE, SEARCH, or
  2829. * LISTEN_ONLY state, which are the only allowed states to issue a PD
  2830. * requests in, if this was still pending and then raise notification.
  2831. */
  2832. if (!success) {
  2833. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2834. if (p2p->user_initiated_pd &&
  2835. (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
  2836. {
  2837. /* Retry request from timeout to avoid busy loops */
  2838. p2p->pending_action_state = P2P_PENDING_PD;
  2839. p2p_set_timeout(p2p, 0, 50000);
  2840. } else if (p2p->state != P2P_IDLE)
  2841. p2p_continue_find(p2p);
  2842. else if (p2p->user_initiated_pd) {
  2843. p2p->pending_action_state = P2P_PENDING_PD;
  2844. p2p_set_timeout(p2p, 0, 300000);
  2845. }
  2846. return;
  2847. }
  2848. /*
  2849. * If after PD Request the peer doesn't expect to receive PD Response
  2850. * the PD Request ACK indicates a completion of the current PD. This
  2851. * happens only on the advertiser side sending the follow-on PD Request
  2852. * with the status different than 12 (Success: accepted by user).
  2853. */
  2854. if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
  2855. p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
  2856. p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
  2857. if (p2p->send_action_in_progress) {
  2858. p2p->send_action_in_progress = 0;
  2859. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2860. }
  2861. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2862. if (p2p->cfg->p2ps_prov_complete) {
  2863. p2p->cfg->p2ps_prov_complete(
  2864. p2p->cfg->cb_ctx,
  2865. p2p->p2ps_prov->status,
  2866. p2p->p2ps_prov->adv_mac,
  2867. p2p->p2ps_prov->adv_mac,
  2868. p2p->p2ps_prov->session_mac,
  2869. NULL, p2p->p2ps_prov->adv_id,
  2870. p2p->p2ps_prov->session_id,
  2871. 0, 0, NULL, 0, 0, 0,
  2872. NULL, NULL, 0, 0, NULL, 0);
  2873. }
  2874. if (p2p->user_initiated_pd)
  2875. p2p_reset_pending_pd(p2p);
  2876. p2ps_prov_free(p2p);
  2877. return;
  2878. }
  2879. /*
  2880. * This postponing, of resetting pending_action_state, needs to be
  2881. * done only for user initiated PD requests and not internal ones.
  2882. */
  2883. if (p2p->user_initiated_pd)
  2884. p2p->pending_action_state = P2P_PENDING_PD;
  2885. else
  2886. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2887. /* Wait for response from the peer */
  2888. if (p2p->state == P2P_SEARCH)
  2889. p2p_set_state(p2p, P2P_PD_DURING_FIND);
  2890. p2p_set_timeout(p2p, 0, 200000);
  2891. }
  2892. static int p2p_check_after_scan_tx_continuation(struct p2p_data *p2p)
  2893. {
  2894. if (p2p->after_scan_tx_in_progress) {
  2895. p2p->after_scan_tx_in_progress = 0;
  2896. if (p2p->start_after_scan != P2P_AFTER_SCAN_NOTHING &&
  2897. p2p_run_after_scan(p2p))
  2898. return 1;
  2899. if (p2p->state == P2P_SEARCH) {
  2900. p2p_dbg(p2p, "Continue find after after_scan_tx completion");
  2901. p2p_continue_find(p2p);
  2902. }
  2903. }
  2904. return 0;
  2905. }
  2906. static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
  2907. {
  2908. p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
  2909. success);
  2910. if (p2p->send_action_in_progress) {
  2911. p2p->send_action_in_progress = 0;
  2912. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  2913. }
  2914. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  2915. if (!success)
  2916. goto continue_search;
  2917. if (!p2p->cfg->prov_disc_resp_cb ||
  2918. p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1)
  2919. goto continue_search;
  2920. p2p_dbg(p2p,
  2921. "Post-Provision Discovery operations started - do not try to continue other P2P operations");
  2922. return;
  2923. continue_search:
  2924. p2p_check_after_scan_tx_continuation(p2p);
  2925. }
  2926. int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
  2927. struct os_reltime *rx_time, int level, const u8 *ies,
  2928. size_t ies_len)
  2929. {
  2930. if (os_reltime_before(rx_time, &p2p->find_start)) {
  2931. /*
  2932. * The driver may have cached (e.g., in cfg80211 BSS table) the
  2933. * scan results for relatively long time. To avoid reporting
  2934. * stale information, update P2P peers only based on results
  2935. * that have based on frames received after the last p2p_find
  2936. * operation was started.
  2937. */
  2938. p2p_dbg(p2p, "Ignore old scan result for " MACSTR
  2939. " (rx_time=%u.%06u find_start=%u.%06u)",
  2940. MAC2STR(bssid), (unsigned int) rx_time->sec,
  2941. (unsigned int) rx_time->usec,
  2942. (unsigned int) p2p->find_start.sec,
  2943. (unsigned int) p2p->find_start.usec);
  2944. return 0;
  2945. }
  2946. p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
  2947. return 0;
  2948. }
  2949. void p2p_scan_res_handled(struct p2p_data *p2p)
  2950. {
  2951. if (!p2p->p2p_scan_running) {
  2952. p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
  2953. }
  2954. p2p->p2p_scan_running = 0;
  2955. eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
  2956. if (p2p_run_after_scan(p2p))
  2957. return;
  2958. if (p2p->state == P2P_SEARCH)
  2959. p2p_continue_find(p2p);
  2960. }
  2961. void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
  2962. unsigned int bands)
  2963. {
  2964. u8 dev_capab;
  2965. u8 *len;
  2966. #ifdef CONFIG_WIFI_DISPLAY
  2967. if (p2p->wfd_ie_probe_req)
  2968. wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
  2969. #endif /* CONFIG_WIFI_DISPLAY */
  2970. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
  2971. wpabuf_put_buf(ies,
  2972. p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
  2973. len = p2p_buf_add_ie_hdr(ies);
  2974. dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  2975. /* P2PS requires Probe Request frames to include SD bit */
  2976. if (p2p->p2ps_seek && p2p->p2ps_seek_count)
  2977. dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
  2978. p2p_buf_add_capability(ies, dev_capab, 0);
  2979. if (dev_id)
  2980. p2p_buf_add_device_id(ies, dev_id);
  2981. if (p2p->cfg->reg_class && p2p->cfg->channel)
  2982. p2p_buf_add_listen_channel(ies, p2p->cfg->country,
  2983. p2p->cfg->reg_class,
  2984. p2p->cfg->channel);
  2985. if (p2p->ext_listen_interval)
  2986. p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
  2987. p2p->ext_listen_interval);
  2988. if (bands & BAND_60_GHZ)
  2989. p2p_buf_add_device_info(ies, p2p, NULL);
  2990. if (p2p->p2ps_seek && p2p->p2ps_seek_count)
  2991. p2p_buf_add_service_hash(ies, p2p);
  2992. /* TODO: p2p_buf_add_operating_channel() if GO */
  2993. p2p_buf_update_ie_hdr(ies, len);
  2994. }
  2995. size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
  2996. {
  2997. size_t len = 100;
  2998. #ifdef CONFIG_WIFI_DISPLAY
  2999. if (p2p && p2p->wfd_ie_probe_req)
  3000. len += wpabuf_len(p2p->wfd_ie_probe_req);
  3001. #endif /* CONFIG_WIFI_DISPLAY */
  3002. if (p2p && p2p->vendor_elem &&
  3003. p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
  3004. len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
  3005. return len;
  3006. }
  3007. int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
  3008. {
  3009. return p2p_attr_text(p2p_ie, buf, end);
  3010. }
  3011. static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
  3012. {
  3013. struct p2p_device *dev = p2p->go_neg_peer;
  3014. int timeout;
  3015. p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
  3016. if (dev == NULL) {
  3017. p2p_dbg(p2p, "No pending GO Negotiation");
  3018. return;
  3019. }
  3020. if (success) {
  3021. if (dev->flags & P2P_DEV_USER_REJECTED) {
  3022. p2p_set_state(p2p, P2P_IDLE);
  3023. return;
  3024. }
  3025. } else if (dev->go_neg_req_sent) {
  3026. /* Cancel the increment from p2p_connect_send() on failure */
  3027. dev->go_neg_req_sent--;
  3028. }
  3029. if (!success &&
  3030. (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
  3031. !is_zero_ether_addr(dev->member_in_go_dev)) {
  3032. p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
  3033. MAC2STR(dev->info.p2p_device_addr));
  3034. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  3035. p2p_send_dev_disc_req(p2p, dev);
  3036. return;
  3037. }
  3038. /*
  3039. * Use P2P find, if needed, to find the other device from its listen
  3040. * channel.
  3041. */
  3042. p2p_set_state(p2p, P2P_CONNECT);
  3043. timeout = success ? 500000 : 100000;
  3044. if (!success && p2p->go_neg_peer &&
  3045. (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
  3046. unsigned int r;
  3047. /*
  3048. * Peer is expected to wait our response and we will skip the
  3049. * listen phase. Add some randomness to the wait time here to
  3050. * make it less likely to hit cases where we could end up in
  3051. * sync with peer not listening.
  3052. */
  3053. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  3054. r = 0;
  3055. timeout += r % 100000;
  3056. }
  3057. p2p_set_timeout(p2p, 0, timeout);
  3058. }
  3059. static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
  3060. {
  3061. p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
  3062. success);
  3063. if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
  3064. p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
  3065. return;
  3066. }
  3067. p2p_set_state(p2p, P2P_CONNECT);
  3068. p2p_set_timeout(p2p, 0, 500000);
  3069. }
  3070. static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
  3071. const u8 *addr)
  3072. {
  3073. p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
  3074. if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
  3075. p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
  3076. return;
  3077. }
  3078. if (success) {
  3079. struct p2p_device *dev;
  3080. dev = p2p_get_device(p2p, addr);
  3081. if (dev &&
  3082. dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
  3083. dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
  3084. }
  3085. if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
  3086. p2p_continue_find(p2p);
  3087. }
  3088. static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
  3089. enum p2p_send_action_result result)
  3090. {
  3091. struct p2p_device *dev;
  3092. p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
  3093. if (result == P2P_SEND_ACTION_FAILED) {
  3094. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  3095. p2p_go_neg_failed(p2p, -1);
  3096. return;
  3097. }
  3098. dev = p2p->go_neg_peer;
  3099. if (result == P2P_SEND_ACTION_NO_ACK) {
  3100. /*
  3101. * Retry GO Negotiation Confirmation
  3102. * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
  3103. * ACK for confirmation.
  3104. */
  3105. if (dev && dev->go_neg_conf &&
  3106. dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
  3107. p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
  3108. dev->go_neg_conf_sent);
  3109. p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
  3110. if (p2p_send_action(p2p, dev->go_neg_conf_freq,
  3111. dev->info.p2p_device_addr,
  3112. p2p->cfg->dev_addr,
  3113. dev->info.p2p_device_addr,
  3114. wpabuf_head(dev->go_neg_conf),
  3115. wpabuf_len(dev->go_neg_conf), 0) >=
  3116. 0) {
  3117. dev->go_neg_conf_sent++;
  3118. return;
  3119. }
  3120. p2p_dbg(p2p, "Failed to re-send Action frame");
  3121. /*
  3122. * Continue with the assumption that the first attempt
  3123. * went through and just the ACK frame was lost.
  3124. */
  3125. }
  3126. /*
  3127. * It looks like the TX status for GO Negotiation Confirm is
  3128. * often showing failure even when the peer has actually
  3129. * received the frame. Since the peer may change channels
  3130. * immediately after having received the frame, we may not see
  3131. * an Ack for retries, so just dropping a single frame may
  3132. * trigger this. To allow the group formation to succeed if the
  3133. * peer did indeed receive the frame, continue regardless of
  3134. * the TX status.
  3135. */
  3136. p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported");
  3137. }
  3138. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  3139. if (dev == NULL)
  3140. return;
  3141. p2p_go_complete(p2p, dev);
  3142. }
  3143. void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
  3144. const u8 *src, const u8 *bssid,
  3145. enum p2p_send_action_result result)
  3146. {
  3147. enum p2p_pending_action_state state;
  3148. int success;
  3149. p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
  3150. " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
  3151. p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
  3152. MAC2STR(bssid), result, p2p_state_txt(p2p->state));
  3153. success = result == P2P_SEND_ACTION_SUCCESS;
  3154. state = p2p->pending_action_state;
  3155. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  3156. switch (state) {
  3157. case P2P_NO_PENDING_ACTION:
  3158. if (p2p->send_action_in_progress) {
  3159. p2p->send_action_in_progress = 0;
  3160. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  3161. }
  3162. p2p_check_after_scan_tx_continuation(p2p);
  3163. break;
  3164. case P2P_PENDING_GO_NEG_REQUEST:
  3165. p2p_go_neg_req_cb(p2p, success);
  3166. break;
  3167. case P2P_PENDING_GO_NEG_RESPONSE:
  3168. p2p_go_neg_resp_cb(p2p, success);
  3169. break;
  3170. case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
  3171. p2p_go_neg_resp_failure_cb(p2p, success, dst);
  3172. break;
  3173. case P2P_PENDING_GO_NEG_CONFIRM:
  3174. p2p_go_neg_conf_cb(p2p, result);
  3175. break;
  3176. case P2P_PENDING_SD:
  3177. p2p_sd_cb(p2p, success);
  3178. break;
  3179. case P2P_PENDING_PD:
  3180. p2p_prov_disc_cb(p2p, success);
  3181. break;
  3182. case P2P_PENDING_PD_RESPONSE:
  3183. p2p_prov_disc_resp_cb(p2p, success);
  3184. break;
  3185. case P2P_PENDING_INVITATION_REQUEST:
  3186. p2p_invitation_req_cb(p2p, success);
  3187. break;
  3188. case P2P_PENDING_INVITATION_RESPONSE:
  3189. p2p_invitation_resp_cb(p2p, success);
  3190. if (p2p->inv_status != P2P_SC_SUCCESS)
  3191. p2p_check_after_scan_tx_continuation(p2p);
  3192. break;
  3193. case P2P_PENDING_DEV_DISC_REQUEST:
  3194. p2p_dev_disc_req_cb(p2p, success);
  3195. break;
  3196. case P2P_PENDING_DEV_DISC_RESPONSE:
  3197. p2p_dev_disc_resp_cb(p2p, success);
  3198. break;
  3199. case P2P_PENDING_GO_DISC_REQ:
  3200. p2p_go_disc_req_cb(p2p, success);
  3201. break;
  3202. }
  3203. p2p->after_scan_tx_in_progress = 0;
  3204. }
  3205. void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
  3206. unsigned int duration)
  3207. {
  3208. if (freq == p2p->pending_client_disc_freq) {
  3209. p2p_dbg(p2p, "Client discoverability remain-awake completed");
  3210. p2p->pending_client_disc_freq = 0;
  3211. return;
  3212. }
  3213. if (freq != p2p->pending_listen_freq) {
  3214. p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
  3215. freq, duration, p2p->pending_listen_freq);
  3216. return;
  3217. }
  3218. p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
  3219. p2p->pending_listen_sec, p2p->pending_listen_usec,
  3220. p2p->pending_listen_freq);
  3221. p2p->in_listen = 1;
  3222. p2p->drv_in_listen = freq;
  3223. if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
  3224. /*
  3225. * Add 20 msec extra wait to avoid race condition with driver
  3226. * remain-on-channel end event, i.e., give driver more time to
  3227. * complete the operation before our timeout expires.
  3228. */
  3229. p2p_set_timeout(p2p, p2p->pending_listen_sec,
  3230. p2p->pending_listen_usec + 20000);
  3231. }
  3232. p2p->pending_listen_freq = 0;
  3233. }
  3234. int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
  3235. {
  3236. p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
  3237. p2p->drv_in_listen = 0;
  3238. if (p2p->in_listen)
  3239. return 0; /* Internal timeout will trigger the next step */
  3240. if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
  3241. if (p2p->go_neg_peer->connect_reqs >= 120) {
  3242. p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
  3243. p2p_go_neg_failed(p2p, -1);
  3244. return 0;
  3245. }
  3246. p2p_set_state(p2p, P2P_CONNECT);
  3247. p2p_connect_send(p2p, p2p->go_neg_peer);
  3248. return 1;
  3249. } else if (p2p->state == P2P_SEARCH) {
  3250. if (p2p->p2p_scan_running) {
  3251. /*
  3252. * Search is already in progress. This can happen if
  3253. * an Action frame RX is reported immediately after
  3254. * the end of a remain-on-channel operation and the
  3255. * response frame to that is sent using an offchannel
  3256. * operation while in p2p_find. Avoid an attempt to
  3257. * restart a scan here.
  3258. */
  3259. p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
  3260. return 1;
  3261. }
  3262. if (p2p->pending_listen_freq) {
  3263. /*
  3264. * Better wait a bit if the driver is unable to start
  3265. * offchannel operation for some reason. p2p_search()
  3266. * will be started from internal timeout.
  3267. */
  3268. p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
  3269. p2p_set_timeout(p2p, 0, 100000);
  3270. return 1;
  3271. }
  3272. if (p2p->search_delay) {
  3273. p2p_dbg(p2p, "Delay search operation by %u ms",
  3274. p2p->search_delay);
  3275. p2p_set_timeout(p2p, p2p->search_delay / 1000,
  3276. (p2p->search_delay % 1000) * 1000);
  3277. return 1;
  3278. }
  3279. p2p_search(p2p);
  3280. return 1;
  3281. }
  3282. return 0;
  3283. }
  3284. static void p2p_timeout_connect(struct p2p_data *p2p)
  3285. {
  3286. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  3287. if (p2p->go_neg_peer &&
  3288. (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
  3289. p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
  3290. p2p_go_neg_failed(p2p, -1);
  3291. return;
  3292. }
  3293. if (p2p->go_neg_peer &&
  3294. (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
  3295. p2p->go_neg_peer->connect_reqs < 120) {
  3296. p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
  3297. p2p_connect_send(p2p, p2p->go_neg_peer);
  3298. return;
  3299. }
  3300. if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
  3301. p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
  3302. p2p_set_state(p2p, P2P_CONNECT_LISTEN);
  3303. p2p_set_timeout(p2p, 0, 30000);
  3304. return;
  3305. }
  3306. p2p_set_state(p2p, P2P_CONNECT_LISTEN);
  3307. p2p_listen_in_find(p2p, 0);
  3308. }
  3309. static void p2p_timeout_connect_listen(struct p2p_data *p2p)
  3310. {
  3311. if (p2p->go_neg_peer) {
  3312. if (p2p->drv_in_listen) {
  3313. p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
  3314. return;
  3315. }
  3316. if (p2p->go_neg_peer->connect_reqs >= 120) {
  3317. p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
  3318. p2p_go_neg_failed(p2p, -1);
  3319. return;
  3320. }
  3321. p2p_set_state(p2p, P2P_CONNECT);
  3322. p2p_connect_send(p2p, p2p->go_neg_peer);
  3323. } else
  3324. p2p_set_state(p2p, P2P_IDLE);
  3325. }
  3326. static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
  3327. {
  3328. p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
  3329. if (p2p->cfg->is_concurrent_session_active &&
  3330. p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
  3331. p2p_set_timeout(p2p, 0, 500000);
  3332. else
  3333. p2p_set_timeout(p2p, 0, 200000);
  3334. }
  3335. static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
  3336. {
  3337. struct p2p_device *dev = p2p->go_neg_peer;
  3338. if (dev == NULL) {
  3339. p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
  3340. return;
  3341. }
  3342. p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
  3343. p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
  3344. p2p_listen_in_find(p2p, 0);
  3345. }
  3346. static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
  3347. {
  3348. p2p_dbg(p2p, "Service Discovery Query timeout");
  3349. if (p2p->sd_peer) {
  3350. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  3351. p2p->sd_peer = NULL;
  3352. }
  3353. p2p_continue_find(p2p);
  3354. }
  3355. static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
  3356. {
  3357. p2p_dbg(p2p, "Provision Discovery Request timeout");
  3358. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  3359. p2p_continue_find(p2p);
  3360. }
  3361. static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
  3362. {
  3363. u32 adv_id = 0;
  3364. u8 *adv_mac = NULL;
  3365. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  3366. /*
  3367. * For user initiated PD requests that we have not gotten any responses
  3368. * for while in IDLE state, we retry them a couple of times before
  3369. * giving up.
  3370. */
  3371. if (!p2p->user_initiated_pd)
  3372. return;
  3373. p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
  3374. if (p2p->pd_retries) {
  3375. p2p->pd_retries--;
  3376. p2p_retry_pd(p2p);
  3377. } else {
  3378. struct p2p_device *dev;
  3379. int for_join = 0;
  3380. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  3381. if (os_memcmp(p2p->pending_pd_devaddr,
  3382. dev->info.p2p_device_addr, ETH_ALEN) != 0)
  3383. continue;
  3384. if (dev->req_config_methods &&
  3385. (dev->flags & P2P_DEV_PD_FOR_JOIN))
  3386. for_join = 1;
  3387. }
  3388. if (p2p->p2ps_prov) {
  3389. adv_id = p2p->p2ps_prov->adv_id;
  3390. adv_mac = p2p->p2ps_prov->adv_mac;
  3391. }
  3392. if (p2p->cfg->prov_disc_fail)
  3393. p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
  3394. p2p->pending_pd_devaddr,
  3395. for_join ?
  3396. P2P_PROV_DISC_TIMEOUT_JOIN :
  3397. P2P_PROV_DISC_TIMEOUT,
  3398. adv_id, adv_mac, NULL);
  3399. p2p_reset_pending_pd(p2p);
  3400. }
  3401. }
  3402. static void p2p_timeout_invite(struct p2p_data *p2p)
  3403. {
  3404. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  3405. p2p_set_state(p2p, P2P_INVITE_LISTEN);
  3406. if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
  3407. /*
  3408. * Better remain on operating channel instead of listen channel
  3409. * when running a group.
  3410. */
  3411. p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
  3412. p2p_set_timeout(p2p, 0, 100000);
  3413. return;
  3414. }
  3415. p2p_listen_in_find(p2p, 0);
  3416. }
  3417. static void p2p_timeout_invite_listen(struct p2p_data *p2p)
  3418. {
  3419. if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
  3420. p2p_set_state(p2p, P2P_INVITE);
  3421. p2p_invite_send(p2p, p2p->invite_peer,
  3422. p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
  3423. } else {
  3424. if (p2p->invite_peer) {
  3425. p2p_dbg(p2p, "Invitation Request retry limit reached");
  3426. if (p2p->cfg->invitation_result)
  3427. p2p->cfg->invitation_result(
  3428. p2p->cfg->cb_ctx, -1, NULL, NULL,
  3429. p2p->invite_peer->info.p2p_device_addr,
  3430. 0, 0);
  3431. }
  3432. p2p_set_state(p2p, P2P_IDLE);
  3433. }
  3434. }
  3435. static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
  3436. {
  3437. struct p2p_data *p2p = eloop_ctx;
  3438. p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
  3439. p2p->in_listen = 0;
  3440. if (p2p->drv_in_listen) {
  3441. p2p_dbg(p2p, "Driver is still in listen state - stop it");
  3442. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  3443. }
  3444. switch (p2p->state) {
  3445. case P2P_IDLE:
  3446. /* Check if we timed out waiting for PD req */
  3447. if (p2p->pending_action_state == P2P_PENDING_PD)
  3448. p2p_timeout_prov_disc_req(p2p);
  3449. break;
  3450. case P2P_SEARCH:
  3451. /* Check if we timed out waiting for PD req */
  3452. if (p2p->pending_action_state == P2P_PENDING_PD)
  3453. p2p_timeout_prov_disc_req(p2p);
  3454. if (p2p->search_delay && !p2p->in_search_delay) {
  3455. p2p_dbg(p2p, "Delay search operation by %u ms",
  3456. p2p->search_delay);
  3457. p2p->in_search_delay = 1;
  3458. p2p_set_timeout(p2p, p2p->search_delay / 1000,
  3459. (p2p->search_delay % 1000) * 1000);
  3460. break;
  3461. }
  3462. p2p->in_search_delay = 0;
  3463. p2p_search(p2p);
  3464. break;
  3465. case P2P_CONNECT:
  3466. p2p_timeout_connect(p2p);
  3467. break;
  3468. case P2P_CONNECT_LISTEN:
  3469. p2p_timeout_connect_listen(p2p);
  3470. break;
  3471. case P2P_GO_NEG:
  3472. break;
  3473. case P2P_LISTEN_ONLY:
  3474. /* Check if we timed out waiting for PD req */
  3475. if (p2p->pending_action_state == P2P_PENDING_PD)
  3476. p2p_timeout_prov_disc_req(p2p);
  3477. if (p2p->ext_listen_only) {
  3478. p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
  3479. p2p->ext_listen_only = 0;
  3480. p2p_set_state(p2p, P2P_IDLE);
  3481. }
  3482. break;
  3483. case P2P_WAIT_PEER_CONNECT:
  3484. p2p_timeout_wait_peer_connect(p2p);
  3485. break;
  3486. case P2P_WAIT_PEER_IDLE:
  3487. p2p_timeout_wait_peer_idle(p2p);
  3488. break;
  3489. case P2P_SD_DURING_FIND:
  3490. p2p_timeout_sd_during_find(p2p);
  3491. break;
  3492. case P2P_PROVISIONING:
  3493. break;
  3494. case P2P_PD_DURING_FIND:
  3495. p2p_timeout_prov_disc_during_find(p2p);
  3496. break;
  3497. case P2P_INVITE:
  3498. p2p_timeout_invite(p2p);
  3499. break;
  3500. case P2P_INVITE_LISTEN:
  3501. p2p_timeout_invite_listen(p2p);
  3502. break;
  3503. }
  3504. }
  3505. int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
  3506. {
  3507. struct p2p_device *dev;
  3508. dev = p2p_get_device(p2p, peer_addr);
  3509. p2p_dbg(p2p, "Local request to reject connection attempts by peer "
  3510. MACSTR, MAC2STR(peer_addr));
  3511. if (dev == NULL) {
  3512. p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
  3513. return -1;
  3514. }
  3515. dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
  3516. dev->flags |= P2P_DEV_USER_REJECTED;
  3517. return 0;
  3518. }
  3519. const char * p2p_wps_method_text(enum p2p_wps_method method)
  3520. {
  3521. switch (method) {
  3522. case WPS_NOT_READY:
  3523. return "not-ready";
  3524. case WPS_PIN_DISPLAY:
  3525. return "Display";
  3526. case WPS_PIN_KEYPAD:
  3527. return "Keypad";
  3528. case WPS_PBC:
  3529. return "PBC";
  3530. case WPS_NFC:
  3531. return "NFC";
  3532. case WPS_P2PS:
  3533. return "P2PS";
  3534. }
  3535. return "??";
  3536. }
  3537. static const char * p2p_go_state_text(enum p2p_go_state go_state)
  3538. {
  3539. switch (go_state) {
  3540. case UNKNOWN_GO:
  3541. return "unknown";
  3542. case LOCAL_GO:
  3543. return "local";
  3544. case REMOTE_GO:
  3545. return "remote";
  3546. }
  3547. return "??";
  3548. }
  3549. const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
  3550. const u8 *addr, int next)
  3551. {
  3552. struct p2p_device *dev;
  3553. if (addr)
  3554. dev = p2p_get_device(p2p, addr);
  3555. else
  3556. dev = dl_list_first(&p2p->devices, struct p2p_device, list);
  3557. if (dev && next) {
  3558. dev = dl_list_first(&dev->list, struct p2p_device, list);
  3559. if (&dev->list == &p2p->devices)
  3560. dev = NULL;
  3561. }
  3562. if (dev == NULL)
  3563. return NULL;
  3564. return &dev->info;
  3565. }
  3566. int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
  3567. char *buf, size_t buflen)
  3568. {
  3569. struct p2p_device *dev;
  3570. int res;
  3571. char *pos, *end;
  3572. struct os_reltime now;
  3573. if (info == NULL)
  3574. return -1;
  3575. dev = (struct p2p_device *) (((u8 *) info) -
  3576. offsetof(struct p2p_device, info));
  3577. pos = buf;
  3578. end = buf + buflen;
  3579. os_get_reltime(&now);
  3580. res = os_snprintf(pos, end - pos,
  3581. "age=%d\n"
  3582. "listen_freq=%d\n"
  3583. "wps_method=%s\n"
  3584. "interface_addr=" MACSTR "\n"
  3585. "member_in_go_dev=" MACSTR "\n"
  3586. "member_in_go_iface=" MACSTR "\n"
  3587. "go_neg_req_sent=%d\n"
  3588. "go_state=%s\n"
  3589. "dialog_token=%u\n"
  3590. "intended_addr=" MACSTR "\n"
  3591. "country=%c%c\n"
  3592. "oper_freq=%d\n"
  3593. "req_config_methods=0x%x\n"
  3594. "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
  3595. "status=%d\n"
  3596. "invitation_reqs=%u\n",
  3597. (int) (now.sec - dev->last_seen.sec),
  3598. dev->listen_freq,
  3599. p2p_wps_method_text(dev->wps_method),
  3600. MAC2STR(dev->interface_addr),
  3601. MAC2STR(dev->member_in_go_dev),
  3602. MAC2STR(dev->member_in_go_iface),
  3603. dev->go_neg_req_sent,
  3604. p2p_go_state_text(dev->go_state),
  3605. dev->dialog_token,
  3606. MAC2STR(dev->intended_addr),
  3607. dev->country[0] ? dev->country[0] : '_',
  3608. dev->country[1] ? dev->country[1] : '_',
  3609. dev->oper_freq,
  3610. dev->req_config_methods,
  3611. dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
  3612. "[PROBE_REQ_ONLY]" : "",
  3613. dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
  3614. dev->flags & P2P_DEV_NOT_YET_READY ?
  3615. "[NOT_YET_READY]" : "",
  3616. dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
  3617. "[PD_PEER_DISPLAY]" : "",
  3618. dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
  3619. "[PD_PEER_KEYPAD]" : "",
  3620. dev->flags & P2P_DEV_PD_PEER_P2PS ?
  3621. "[PD_PEER_P2PS]" : "",
  3622. dev->flags & P2P_DEV_USER_REJECTED ?
  3623. "[USER_REJECTED]" : "",
  3624. dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
  3625. "[PEER_WAITING_RESPONSE]" : "",
  3626. dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
  3627. "[PREFER_PERSISTENT_GROUP]" : "",
  3628. dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
  3629. "[WAIT_GO_NEG_RESPONSE]" : "",
  3630. dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
  3631. "[WAIT_GO_NEG_CONFIRM]" : "",
  3632. dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
  3633. "[GROUP_CLIENT_ONLY]" : "",
  3634. dev->flags & P2P_DEV_FORCE_FREQ ?
  3635. "[FORCE_FREQ]" : "",
  3636. dev->flags & P2P_DEV_PD_FOR_JOIN ?
  3637. "[PD_FOR_JOIN]" : "",
  3638. dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
  3639. "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
  3640. dev->status,
  3641. dev->invitation_reqs);
  3642. if (os_snprintf_error(end - pos, res))
  3643. return pos - buf;
  3644. pos += res;
  3645. if (dev->ext_listen_period) {
  3646. res = os_snprintf(pos, end - pos,
  3647. "ext_listen_period=%u\n"
  3648. "ext_listen_interval=%u\n",
  3649. dev->ext_listen_period,
  3650. dev->ext_listen_interval);
  3651. if (os_snprintf_error(end - pos, res))
  3652. return pos - buf;
  3653. pos += res;
  3654. }
  3655. if (dev->oper_ssid_len) {
  3656. res = os_snprintf(pos, end - pos,
  3657. "oper_ssid=%s\n",
  3658. wpa_ssid_txt(dev->oper_ssid,
  3659. dev->oper_ssid_len));
  3660. if (os_snprintf_error(end - pos, res))
  3661. return pos - buf;
  3662. pos += res;
  3663. }
  3664. #ifdef CONFIG_WIFI_DISPLAY
  3665. if (dev->info.wfd_subelems) {
  3666. res = os_snprintf(pos, end - pos, "wfd_subelems=");
  3667. if (os_snprintf_error(end - pos, res))
  3668. return pos - buf;
  3669. pos += res;
  3670. pos += wpa_snprintf_hex(pos, end - pos,
  3671. wpabuf_head(dev->info.wfd_subelems),
  3672. wpabuf_len(dev->info.wfd_subelems));
  3673. res = os_snprintf(pos, end - pos, "\n");
  3674. if (os_snprintf_error(end - pos, res))
  3675. return pos - buf;
  3676. pos += res;
  3677. }
  3678. #endif /* CONFIG_WIFI_DISPLAY */
  3679. return pos - buf;
  3680. }
  3681. int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
  3682. {
  3683. return p2p_get_device(p2p, addr) != NULL;
  3684. }
  3685. void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
  3686. {
  3687. if (enabled) {
  3688. p2p_dbg(p2p, "Client discoverability enabled");
  3689. p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  3690. } else {
  3691. p2p_dbg(p2p, "Client discoverability disabled");
  3692. p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
  3693. }
  3694. }
  3695. static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
  3696. u32 duration2, u32 interval2)
  3697. {
  3698. struct wpabuf *req;
  3699. struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
  3700. u8 *len;
  3701. req = wpabuf_alloc(100);
  3702. if (req == NULL)
  3703. return NULL;
  3704. if (duration1 || interval1) {
  3705. os_memset(&desc1, 0, sizeof(desc1));
  3706. desc1.count_type = 1;
  3707. desc1.duration = duration1;
  3708. desc1.interval = interval1;
  3709. ptr1 = &desc1;
  3710. if (duration2 || interval2) {
  3711. os_memset(&desc2, 0, sizeof(desc2));
  3712. desc2.count_type = 2;
  3713. desc2.duration = duration2;
  3714. desc2.interval = interval2;
  3715. ptr2 = &desc2;
  3716. }
  3717. }
  3718. p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
  3719. len = p2p_buf_add_ie_hdr(req);
  3720. p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
  3721. p2p_buf_update_ie_hdr(req, len);
  3722. return req;
  3723. }
  3724. int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
  3725. const u8 *own_interface_addr, unsigned int freq,
  3726. u32 duration1, u32 interval1, u32 duration2,
  3727. u32 interval2)
  3728. {
  3729. struct wpabuf *req;
  3730. p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
  3731. " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
  3732. "dur2=%u int2=%u",
  3733. MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
  3734. freq, duration1, interval1, duration2, interval2);
  3735. req = p2p_build_presence_req(duration1, interval1, duration2,
  3736. interval2);
  3737. if (req == NULL)
  3738. return -1;
  3739. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  3740. if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
  3741. go_interface_addr,
  3742. wpabuf_head(req), wpabuf_len(req), 200) < 0) {
  3743. p2p_dbg(p2p, "Failed to send Action frame");
  3744. }
  3745. wpabuf_free(req);
  3746. return 0;
  3747. }
  3748. static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
  3749. size_t noa_len, u8 dialog_token)
  3750. {
  3751. struct wpabuf *resp;
  3752. u8 *len;
  3753. resp = wpabuf_alloc(100 + noa_len);
  3754. if (resp == NULL)
  3755. return NULL;
  3756. p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
  3757. len = p2p_buf_add_ie_hdr(resp);
  3758. p2p_buf_add_status(resp, status);
  3759. if (noa) {
  3760. wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
  3761. wpabuf_put_le16(resp, noa_len);
  3762. wpabuf_put_data(resp, noa, noa_len);
  3763. } else
  3764. p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
  3765. p2p_buf_update_ie_hdr(resp, len);
  3766. return resp;
  3767. }
  3768. static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
  3769. const u8 *sa, const u8 *data, size_t len,
  3770. int rx_freq)
  3771. {
  3772. struct p2p_message msg;
  3773. u8 status;
  3774. struct wpabuf *resp;
  3775. size_t g;
  3776. struct p2p_group *group = NULL;
  3777. int parsed = 0;
  3778. u8 noa[50];
  3779. int noa_len;
  3780. p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
  3781. for (g = 0; g < p2p->num_groups; g++) {
  3782. if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
  3783. ETH_ALEN) == 0) {
  3784. group = p2p->groups[g];
  3785. break;
  3786. }
  3787. }
  3788. if (group == NULL) {
  3789. p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
  3790. MACSTR, MAC2STR(da));
  3791. return;
  3792. }
  3793. if (p2p_parse(data, len, &msg) < 0) {
  3794. p2p_dbg(p2p, "Failed to parse P2P Presence Request");
  3795. status = P2P_SC_FAIL_INVALID_PARAMS;
  3796. goto fail;
  3797. }
  3798. parsed = 1;
  3799. if (msg.noa == NULL) {
  3800. p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
  3801. status = P2P_SC_FAIL_INVALID_PARAMS;
  3802. goto fail;
  3803. }
  3804. status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
  3805. fail:
  3806. if (p2p->cfg->get_noa)
  3807. noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
  3808. sizeof(noa));
  3809. else
  3810. noa_len = -1;
  3811. resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
  3812. noa_len > 0 ? noa_len : 0,
  3813. msg.dialog_token);
  3814. if (parsed)
  3815. p2p_parse_free(&msg);
  3816. if (resp == NULL)
  3817. return;
  3818. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  3819. if (p2p_send_action(p2p, rx_freq, sa, da, da,
  3820. wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
  3821. p2p_dbg(p2p, "Failed to send Action frame");
  3822. }
  3823. wpabuf_free(resp);
  3824. }
  3825. static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
  3826. const u8 *sa, const u8 *data, size_t len)
  3827. {
  3828. struct p2p_message msg;
  3829. p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
  3830. if (p2p_parse(data, len, &msg) < 0) {
  3831. p2p_dbg(p2p, "Failed to parse P2P Presence Response");
  3832. return;
  3833. }
  3834. if (msg.status == NULL || msg.noa == NULL) {
  3835. p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
  3836. p2p_parse_free(&msg);
  3837. return;
  3838. }
  3839. if (p2p->cfg->presence_resp) {
  3840. p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
  3841. msg.noa, msg.noa_len);
  3842. }
  3843. if (*msg.status) {
  3844. p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
  3845. *msg.status);
  3846. p2p_parse_free(&msg);
  3847. return;
  3848. }
  3849. p2p_dbg(p2p, "P2P Presence Request was accepted");
  3850. wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
  3851. msg.noa, msg.noa_len);
  3852. /* TODO: process NoA */
  3853. p2p_parse_free(&msg);
  3854. }
  3855. static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  3856. {
  3857. struct p2p_data *p2p = eloop_ctx;
  3858. if (p2p->ext_listen_interval) {
  3859. /* Schedule next extended listen timeout */
  3860. eloop_register_timeout(p2p->ext_listen_interval_sec,
  3861. p2p->ext_listen_interval_usec,
  3862. p2p_ext_listen_timeout, p2p, NULL);
  3863. }
  3864. if ((p2p->cfg->is_p2p_in_progress &&
  3865. p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
  3866. (p2p->pending_action_state == P2P_PENDING_PD &&
  3867. p2p->pd_retries > 0)) {
  3868. p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
  3869. p2p_state_txt(p2p->state));
  3870. return;
  3871. }
  3872. if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
  3873. /*
  3874. * This should not really happen, but it looks like the Listen
  3875. * command may fail is something else (e.g., a scan) was
  3876. * running at an inconvenient time. As a workaround, allow new
  3877. * Extended Listen operation to be started.
  3878. */
  3879. p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
  3880. p2p->ext_listen_only = 0;
  3881. p2p_set_state(p2p, P2P_IDLE);
  3882. }
  3883. if (p2p->state != P2P_IDLE) {
  3884. p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
  3885. return;
  3886. }
  3887. p2p_dbg(p2p, "Extended Listen timeout");
  3888. p2p->ext_listen_only = 1;
  3889. if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
  3890. p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
  3891. p2p->ext_listen_only = 0;
  3892. }
  3893. }
  3894. int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
  3895. unsigned int interval)
  3896. {
  3897. if (period > 65535 || interval > 65535 || period > interval ||
  3898. (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
  3899. p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
  3900. period, interval);
  3901. return -1;
  3902. }
  3903. eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
  3904. if (interval == 0) {
  3905. p2p_dbg(p2p, "Disabling Extended Listen Timing");
  3906. p2p->ext_listen_period = 0;
  3907. p2p->ext_listen_interval = 0;
  3908. return 0;
  3909. }
  3910. p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
  3911. period, interval);
  3912. p2p->ext_listen_period = period;
  3913. p2p->ext_listen_interval = interval;
  3914. p2p->ext_listen_interval_sec = interval / 1000;
  3915. p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
  3916. eloop_register_timeout(p2p->ext_listen_interval_sec,
  3917. p2p->ext_listen_interval_usec,
  3918. p2p_ext_listen_timeout, p2p, NULL);
  3919. return 0;
  3920. }
  3921. void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
  3922. const u8 *ie, size_t ie_len)
  3923. {
  3924. struct p2p_message msg;
  3925. if (bssid == NULL || ie == NULL)
  3926. return;
  3927. os_memset(&msg, 0, sizeof(msg));
  3928. if (p2p_parse_ies(ie, ie_len, &msg))
  3929. return;
  3930. if (msg.minor_reason_code == NULL) {
  3931. p2p_parse_free(&msg);
  3932. return;
  3933. }
  3934. p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
  3935. " reason_code=%u minor_reason_code=%u",
  3936. MAC2STR(bssid), reason_code, *msg.minor_reason_code);
  3937. p2p_parse_free(&msg);
  3938. }
  3939. void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
  3940. const u8 *ie, size_t ie_len)
  3941. {
  3942. struct p2p_message msg;
  3943. if (bssid == NULL || ie == NULL)
  3944. return;
  3945. os_memset(&msg, 0, sizeof(msg));
  3946. if (p2p_parse_ies(ie, ie_len, &msg))
  3947. return;
  3948. if (msg.minor_reason_code == NULL) {
  3949. p2p_parse_free(&msg);
  3950. return;
  3951. }
  3952. p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
  3953. " reason_code=%u minor_reason_code=%u",
  3954. MAC2STR(bssid), reason_code, *msg.minor_reason_code);
  3955. p2p_parse_free(&msg);
  3956. }
  3957. void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
  3958. {
  3959. if (enabled) {
  3960. p2p_dbg(p2p, "Managed P2P Device operations enabled");
  3961. p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
  3962. } else {
  3963. p2p_dbg(p2p, "Managed P2P Device operations disabled");
  3964. p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
  3965. }
  3966. }
  3967. int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
  3968. u8 *op_channel)
  3969. {
  3970. return p2p_channel_random_social(&p2p->channels, op_class, op_channel);
  3971. }
  3972. int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
  3973. u8 forced)
  3974. {
  3975. if (p2p_channel_to_freq(reg_class, channel) < 0)
  3976. return -1;
  3977. /*
  3978. * Listen channel was set in configuration or set by control interface;
  3979. * cannot override it.
  3980. */
  3981. if (p2p->cfg->channel_forced && forced == 0) {
  3982. p2p_dbg(p2p,
  3983. "Listen channel was previously configured - do not override based on optimization");
  3984. return -1;
  3985. }
  3986. p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
  3987. reg_class, channel);
  3988. if (p2p->state == P2P_IDLE) {
  3989. p2p->cfg->reg_class = reg_class;
  3990. p2p->cfg->channel = channel;
  3991. p2p->cfg->channel_forced = forced;
  3992. } else {
  3993. p2p_dbg(p2p, "Defer setting listen channel");
  3994. p2p->pending_reg_class = reg_class;
  3995. p2p->pending_channel = channel;
  3996. p2p->pending_channel_forced = forced;
  3997. }
  3998. return 0;
  3999. }
  4000. u8 p2p_get_listen_channel(struct p2p_data *p2p)
  4001. {
  4002. return p2p->cfg->channel;
  4003. }
  4004. int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
  4005. {
  4006. p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
  4007. if (postfix == NULL) {
  4008. p2p->cfg->ssid_postfix_len = 0;
  4009. return 0;
  4010. }
  4011. if (len > sizeof(p2p->cfg->ssid_postfix))
  4012. return -1;
  4013. os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
  4014. p2p->cfg->ssid_postfix_len = len;
  4015. return 0;
  4016. }
  4017. int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
  4018. int cfg_op_channel)
  4019. {
  4020. if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
  4021. return -1;
  4022. p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
  4023. op_reg_class, op_channel);
  4024. p2p->cfg->op_reg_class = op_reg_class;
  4025. p2p->cfg->op_channel = op_channel;
  4026. p2p->cfg->cfg_op_channel = cfg_op_channel;
  4027. return 0;
  4028. }
  4029. int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
  4030. const struct p2p_channel *pref_chan)
  4031. {
  4032. struct p2p_channel *n;
  4033. if (pref_chan) {
  4034. n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
  4035. if (n == NULL)
  4036. return -1;
  4037. os_memcpy(n, pref_chan,
  4038. num_pref_chan * sizeof(struct p2p_channel));
  4039. } else
  4040. n = NULL;
  4041. os_free(p2p->cfg->pref_chan);
  4042. p2p->cfg->pref_chan = n;
  4043. p2p->cfg->num_pref_chan = num_pref_chan;
  4044. return 0;
  4045. }
  4046. int p2p_set_no_go_freq(struct p2p_data *p2p,
  4047. const struct wpa_freq_range_list *list)
  4048. {
  4049. struct wpa_freq_range *tmp;
  4050. if (list == NULL || list->num == 0) {
  4051. os_free(p2p->no_go_freq.range);
  4052. p2p->no_go_freq.range = NULL;
  4053. p2p->no_go_freq.num = 0;
  4054. return 0;
  4055. }
  4056. tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
  4057. if (tmp == NULL)
  4058. return -1;
  4059. os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
  4060. os_free(p2p->no_go_freq.range);
  4061. p2p->no_go_freq.range = tmp;
  4062. p2p->no_go_freq.num = list->num;
  4063. p2p_dbg(p2p, "Updated no GO chan list");
  4064. return 0;
  4065. }
  4066. int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
  4067. u8 *iface_addr)
  4068. {
  4069. struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
  4070. if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
  4071. return -1;
  4072. os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
  4073. return 0;
  4074. }
  4075. int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
  4076. u8 *dev_addr)
  4077. {
  4078. struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
  4079. if (dev == NULL)
  4080. return -1;
  4081. os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
  4082. return 0;
  4083. }
  4084. void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
  4085. {
  4086. os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
  4087. if (is_zero_ether_addr(p2p->peer_filter))
  4088. p2p_dbg(p2p, "Disable peer filter");
  4089. else
  4090. p2p_dbg(p2p, "Enable peer filter for " MACSTR,
  4091. MAC2STR(p2p->peer_filter));
  4092. }
  4093. void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
  4094. {
  4095. p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
  4096. if (p2p->cross_connect == enabled)
  4097. return;
  4098. p2p->cross_connect = enabled;
  4099. /* TODO: may need to tear down any action group where we are GO(?) */
  4100. }
  4101. int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
  4102. {
  4103. struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
  4104. if (dev == NULL)
  4105. return -1;
  4106. if (dev->oper_freq <= 0)
  4107. return -1;
  4108. return dev->oper_freq;
  4109. }
  4110. void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
  4111. {
  4112. p2p_dbg(p2p, "Intra BSS distribution %s",
  4113. enabled ? "enabled" : "disabled");
  4114. p2p->cfg->p2p_intra_bss = enabled;
  4115. }
  4116. void p2p_update_channel_list(struct p2p_data *p2p,
  4117. const struct p2p_channels *chan,
  4118. const struct p2p_channels *cli_chan)
  4119. {
  4120. p2p_dbg(p2p, "Update channel list");
  4121. os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
  4122. p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
  4123. os_memcpy(&p2p->cfg->cli_channels, cli_chan,
  4124. sizeof(struct p2p_channels));
  4125. p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
  4126. }
  4127. int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
  4128. const u8 *src, const u8 *bssid, const u8 *buf,
  4129. size_t len, unsigned int wait_time)
  4130. {
  4131. if (p2p->p2p_scan_running) {
  4132. p2p_dbg(p2p, "Delay Action frame TX until p2p_scan completes");
  4133. if (p2p->after_scan_tx) {
  4134. p2p_dbg(p2p, "Dropped previous pending Action frame TX");
  4135. os_free(p2p->after_scan_tx);
  4136. }
  4137. p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
  4138. len);
  4139. if (p2p->after_scan_tx == NULL)
  4140. return -1;
  4141. p2p->after_scan_tx->freq = freq;
  4142. os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
  4143. os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
  4144. os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
  4145. p2p->after_scan_tx->len = len;
  4146. p2p->after_scan_tx->wait_time = wait_time;
  4147. os_memcpy(p2p->after_scan_tx + 1, buf, len);
  4148. return 0;
  4149. }
  4150. return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
  4151. buf, len, wait_time);
  4152. }
  4153. void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
  4154. int freq_overall)
  4155. {
  4156. p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
  4157. freq_24, freq_5, freq_overall);
  4158. p2p->best_freq_24 = freq_24;
  4159. p2p->best_freq_5 = freq_5;
  4160. p2p->best_freq_overall = freq_overall;
  4161. }
  4162. void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
  4163. {
  4164. p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
  4165. p2p->own_freq_preference = freq;
  4166. }
  4167. const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
  4168. {
  4169. if (p2p == NULL || p2p->go_neg_peer == NULL)
  4170. return NULL;
  4171. return p2p->go_neg_peer->info.p2p_device_addr;
  4172. }
  4173. const struct p2p_peer_info *
  4174. p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
  4175. {
  4176. struct p2p_device *dev;
  4177. if (addr) {
  4178. dev = p2p_get_device(p2p, addr);
  4179. if (!dev)
  4180. return NULL;
  4181. if (!next) {
  4182. if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
  4183. return NULL;
  4184. return &dev->info;
  4185. } else {
  4186. do {
  4187. dev = dl_list_first(&dev->list,
  4188. struct p2p_device,
  4189. list);
  4190. if (!dev || &dev->list == &p2p->devices)
  4191. return NULL;
  4192. } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
  4193. }
  4194. } else {
  4195. dev = dl_list_first(&p2p->devices, struct p2p_device, list);
  4196. if (!dev)
  4197. return NULL;
  4198. while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
  4199. dev = dl_list_first(&dev->list,
  4200. struct p2p_device,
  4201. list);
  4202. if (!dev || &dev->list == &p2p->devices)
  4203. return NULL;
  4204. }
  4205. }
  4206. return &dev->info;
  4207. }
  4208. int p2p_in_progress(struct p2p_data *p2p)
  4209. {
  4210. if (p2p == NULL)
  4211. return 0;
  4212. if (p2p->state == P2P_SEARCH)
  4213. return 2;
  4214. return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
  4215. }
  4216. void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
  4217. u8 client_timeout)
  4218. {
  4219. if (p2p) {
  4220. p2p->go_timeout = go_timeout;
  4221. p2p->client_timeout = client_timeout;
  4222. }
  4223. }
  4224. #ifdef CONFIG_WIFI_DISPLAY
  4225. static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
  4226. {
  4227. size_t g;
  4228. struct p2p_group *group;
  4229. for (g = 0; g < p2p->num_groups; g++) {
  4230. group = p2p->groups[g];
  4231. p2p_group_force_beacon_update_ies(group);
  4232. }
  4233. }
  4234. int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
  4235. {
  4236. wpabuf_free(p2p->wfd_ie_beacon);
  4237. p2p->wfd_ie_beacon = ie;
  4238. p2p_update_wfd_ie_groups(p2p);
  4239. return 0;
  4240. }
  4241. int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
  4242. {
  4243. wpabuf_free(p2p->wfd_ie_probe_req);
  4244. p2p->wfd_ie_probe_req = ie;
  4245. return 0;
  4246. }
  4247. int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
  4248. {
  4249. wpabuf_free(p2p->wfd_ie_probe_resp);
  4250. p2p->wfd_ie_probe_resp = ie;
  4251. p2p_update_wfd_ie_groups(p2p);
  4252. return 0;
  4253. }
  4254. int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
  4255. {
  4256. wpabuf_free(p2p->wfd_ie_assoc_req);
  4257. p2p->wfd_ie_assoc_req = ie;
  4258. return 0;
  4259. }
  4260. int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
  4261. {
  4262. wpabuf_free(p2p->wfd_ie_invitation);
  4263. p2p->wfd_ie_invitation = ie;
  4264. return 0;
  4265. }
  4266. int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
  4267. {
  4268. wpabuf_free(p2p->wfd_ie_prov_disc_req);
  4269. p2p->wfd_ie_prov_disc_req = ie;
  4270. return 0;
  4271. }
  4272. int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
  4273. {
  4274. wpabuf_free(p2p->wfd_ie_prov_disc_resp);
  4275. p2p->wfd_ie_prov_disc_resp = ie;
  4276. return 0;
  4277. }
  4278. int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
  4279. {
  4280. wpabuf_free(p2p->wfd_ie_go_neg);
  4281. p2p->wfd_ie_go_neg = ie;
  4282. return 0;
  4283. }
  4284. int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
  4285. {
  4286. wpabuf_free(p2p->wfd_dev_info);
  4287. if (elem) {
  4288. p2p->wfd_dev_info = wpabuf_dup(elem);
  4289. if (p2p->wfd_dev_info == NULL)
  4290. return -1;
  4291. } else
  4292. p2p->wfd_dev_info = NULL;
  4293. return 0;
  4294. }
  4295. int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
  4296. {
  4297. wpabuf_free(p2p->wfd_assoc_bssid);
  4298. if (elem) {
  4299. p2p->wfd_assoc_bssid = wpabuf_dup(elem);
  4300. if (p2p->wfd_assoc_bssid == NULL)
  4301. return -1;
  4302. } else
  4303. p2p->wfd_assoc_bssid = NULL;
  4304. return 0;
  4305. }
  4306. int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
  4307. const struct wpabuf *elem)
  4308. {
  4309. wpabuf_free(p2p->wfd_coupled_sink_info);
  4310. if (elem) {
  4311. p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
  4312. if (p2p->wfd_coupled_sink_info == NULL)
  4313. return -1;
  4314. } else
  4315. p2p->wfd_coupled_sink_info = NULL;
  4316. return 0;
  4317. }
  4318. #endif /* CONFIG_WIFI_DISPLAY */
  4319. int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
  4320. int max_disc_tu)
  4321. {
  4322. if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
  4323. return -1;
  4324. p2p->min_disc_int = min_disc_int;
  4325. p2p->max_disc_int = max_disc_int;
  4326. p2p->max_disc_tu = max_disc_tu;
  4327. p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
  4328. min_disc_int, max_disc_int, max_disc_tu);
  4329. return 0;
  4330. }
  4331. void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
  4332. {
  4333. va_list ap;
  4334. char buf[500];
  4335. if (!p2p->cfg->debug_print)
  4336. return;
  4337. va_start(ap, fmt);
  4338. vsnprintf(buf, sizeof(buf), fmt, ap);
  4339. buf[sizeof(buf) - 1] = '\0';
  4340. va_end(ap);
  4341. p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
  4342. }
  4343. void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
  4344. {
  4345. va_list ap;
  4346. char buf[500];
  4347. if (!p2p->cfg->debug_print)
  4348. return;
  4349. va_start(ap, fmt);
  4350. vsnprintf(buf, sizeof(buf), fmt, ap);
  4351. buf[sizeof(buf) - 1] = '\0';
  4352. va_end(ap);
  4353. p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
  4354. }
  4355. void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
  4356. {
  4357. va_list ap;
  4358. char buf[500];
  4359. if (!p2p->cfg->debug_print)
  4360. return;
  4361. va_start(ap, fmt);
  4362. vsnprintf(buf, sizeof(buf), fmt, ap);
  4363. buf[sizeof(buf) - 1] = '\0';
  4364. va_end(ap);
  4365. p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
  4366. }
  4367. void p2p_loop_on_known_peers(struct p2p_data *p2p,
  4368. void (*peer_callback)(struct p2p_peer_info *peer,
  4369. void *user_data),
  4370. void *user_data)
  4371. {
  4372. struct p2p_device *dev, *n;
  4373. dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
  4374. peer_callback(&dev->info, user_data);
  4375. }
  4376. }
  4377. #ifdef CONFIG_WPS_NFC
  4378. static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
  4379. int client_freq,
  4380. const u8 *go_dev_addr,
  4381. const u8 *ssid, size_t ssid_len)
  4382. {
  4383. struct wpabuf *buf;
  4384. u8 op_class, channel;
  4385. enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
  4386. buf = wpabuf_alloc(1000);
  4387. if (buf == NULL)
  4388. return NULL;
  4389. op_class = p2p->cfg->reg_class;
  4390. channel = p2p->cfg->channel;
  4391. p2p_buf_add_capability(buf, p2p->dev_capab &
  4392. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
  4393. p2p_buf_add_device_info(buf, p2p, NULL);
  4394. if (p2p->num_groups > 0) {
  4395. int freq = p2p_group_get_freq(p2p->groups[0]);
  4396. role = P2P_GO_IN_A_GROUP;
  4397. if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
  4398. p2p_dbg(p2p,
  4399. "Unknown GO operating frequency %d MHz for NFC handover",
  4400. freq);
  4401. wpabuf_free(buf);
  4402. return NULL;
  4403. }
  4404. } else if (client_freq > 0) {
  4405. role = P2P_CLIENT_IN_A_GROUP;
  4406. if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
  4407. p2p_dbg(p2p,
  4408. "Unknown client operating frequency %d MHz for NFC handover",
  4409. client_freq);
  4410. wpabuf_free(buf);
  4411. return NULL;
  4412. }
  4413. }
  4414. p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
  4415. channel, role);
  4416. if (p2p->num_groups > 0) {
  4417. /* Limit number of clients to avoid very long message */
  4418. p2p_buf_add_group_info(p2p->groups[0], buf, 5);
  4419. p2p_group_buf_add_id(p2p->groups[0], buf);
  4420. } else if (client_freq > 0 &&
  4421. go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
  4422. ssid && ssid_len > 0) {
  4423. /*
  4424. * Add the optional P2P Group ID to indicate in which group this
  4425. * device is a P2P Client.
  4426. */
  4427. p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
  4428. }
  4429. return buf;
  4430. }
  4431. struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
  4432. int client_freq,
  4433. const u8 *go_dev_addr,
  4434. const u8 *ssid, size_t ssid_len)
  4435. {
  4436. return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
  4437. ssid_len);
  4438. }
  4439. struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
  4440. int client_freq,
  4441. const u8 *go_dev_addr,
  4442. const u8 *ssid, size_t ssid_len)
  4443. {
  4444. return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
  4445. ssid_len);
  4446. }
  4447. int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
  4448. struct p2p_nfc_params *params)
  4449. {
  4450. struct p2p_message msg;
  4451. struct p2p_device *dev;
  4452. const u8 *p2p_dev_addr;
  4453. int freq;
  4454. enum p2p_role_indication role;
  4455. params->next_step = NO_ACTION;
  4456. if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
  4457. params->p2p_attr, params->p2p_len, &msg)) {
  4458. p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
  4459. p2p_parse_free(&msg);
  4460. return -1;
  4461. }
  4462. if (msg.p2p_device_addr)
  4463. p2p_dev_addr = msg.p2p_device_addr;
  4464. else if (msg.device_id)
  4465. p2p_dev_addr = msg.device_id;
  4466. else {
  4467. p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
  4468. p2p_parse_free(&msg);
  4469. return -1;
  4470. }
  4471. if (msg.oob_dev_password) {
  4472. os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
  4473. msg.oob_dev_password_len);
  4474. params->oob_dev_pw_len = msg.oob_dev_password_len;
  4475. }
  4476. dev = p2p_create_device(p2p, p2p_dev_addr);
  4477. if (dev == NULL) {
  4478. p2p_parse_free(&msg);
  4479. return -1;
  4480. }
  4481. params->peer = &dev->info;
  4482. os_get_reltime(&dev->last_seen);
  4483. dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
  4484. p2p_copy_wps_info(p2p, dev, 0, &msg);
  4485. if (!msg.oob_go_neg_channel) {
  4486. p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
  4487. p2p_parse_free(&msg);
  4488. return -1;
  4489. }
  4490. if (msg.oob_go_neg_channel[3] == 0 &&
  4491. msg.oob_go_neg_channel[4] == 0)
  4492. freq = 0;
  4493. else
  4494. freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
  4495. msg.oob_go_neg_channel[4]);
  4496. if (freq < 0) {
  4497. p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
  4498. p2p_parse_free(&msg);
  4499. return -1;
  4500. }
  4501. role = msg.oob_go_neg_channel[5];
  4502. if (role == P2P_GO_IN_A_GROUP) {
  4503. p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
  4504. params->go_freq = freq;
  4505. } else if (role == P2P_CLIENT_IN_A_GROUP) {
  4506. p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
  4507. freq);
  4508. params->go_freq = freq;
  4509. } else
  4510. p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
  4511. dev->oob_go_neg_freq = freq;
  4512. if (!params->sel && role != P2P_GO_IN_A_GROUP) {
  4513. freq = p2p_channel_to_freq(p2p->cfg->reg_class,
  4514. p2p->cfg->channel);
  4515. if (freq < 0) {
  4516. p2p_dbg(p2p, "Own listen channel not known");
  4517. p2p_parse_free(&msg);
  4518. return -1;
  4519. }
  4520. p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
  4521. dev->oob_go_neg_freq = freq;
  4522. }
  4523. if (msg.group_id) {
  4524. os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
  4525. params->go_ssid_len = msg.group_id_len - ETH_ALEN;
  4526. os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
  4527. params->go_ssid_len);
  4528. }
  4529. if (dev->flags & P2P_DEV_USER_REJECTED) {
  4530. p2p_dbg(p2p, "Do not report rejected device");
  4531. p2p_parse_free(&msg);
  4532. return 0;
  4533. }
  4534. if (!(dev->flags & P2P_DEV_REPORTED)) {
  4535. p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
  4536. !(dev->flags & P2P_DEV_REPORTED_ONCE));
  4537. dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
  4538. }
  4539. p2p_parse_free(&msg);
  4540. if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
  4541. params->next_step = BOTH_GO;
  4542. else if (role == P2P_GO_IN_A_GROUP)
  4543. params->next_step = JOIN_GROUP;
  4544. else if (role == P2P_CLIENT_IN_A_GROUP) {
  4545. dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
  4546. params->next_step = PEER_CLIENT;
  4547. } else if (p2p->num_groups > 0)
  4548. params->next_step = AUTH_JOIN;
  4549. else if (params->sel)
  4550. params->next_step = INIT_GO_NEG;
  4551. else
  4552. params->next_step = RESP_GO_NEG;
  4553. return 0;
  4554. }
  4555. void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
  4556. int go_intent,
  4557. const u8 *own_interface_addr)
  4558. {
  4559. p2p->authorized_oob_dev_pw_id = dev_pw_id;
  4560. if (dev_pw_id == 0) {
  4561. p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
  4562. return;
  4563. }
  4564. p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
  4565. dev_pw_id);
  4566. p2p->go_intent = go_intent;
  4567. os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
  4568. }
  4569. #endif /* CONFIG_WPS_NFC */
  4570. int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
  4571. {
  4572. if (len < 8 || len > 63)
  4573. return -1;
  4574. p2p->cfg->passphrase_len = len;
  4575. return 0;
  4576. }
  4577. void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
  4578. {
  4579. p2p->vendor_elem = vendor_elem;
  4580. }
  4581. void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
  4582. {
  4583. struct p2p_data *p2p = eloop_ctx;
  4584. p2p_dbg(p2p,
  4585. "Timeout on waiting peer to become ready for GO Negotiation");
  4586. p2p_go_neg_failed(p2p, -1);
  4587. }
  4588. void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
  4589. const unsigned int *pref_freq_list,
  4590. unsigned int size)
  4591. {
  4592. unsigned int i;
  4593. if (size > P2P_MAX_PREF_CHANNELS)
  4594. size = P2P_MAX_PREF_CHANNELS;
  4595. p2p->num_pref_freq = size;
  4596. for (i = 0; i < size; i++) {
  4597. p2p->pref_freq_list[i] = pref_freq_list[i];
  4598. p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
  4599. i, p2p->pref_freq_list[i]);
  4600. }
  4601. }
  4602. struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
  4603. unsigned int freq)
  4604. {
  4605. struct wpabuf *ies, *buf;
  4606. u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  4607. int ret;
  4608. ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
  4609. if (!ies) {
  4610. wpa_printf(MSG_ERROR,
  4611. "CTRL: Failed to build Probe Response IEs");
  4612. return NULL;
  4613. }
  4614. buf = wpabuf_alloc(200 + wpabuf_len(ies));
  4615. if (!buf) {
  4616. wpabuf_free(ies);
  4617. return NULL;
  4618. }
  4619. ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
  4620. wpabuf_free(ies);
  4621. if (ret) {
  4622. wpabuf_free(buf);
  4623. return NULL;
  4624. }
  4625. return buf;
  4626. }