p2p_supplicant.c 174 KB

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