p2p_supplicant.c 263 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "eloop.h"
  12. #include "common/ieee802_11_common.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "wps/wps_i.h"
  16. #include "p2p/p2p.h"
  17. #include "ap/hostapd.h"
  18. #include "ap/ap_config.h"
  19. #include "ap/sta_info.h"
  20. #include "ap/ap_drv_ops.h"
  21. #include "ap/wps_hostapd.h"
  22. #include "ap/p2p_hostapd.h"
  23. #include "ap/dfs.h"
  24. #include "eapol_supp/eapol_supp_sm.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "wpa_supplicant_i.h"
  27. #include "driver_i.h"
  28. #include "ap.h"
  29. #include "config_ssid.h"
  30. #include "config.h"
  31. #include "notify.h"
  32. #include "scan.h"
  33. #include "bss.h"
  34. #include "offchannel.h"
  35. #include "wps_supplicant.h"
  36. #include "p2p_supplicant.h"
  37. #include "wifi_display.h"
  38. /*
  39. * How many times to try to scan to find the GO before giving up on join
  40. * request.
  41. */
  42. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  43. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  44. /**
  45. * Defines time interval in seconds when a GO needs to evacuate a frequency that
  46. * it is currently using, but is no longer valid for P2P use cases.
  47. */
  48. #define P2P_GO_FREQ_CHANGE_TIME 5
  49. /**
  50. * Defines CSA parameters which are used when GO evacuates the no longer valid
  51. * channel (and if the driver supports channel switch).
  52. */
  53. #define P2P_GO_CSA_COUNT 7
  54. #define P2P_GO_CSA_BLOCK_TX 0
  55. #ifndef P2P_MAX_CLIENT_IDLE
  56. /*
  57. * How many seconds to try to reconnect to the GO when connection in P2P client
  58. * role has been lost.
  59. */
  60. #define P2P_MAX_CLIENT_IDLE 10
  61. #endif /* P2P_MAX_CLIENT_IDLE */
  62. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  63. /*
  64. * How many seconds to wait for initial 4-way handshake to get completed after
  65. * WPS provisioning step or after the re-invocation of a persistent group on a
  66. * P2P Client.
  67. */
  68. #define P2P_MAX_INITIAL_CONN_WAIT 10
  69. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  70. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
  71. /*
  72. * How many seconds to wait for initial 4-way handshake to get completed after
  73. * WPS provisioning step on the GO. This controls the extra time the P2P
  74. * operation is considered to be in progress (e.g., to delay other scans) after
  75. * WPS provisioning has been completed on the GO during group formation.
  76. */
  77. #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
  78. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
  79. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
  80. /*
  81. * How many seconds to wait for initial 4-way handshake to get completed after
  82. * re-invocation of a persistent group on the GO when the client is expected
  83. * to connect automatically (no user interaction).
  84. */
  85. #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
  86. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
  87. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  88. /*
  89. * How many seconds to wait to re-attempt to move GOs, in case previous attempt
  90. * was not possible.
  91. */
  92. #define P2P_RECONSIDER_GO_MOVE_DELAY 30
  93. enum p2p_group_removal_reason {
  94. P2P_GROUP_REMOVAL_UNKNOWN,
  95. P2P_GROUP_REMOVAL_SILENT,
  96. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  97. P2P_GROUP_REMOVAL_REQUESTED,
  98. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  99. P2P_GROUP_REMOVAL_UNAVAILABLE,
  100. P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
  101. P2P_GROUP_REMOVAL_PSK_FAILURE,
  102. P2P_GROUP_REMOVAL_FREQ_CONFLICT,
  103. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
  104. };
  105. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  106. static struct wpa_supplicant *
  107. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  108. int go);
  109. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
  110. const u8 *ssid, size_t ssid_len);
  111. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  112. int *force_freq, int *pref_freq, int go,
  113. unsigned int *pref_freq_list,
  114. unsigned int *num_pref_freq);
  115. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  116. const u8 *ssid, size_t ssid_len);
  117. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  118. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  119. const u8 *dev_addr, enum p2p_wps_method wps_method,
  120. int auto_join, int freq,
  121. const u8 *ssid, size_t ssid_len);
  122. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  123. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  124. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  125. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  126. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  127. void *timeout_ctx);
  128. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
  129. static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  130. int group_added);
  131. static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  132. static void wpas_stop_listen(void *ctx);
  133. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
  134. static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
  135. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  136. enum wpa_driver_if_type type);
  137. static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
  138. int already_deleted);
  139. static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
  140. struct wpa_used_freq_data *freqs,
  141. unsigned int num);
  142. static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
  143. static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
  144. static void
  145. wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
  146. struct wpa_used_freq_data *freqs, unsigned int num,
  147. enum wpas_p2p_channel_update_trig trig);
  148. static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
  149. /*
  150. * Get the number of concurrent channels that the HW can operate, but that are
  151. * currently not in use by any of the wpa_supplicant interfaces.
  152. */
  153. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  154. {
  155. int *freqs;
  156. int num, unused;
  157. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  158. if (!freqs)
  159. return -1;
  160. num = get_shared_radio_freqs(wpa_s, freqs,
  161. wpa_s->num_multichan_concurrent);
  162. os_free(freqs);
  163. unused = wpa_s->num_multichan_concurrent - num;
  164. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
  165. return unused;
  166. }
  167. /*
  168. * Get the frequencies that are currently in use by one or more of the virtual
  169. * interfaces, and that are also valid for P2P operation.
  170. */
  171. static unsigned int
  172. wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  173. struct wpa_used_freq_data *p2p_freqs,
  174. unsigned int len)
  175. {
  176. struct wpa_used_freq_data *freqs;
  177. unsigned int num, i, j;
  178. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  179. sizeof(struct wpa_used_freq_data));
  180. if (!freqs)
  181. return 0;
  182. num = get_shared_radio_freqs_data(wpa_s, freqs,
  183. wpa_s->num_multichan_concurrent);
  184. os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
  185. for (i = 0, j = 0; i < num && j < len; i++) {
  186. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
  187. p2p_freqs[j++] = freqs[i];
  188. }
  189. os_free(freqs);
  190. dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
  191. return j;
  192. }
  193. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  194. int freq)
  195. {
  196. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  197. return;
  198. /* Use the wpa_s used to control the P2P Device operation */
  199. wpa_s = wpa_s->global->p2p_init_wpa_s;
  200. if (wpa_s->conf->p2p_ignore_shared_freq &&
  201. freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  202. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  203. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
  204. freq);
  205. freq = 0;
  206. }
  207. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  208. }
  209. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  210. struct wpa_scan_results *scan_res)
  211. {
  212. size_t i;
  213. if (wpa_s->p2p_scan_work) {
  214. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  215. wpa_s->p2p_scan_work = NULL;
  216. radio_work_done(work);
  217. }
  218. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  219. return;
  220. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  221. (int) scan_res->num);
  222. for (i = 0; i < scan_res->num; i++) {
  223. struct wpa_scan_res *bss = scan_res->res[i];
  224. struct os_reltime time_tmp_age, entry_ts;
  225. const u8 *ies;
  226. size_t ies_len;
  227. time_tmp_age.sec = bss->age / 1000;
  228. time_tmp_age.usec = (bss->age % 1000) * 1000;
  229. os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  230. ies = (const u8 *) (bss + 1);
  231. ies_len = bss->ie_len;
  232. if (bss->beacon_ie_len > 0 &&
  233. !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  234. wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  235. wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
  236. MACSTR, MAC2STR(bss->bssid));
  237. ies = ies + ies_len;
  238. ies_len = bss->beacon_ie_len;
  239. }
  240. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  241. bss->freq, &entry_ts, bss->level,
  242. ies, ies_len) > 0)
  243. break;
  244. }
  245. p2p_scan_res_handled(wpa_s->global->p2p);
  246. }
  247. static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
  248. {
  249. struct wpa_supplicant *wpa_s = work->wpa_s;
  250. struct wpa_driver_scan_params *params = work->ctx;
  251. int ret;
  252. if (deinit) {
  253. if (!work->started) {
  254. wpa_scan_free_params(params);
  255. return;
  256. }
  257. wpa_s->p2p_scan_work = NULL;
  258. return;
  259. }
  260. if (wpa_s->clear_driver_scan_cache) {
  261. wpa_printf(MSG_DEBUG,
  262. "Request driver to clear scan cache due to local BSS flush");
  263. params->only_new_results = 1;
  264. }
  265. ret = wpa_drv_scan(wpa_s, params);
  266. if (ret == 0)
  267. wpa_s->curr_scan_cookie = params->scan_cookie;
  268. wpa_scan_free_params(params);
  269. work->ctx = NULL;
  270. if (ret) {
  271. radio_work_done(work);
  272. p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
  273. return;
  274. }
  275. p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
  276. os_get_reltime(&wpa_s->scan_trigger_time);
  277. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  278. wpa_s->own_scan_requested = 1;
  279. wpa_s->clear_driver_scan_cache = 0;
  280. wpa_s->p2p_scan_work = work;
  281. }
  282. static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
  283. int freq)
  284. {
  285. if (wpa_s->global->p2p_24ghz_social_channels &&
  286. (freq == 2412 || freq == 2437 || freq == 2462)) {
  287. /*
  288. * Search all social channels regardless of whether these have
  289. * been disabled for P2P operating channel use to avoid missing
  290. * peers.
  291. */
  292. return 1;
  293. }
  294. return p2p_supported_freq(wpa_s->global->p2p, freq);
  295. }
  296. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  297. unsigned int num_req_dev_types,
  298. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  299. {
  300. struct wpa_supplicant *wpa_s = ctx;
  301. struct wpa_driver_scan_params *params = NULL;
  302. struct wpabuf *wps_ie, *ies;
  303. unsigned int num_channels = 0;
  304. int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
  305. size_t ielen;
  306. u8 *n, i;
  307. unsigned int bands;
  308. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  309. return -1;
  310. if (wpa_s->p2p_scan_work) {
  311. wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
  312. return -1;
  313. }
  314. params = os_zalloc(sizeof(*params));
  315. if (params == NULL)
  316. return -1;
  317. /* P2P Wildcard SSID */
  318. params->num_ssids = 1;
  319. n = os_malloc(P2P_WILDCARD_SSID_LEN);
  320. if (n == NULL)
  321. goto fail;
  322. os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  323. params->ssids[0].ssid = n;
  324. params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  325. wpa_s->wps->dev.p2p = 1;
  326. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  327. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  328. num_req_dev_types, req_dev_types);
  329. if (wps_ie == NULL)
  330. goto fail;
  331. switch (type) {
  332. case P2P_SCAN_SOCIAL:
  333. params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
  334. sizeof(int));
  335. if (params->freqs == NULL)
  336. goto fail;
  337. for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
  338. if (wpas_p2p_search_social_channel(
  339. wpa_s, social_channels_freq[i]))
  340. params->freqs[num_channels++] =
  341. social_channels_freq[i];
  342. }
  343. params->freqs[num_channels++] = 0;
  344. break;
  345. case P2P_SCAN_FULL:
  346. break;
  347. case P2P_SCAN_SPECIFIC:
  348. params->freqs = os_calloc(2, sizeof(int));
  349. if (params->freqs == NULL)
  350. goto fail;
  351. params->freqs[0] = freq;
  352. params->freqs[1] = 0;
  353. break;
  354. case P2P_SCAN_SOCIAL_PLUS_ONE:
  355. params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
  356. sizeof(int));
  357. if (params->freqs == NULL)
  358. goto fail;
  359. for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
  360. if (wpas_p2p_search_social_channel(
  361. wpa_s, social_channels_freq[i]))
  362. params->freqs[num_channels++] =
  363. social_channels_freq[i];
  364. }
  365. if (p2p_supported_freq(wpa_s->global->p2p, freq))
  366. params->freqs[num_channels++] = freq;
  367. params->freqs[num_channels++] = 0;
  368. break;
  369. }
  370. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  371. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  372. if (ies == NULL) {
  373. wpabuf_free(wps_ie);
  374. goto fail;
  375. }
  376. wpabuf_put_buf(ies, wps_ie);
  377. wpabuf_free(wps_ie);
  378. bands = wpas_get_bands(wpa_s, params->freqs);
  379. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
  380. params->p2p_probe = 1;
  381. n = os_malloc(wpabuf_len(ies));
  382. if (n == NULL) {
  383. wpabuf_free(ies);
  384. goto fail;
  385. }
  386. os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
  387. params->extra_ies = n;
  388. params->extra_ies_len = wpabuf_len(ies);
  389. wpabuf_free(ies);
  390. radio_remove_works(wpa_s, "p2p-scan", 0);
  391. if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
  392. params) < 0)
  393. goto fail;
  394. return 0;
  395. fail:
  396. wpa_scan_free_params(params);
  397. return -1;
  398. }
  399. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  400. {
  401. switch (p2p_group_interface) {
  402. case P2P_GROUP_INTERFACE_PENDING:
  403. return WPA_IF_P2P_GROUP;
  404. case P2P_GROUP_INTERFACE_GO:
  405. return WPA_IF_P2P_GO;
  406. case P2P_GROUP_INTERFACE_CLIENT:
  407. return WPA_IF_P2P_CLIENT;
  408. }
  409. return WPA_IF_P2P_GROUP;
  410. }
  411. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  412. const u8 *ssid,
  413. size_t ssid_len, int *go)
  414. {
  415. struct wpa_ssid *s;
  416. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  417. for (s = wpa_s->conf->ssid; s; s = s->next) {
  418. if (s->disabled != 0 || !s->p2p_group ||
  419. s->ssid_len != ssid_len ||
  420. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  421. continue;
  422. if (s->mode == WPAS_MODE_P2P_GO &&
  423. s != wpa_s->current_ssid)
  424. continue;
  425. if (go)
  426. *go = s->mode == WPAS_MODE_P2P_GO;
  427. return wpa_s;
  428. }
  429. }
  430. return NULL;
  431. }
  432. static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
  433. {
  434. struct wpa_supplicant *wpa_s = eloop_ctx;
  435. wpa_printf(MSG_DEBUG,
  436. "P2P: Complete previously requested removal of %s",
  437. wpa_s->ifname);
  438. wpas_p2p_disconnect(wpa_s);
  439. }
  440. static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
  441. struct wpa_supplicant *calling_wpa_s)
  442. {
  443. if (calling_wpa_s == wpa_s && wpa_s &&
  444. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  445. /*
  446. * The calling wpa_s instance is going to be removed. Do that
  447. * from an eloop callback to keep the instance available until
  448. * the caller has returned. This my be needed, e.g., to provide
  449. * control interface responses on the per-interface socket.
  450. */
  451. if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
  452. wpa_s, NULL) < 0)
  453. return -1;
  454. return 0;
  455. }
  456. return wpas_p2p_disconnect(wpa_s);
  457. }
  458. /* Determine total number of clients in active groups where we are the GO */
  459. static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
  460. {
  461. unsigned int count = 0;
  462. struct wpa_ssid *s;
  463. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  464. for (s = wpa_s->conf->ssid; s; s = s->next) {
  465. wpa_printf(MSG_DEBUG,
  466. "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
  467. wpa_s, s, s->disabled, s->p2p_group,
  468. s->mode);
  469. if (!s->disabled && s->p2p_group &&
  470. s->mode == WPAS_MODE_P2P_GO) {
  471. count += p2p_get_group_num_members(
  472. wpa_s->p2p_group);
  473. }
  474. }
  475. }
  476. return count;
  477. }
  478. static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
  479. {
  480. return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
  481. !wpa_s->current_ssid->disabled &&
  482. wpa_s->current_ssid->p2p_group &&
  483. wpa_s->current_ssid->p2p_persistent_group;
  484. }
  485. static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
  486. {
  487. return p2p_is_active_persistent_group(wpa_s) &&
  488. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
  489. }
  490. /* Find an interface for a P2P group where we are the GO */
  491. static struct wpa_supplicant *
  492. wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
  493. {
  494. struct wpa_supplicant *save = NULL;
  495. if (!wpa_s)
  496. return NULL;
  497. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  498. if (!p2p_is_active_persistent_go(wpa_s))
  499. continue;
  500. /* Prefer a group with connected clients */
  501. if (p2p_get_group_num_members(wpa_s->p2p_group))
  502. return wpa_s;
  503. save = wpa_s;
  504. }
  505. /* No group with connected clients, so pick the one without (if any) */
  506. return save;
  507. }
  508. static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
  509. {
  510. return p2p_is_active_persistent_group(wpa_s) &&
  511. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  512. }
  513. /* Find an interface for a P2P group where we are the P2P Client */
  514. static struct wpa_supplicant *
  515. wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
  516. {
  517. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  518. if (p2p_is_active_persistent_cli(wpa_s))
  519. return wpa_s;
  520. }
  521. return NULL;
  522. }
  523. /* Find a persistent group where we are the GO */
  524. static struct wpa_ssid *
  525. wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
  526. {
  527. struct wpa_ssid *s;
  528. for (s = wpa_s->conf->ssid; s; s = s->next) {
  529. if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
  530. return s;
  531. }
  532. return NULL;
  533. }
  534. static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
  535. unsigned int *force_freq,
  536. unsigned int *pref_freq)
  537. {
  538. struct wpa_supplicant *wpa_s = ctx;
  539. struct wpa_ssid *s;
  540. u8 conncap = P2PS_SETUP_NONE;
  541. unsigned int owned_members = 0;
  542. struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
  543. struct wpa_ssid *persistent_go;
  544. int p2p_no_group_iface;
  545. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  546. wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
  547. if (force_freq)
  548. *force_freq = 0;
  549. if (pref_freq)
  550. *pref_freq = 0;
  551. size = P2P_MAX_PREF_CHANNELS;
  552. if (force_freq && pref_freq &&
  553. !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
  554. (int *) pref_freq, 0, pref_freq_list, &size))
  555. wpas_p2p_set_own_freq_preference(wpa_s,
  556. *force_freq ? *force_freq :
  557. *pref_freq);
  558. /*
  559. * For non-concurrent capable devices:
  560. * If persistent_go, then no new.
  561. * If GO, then no client.
  562. * If client, then no GO.
  563. */
  564. go_wpa_s = wpas_p2p_get_go_group(wpa_s);
  565. if (go_wpa_s)
  566. owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
  567. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  568. p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
  569. cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
  570. wpa_printf(MSG_DEBUG,
  571. "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
  572. go_wpa_s, owned_members, cli_wpa_s, persistent_go);
  573. /* If not concurrent, restrict our choices */
  574. if (p2p_no_group_iface) {
  575. wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
  576. if (cli_wpa_s)
  577. return P2PS_SETUP_NONE;
  578. if (go_wpa_s) {
  579. if (role == P2PS_SETUP_CLIENT ||
  580. incoming == P2PS_SETUP_GROUP_OWNER ||
  581. p2p_client_limit_reached(go_wpa_s->p2p_group))
  582. return P2PS_SETUP_NONE;
  583. return P2PS_SETUP_GROUP_OWNER;
  584. }
  585. if (persistent_go) {
  586. if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
  587. if (!incoming)
  588. return P2PS_SETUP_GROUP_OWNER |
  589. P2PS_SETUP_CLIENT;
  590. if (incoming == P2PS_SETUP_NEW) {
  591. u8 r;
  592. if (os_get_random(&r, sizeof(r)) < 0 ||
  593. (r & 1))
  594. return P2PS_SETUP_CLIENT;
  595. return P2PS_SETUP_GROUP_OWNER;
  596. }
  597. }
  598. }
  599. }
  600. /* If a required role has been specified, handle it here */
  601. if (role && role != P2PS_SETUP_NEW) {
  602. switch (incoming) {
  603. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
  604. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
  605. /*
  606. * Peer has an active GO, so if the role allows it and
  607. * we do not have any active roles, become client.
  608. */
  609. if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
  610. !cli_wpa_s)
  611. return P2PS_SETUP_CLIENT;
  612. /* fall through */
  613. case P2PS_SETUP_NONE:
  614. case P2PS_SETUP_NEW:
  615. conncap = role;
  616. goto grp_owner;
  617. case P2PS_SETUP_GROUP_OWNER:
  618. /*
  619. * Must be a complimentary role - cannot be a client to
  620. * more than one peer.
  621. */
  622. if (incoming == role || cli_wpa_s)
  623. return P2PS_SETUP_NONE;
  624. return P2PS_SETUP_CLIENT;
  625. case P2PS_SETUP_CLIENT:
  626. /* Must be a complimentary role */
  627. if (incoming != role) {
  628. conncap = P2PS_SETUP_GROUP_OWNER;
  629. goto grp_owner;
  630. }
  631. default:
  632. return P2PS_SETUP_NONE;
  633. }
  634. }
  635. /*
  636. * For now, we only will support ownership of one group, and being a
  637. * client of one group. Therefore, if we have either an existing GO
  638. * group, or an existing client group, we will not do a new GO
  639. * negotiation, but rather try to re-use the existing groups.
  640. */
  641. switch (incoming) {
  642. case P2PS_SETUP_NONE:
  643. case P2PS_SETUP_NEW:
  644. if (cli_wpa_s)
  645. conncap = P2PS_SETUP_GROUP_OWNER;
  646. else if (!owned_members)
  647. conncap = P2PS_SETUP_NEW;
  648. else if (incoming == P2PS_SETUP_NONE)
  649. conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
  650. else
  651. conncap = P2PS_SETUP_CLIENT;
  652. break;
  653. case P2PS_SETUP_CLIENT:
  654. conncap = P2PS_SETUP_GROUP_OWNER;
  655. break;
  656. case P2PS_SETUP_GROUP_OWNER:
  657. if (!cli_wpa_s)
  658. conncap = P2PS_SETUP_CLIENT;
  659. break;
  660. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
  661. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
  662. if (cli_wpa_s)
  663. conncap = P2PS_SETUP_GROUP_OWNER;
  664. else {
  665. u8 r;
  666. if (os_get_random(&r, sizeof(r)) < 0 ||
  667. (r & 1))
  668. conncap = P2PS_SETUP_CLIENT;
  669. else
  670. conncap = P2PS_SETUP_GROUP_OWNER;
  671. }
  672. break;
  673. default:
  674. return P2PS_SETUP_NONE;
  675. }
  676. grp_owner:
  677. if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
  678. (!incoming && (conncap & P2PS_SETUP_NEW))) {
  679. if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
  680. conncap &= ~P2PS_SETUP_GROUP_OWNER;
  681. s = wpas_p2p_get_persistent_go(wpa_s);
  682. if (!s && !go_wpa_s && p2p_no_group_iface) {
  683. p2p_set_intended_addr(wpa_s->global->p2p,
  684. wpa_s->p2p_mgmt ?
  685. wpa_s->parent->own_addr :
  686. wpa_s->own_addr);
  687. } else if (!s && !go_wpa_s) {
  688. if (wpas_p2p_add_group_interface(wpa_s,
  689. WPA_IF_P2P_GROUP) < 0) {
  690. wpa_printf(MSG_ERROR,
  691. "P2P: Failed to allocate a new interface for the group");
  692. return P2PS_SETUP_NONE;
  693. }
  694. wpa_s->global->pending_group_iface_for_p2ps = 1;
  695. p2p_set_intended_addr(wpa_s->global->p2p,
  696. wpa_s->pending_interface_addr);
  697. }
  698. }
  699. return conncap;
  700. }
  701. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  702. enum p2p_group_removal_reason removal_reason)
  703. {
  704. struct wpa_ssid *ssid;
  705. char *gtype;
  706. const char *reason;
  707. ssid = wpa_s->current_ssid;
  708. if (ssid == NULL) {
  709. /*
  710. * The current SSID was not known, but there may still be a
  711. * pending P2P group interface waiting for provisioning or a
  712. * P2P group that is trying to reconnect.
  713. */
  714. ssid = wpa_s->conf->ssid;
  715. while (ssid) {
  716. if (ssid->p2p_group && ssid->disabled != 2)
  717. break;
  718. ssid = ssid->next;
  719. }
  720. if (ssid == NULL &&
  721. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  722. {
  723. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  724. "not found");
  725. return -1;
  726. }
  727. }
  728. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  729. gtype = "GO";
  730. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  731. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  732. wpa_s->reassociate = 0;
  733. wpa_s->disconnected = 1;
  734. gtype = "client";
  735. } else
  736. gtype = "GO";
  737. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  738. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  739. if (os_strcmp(gtype, "client") == 0) {
  740. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  741. if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
  742. wpa_s, NULL)) {
  743. wpa_printf(MSG_DEBUG,
  744. "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
  745. removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
  746. eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
  747. wpa_s, NULL);
  748. }
  749. }
  750. if (wpa_s->cross_connect_in_use) {
  751. wpa_s->cross_connect_in_use = 0;
  752. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  753. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  754. wpa_s->ifname, wpa_s->cross_connect_uplink);
  755. }
  756. switch (removal_reason) {
  757. case P2P_GROUP_REMOVAL_REQUESTED:
  758. reason = " reason=REQUESTED";
  759. break;
  760. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  761. reason = " reason=FORMATION_FAILED";
  762. break;
  763. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  764. reason = " reason=IDLE";
  765. break;
  766. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  767. reason = " reason=UNAVAILABLE";
  768. break;
  769. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  770. reason = " reason=GO_ENDING_SESSION";
  771. break;
  772. case P2P_GROUP_REMOVAL_PSK_FAILURE:
  773. reason = " reason=PSK_FAILURE";
  774. break;
  775. case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
  776. reason = " reason=FREQ_CONFLICT";
  777. break;
  778. default:
  779. reason = "";
  780. break;
  781. }
  782. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  783. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  784. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  785. wpa_s->ifname, gtype, reason);
  786. }
  787. if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
  788. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
  789. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  790. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  791. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  792. wpa_s->p2pdev, NULL) > 0) {
  793. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  794. "timeout");
  795. wpa_s->p2p_in_provisioning = 0;
  796. wpas_p2p_group_formation_failed(wpa_s, 1);
  797. }
  798. wpa_s->p2p_in_invitation = 0;
  799. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  800. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
  801. /*
  802. * Make sure wait for the first client does not remain active after the
  803. * group has been removed.
  804. */
  805. wpa_s->global->p2p_go_wait_client.sec = 0;
  806. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  807. struct wpa_global *global;
  808. char *ifname;
  809. enum wpa_driver_if_type type;
  810. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  811. wpa_s->ifname);
  812. global = wpa_s->global;
  813. ifname = os_strdup(wpa_s->ifname);
  814. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  815. eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
  816. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  817. wpa_s = global->ifaces;
  818. if (wpa_s && ifname)
  819. wpa_drv_if_remove(wpa_s, type, ifname);
  820. os_free(ifname);
  821. return 1;
  822. }
  823. /*
  824. * The primary interface was used for P2P group operations, so
  825. * need to reset its p2pdev.
  826. */
  827. wpa_s->p2pdev = wpa_s->parent;
  828. if (!wpa_s->p2p_go_group_formation_completed) {
  829. wpa_s->global->p2p_group_formation = NULL;
  830. wpa_s->p2p_in_provisioning = 0;
  831. }
  832. wpa_s->show_group_started = 0;
  833. os_free(wpa_s->go_params);
  834. wpa_s->go_params = NULL;
  835. os_free(wpa_s->p2p_group_common_freqs);
  836. wpa_s->p2p_group_common_freqs = NULL;
  837. wpa_s->p2p_group_common_freqs_num = 0;
  838. wpa_s->waiting_presence_resp = 0;
  839. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  840. if (ssid && (ssid->p2p_group ||
  841. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  842. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  843. int id = ssid->id;
  844. if (ssid == wpa_s->current_ssid) {
  845. wpa_sm_set_config(wpa_s->wpa, NULL);
  846. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  847. wpa_s->current_ssid = NULL;
  848. }
  849. /*
  850. * Networks objects created during any P2P activities are not
  851. * exposed out as they might/will confuse certain non-P2P aware
  852. * applications since these network objects won't behave like
  853. * regular ones.
  854. *
  855. * Likewise, we don't send out network removed signals for such
  856. * network objects.
  857. */
  858. wpa_config_remove_network(wpa_s->conf, id);
  859. wpa_supplicant_clear_status(wpa_s);
  860. wpa_supplicant_cancel_sched_scan(wpa_s);
  861. } else {
  862. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  863. "found");
  864. }
  865. if (wpa_s->ap_iface)
  866. wpa_supplicant_ap_deinit(wpa_s);
  867. else
  868. wpa_drv_deinit_p2p_cli(wpa_s);
  869. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  870. return 0;
  871. }
  872. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  873. u8 *go_dev_addr,
  874. const u8 *ssid, size_t ssid_len)
  875. {
  876. struct wpa_bss *bss;
  877. const u8 *bssid;
  878. struct wpabuf *p2p;
  879. u8 group_capab;
  880. const u8 *addr;
  881. if (wpa_s->go_params)
  882. bssid = wpa_s->go_params->peer_interface_addr;
  883. else
  884. bssid = wpa_s->bssid;
  885. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  886. if (bss == NULL && wpa_s->go_params &&
  887. !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
  888. bss = wpa_bss_get_p2p_dev_addr(
  889. wpa_s, wpa_s->go_params->peer_device_addr);
  890. if (bss == NULL) {
  891. u8 iface_addr[ETH_ALEN];
  892. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  893. iface_addr) == 0)
  894. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  895. }
  896. if (bss == NULL) {
  897. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  898. "group is persistent - BSS " MACSTR " not found",
  899. MAC2STR(bssid));
  900. return 0;
  901. }
  902. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  903. if (p2p == NULL)
  904. p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
  905. P2P_IE_VENDOR_TYPE);
  906. if (p2p == NULL) {
  907. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  908. "group is persistent - BSS " MACSTR
  909. " did not include P2P IE", MAC2STR(bssid));
  910. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  911. (u8 *) (bss + 1), bss->ie_len);
  912. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  913. ((u8 *) bss + 1) + bss->ie_len,
  914. bss->beacon_ie_len);
  915. return 0;
  916. }
  917. group_capab = p2p_get_group_capab(p2p);
  918. addr = p2p_get_go_dev_addr(p2p);
  919. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  920. "group_capab=0x%x", group_capab);
  921. if (addr) {
  922. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  923. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  924. MAC2STR(addr));
  925. } else
  926. os_memset(go_dev_addr, 0, ETH_ALEN);
  927. wpabuf_free(p2p);
  928. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  929. "go_dev_addr=" MACSTR,
  930. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  931. return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
  932. }
  933. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  934. struct wpa_ssid *ssid,
  935. const u8 *go_dev_addr)
  936. {
  937. struct wpa_ssid *s;
  938. int changed = 0;
  939. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  940. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  941. for (s = wpa_s->conf->ssid; s; s = s->next) {
  942. if (s->disabled == 2 &&
  943. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  944. s->ssid_len == ssid->ssid_len &&
  945. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  946. break;
  947. }
  948. if (s) {
  949. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  950. "entry");
  951. if (ssid->passphrase && !s->passphrase)
  952. changed = 1;
  953. else if (ssid->passphrase && s->passphrase &&
  954. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  955. changed = 1;
  956. } else {
  957. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  958. "entry");
  959. changed = 1;
  960. s = wpa_config_add_network(wpa_s->conf);
  961. if (s == NULL)
  962. return -1;
  963. /*
  964. * Instead of network_added we emit persistent_group_added
  965. * notification. Also to keep the defense checks in
  966. * persistent_group obj registration method, we set the
  967. * relevant flags in s to designate it as a persistent group.
  968. */
  969. s->p2p_group = 1;
  970. s->p2p_persistent_group = 1;
  971. wpas_notify_persistent_group_added(wpa_s, s);
  972. wpa_config_set_network_defaults(s);
  973. }
  974. s->p2p_group = 1;
  975. s->p2p_persistent_group = 1;
  976. s->disabled = 2;
  977. s->bssid_set = 1;
  978. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  979. s->mode = ssid->mode;
  980. s->auth_alg = WPA_AUTH_ALG_OPEN;
  981. s->key_mgmt = WPA_KEY_MGMT_PSK;
  982. s->proto = WPA_PROTO_RSN;
  983. s->pbss = ssid->pbss;
  984. s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
  985. s->export_keys = 1;
  986. if (ssid->passphrase) {
  987. os_free(s->passphrase);
  988. s->passphrase = os_strdup(ssid->passphrase);
  989. }
  990. if (ssid->psk_set) {
  991. s->psk_set = 1;
  992. os_memcpy(s->psk, ssid->psk, 32);
  993. }
  994. if (s->passphrase && !s->psk_set)
  995. wpa_config_update_psk(s);
  996. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  997. os_free(s->ssid);
  998. s->ssid = os_malloc(ssid->ssid_len);
  999. }
  1000. if (s->ssid) {
  1001. s->ssid_len = ssid->ssid_len;
  1002. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  1003. }
  1004. if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
  1005. dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
  1006. wpa_s->global->add_psk = NULL;
  1007. changed = 1;
  1008. }
  1009. if (changed && wpa_s->conf->update_config &&
  1010. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  1011. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1012. }
  1013. return s->id;
  1014. }
  1015. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  1016. const u8 *addr)
  1017. {
  1018. struct wpa_ssid *ssid, *s;
  1019. u8 *n;
  1020. size_t i;
  1021. int found = 0;
  1022. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  1023. ssid = wpa_s->current_ssid;
  1024. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  1025. !ssid->p2p_persistent_group)
  1026. return;
  1027. for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
  1028. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  1029. continue;
  1030. if (s->ssid_len == ssid->ssid_len &&
  1031. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  1032. break;
  1033. }
  1034. if (s == NULL)
  1035. return;
  1036. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  1037. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
  1038. ETH_ALEN) != 0)
  1039. continue;
  1040. if (i == s->num_p2p_clients - 1)
  1041. return; /* already the most recent entry */
  1042. /* move the entry to mark it most recent */
  1043. os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
  1044. s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  1045. (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  1046. os_memcpy(s->p2p_client_list +
  1047. (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
  1048. ETH_ALEN);
  1049. os_memset(s->p2p_client_list +
  1050. (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
  1051. 0xff, ETH_ALEN);
  1052. found = 1;
  1053. break;
  1054. }
  1055. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  1056. n = os_realloc_array(s->p2p_client_list,
  1057. s->num_p2p_clients + 1, 2 * ETH_ALEN);
  1058. if (n == NULL)
  1059. return;
  1060. os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
  1061. ETH_ALEN);
  1062. os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
  1063. 0xff, ETH_ALEN);
  1064. s->p2p_client_list = n;
  1065. s->num_p2p_clients++;
  1066. } else if (!found && s->p2p_client_list) {
  1067. /* Not enough room for an additional entry - drop the oldest
  1068. * entry */
  1069. os_memmove(s->p2p_client_list,
  1070. s->p2p_client_list + 2 * ETH_ALEN,
  1071. (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
  1072. os_memcpy(s->p2p_client_list +
  1073. (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
  1074. addr, ETH_ALEN);
  1075. os_memset(s->p2p_client_list +
  1076. (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
  1077. 0xff, ETH_ALEN);
  1078. }
  1079. if (p2p_wpa_s->conf->update_config &&
  1080. wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
  1081. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1082. }
  1083. static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
  1084. int go, struct wpa_ssid *ssid, int freq,
  1085. const u8 *psk, const char *passphrase,
  1086. const u8 *go_dev_addr, int persistent,
  1087. const char *extra)
  1088. {
  1089. const char *ssid_txt;
  1090. char psk_txt[65];
  1091. if (psk)
  1092. wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
  1093. else
  1094. psk_txt[0] = '\0';
  1095. if (ssid)
  1096. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  1097. else
  1098. ssid_txt = "";
  1099. if (passphrase && passphrase[0] == '\0')
  1100. passphrase = NULL;
  1101. /*
  1102. * Include PSK/passphrase only in the control interface message and
  1103. * leave it out from the debug log entry.
  1104. */
  1105. wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
  1106. P2P_EVENT_GROUP_STARTED
  1107. "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
  1108. MACSTR "%s%s",
  1109. wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
  1110. psk ? " psk=" : "", psk_txt,
  1111. passphrase ? " passphrase=\"" : "",
  1112. passphrase ? passphrase : "",
  1113. passphrase ? "\"" : "",
  1114. MAC2STR(go_dev_addr),
  1115. persistent ? " [PERSISTENT]" : "", extra);
  1116. wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
  1117. "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
  1118. wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
  1119. MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
  1120. extra);
  1121. }
  1122. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  1123. int success, int already_deleted)
  1124. {
  1125. struct wpa_ssid *ssid;
  1126. int client;
  1127. int persistent;
  1128. u8 go_dev_addr[ETH_ALEN];
  1129. /*
  1130. * This callback is likely called for the main interface. Update wpa_s
  1131. * to use the group interface if a new interface was created for the
  1132. * group.
  1133. */
  1134. if (wpa_s->global->p2p_group_formation)
  1135. wpa_s = wpa_s->global->p2p_group_formation;
  1136. if (wpa_s->p2p_go_group_formation_completed) {
  1137. wpa_s->global->p2p_group_formation = NULL;
  1138. wpa_s->p2p_in_provisioning = 0;
  1139. }
  1140. wpa_s->p2p_in_invitation = 0;
  1141. wpa_s->group_formation_reported = 1;
  1142. if (!success) {
  1143. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  1144. P2P_EVENT_GROUP_FORMATION_FAILURE);
  1145. wpas_notify_p2p_group_formation_failure(wpa_s, "");
  1146. if (already_deleted)
  1147. return;
  1148. wpas_p2p_group_delete(wpa_s,
  1149. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  1150. return;
  1151. }
  1152. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  1153. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  1154. ssid = wpa_s->current_ssid;
  1155. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  1156. ssid->mode = WPAS_MODE_P2P_GO;
  1157. p2p_group_notif_formation_done(wpa_s->p2p_group);
  1158. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  1159. }
  1160. persistent = 0;
  1161. if (ssid) {
  1162. client = ssid->mode == WPAS_MODE_INFRA;
  1163. if (ssid->mode == WPAS_MODE_P2P_GO) {
  1164. persistent = ssid->p2p_persistent_group;
  1165. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  1166. ETH_ALEN);
  1167. } else
  1168. persistent = wpas_p2p_persistent_group(wpa_s,
  1169. go_dev_addr,
  1170. ssid->ssid,
  1171. ssid->ssid_len);
  1172. } else {
  1173. client = wpa_s->p2p_group_interface ==
  1174. P2P_GROUP_INTERFACE_CLIENT;
  1175. os_memset(go_dev_addr, 0, ETH_ALEN);
  1176. }
  1177. wpa_s->show_group_started = 0;
  1178. if (client) {
  1179. /*
  1180. * Indicate event only after successfully completed 4-way
  1181. * handshake, i.e., when the interface is ready for data
  1182. * packets.
  1183. */
  1184. wpa_s->show_group_started = 1;
  1185. } else {
  1186. wpas_p2p_group_started(wpa_s, 1, ssid,
  1187. ssid ? ssid->frequency : 0,
  1188. ssid && ssid->passphrase == NULL &&
  1189. ssid->psk_set ? ssid->psk : NULL,
  1190. ssid ? ssid->passphrase : NULL,
  1191. go_dev_addr, persistent, "");
  1192. wpas_p2p_cross_connect_setup(wpa_s);
  1193. wpas_p2p_set_group_idle_timeout(wpa_s);
  1194. }
  1195. if (persistent)
  1196. wpas_p2p_store_persistent_group(wpa_s->p2pdev,
  1197. ssid, go_dev_addr);
  1198. else {
  1199. os_free(wpa_s->global->add_psk);
  1200. wpa_s->global->add_psk = NULL;
  1201. }
  1202. if (!client) {
  1203. wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
  1204. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  1205. }
  1206. }
  1207. struct send_action_work {
  1208. unsigned int freq;
  1209. u8 dst[ETH_ALEN];
  1210. u8 src[ETH_ALEN];
  1211. u8 bssid[ETH_ALEN];
  1212. size_t len;
  1213. unsigned int wait_time;
  1214. u8 buf[0];
  1215. };
  1216. static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
  1217. {
  1218. struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
  1219. wpa_printf(MSG_DEBUG,
  1220. "P2P: Free Action frame radio work @%p (freq=%u dst="
  1221. MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
  1222. wpa_s->p2p_send_action_work, awork->freq,
  1223. MAC2STR(awork->dst), MAC2STR(awork->src),
  1224. MAC2STR(awork->bssid), awork->wait_time);
  1225. wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
  1226. awork->buf, awork->len);
  1227. os_free(awork);
  1228. wpa_s->p2p_send_action_work->ctx = NULL;
  1229. radio_work_done(wpa_s->p2p_send_action_work);
  1230. wpa_s->p2p_send_action_work = NULL;
  1231. }
  1232. static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
  1233. void *timeout_ctx)
  1234. {
  1235. struct wpa_supplicant *wpa_s = eloop_ctx;
  1236. if (!wpa_s->p2p_send_action_work)
  1237. return;
  1238. wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
  1239. wpas_p2p_free_send_action_work(wpa_s);
  1240. }
  1241. static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
  1242. {
  1243. if (wpa_s->p2p_send_action_work) {
  1244. struct send_action_work *awork;
  1245. awork = wpa_s->p2p_send_action_work->ctx;
  1246. wpa_printf(MSG_DEBUG,
  1247. "P2P: Clear Action TX work @%p (wait_time=%u)",
  1248. wpa_s->p2p_send_action_work, awork->wait_time);
  1249. if (awork->wait_time == 0) {
  1250. wpas_p2p_free_send_action_work(wpa_s);
  1251. } else {
  1252. /*
  1253. * In theory, this should not be needed, but number of
  1254. * places in the P2P code is still using non-zero wait
  1255. * time for the last Action frame in the sequence and
  1256. * some of these do not call send_action_done().
  1257. */
  1258. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1259. wpa_s, NULL);
  1260. eloop_register_timeout(
  1261. 0, awork->wait_time * 1000,
  1262. wpas_p2p_send_action_work_timeout,
  1263. wpa_s, NULL);
  1264. }
  1265. }
  1266. }
  1267. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  1268. unsigned int freq,
  1269. const u8 *dst, const u8 *src,
  1270. const u8 *bssid,
  1271. const u8 *data, size_t data_len,
  1272. enum offchannel_send_action_result
  1273. result)
  1274. {
  1275. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  1276. wpas_p2p_action_tx_clear(wpa_s);
  1277. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  1278. return;
  1279. switch (result) {
  1280. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  1281. res = P2P_SEND_ACTION_SUCCESS;
  1282. break;
  1283. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  1284. res = P2P_SEND_ACTION_NO_ACK;
  1285. break;
  1286. case OFFCHANNEL_SEND_ACTION_FAILED:
  1287. res = P2P_SEND_ACTION_FAILED;
  1288. break;
  1289. }
  1290. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  1291. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  1292. wpa_s->pending_pd_before_join &&
  1293. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  1294. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  1295. wpa_s->p2p_fallback_to_go_neg) {
  1296. wpa_s->pending_pd_before_join = 0;
  1297. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  1298. "during p2p_connect-auto");
  1299. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  1300. P2P_EVENT_FALLBACK_TO_GO_NEG
  1301. "reason=no-ACK-to-PD-Req");
  1302. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  1303. return;
  1304. }
  1305. }
  1306. static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
  1307. {
  1308. struct wpa_supplicant *wpa_s = work->wpa_s;
  1309. struct send_action_work *awork = work->ctx;
  1310. if (deinit) {
  1311. if (work->started) {
  1312. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1313. wpa_s, NULL);
  1314. wpa_s->p2p_send_action_work = NULL;
  1315. offchannel_send_action_done(wpa_s);
  1316. }
  1317. os_free(awork);
  1318. return;
  1319. }
  1320. if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
  1321. awork->bssid, awork->buf, awork->len,
  1322. awork->wait_time,
  1323. wpas_p2p_send_action_tx_status, 1) < 0) {
  1324. os_free(awork);
  1325. radio_work_done(work);
  1326. return;
  1327. }
  1328. wpa_s->p2p_send_action_work = work;
  1329. }
  1330. static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
  1331. unsigned int freq, const u8 *dst,
  1332. const u8 *src, const u8 *bssid, const u8 *buf,
  1333. size_t len, unsigned int wait_time)
  1334. {
  1335. struct send_action_work *awork;
  1336. if (wpa_s->p2p_send_action_work) {
  1337. wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
  1338. return -1;
  1339. }
  1340. awork = os_zalloc(sizeof(*awork) + len);
  1341. if (awork == NULL)
  1342. return -1;
  1343. awork->freq = freq;
  1344. os_memcpy(awork->dst, dst, ETH_ALEN);
  1345. os_memcpy(awork->src, src, ETH_ALEN);
  1346. os_memcpy(awork->bssid, bssid, ETH_ALEN);
  1347. awork->len = len;
  1348. awork->wait_time = wait_time;
  1349. os_memcpy(awork->buf, buf, len);
  1350. if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
  1351. wpas_send_action_cb, awork) < 0) {
  1352. os_free(awork);
  1353. return -1;
  1354. }
  1355. return 0;
  1356. }
  1357. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  1358. const u8 *src, const u8 *bssid, const u8 *buf,
  1359. size_t len, unsigned int wait_time)
  1360. {
  1361. struct wpa_supplicant *wpa_s = ctx;
  1362. int listen_freq = -1, send_freq = -1;
  1363. if (wpa_s->p2p_listen_work)
  1364. listen_freq = wpa_s->p2p_listen_work->freq;
  1365. if (wpa_s->p2p_send_action_work)
  1366. send_freq = wpa_s->p2p_send_action_work->freq;
  1367. if (listen_freq != (int) freq && send_freq != (int) freq) {
  1368. wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
  1369. listen_freq, send_freq);
  1370. return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
  1371. len, wait_time);
  1372. }
  1373. wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
  1374. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  1375. wait_time,
  1376. wpas_p2p_send_action_tx_status, 1);
  1377. }
  1378. static void wpas_send_action_done(void *ctx)
  1379. {
  1380. struct wpa_supplicant *wpa_s = ctx;
  1381. if (wpa_s->p2p_send_action_work) {
  1382. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1383. wpa_s, NULL);
  1384. os_free(wpa_s->p2p_send_action_work->ctx);
  1385. radio_work_done(wpa_s->p2p_send_action_work);
  1386. wpa_s->p2p_send_action_work = NULL;
  1387. }
  1388. offchannel_send_action_done(wpa_s);
  1389. }
  1390. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  1391. struct p2p_go_neg_results *params)
  1392. {
  1393. if (wpa_s->go_params == NULL) {
  1394. wpa_s->go_params = os_malloc(sizeof(*params));
  1395. if (wpa_s->go_params == NULL)
  1396. return -1;
  1397. }
  1398. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  1399. return 0;
  1400. }
  1401. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  1402. struct p2p_go_neg_results *res)
  1403. {
  1404. wpa_s->group_formation_reported = 0;
  1405. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
  1406. " dev_addr " MACSTR " wps_method %d",
  1407. MAC2STR(res->peer_interface_addr),
  1408. MAC2STR(res->peer_device_addr), res->wps_method);
  1409. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  1410. res->ssid, res->ssid_len);
  1411. wpa_supplicant_ap_deinit(wpa_s);
  1412. wpas_copy_go_neg_results(wpa_s, res);
  1413. if (res->wps_method == WPS_PBC) {
  1414. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  1415. #ifdef CONFIG_WPS_NFC
  1416. } else if (res->wps_method == WPS_NFC) {
  1417. wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
  1418. res->peer_interface_addr,
  1419. wpa_s->p2pdev->p2p_oob_dev_pw,
  1420. wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
  1421. wpa_s->p2pdev->p2p_oob_dev_pw_id ==
  1422. DEV_PW_NFC_CONNECTION_HANDOVER ?
  1423. wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
  1424. NULL,
  1425. NULL, 0, 0);
  1426. #endif /* CONFIG_WPS_NFC */
  1427. } else {
  1428. u16 dev_pw_id = DEV_PW_DEFAULT;
  1429. if (wpa_s->p2p_wps_method == WPS_P2PS)
  1430. dev_pw_id = DEV_PW_P2PS_DEFAULT;
  1431. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  1432. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  1433. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  1434. wpa_s->p2p_pin, 1, dev_pw_id);
  1435. }
  1436. }
  1437. static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
  1438. struct wpa_ssid *ssid)
  1439. {
  1440. struct wpa_ssid *persistent;
  1441. struct psk_list_entry *psk;
  1442. struct hostapd_data *hapd;
  1443. if (!wpa_s->ap_iface)
  1444. return;
  1445. persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
  1446. ssid->ssid_len);
  1447. if (persistent == NULL)
  1448. return;
  1449. hapd = wpa_s->ap_iface->bss[0];
  1450. dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
  1451. list) {
  1452. struct hostapd_wpa_psk *hpsk;
  1453. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
  1454. MACSTR " psk=%d",
  1455. MAC2STR(psk->addr), psk->p2p);
  1456. hpsk = os_zalloc(sizeof(*hpsk));
  1457. if (hpsk == NULL)
  1458. break;
  1459. os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
  1460. if (psk->p2p)
  1461. os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
  1462. else
  1463. os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
  1464. hpsk->next = hapd->conf->ssid.wpa_psk;
  1465. hapd->conf->ssid.wpa_psk = hpsk;
  1466. }
  1467. }
  1468. static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
  1469. {
  1470. unsigned int i;
  1471. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
  1472. wpa_s->p2p_group_common_freqs_num);
  1473. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
  1474. wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
  1475. i, wpa_s->p2p_group_common_freqs[i]);
  1476. }
  1477. static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
  1478. struct p2p_go_neg_results *params)
  1479. {
  1480. unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
  1481. wpa_s->p2p_group_common_freqs_num = 0;
  1482. os_free(wpa_s->p2p_group_common_freqs);
  1483. wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
  1484. if (!wpa_s->p2p_group_common_freqs)
  1485. return;
  1486. for (i = 0; i < len; i++) {
  1487. if (!wpa_s->go_params->freq_list[i])
  1488. break;
  1489. wpa_s->p2p_group_common_freqs[i] =
  1490. wpa_s->go_params->freq_list[i];
  1491. }
  1492. wpa_s->p2p_group_common_freqs_num = i;
  1493. }
  1494. static void p2p_config_write(struct wpa_supplicant *wpa_s)
  1495. {
  1496. #ifndef CONFIG_NO_CONFIG_WRITE
  1497. if (wpa_s->p2pdev->conf->update_config &&
  1498. wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
  1499. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1500. #endif /* CONFIG_NO_CONFIG_WRITE */
  1501. }
  1502. static void p2p_go_configured(void *ctx, void *data)
  1503. {
  1504. struct wpa_supplicant *wpa_s = ctx;
  1505. struct p2p_go_neg_results *params = data;
  1506. struct wpa_ssid *ssid;
  1507. wpa_s->ap_configured_cb = NULL;
  1508. wpa_s->ap_configured_cb_ctx = NULL;
  1509. wpa_s->ap_configured_cb_data = NULL;
  1510. if (!wpa_s->go_params) {
  1511. wpa_printf(MSG_ERROR,
  1512. "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
  1513. return;
  1514. }
  1515. p2p_go_save_group_common_freqs(wpa_s, params);
  1516. p2p_go_dump_common_freqs(wpa_s);
  1517. ssid = wpa_s->current_ssid;
  1518. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  1519. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  1520. if (wpa_s->global->p2p_group_formation == wpa_s)
  1521. wpa_s->global->p2p_group_formation = NULL;
  1522. wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
  1523. params->passphrase[0] == '\0' ?
  1524. params->psk : NULL,
  1525. params->passphrase,
  1526. wpa_s->global->p2p_dev_addr,
  1527. params->persistent_group, "");
  1528. wpa_s->group_formation_reported = 1;
  1529. if (wpa_s->p2pdev->p2ps_method_config_any) {
  1530. if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
  1531. wpa_dbg(wpa_s, MSG_DEBUG,
  1532. "P2PS: Setting default PIN for ANY");
  1533. wpa_supplicant_ap_wps_pin(wpa_s, NULL,
  1534. "12345670", NULL, 0,
  1535. 0);
  1536. } else {
  1537. wpa_dbg(wpa_s, MSG_DEBUG,
  1538. "P2PS: Setting default PIN for " MACSTR,
  1539. MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
  1540. wpa_supplicant_ap_wps_pin(
  1541. wpa_s, wpa_s->p2pdev->p2ps_join_addr,
  1542. "12345670", NULL, 0, 0);
  1543. }
  1544. wpa_s->p2pdev->p2ps_method_config_any = 0;
  1545. }
  1546. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  1547. if (params->persistent_group) {
  1548. wpas_p2p_store_persistent_group(
  1549. wpa_s->p2pdev, ssid,
  1550. wpa_s->global->p2p_dev_addr);
  1551. wpas_p2p_add_psk_list(wpa_s, ssid);
  1552. }
  1553. wpas_notify_p2p_group_started(wpa_s, ssid,
  1554. params->persistent_group, 0,
  1555. NULL);
  1556. wpas_p2p_cross_connect_setup(wpa_s);
  1557. wpas_p2p_set_group_idle_timeout(wpa_s);
  1558. if (wpa_s->p2p_first_connection_timeout) {
  1559. wpa_dbg(wpa_s, MSG_DEBUG,
  1560. "P2P: Start group formation timeout of %d seconds until first data connection on GO",
  1561. wpa_s->p2p_first_connection_timeout);
  1562. wpa_s->p2p_go_group_formation_completed = 0;
  1563. wpa_s->global->p2p_group_formation = wpa_s;
  1564. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1565. wpa_s->p2pdev, NULL);
  1566. eloop_register_timeout(
  1567. wpa_s->p2p_first_connection_timeout, 0,
  1568. wpas_p2p_group_formation_timeout,
  1569. wpa_s->p2pdev, NULL);
  1570. }
  1571. return;
  1572. }
  1573. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  1574. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  1575. params->peer_interface_addr)) {
  1576. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  1577. "filtering");
  1578. return;
  1579. }
  1580. if (params->wps_method == WPS_PBC) {
  1581. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  1582. params->peer_device_addr);
  1583. #ifdef CONFIG_WPS_NFC
  1584. } else if (params->wps_method == WPS_NFC) {
  1585. if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
  1586. DEV_PW_NFC_CONNECTION_HANDOVER &&
  1587. !wpa_s->p2pdev->p2p_oob_dev_pw) {
  1588. wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
  1589. return;
  1590. }
  1591. wpas_ap_wps_add_nfc_pw(
  1592. wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
  1593. wpa_s->p2pdev->p2p_oob_dev_pw,
  1594. wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
  1595. wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
  1596. #endif /* CONFIG_WPS_NFC */
  1597. } else if (wpa_s->p2p_pin[0])
  1598. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  1599. wpa_s->p2p_pin, NULL, 0, 0);
  1600. os_free(wpa_s->go_params);
  1601. wpa_s->go_params = NULL;
  1602. }
  1603. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  1604. struct p2p_go_neg_results *params,
  1605. int group_formation)
  1606. {
  1607. struct wpa_ssid *ssid;
  1608. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  1609. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  1610. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  1611. "results");
  1612. return;
  1613. }
  1614. ssid = wpa_config_add_network(wpa_s->conf);
  1615. if (ssid == NULL) {
  1616. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  1617. return;
  1618. }
  1619. wpa_s->show_group_started = 0;
  1620. wpa_s->p2p_go_group_formation_completed = 0;
  1621. wpa_s->group_formation_reported = 0;
  1622. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  1623. wpa_config_set_network_defaults(ssid);
  1624. ssid->temporary = 1;
  1625. ssid->p2p_group = 1;
  1626. ssid->p2p_persistent_group = !!params->persistent_group;
  1627. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  1628. WPAS_MODE_P2P_GO;
  1629. ssid->frequency = params->freq;
  1630. ssid->ht40 = params->ht40;
  1631. ssid->vht = params->vht;
  1632. ssid->max_oper_chwidth = params->max_oper_chwidth;
  1633. ssid->vht_center_freq2 = params->vht_center_freq2;
  1634. ssid->ssid = os_zalloc(params->ssid_len + 1);
  1635. if (ssid->ssid) {
  1636. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  1637. ssid->ssid_len = params->ssid_len;
  1638. }
  1639. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  1640. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  1641. ssid->proto = WPA_PROTO_RSN;
  1642. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  1643. ssid->group_cipher = WPA_CIPHER_CCMP;
  1644. if (params->freq > 56160) {
  1645. /*
  1646. * Enable GCMP instead of CCMP as pairwise_cipher and
  1647. * group_cipher in 60 GHz.
  1648. */
  1649. ssid->pairwise_cipher = WPA_CIPHER_GCMP;
  1650. ssid->group_cipher = WPA_CIPHER_GCMP;
  1651. /* P2P GO in 60 GHz is always a PCP (PBSS) */
  1652. ssid->pbss = 1;
  1653. }
  1654. if (os_strlen(params->passphrase) > 0) {
  1655. ssid->passphrase = os_strdup(params->passphrase);
  1656. if (ssid->passphrase == NULL) {
  1657. wpa_msg_global(wpa_s, MSG_ERROR,
  1658. "P2P: Failed to copy passphrase for GO");
  1659. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1660. return;
  1661. }
  1662. } else
  1663. ssid->passphrase = NULL;
  1664. ssid->psk_set = params->psk_set;
  1665. if (ssid->psk_set)
  1666. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  1667. else if (ssid->passphrase)
  1668. wpa_config_update_psk(ssid);
  1669. ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
  1670. wpa_s->ap_configured_cb = p2p_go_configured;
  1671. wpa_s->ap_configured_cb_ctx = wpa_s;
  1672. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  1673. wpa_s->scan_req = NORMAL_SCAN_REQ;
  1674. wpa_s->connect_without_scan = ssid;
  1675. wpa_s->reassociate = 1;
  1676. wpa_s->disconnected = 0;
  1677. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  1678. "start GO)");
  1679. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1680. }
  1681. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  1682. const struct wpa_supplicant *src)
  1683. {
  1684. struct wpa_config *d;
  1685. const struct wpa_config *s;
  1686. d = dst->conf;
  1687. s = src->conf;
  1688. #define C(n) \
  1689. do { \
  1690. if (s->n && !d->n) \
  1691. d->n = os_strdup(s->n); \
  1692. } while (0)
  1693. C(device_name);
  1694. C(manufacturer);
  1695. C(model_name);
  1696. C(model_number);
  1697. C(serial_number);
  1698. C(config_methods);
  1699. #undef C
  1700. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  1701. os_memcpy(d->sec_device_type, s->sec_device_type,
  1702. sizeof(d->sec_device_type));
  1703. d->num_sec_device_types = s->num_sec_device_types;
  1704. d->p2p_group_idle = s->p2p_group_idle;
  1705. d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
  1706. d->p2p_intra_bss = s->p2p_intra_bss;
  1707. d->persistent_reconnect = s->persistent_reconnect;
  1708. d->max_num_sta = s->max_num_sta;
  1709. d->pbc_in_m1 = s->pbc_in_m1;
  1710. d->ignore_old_scan_res = s->ignore_old_scan_res;
  1711. d->beacon_int = s->beacon_int;
  1712. d->dtim_period = s->dtim_period;
  1713. d->p2p_go_ctwindow = s->p2p_go_ctwindow;
  1714. d->disassoc_low_ack = s->disassoc_low_ack;
  1715. d->disable_scan_offload = s->disable_scan_offload;
  1716. d->passive_scan = s->passive_scan;
  1717. if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
  1718. !d->wps_nfc_pw_from_config) {
  1719. wpabuf_free(d->wps_nfc_dh_privkey);
  1720. wpabuf_free(d->wps_nfc_dh_pubkey);
  1721. d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
  1722. d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
  1723. }
  1724. d->p2p_cli_probe = s->p2p_cli_probe;
  1725. }
  1726. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  1727. char *ifname, size_t len)
  1728. {
  1729. char *ifname_ptr = wpa_s->ifname;
  1730. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  1731. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  1732. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  1733. }
  1734. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  1735. if (os_strlen(ifname) >= IFNAMSIZ &&
  1736. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  1737. int res;
  1738. /* Try to avoid going over the IFNAMSIZ length limit */
  1739. res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
  1740. if (os_snprintf_error(len, res) && len)
  1741. ifname[len - 1] = '\0';
  1742. }
  1743. }
  1744. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  1745. enum wpa_driver_if_type type)
  1746. {
  1747. char ifname[120], force_ifname[120];
  1748. if (wpa_s->pending_interface_name[0]) {
  1749. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  1750. "- skip creation of a new one");
  1751. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  1752. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  1753. "unknown?! ifname='%s'",
  1754. wpa_s->pending_interface_name);
  1755. return -1;
  1756. }
  1757. return 0;
  1758. }
  1759. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  1760. force_ifname[0] = '\0';
  1761. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  1762. ifname);
  1763. wpa_s->p2p_group_idx++;
  1764. wpa_s->pending_interface_type = type;
  1765. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  1766. wpa_s->pending_interface_addr, NULL) < 0) {
  1767. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  1768. "interface");
  1769. return -1;
  1770. }
  1771. if (force_ifname[0]) {
  1772. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  1773. force_ifname);
  1774. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  1775. sizeof(wpa_s->pending_interface_name));
  1776. } else
  1777. os_strlcpy(wpa_s->pending_interface_name, ifname,
  1778. sizeof(wpa_s->pending_interface_name));
  1779. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  1780. MACSTR, wpa_s->pending_interface_name,
  1781. MAC2STR(wpa_s->pending_interface_addr));
  1782. return 0;
  1783. }
  1784. static void wpas_p2p_remove_pending_group_interface(
  1785. struct wpa_supplicant *wpa_s)
  1786. {
  1787. if (!wpa_s->pending_interface_name[0] ||
  1788. is_zero_ether_addr(wpa_s->pending_interface_addr))
  1789. return; /* No pending virtual interface */
  1790. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  1791. wpa_s->pending_interface_name);
  1792. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  1793. wpa_s->pending_interface_name);
  1794. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1795. wpa_s->pending_interface_name[0] = '\0';
  1796. wpa_s->global->pending_group_iface_for_p2ps = 0;
  1797. }
  1798. static struct wpa_supplicant *
  1799. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  1800. {
  1801. struct wpa_interface iface;
  1802. struct wpa_supplicant *group_wpa_s;
  1803. if (!wpa_s->pending_interface_name[0]) {
  1804. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  1805. if (!wpas_p2p_create_iface(wpa_s))
  1806. return NULL;
  1807. /*
  1808. * Something has forced us to remove the pending interface; try
  1809. * to create a new one and hope for the best that we will get
  1810. * the same local address.
  1811. */
  1812. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  1813. WPA_IF_P2P_CLIENT) < 0)
  1814. return NULL;
  1815. }
  1816. os_memset(&iface, 0, sizeof(iface));
  1817. iface.ifname = wpa_s->pending_interface_name;
  1818. iface.driver = wpa_s->driver->name;
  1819. if (wpa_s->conf->ctrl_interface == NULL &&
  1820. wpa_s->parent != wpa_s &&
  1821. wpa_s->p2p_mgmt &&
  1822. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1823. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1824. else
  1825. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1826. iface.driver_param = wpa_s->conf->driver_param;
  1827. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
  1828. if (group_wpa_s == NULL) {
  1829. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1830. "wpa_supplicant interface");
  1831. return NULL;
  1832. }
  1833. wpa_s->pending_interface_name[0] = '\0';
  1834. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1835. P2P_GROUP_INTERFACE_CLIENT;
  1836. wpa_s->global->p2p_group_formation = group_wpa_s;
  1837. wpa_s->global->pending_group_iface_for_p2ps = 0;
  1838. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1839. return group_wpa_s;
  1840. }
  1841. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1842. void *timeout_ctx)
  1843. {
  1844. struct wpa_supplicant *wpa_s = eloop_ctx;
  1845. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1846. wpas_p2p_group_formation_failed(wpa_s, 0);
  1847. }
  1848. static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
  1849. int already_deleted)
  1850. {
  1851. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1852. wpa_s->p2pdev, NULL);
  1853. if (wpa_s->global->p2p)
  1854. p2p_group_formation_failed(wpa_s->global->p2p);
  1855. wpas_group_formation_completed(wpa_s, 0, already_deleted);
  1856. }
  1857. static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
  1858. {
  1859. wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
  1860. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1861. wpa_s->p2pdev, NULL);
  1862. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1863. wpa_s->p2pdev, NULL);
  1864. wpa_s->global->p2p_fail_on_wps_complete = 0;
  1865. }
  1866. void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
  1867. {
  1868. if (wpa_s->global->p2p_group_formation != wpa_s)
  1869. return;
  1870. /* Speed up group formation timeout since this cannot succeed */
  1871. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1872. wpa_s->p2pdev, NULL);
  1873. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1874. wpa_s->p2pdev, NULL);
  1875. }
  1876. static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1877. {
  1878. struct wpa_supplicant *wpa_s = ctx;
  1879. struct wpa_supplicant *group_wpa_s;
  1880. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1881. wpa_drv_cancel_remain_on_channel(wpa_s);
  1882. wpa_s->off_channel_freq = 0;
  1883. wpa_s->roc_waiting_drv_freq = 0;
  1884. }
  1885. if (res->status) {
  1886. wpa_msg_global(wpa_s, MSG_INFO,
  1887. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1888. res->status);
  1889. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1890. wpas_p2p_remove_pending_group_interface(wpa_s);
  1891. return;
  1892. }
  1893. if (!res->role_go) {
  1894. /* Inform driver of the operating channel of GO. */
  1895. wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
  1896. }
  1897. if (wpa_s->p2p_go_ht40)
  1898. res->ht40 = 1;
  1899. if (wpa_s->p2p_go_vht)
  1900. res->vht = 1;
  1901. res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
  1902. res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
  1903. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
  1904. "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
  1905. " wps_method=%s",
  1906. res->role_go ? "GO" : "client", res->freq, res->ht40,
  1907. MAC2STR(res->peer_device_addr),
  1908. MAC2STR(res->peer_interface_addr),
  1909. p2p_wps_method_text(res->wps_method));
  1910. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1911. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1912. struct wpa_ssid *ssid;
  1913. ssid = wpa_config_get_network(wpa_s->conf,
  1914. wpa_s->p2p_persistent_id);
  1915. if (ssid && ssid->disabled == 2 &&
  1916. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1917. size_t len = os_strlen(ssid->passphrase);
  1918. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1919. "on requested persistent group");
  1920. os_memcpy(res->passphrase, ssid->passphrase, len);
  1921. res->passphrase[len] = '\0';
  1922. }
  1923. }
  1924. if (wpa_s->create_p2p_iface) {
  1925. group_wpa_s =
  1926. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1927. if (group_wpa_s == NULL) {
  1928. wpas_p2p_remove_pending_group_interface(wpa_s);
  1929. eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
  1930. wpa_s, NULL);
  1931. wpas_p2p_group_formation_failed(wpa_s, 1);
  1932. return;
  1933. }
  1934. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1935. wpa_s->pending_interface_name[0] = '\0';
  1936. } else {
  1937. group_wpa_s = wpa_s->parent;
  1938. wpa_s->global->p2p_group_formation = group_wpa_s;
  1939. if (group_wpa_s != wpa_s)
  1940. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1941. }
  1942. group_wpa_s->p2p_in_provisioning = 1;
  1943. group_wpa_s->p2pdev = wpa_s;
  1944. if (group_wpa_s != wpa_s) {
  1945. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1946. sizeof(group_wpa_s->p2p_pin));
  1947. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1948. }
  1949. if (res->role_go) {
  1950. wpas_start_wps_go(group_wpa_s, res, 1);
  1951. } else {
  1952. os_get_reltime(&group_wpa_s->scan_min_time);
  1953. wpas_start_wps_enrollee(group_wpa_s, res);
  1954. }
  1955. wpa_s->p2p_long_listen = 0;
  1956. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1957. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1958. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1959. (res->peer_config_timeout % 100) * 10000,
  1960. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1961. }
  1962. static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
  1963. u8 go_intent)
  1964. {
  1965. struct wpa_supplicant *wpa_s = ctx;
  1966. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1967. " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
  1968. dev_passwd_id, go_intent);
  1969. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
  1970. }
  1971. static void wpas_dev_found(void *ctx, const u8 *addr,
  1972. const struct p2p_peer_info *info,
  1973. int new_device)
  1974. {
  1975. #ifndef CONFIG_NO_STDOUT_DEBUG
  1976. struct wpa_supplicant *wpa_s = ctx;
  1977. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1978. char *wfd_dev_info_hex = NULL;
  1979. #ifdef CONFIG_WIFI_DISPLAY
  1980. wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
  1981. WFD_SUBELEM_DEVICE_INFO);
  1982. #endif /* CONFIG_WIFI_DISPLAY */
  1983. if (info->p2ps_instance) {
  1984. char str[256];
  1985. const u8 *buf = wpabuf_head(info->p2ps_instance);
  1986. size_t len = wpabuf_len(info->p2ps_instance);
  1987. while (len) {
  1988. u32 id;
  1989. u16 methods;
  1990. u8 str_len;
  1991. if (len < 4 + 2 + 1)
  1992. break;
  1993. id = WPA_GET_LE32(buf);
  1994. buf += sizeof(u32);
  1995. methods = WPA_GET_BE16(buf);
  1996. buf += sizeof(u16);
  1997. str_len = *buf++;
  1998. if (str_len > len - 4 - 2 - 1)
  1999. break;
  2000. os_memcpy(str, buf, str_len);
  2001. str[str_len] = '\0';
  2002. buf += str_len;
  2003. len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
  2004. wpa_msg_global(wpa_s, MSG_INFO,
  2005. P2P_EVENT_DEVICE_FOUND MACSTR
  2006. " p2p_dev_addr=" MACSTR
  2007. " pri_dev_type=%s name='%s'"
  2008. " config_methods=0x%x"
  2009. " dev_capab=0x%x"
  2010. " group_capab=0x%x"
  2011. " adv_id=%x asp_svc=%s%s",
  2012. MAC2STR(addr),
  2013. MAC2STR(info->p2p_device_addr),
  2014. wps_dev_type_bin2str(
  2015. info->pri_dev_type,
  2016. devtype, sizeof(devtype)),
  2017. info->device_name, methods,
  2018. info->dev_capab, info->group_capab,
  2019. id, str,
  2020. info->vendor_elems ?
  2021. " vendor_elems=1" : "");
  2022. }
  2023. goto done;
  2024. }
  2025. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  2026. " p2p_dev_addr=" MACSTR
  2027. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2028. "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
  2029. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  2030. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  2031. sizeof(devtype)),
  2032. info->device_name, info->config_methods,
  2033. info->dev_capab, info->group_capab,
  2034. wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
  2035. wfd_dev_info_hex ? wfd_dev_info_hex : "",
  2036. info->vendor_elems ? " vendor_elems=1" : "",
  2037. new_device);
  2038. done:
  2039. os_free(wfd_dev_info_hex);
  2040. #endif /* CONFIG_NO_STDOUT_DEBUG */
  2041. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  2042. }
  2043. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  2044. {
  2045. struct wpa_supplicant *wpa_s = ctx;
  2046. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  2047. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  2048. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  2049. }
  2050. static void wpas_find_stopped(void *ctx)
  2051. {
  2052. struct wpa_supplicant *wpa_s = ctx;
  2053. if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
  2054. wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
  2055. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  2056. wpas_notify_p2p_find_stopped(wpa_s);
  2057. }
  2058. struct wpas_p2p_listen_work {
  2059. unsigned int freq;
  2060. unsigned int duration;
  2061. struct wpabuf *probe_resp_ie;
  2062. };
  2063. static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
  2064. {
  2065. if (lwork == NULL)
  2066. return;
  2067. wpabuf_free(lwork->probe_resp_ie);
  2068. os_free(lwork);
  2069. }
  2070. static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
  2071. {
  2072. struct wpas_p2p_listen_work *lwork;
  2073. if (!wpa_s->p2p_listen_work)
  2074. return;
  2075. lwork = wpa_s->p2p_listen_work->ctx;
  2076. wpas_p2p_listen_work_free(lwork);
  2077. radio_work_done(wpa_s->p2p_listen_work);
  2078. wpa_s->p2p_listen_work = NULL;
  2079. }
  2080. static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
  2081. {
  2082. struct wpa_supplicant *wpa_s = work->wpa_s;
  2083. struct wpas_p2p_listen_work *lwork = work->ctx;
  2084. unsigned int duration;
  2085. if (deinit) {
  2086. if (work->started) {
  2087. wpa_s->p2p_listen_work = NULL;
  2088. wpas_stop_listen(wpa_s);
  2089. }
  2090. wpas_p2p_listen_work_free(lwork);
  2091. return;
  2092. }
  2093. wpa_s->p2p_listen_work = work;
  2094. wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
  2095. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  2096. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  2097. "report received Probe Request frames");
  2098. wpas_p2p_listen_work_done(wpa_s);
  2099. return;
  2100. }
  2101. wpa_s->pending_listen_freq = lwork->freq;
  2102. wpa_s->pending_listen_duration = lwork->duration;
  2103. duration = lwork->duration;
  2104. #ifdef CONFIG_TESTING_OPTIONS
  2105. if (wpa_s->extra_roc_dur) {
  2106. wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
  2107. duration, duration + wpa_s->extra_roc_dur);
  2108. duration += wpa_s->extra_roc_dur;
  2109. }
  2110. #endif /* CONFIG_TESTING_OPTIONS */
  2111. if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
  2112. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  2113. "to remain on channel (%u MHz) for Listen "
  2114. "state", lwork->freq);
  2115. wpas_p2p_listen_work_done(wpa_s);
  2116. wpa_s->pending_listen_freq = 0;
  2117. return;
  2118. }
  2119. wpa_s->off_channel_freq = 0;
  2120. wpa_s->roc_waiting_drv_freq = lwork->freq;
  2121. }
  2122. static int wpas_start_listen(void *ctx, unsigned int freq,
  2123. unsigned int duration,
  2124. const struct wpabuf *probe_resp_ie)
  2125. {
  2126. struct wpa_supplicant *wpa_s = ctx;
  2127. struct wpas_p2p_listen_work *lwork;
  2128. if (wpa_s->p2p_listen_work) {
  2129. wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
  2130. return -1;
  2131. }
  2132. lwork = os_zalloc(sizeof(*lwork));
  2133. if (lwork == NULL)
  2134. return -1;
  2135. lwork->freq = freq;
  2136. lwork->duration = duration;
  2137. if (probe_resp_ie) {
  2138. lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
  2139. if (lwork->probe_resp_ie == NULL) {
  2140. wpas_p2p_listen_work_free(lwork);
  2141. return -1;
  2142. }
  2143. }
  2144. if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
  2145. lwork) < 0) {
  2146. wpas_p2p_listen_work_free(lwork);
  2147. return -1;
  2148. }
  2149. return 0;
  2150. }
  2151. static void wpas_stop_listen(void *ctx)
  2152. {
  2153. struct wpa_supplicant *wpa_s = ctx;
  2154. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  2155. wpa_drv_cancel_remain_on_channel(wpa_s);
  2156. wpa_s->off_channel_freq = 0;
  2157. wpa_s->roc_waiting_drv_freq = 0;
  2158. }
  2159. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  2160. /*
  2161. * Don't cancel Probe Request RX reporting for a connected P2P Client
  2162. * handling Probe Request frames.
  2163. */
  2164. if (!wpa_s->p2p_cli_probe)
  2165. wpa_drv_probe_req_report(wpa_s, 0);
  2166. wpas_p2p_listen_work_done(wpa_s);
  2167. }
  2168. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
  2169. unsigned int freq)
  2170. {
  2171. struct wpa_supplicant *wpa_s = ctx;
  2172. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
  2173. freq);
  2174. }
  2175. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  2176. const u8 *peer, const char *params,
  2177. unsigned int generated_pin)
  2178. {
  2179. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  2180. " %08d%s", MAC2STR(peer), generated_pin, params);
  2181. }
  2182. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  2183. const u8 *peer, const char *params)
  2184. {
  2185. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  2186. "%s", MAC2STR(peer), params);
  2187. }
  2188. static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  2189. const u8 *dev_addr, const u8 *pri_dev_type,
  2190. const char *dev_name, u16 supp_config_methods,
  2191. u8 dev_capab, u8 group_capab, const u8 *group_id,
  2192. size_t group_id_len)
  2193. {
  2194. struct wpa_supplicant *wpa_s = ctx;
  2195. char devtype[WPS_DEV_TYPE_BUFSIZE];
  2196. char params[300];
  2197. u8 empty_dev_type[8];
  2198. unsigned int generated_pin = 0;
  2199. struct wpa_supplicant *group = NULL;
  2200. int res;
  2201. if (group_id) {
  2202. for (group = wpa_s->global->ifaces; group; group = group->next)
  2203. {
  2204. struct wpa_ssid *s = group->current_ssid;
  2205. if (s != NULL &&
  2206. s->mode == WPAS_MODE_P2P_GO &&
  2207. group_id_len - ETH_ALEN == s->ssid_len &&
  2208. os_memcmp(group_id + ETH_ALEN, s->ssid,
  2209. s->ssid_len) == 0)
  2210. break;
  2211. }
  2212. }
  2213. if (pri_dev_type == NULL) {
  2214. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  2215. pri_dev_type = empty_dev_type;
  2216. }
  2217. res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  2218. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2219. "dev_capab=0x%x group_capab=0x%x%s%s",
  2220. MAC2STR(dev_addr),
  2221. wps_dev_type_bin2str(pri_dev_type, devtype,
  2222. sizeof(devtype)),
  2223. dev_name, supp_config_methods, dev_capab, group_capab,
  2224. group ? " group=" : "",
  2225. group ? group->ifname : "");
  2226. if (os_snprintf_error(sizeof(params), res))
  2227. wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
  2228. params[sizeof(params) - 1] = '\0';
  2229. if (config_methods & WPS_CONFIG_DISPLAY) {
  2230. if (wps_generate_pin(&generated_pin) < 0) {
  2231. wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
  2232. wpas_notify_p2p_provision_discovery(
  2233. wpa_s, peer, 0 /* response */,
  2234. P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
  2235. return;
  2236. }
  2237. wpas_prov_disc_local_display(wpa_s, peer, params,
  2238. generated_pin);
  2239. } else if (config_methods & WPS_CONFIG_KEYPAD)
  2240. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2241. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2242. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2243. MACSTR "%s", MAC2STR(peer), params);
  2244. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2245. P2P_PROV_DISC_SUCCESS,
  2246. config_methods, generated_pin);
  2247. }
  2248. static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2249. {
  2250. struct wpa_supplicant *wpa_s = ctx;
  2251. unsigned int generated_pin = 0;
  2252. char params[20];
  2253. if (wpa_s->pending_pd_before_join &&
  2254. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2255. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2256. wpa_s->pending_pd_before_join = 0;
  2257. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2258. "join-existing-group operation");
  2259. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  2260. return;
  2261. }
  2262. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2263. wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
  2264. int res;
  2265. res = os_snprintf(params, sizeof(params), " peer_go=%d",
  2266. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2267. if (os_snprintf_error(sizeof(params), res))
  2268. params[sizeof(params) - 1] = '\0';
  2269. } else
  2270. params[0] = '\0';
  2271. if (config_methods & WPS_CONFIG_DISPLAY)
  2272. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2273. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2274. if (wps_generate_pin(&generated_pin) < 0) {
  2275. wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
  2276. wpas_notify_p2p_provision_discovery(
  2277. wpa_s, peer, 0 /* response */,
  2278. P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
  2279. return;
  2280. }
  2281. wpas_prov_disc_local_display(wpa_s, peer, params,
  2282. generated_pin);
  2283. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2284. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2285. MACSTR "%s", MAC2STR(peer), params);
  2286. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2287. P2P_PROV_DISC_SUCCESS,
  2288. config_methods, generated_pin);
  2289. }
  2290. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2291. enum p2p_prov_disc_status status,
  2292. u32 adv_id, const u8 *adv_mac,
  2293. const char *deferred_session_resp)
  2294. {
  2295. struct wpa_supplicant *wpa_s = ctx;
  2296. if (wpa_s->p2p_fallback_to_go_neg) {
  2297. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2298. "failed - fall back to GO Negotiation");
  2299. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  2300. P2P_EVENT_FALLBACK_TO_GO_NEG
  2301. "reason=PD-failed");
  2302. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2303. return;
  2304. }
  2305. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2306. wpa_s->pending_pd_before_join = 0;
  2307. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2308. "join-existing-group operation (no ACK for PD "
  2309. "Req attempts)");
  2310. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  2311. return;
  2312. }
  2313. if (adv_id && adv_mac && deferred_session_resp) {
  2314. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2315. " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
  2316. " deferred_session_resp='%s'",
  2317. MAC2STR(peer), status, adv_id,
  2318. deferred_session_resp);
  2319. } else if (adv_id && adv_mac) {
  2320. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2321. " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
  2322. MAC2STR(peer), status, adv_id);
  2323. } else {
  2324. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2325. " p2p_dev_addr=" MACSTR " status=%d",
  2326. MAC2STR(peer), status);
  2327. }
  2328. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2329. status, 0, 0);
  2330. }
  2331. static int freq_included(struct wpa_supplicant *wpa_s,
  2332. const struct p2p_channels *channels,
  2333. unsigned int freq)
  2334. {
  2335. if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
  2336. wpas_p2p_go_is_peer_freq(wpa_s, freq))
  2337. return 1;
  2338. return 0;
  2339. }
  2340. static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
  2341. {
  2342. unsigned int num = P2P_MAX_CHANNELS;
  2343. int *common_freqs;
  2344. int ret;
  2345. p2p_go_dump_common_freqs(wpa_s);
  2346. common_freqs = os_calloc(num, sizeof(int));
  2347. if (!common_freqs)
  2348. return;
  2349. ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
  2350. if (ret < 0) {
  2351. wpa_dbg(wpa_s, MSG_DEBUG,
  2352. "P2P: Failed to get group common freqs");
  2353. os_free(common_freqs);
  2354. return;
  2355. }
  2356. os_free(wpa_s->p2p_group_common_freqs);
  2357. wpa_s->p2p_group_common_freqs = common_freqs;
  2358. wpa_s->p2p_group_common_freqs_num = num;
  2359. p2p_go_dump_common_freqs(wpa_s);
  2360. }
  2361. /*
  2362. * Check if the given frequency is one of the possible operating frequencies
  2363. * set after the completion of the GO Negotiation.
  2364. */
  2365. static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
  2366. {
  2367. unsigned int i;
  2368. p2p_go_dump_common_freqs(wpa_s);
  2369. /* assume no restrictions */
  2370. if (!wpa_s->p2p_group_common_freqs_num)
  2371. return 1;
  2372. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
  2373. if (wpa_s->p2p_group_common_freqs[i] == freq)
  2374. return 1;
  2375. }
  2376. return 0;
  2377. }
  2378. static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
  2379. struct sta_info *sta, void *ctx)
  2380. {
  2381. int *ecsa_support = ctx;
  2382. *ecsa_support &= sta->ecsa_supported;
  2383. return 0;
  2384. }
  2385. /* Check if all the peers support eCSA */
  2386. static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
  2387. {
  2388. int ecsa_support = 1;
  2389. ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
  2390. &ecsa_support);
  2391. return ecsa_support;
  2392. }
  2393. /**
  2394. * Pick the best frequency to use from all the currently used frequencies.
  2395. */
  2396. static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
  2397. struct wpa_used_freq_data *freqs,
  2398. unsigned int num)
  2399. {
  2400. unsigned int i, c;
  2401. /* find a candidate freq that is supported by P2P */
  2402. for (c = 0; c < num; c++)
  2403. if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
  2404. break;
  2405. if (c == num)
  2406. return 0;
  2407. /* once we have a candidate, try to find a 'better' one */
  2408. for (i = c + 1; i < num; i++) {
  2409. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
  2410. continue;
  2411. /*
  2412. * 1. Infrastructure station interfaces have higher preference.
  2413. * 2. P2P Clients have higher preference.
  2414. * 3. All others.
  2415. */
  2416. if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
  2417. c = i;
  2418. break;
  2419. }
  2420. if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
  2421. c = i;
  2422. }
  2423. return freqs[c].freq;
  2424. }
  2425. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2426. const u8 *go_dev_addr, const u8 *ssid,
  2427. size_t ssid_len, int *go, u8 *group_bssid,
  2428. int *force_freq, int persistent_group,
  2429. const struct p2p_channels *channels,
  2430. int dev_pw_id)
  2431. {
  2432. struct wpa_supplicant *wpa_s = ctx;
  2433. struct wpa_ssid *s;
  2434. struct wpa_used_freq_data *freqs;
  2435. struct wpa_supplicant *grp;
  2436. int best_freq;
  2437. if (!persistent_group) {
  2438. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2439. " to join an active group (SSID: %s)",
  2440. MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
  2441. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2442. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2443. == 0 ||
  2444. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2445. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2446. "authorized invitation");
  2447. goto accept_inv;
  2448. }
  2449. #ifdef CONFIG_WPS_NFC
  2450. if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
  2451. dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
  2452. wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
  2453. wpa_s->p2p_wps_method = WPS_NFC;
  2454. wpa_s->pending_join_wps_method = WPS_NFC;
  2455. os_memcpy(wpa_s->pending_join_dev_addr,
  2456. go_dev_addr, ETH_ALEN);
  2457. os_memcpy(wpa_s->pending_join_iface_addr,
  2458. bssid, ETH_ALEN);
  2459. goto accept_inv;
  2460. }
  2461. #endif /* CONFIG_WPS_NFC */
  2462. /*
  2463. * Do not accept the invitation automatically; notify user and
  2464. * request approval.
  2465. */
  2466. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2467. }
  2468. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2469. if (grp) {
  2470. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2471. "running persistent group");
  2472. if (*go)
  2473. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2474. goto accept_inv;
  2475. }
  2476. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2477. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2478. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2479. "invitation to re-invoke a persistent group");
  2480. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2481. } else if (!wpa_s->conf->persistent_reconnect)
  2482. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2483. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2484. if (s->disabled == 2 &&
  2485. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2486. s->ssid_len == ssid_len &&
  2487. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2488. break;
  2489. }
  2490. if (!s) {
  2491. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2492. " requested reinvocation of an unknown group",
  2493. MAC2STR(sa));
  2494. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2495. }
  2496. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2497. *go = 1;
  2498. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2499. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2500. "interface is already in use - reject "
  2501. "invitation");
  2502. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2503. }
  2504. if (wpa_s->p2p_mgmt)
  2505. os_memcpy(group_bssid, wpa_s->parent->own_addr,
  2506. ETH_ALEN);
  2507. else
  2508. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2509. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2510. *go = 1;
  2511. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2512. {
  2513. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2514. "interface address for the group");
  2515. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2516. }
  2517. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2518. ETH_ALEN);
  2519. }
  2520. accept_inv:
  2521. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2522. best_freq = 0;
  2523. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  2524. sizeof(struct wpa_used_freq_data));
  2525. if (freqs) {
  2526. int num_channels = wpa_s->num_multichan_concurrent;
  2527. int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
  2528. best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  2529. os_free(freqs);
  2530. }
  2531. /* Get one of the frequencies currently in use */
  2532. if (best_freq > 0) {
  2533. wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
  2534. wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
  2535. if (wpa_s->num_multichan_concurrent < 2 ||
  2536. wpas_p2p_num_unused_channels(wpa_s) < 1) {
  2537. wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
  2538. *force_freq = best_freq;
  2539. }
  2540. }
  2541. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2542. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2543. if (*go == 0) {
  2544. /* We are the client */
  2545. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2546. "running a GO but we are capable of MCC, "
  2547. "figure out the best channel to use");
  2548. *force_freq = 0;
  2549. } else if (!freq_included(wpa_s, channels, *force_freq)) {
  2550. /* We are the GO, and *force_freq is not in the
  2551. * intersection */
  2552. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2553. "in intersection but we are capable of MCC, "
  2554. "figure out the best channel to use",
  2555. *force_freq);
  2556. *force_freq = 0;
  2557. }
  2558. }
  2559. return P2P_SC_SUCCESS;
  2560. }
  2561. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2562. const u8 *ssid, size_t ssid_len,
  2563. const u8 *go_dev_addr, u8 status,
  2564. int op_freq)
  2565. {
  2566. struct wpa_supplicant *wpa_s = ctx;
  2567. struct wpa_ssid *s;
  2568. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2569. if (s->disabled == 2 &&
  2570. s->ssid_len == ssid_len &&
  2571. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2572. break;
  2573. }
  2574. if (status == P2P_SC_SUCCESS) {
  2575. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2576. " was accepted; op_freq=%d MHz, SSID=%s",
  2577. MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
  2578. if (s) {
  2579. int go = s->mode == WPAS_MODE_P2P_GO;
  2580. if (go) {
  2581. wpa_msg_global(wpa_s, MSG_INFO,
  2582. P2P_EVENT_INVITATION_ACCEPTED
  2583. "sa=" MACSTR
  2584. " persistent=%d freq=%d",
  2585. MAC2STR(sa), s->id, op_freq);
  2586. } else {
  2587. wpa_msg_global(wpa_s, MSG_INFO,
  2588. P2P_EVENT_INVITATION_ACCEPTED
  2589. "sa=" MACSTR
  2590. " persistent=%d",
  2591. MAC2STR(sa), s->id);
  2592. }
  2593. wpas_p2p_group_add_persistent(
  2594. wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
  2595. go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
  2596. 1);
  2597. } else if (bssid) {
  2598. wpa_s->user_initiated_pd = 0;
  2599. wpa_msg_global(wpa_s, MSG_INFO,
  2600. P2P_EVENT_INVITATION_ACCEPTED
  2601. "sa=" MACSTR " go_dev_addr=" MACSTR
  2602. " bssid=" MACSTR " unknown-network",
  2603. MAC2STR(sa), MAC2STR(go_dev_addr),
  2604. MAC2STR(bssid));
  2605. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2606. wpa_s->p2p_wps_method, 0, op_freq,
  2607. ssid, ssid_len);
  2608. }
  2609. return;
  2610. }
  2611. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2612. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2613. " was rejected (status %u)", MAC2STR(sa), status);
  2614. return;
  2615. }
  2616. if (!s) {
  2617. if (bssid) {
  2618. wpa_msg_global(wpa_s, MSG_INFO,
  2619. P2P_EVENT_INVITATION_RECEIVED
  2620. "sa=" MACSTR " go_dev_addr=" MACSTR
  2621. " bssid=" MACSTR " unknown-network",
  2622. MAC2STR(sa), MAC2STR(go_dev_addr),
  2623. MAC2STR(bssid));
  2624. } else {
  2625. wpa_msg_global(wpa_s, MSG_INFO,
  2626. P2P_EVENT_INVITATION_RECEIVED
  2627. "sa=" MACSTR " go_dev_addr=" MACSTR
  2628. " unknown-network",
  2629. MAC2STR(sa), MAC2STR(go_dev_addr));
  2630. }
  2631. wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
  2632. bssid, 0, op_freq);
  2633. return;
  2634. }
  2635. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2636. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2637. "sa=" MACSTR " persistent=%d freq=%d",
  2638. MAC2STR(sa), s->id, op_freq);
  2639. } else {
  2640. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2641. "sa=" MACSTR " persistent=%d",
  2642. MAC2STR(sa), s->id);
  2643. }
  2644. wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
  2645. s->id, op_freq);
  2646. }
  2647. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2648. struct wpa_ssid *ssid,
  2649. const u8 *peer, int inv)
  2650. {
  2651. size_t i;
  2652. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  2653. if (ssid == NULL)
  2654. return;
  2655. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2656. if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
  2657. ETH_ALEN) == 0)
  2658. break;
  2659. }
  2660. if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
  2661. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2662. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2663. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2664. "due to invitation result", ssid->id);
  2665. wpas_notify_network_removed(wpa_s, ssid);
  2666. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2667. return;
  2668. }
  2669. return; /* Peer not found in client list */
  2670. }
  2671. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2672. "group %d client list%s",
  2673. MAC2STR(peer), ssid->id,
  2674. inv ? " due to invitation result" : "");
  2675. os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
  2676. ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  2677. (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  2678. ssid->num_p2p_clients--;
  2679. if (p2p_wpa_s->conf->update_config &&
  2680. wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
  2681. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2682. }
  2683. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2684. const u8 *peer)
  2685. {
  2686. struct wpa_ssid *ssid;
  2687. wpa_s = wpa_s->global->p2p_invite_group;
  2688. if (wpa_s == NULL)
  2689. return; /* No known invitation group */
  2690. ssid = wpa_s->current_ssid;
  2691. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2692. !ssid->p2p_persistent_group)
  2693. return; /* Not operating as a GO in persistent group */
  2694. ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
  2695. ssid->ssid, ssid->ssid_len);
  2696. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2697. }
  2698. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2699. const struct p2p_channels *channels,
  2700. const u8 *peer, int neg_freq,
  2701. int peer_oper_freq)
  2702. {
  2703. struct wpa_supplicant *wpa_s = ctx;
  2704. struct wpa_ssid *ssid;
  2705. int freq;
  2706. if (bssid) {
  2707. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2708. "status=%d " MACSTR,
  2709. status, MAC2STR(bssid));
  2710. } else {
  2711. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2712. "status=%d ", status);
  2713. }
  2714. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2715. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2716. status, MAC2STR(peer));
  2717. if (wpa_s->pending_invite_ssid_id == -1) {
  2718. struct wpa_supplicant *group_if =
  2719. wpa_s->global->p2p_invite_group;
  2720. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2721. wpas_remove_persistent_client(wpa_s, peer);
  2722. /*
  2723. * Invitation to an active group. If this is successful and we
  2724. * are the GO, set the client wait to postpone some concurrent
  2725. * operations and to allow provisioning and connection to happen
  2726. * more quickly.
  2727. */
  2728. if (status == P2P_SC_SUCCESS &&
  2729. group_if && group_if->current_ssid &&
  2730. group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
  2731. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  2732. #ifdef CONFIG_TESTING_OPTIONS
  2733. if (group_if->p2p_go_csa_on_inv) {
  2734. wpa_printf(MSG_DEBUG,
  2735. "Testing: force P2P GO CSA after invitation");
  2736. eloop_cancel_timeout(
  2737. wpas_p2p_reconsider_moving_go,
  2738. wpa_s, NULL);
  2739. eloop_register_timeout(
  2740. 0, 50000,
  2741. wpas_p2p_reconsider_moving_go,
  2742. wpa_s, NULL);
  2743. }
  2744. #endif /* CONFIG_TESTING_OPTIONS */
  2745. }
  2746. return;
  2747. }
  2748. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2749. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2750. "invitation exchange to indicate readiness for "
  2751. "re-invocation");
  2752. }
  2753. if (status != P2P_SC_SUCCESS) {
  2754. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2755. ssid = wpa_config_get_network(
  2756. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2757. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2758. }
  2759. wpas_p2p_remove_pending_group_interface(wpa_s);
  2760. return;
  2761. }
  2762. ssid = wpa_config_get_network(wpa_s->conf,
  2763. wpa_s->pending_invite_ssid_id);
  2764. if (ssid == NULL) {
  2765. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2766. "data matching with invitation");
  2767. return;
  2768. }
  2769. /*
  2770. * The peer could have missed our ctrl::ack frame for Invitation
  2771. * Response and continue retransmitting the frame. To reduce the
  2772. * likelihood of the peer not getting successful TX status for the
  2773. * Invitation Response frame, wait a short time here before starting
  2774. * the persistent group so that we will remain on the current channel to
  2775. * acknowledge any possible retransmission from the peer.
  2776. */
  2777. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2778. "starting persistent group");
  2779. os_sleep(0, 50000);
  2780. if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
  2781. freq_included(wpa_s, channels, neg_freq))
  2782. freq = neg_freq;
  2783. else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
  2784. freq_included(wpa_s, channels, peer_oper_freq))
  2785. freq = peer_oper_freq;
  2786. else
  2787. freq = 0;
  2788. wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
  2789. freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2790. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2791. ssid->mode == WPAS_MODE_P2P_GO,
  2792. wpa_s->p2p_persistent_go_freq,
  2793. freq,
  2794. wpa_s->p2p_go_vht_center_freq2,
  2795. wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
  2796. wpa_s->p2p_go_max_oper_chwidth,
  2797. channels,
  2798. ssid->mode == WPAS_MODE_P2P_GO ?
  2799. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  2800. 0, 1);
  2801. }
  2802. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2803. unsigned int freq)
  2804. {
  2805. if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
  2806. return 1;
  2807. return freq_range_list_includes(&global->p2p_disallow_freq, freq);
  2808. }
  2809. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2810. {
  2811. reg->channel[reg->channels] = chan;
  2812. reg->channels++;
  2813. }
  2814. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2815. struct p2p_channels *chan,
  2816. struct p2p_channels *cli_chan)
  2817. {
  2818. int i, cla = 0;
  2819. wpa_s->global->p2p_24ghz_social_channels = 1;
  2820. os_memset(cli_chan, 0, sizeof(*cli_chan));
  2821. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2822. "band");
  2823. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2824. chan->reg_class[cla].reg_class = 81;
  2825. chan->reg_class[cla].channels = 0;
  2826. for (i = 0; i < 11; i++) {
  2827. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2828. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2829. }
  2830. if (chan->reg_class[cla].channels)
  2831. cla++;
  2832. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2833. "band");
  2834. /* Operating class 115 - 5 GHz, channels 36-48 */
  2835. chan->reg_class[cla].reg_class = 115;
  2836. chan->reg_class[cla].channels = 0;
  2837. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2838. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2839. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2840. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2841. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2842. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2843. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2844. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2845. if (chan->reg_class[cla].channels)
  2846. cla++;
  2847. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2848. "band");
  2849. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2850. chan->reg_class[cla].reg_class = 124;
  2851. chan->reg_class[cla].channels = 0;
  2852. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2853. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2854. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2855. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2856. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2857. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2858. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2859. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2860. if (chan->reg_class[cla].channels)
  2861. cla++;
  2862. chan->reg_classes = cla;
  2863. return 0;
  2864. }
  2865. static int has_channel(struct wpa_global *global,
  2866. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2867. {
  2868. int i;
  2869. unsigned int freq;
  2870. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2871. chan * 5;
  2872. if (wpas_p2p_disallowed_freq(global, freq))
  2873. return NOT_ALLOWED;
  2874. for (i = 0; i < mode->num_channels; i++) {
  2875. if (mode->channels[i].chan == chan) {
  2876. if (flags)
  2877. *flags = mode->channels[i].flag;
  2878. if (mode->channels[i].flag &
  2879. (HOSTAPD_CHAN_DISABLED |
  2880. HOSTAPD_CHAN_RADAR))
  2881. return NOT_ALLOWED;
  2882. if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
  2883. return NO_IR;
  2884. return ALLOWED;
  2885. }
  2886. }
  2887. return NOT_ALLOWED;
  2888. }
  2889. static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
  2890. struct hostapd_hw_modes *mode,
  2891. u8 channel)
  2892. {
  2893. u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
  2894. size_t i;
  2895. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  2896. return 0;
  2897. for (i = 0; i < ARRAY_SIZE(center_channels); i++)
  2898. /*
  2899. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  2900. * so the center channel is 6 channels away from the start/end.
  2901. */
  2902. if (channel >= center_channels[i] - 6 &&
  2903. channel <= center_channels[i] + 6)
  2904. return center_channels[i];
  2905. return 0;
  2906. }
  2907. static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
  2908. struct hostapd_hw_modes *mode,
  2909. u8 channel, u8 bw)
  2910. {
  2911. u8 center_chan;
  2912. int i, flags;
  2913. enum chan_allowed res, ret = ALLOWED;
  2914. center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  2915. if (!center_chan)
  2916. return NOT_ALLOWED;
  2917. if (center_chan >= 58 && center_chan <= 138)
  2918. return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
  2919. /* check all the channels are available */
  2920. for (i = 0; i < 4; i++) {
  2921. int adj_chan = center_chan - 6 + i * 4;
  2922. res = has_channel(wpa_s->global, mode, adj_chan, &flags);
  2923. if (res == NOT_ALLOWED)
  2924. return NOT_ALLOWED;
  2925. if (res == NO_IR)
  2926. ret = NO_IR;
  2927. if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
  2928. return NOT_ALLOWED;
  2929. if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
  2930. return NOT_ALLOWED;
  2931. if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
  2932. return NOT_ALLOWED;
  2933. if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
  2934. return NOT_ALLOWED;
  2935. }
  2936. return ret;
  2937. }
  2938. static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
  2939. struct hostapd_hw_modes *mode,
  2940. u8 channel)
  2941. {
  2942. u8 center_channels[] = { 50, 114 };
  2943. unsigned int i;
  2944. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  2945. return 0;
  2946. for (i = 0; i < ARRAY_SIZE(center_channels); i++)
  2947. /*
  2948. * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
  2949. * so the center channel is 14 channels away from the start/end.
  2950. */
  2951. if (channel >= center_channels[i] - 14 &&
  2952. channel <= center_channels[i] + 14)
  2953. return center_channels[i];
  2954. return 0;
  2955. }
  2956. static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
  2957. struct hostapd_hw_modes *mode,
  2958. u8 channel, u8 bw)
  2959. {
  2960. u8 center_chan;
  2961. int i, flags;
  2962. enum chan_allowed res, ret = ALLOWED;
  2963. center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
  2964. if (!center_chan)
  2965. return NOT_ALLOWED;
  2966. /* VHT 160 MHz uses DFS channels in most countries. */
  2967. /* Check all the channels are available */
  2968. for (i = 0; i < 8; i++) {
  2969. int adj_chan = center_chan - 14 + i * 4;
  2970. res = has_channel(wpa_s->global, mode, adj_chan, &flags);
  2971. if (res == NOT_ALLOWED)
  2972. return NOT_ALLOWED;
  2973. if (res == NO_IR)
  2974. ret = NO_IR;
  2975. if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
  2976. return NOT_ALLOWED;
  2977. if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
  2978. return NOT_ALLOWED;
  2979. if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
  2980. return NOT_ALLOWED;
  2981. if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
  2982. return NOT_ALLOWED;
  2983. if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
  2984. return NOT_ALLOWED;
  2985. if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
  2986. return NOT_ALLOWED;
  2987. if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
  2988. return NOT_ALLOWED;
  2989. if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
  2990. return NOT_ALLOWED;
  2991. }
  2992. return ret;
  2993. }
  2994. static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2995. struct hostapd_hw_modes *mode,
  2996. u8 channel, u8 bw)
  2997. {
  2998. int flag = 0;
  2999. enum chan_allowed res, res2;
  3000. res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
  3001. if (bw == BW40MINUS) {
  3002. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  3003. return NOT_ALLOWED;
  3004. res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
  3005. } else if (bw == BW40PLUS) {
  3006. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  3007. return NOT_ALLOWED;
  3008. res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
  3009. } else if (bw == BW80) {
  3010. res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
  3011. } else if (bw == BW160) {
  3012. res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
  3013. }
  3014. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  3015. return NOT_ALLOWED;
  3016. if (res == NO_IR || res2 == NO_IR)
  3017. return NO_IR;
  3018. return res;
  3019. }
  3020. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  3021. struct p2p_channels *chan,
  3022. struct p2p_channels *cli_chan)
  3023. {
  3024. struct hostapd_hw_modes *mode;
  3025. int cla, op, cli_cla;
  3026. if (wpa_s->hw.modes == NULL) {
  3027. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  3028. "of all supported channels; assume dualband "
  3029. "support");
  3030. return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
  3031. }
  3032. cla = cli_cla = 0;
  3033. for (op = 0; global_op_class[op].op_class; op++) {
  3034. const struct oper_class_map *o = &global_op_class[op];
  3035. u8 ch;
  3036. struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
  3037. if (o->p2p == NO_P2P_SUPP)
  3038. continue;
  3039. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  3040. if (mode == NULL)
  3041. continue;
  3042. if (mode->mode == HOSTAPD_MODE_IEEE80211G)
  3043. wpa_s->global->p2p_24ghz_social_channels = 1;
  3044. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  3045. enum chan_allowed res;
  3046. res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  3047. if (res == ALLOWED) {
  3048. if (reg == NULL) {
  3049. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
  3050. o->op_class);
  3051. reg = &chan->reg_class[cla];
  3052. cla++;
  3053. reg->reg_class = o->op_class;
  3054. }
  3055. reg->channel[reg->channels] = ch;
  3056. reg->channels++;
  3057. } else if (res == NO_IR &&
  3058. wpa_s->conf->p2p_add_cli_chan) {
  3059. if (cli_reg == NULL) {
  3060. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
  3061. o->op_class);
  3062. cli_reg = &cli_chan->reg_class[cli_cla];
  3063. cli_cla++;
  3064. cli_reg->reg_class = o->op_class;
  3065. }
  3066. cli_reg->channel[cli_reg->channels] = ch;
  3067. cli_reg->channels++;
  3068. }
  3069. }
  3070. if (reg) {
  3071. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  3072. reg->channel, reg->channels);
  3073. }
  3074. if (cli_reg) {
  3075. wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
  3076. cli_reg->channel, cli_reg->channels);
  3077. }
  3078. }
  3079. chan->reg_classes = cla;
  3080. cli_chan->reg_classes = cli_cla;
  3081. return 0;
  3082. }
  3083. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  3084. struct hostapd_hw_modes *mode, u8 channel)
  3085. {
  3086. int op;
  3087. enum chan_allowed ret;
  3088. for (op = 0; global_op_class[op].op_class; op++) {
  3089. const struct oper_class_map *o = &global_op_class[op];
  3090. u8 ch;
  3091. if (o->p2p == NO_P2P_SUPP)
  3092. continue;
  3093. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  3094. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  3095. (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
  3096. ch != channel)
  3097. continue;
  3098. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  3099. if (ret == ALLOWED)
  3100. return (o->bw == BW40MINUS) ? -1 : 1;
  3101. }
  3102. }
  3103. return 0;
  3104. }
  3105. int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
  3106. struct hostapd_hw_modes *mode, u8 channel)
  3107. {
  3108. if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
  3109. return 0;
  3110. return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  3111. }
  3112. int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
  3113. struct hostapd_hw_modes *mode, u8 channel)
  3114. {
  3115. if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
  3116. return 0;
  3117. return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
  3118. }
  3119. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  3120. size_t buf_len)
  3121. {
  3122. struct wpa_supplicant *wpa_s = ctx;
  3123. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3124. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  3125. break;
  3126. }
  3127. if (wpa_s == NULL)
  3128. return -1;
  3129. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  3130. }
  3131. struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
  3132. const u8 *ssid, size_t ssid_len)
  3133. {
  3134. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3135. struct wpa_ssid *s = wpa_s->current_ssid;
  3136. if (s == NULL)
  3137. continue;
  3138. if (s->mode != WPAS_MODE_P2P_GO &&
  3139. s->mode != WPAS_MODE_AP &&
  3140. s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  3141. continue;
  3142. if (s->ssid_len != ssid_len ||
  3143. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  3144. continue;
  3145. return wpa_s;
  3146. }
  3147. return NULL;
  3148. }
  3149. struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
  3150. const u8 *peer_dev_addr)
  3151. {
  3152. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3153. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3154. if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
  3155. continue;
  3156. if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
  3157. return wpa_s;
  3158. }
  3159. return NULL;
  3160. }
  3161. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  3162. {
  3163. struct wpa_supplicant *wpa_s = ctx;
  3164. return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
  3165. }
  3166. static int wpas_is_concurrent_session_active(void *ctx)
  3167. {
  3168. struct wpa_supplicant *wpa_s = ctx;
  3169. struct wpa_supplicant *ifs;
  3170. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  3171. if (ifs == wpa_s)
  3172. continue;
  3173. if (ifs->wpa_state > WPA_ASSOCIATED)
  3174. return 1;
  3175. }
  3176. return 0;
  3177. }
  3178. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  3179. {
  3180. struct wpa_supplicant *wpa_s = ctx;
  3181. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  3182. }
  3183. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
  3184. const char *conf_p2p_dev)
  3185. {
  3186. struct wpa_interface iface;
  3187. struct wpa_supplicant *p2pdev_wpa_s;
  3188. char ifname[100];
  3189. char force_name[100];
  3190. int ret;
  3191. ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  3192. wpa_s->ifname);
  3193. if (os_snprintf_error(sizeof(ifname), ret))
  3194. return -1;
  3195. force_name[0] = '\0';
  3196. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  3197. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  3198. force_name, wpa_s->pending_interface_addr, NULL);
  3199. if (ret < 0) {
  3200. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  3201. return ret;
  3202. }
  3203. os_strlcpy(wpa_s->pending_interface_name, ifname,
  3204. sizeof(wpa_s->pending_interface_name));
  3205. os_memset(&iface, 0, sizeof(iface));
  3206. iface.p2p_mgmt = 1;
  3207. iface.ifname = wpa_s->pending_interface_name;
  3208. iface.driver = wpa_s->driver->name;
  3209. iface.driver_param = wpa_s->conf->driver_param;
  3210. /*
  3211. * If a P2P Device configuration file was given, use it as the interface
  3212. * configuration file (instead of using parent's configuration file.
  3213. */
  3214. if (conf_p2p_dev) {
  3215. iface.confname = conf_p2p_dev;
  3216. iface.ctrl_interface = NULL;
  3217. } else {
  3218. iface.confname = wpa_s->confname;
  3219. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  3220. }
  3221. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
  3222. if (!p2pdev_wpa_s) {
  3223. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  3224. return -1;
  3225. }
  3226. p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
  3227. wpa_s->pending_interface_name[0] = '\0';
  3228. return 0;
  3229. }
  3230. static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
  3231. const u8 *noa, size_t noa_len)
  3232. {
  3233. struct wpa_supplicant *wpa_s, *intf = ctx;
  3234. char hex[100];
  3235. for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3236. if (wpa_s->waiting_presence_resp)
  3237. break;
  3238. }
  3239. if (!wpa_s) {
  3240. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
  3241. return;
  3242. }
  3243. wpa_s->waiting_presence_resp = 0;
  3244. wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
  3245. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
  3246. " status=%u noa=%s", MAC2STR(src), status, hex);
  3247. }
  3248. static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
  3249. size_t ssid_len, u8 *go_dev_addr,
  3250. u8 *ret_ssid, size_t *ret_ssid_len,
  3251. u8 *intended_iface_addr)
  3252. {
  3253. struct wpa_supplicant *wpa_s = ctx;
  3254. struct wpa_ssid *s;
  3255. s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
  3256. if (s) {
  3257. os_memcpy(ret_ssid, s->ssid, s->ssid_len);
  3258. *ret_ssid_len = s->ssid_len;
  3259. os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
  3260. if (s->mode != WPAS_MODE_P2P_GO) {
  3261. os_memset(intended_iface_addr, 0, ETH_ALEN);
  3262. } else if (wpas_p2p_create_iface(wpa_s)) {
  3263. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
  3264. return 0;
  3265. os_memcpy(intended_iface_addr,
  3266. wpa_s->pending_interface_addr, ETH_ALEN);
  3267. } else {
  3268. os_memcpy(intended_iface_addr, wpa_s->own_addr,
  3269. ETH_ALEN);
  3270. }
  3271. return 1;
  3272. }
  3273. return 0;
  3274. }
  3275. static int wpas_get_go_info(void *ctx, u8 *intended_addr,
  3276. u8 *ssid, size_t *ssid_len, int *group_iface,
  3277. unsigned int *freq)
  3278. {
  3279. struct wpa_supplicant *wpa_s = ctx;
  3280. struct wpa_supplicant *go;
  3281. struct wpa_ssid *s;
  3282. /*
  3283. * group_iface will be set to 1 only if a dedicated interface for P2P
  3284. * role is required. First, we try to reuse an active GO. However,
  3285. * if it is not present, we will try to reactivate an existing
  3286. * persistent group and set group_iface to 1, so the caller will know
  3287. * that the pending interface should be used.
  3288. */
  3289. *group_iface = 0;
  3290. if (freq)
  3291. *freq = 0;
  3292. go = wpas_p2p_get_go_group(wpa_s);
  3293. if (!go) {
  3294. s = wpas_p2p_get_persistent_go(wpa_s);
  3295. *group_iface = wpas_p2p_create_iface(wpa_s);
  3296. if (s)
  3297. os_memcpy(intended_addr, s->bssid, ETH_ALEN);
  3298. else
  3299. return 0;
  3300. } else {
  3301. s = go->current_ssid;
  3302. os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
  3303. if (freq)
  3304. *freq = go->assoc_freq;
  3305. }
  3306. os_memcpy(ssid, s->ssid, s->ssid_len);
  3307. *ssid_len = s->ssid_len;
  3308. return 1;
  3309. }
  3310. static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
  3311. const u8 *ssid, size_t ssid_len)
  3312. {
  3313. struct wpa_supplicant *wpa_s = ctx;
  3314. struct wpa_ssid *s;
  3315. int save_config = 0;
  3316. size_t i;
  3317. /* Start with our first choice of Persistent Groups */
  3318. while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
  3319. if (go && ssid && ssid_len &&
  3320. s->ssid_len == ssid_len &&
  3321. os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
  3322. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  3323. break;
  3324. /* Remove stale persistent group */
  3325. if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
  3326. wpa_config_remove_network(wpa_s->conf, s->id);
  3327. save_config = 1;
  3328. continue;
  3329. }
  3330. for (i = 0; i < s->num_p2p_clients; i++) {
  3331. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
  3332. peer, ETH_ALEN) != 0)
  3333. continue;
  3334. os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
  3335. s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  3336. (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  3337. break;
  3338. }
  3339. s->num_p2p_clients--;
  3340. save_config = 1;
  3341. }
  3342. if (save_config)
  3343. p2p_config_write(wpa_s);
  3344. /* Return TRUE if valid SSID remains */
  3345. return s != NULL;
  3346. }
  3347. static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
  3348. const u8 *feat_cap, size_t feat_cap_len)
  3349. {
  3350. static const char pref[] = " feature_cap=";
  3351. int ret;
  3352. buf[0] = '\0';
  3353. /*
  3354. * We expect a feature capability to contain at least one byte to be
  3355. * reported. The string buffer provided by the caller function is
  3356. * expected to be big enough to contain all bytes of the attribute for
  3357. * known specifications. This function truncates the reported bytes if
  3358. * the feature capability data exceeds the string buffer size.
  3359. */
  3360. if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
  3361. return;
  3362. os_memcpy(buf, pref, sizeof(pref));
  3363. ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
  3364. buf_len - sizeof(pref) + 1,
  3365. feat_cap, feat_cap_len);
  3366. if (ret != (2 * (int) feat_cap_len))
  3367. wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
  3368. }
  3369. static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
  3370. const u8 *adv_mac, const u8 *ses_mac,
  3371. const u8 *grp_mac, u32 adv_id, u32 ses_id,
  3372. u8 conncap, int passwd_id,
  3373. const u8 *persist_ssid,
  3374. size_t persist_ssid_size, int response_done,
  3375. int prov_start, const char *session_info,
  3376. const u8 *feat_cap, size_t feat_cap_len,
  3377. unsigned int freq,
  3378. const u8 *group_ssid, size_t group_ssid_len)
  3379. {
  3380. struct wpa_supplicant *wpa_s = ctx;
  3381. u8 mac[ETH_ALEN];
  3382. struct wpa_ssid *persistent_go, *stale, *s = NULL;
  3383. int save_config = 0;
  3384. struct wpa_supplicant *go_wpa_s;
  3385. char feat_cap_str[256];
  3386. if (!dev)
  3387. return;
  3388. os_memset(mac, 0, ETH_ALEN);
  3389. if (!adv_mac)
  3390. adv_mac = mac;
  3391. if (!ses_mac)
  3392. ses_mac = mac;
  3393. if (!grp_mac)
  3394. grp_mac = mac;
  3395. wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
  3396. feat_cap, feat_cap_len);
  3397. if (prov_start) {
  3398. if (session_info == NULL) {
  3399. wpa_msg_global(wpa_s, MSG_INFO,
  3400. P2P_EVENT_P2PS_PROVISION_START MACSTR
  3401. " adv_id=%x conncap=%x"
  3402. " adv_mac=" MACSTR
  3403. " session=%x mac=" MACSTR
  3404. " dev_passwd_id=%d%s",
  3405. MAC2STR(dev), adv_id, conncap,
  3406. MAC2STR(adv_mac),
  3407. ses_id, MAC2STR(ses_mac),
  3408. passwd_id, feat_cap_str);
  3409. } else {
  3410. wpa_msg_global(wpa_s, MSG_INFO,
  3411. P2P_EVENT_P2PS_PROVISION_START MACSTR
  3412. " adv_id=%x conncap=%x"
  3413. " adv_mac=" MACSTR
  3414. " session=%x mac=" MACSTR
  3415. " dev_passwd_id=%d info='%s'%s",
  3416. MAC2STR(dev), adv_id, conncap,
  3417. MAC2STR(adv_mac),
  3418. ses_id, MAC2STR(ses_mac),
  3419. passwd_id, session_info, feat_cap_str);
  3420. }
  3421. return;
  3422. }
  3423. go_wpa_s = wpas_p2p_get_go_group(wpa_s);
  3424. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  3425. if (status && status != P2P_SC_SUCCESS_DEFERRED) {
  3426. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3427. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3428. if (persistent_go && !persistent_go->num_p2p_clients) {
  3429. /* remove empty persistent GO */
  3430. wpa_config_remove_network(wpa_s->conf,
  3431. persistent_go->id);
  3432. }
  3433. wpa_msg_global(wpa_s, MSG_INFO,
  3434. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3435. " status=%d"
  3436. " adv_id=%x adv_mac=" MACSTR
  3437. " session=%x mac=" MACSTR "%s",
  3438. MAC2STR(dev), status,
  3439. adv_id, MAC2STR(adv_mac),
  3440. ses_id, MAC2STR(ses_mac), feat_cap_str);
  3441. return;
  3442. }
  3443. /* Clean up stale persistent groups with this device */
  3444. if (persist_ssid && persist_ssid_size)
  3445. s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
  3446. persist_ssid_size);
  3447. if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
  3448. is_zero_ether_addr(grp_mac)) {
  3449. wpa_dbg(wpa_s, MSG_ERROR,
  3450. "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
  3451. return;
  3452. }
  3453. for (;;) {
  3454. stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
  3455. if (!stale)
  3456. break;
  3457. if (s && s->ssid_len == stale->ssid_len &&
  3458. os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
  3459. os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
  3460. break;
  3461. /* Remove stale persistent group */
  3462. if (stale->mode != WPAS_MODE_P2P_GO ||
  3463. stale->num_p2p_clients <= 1) {
  3464. wpa_config_remove_network(wpa_s->conf, stale->id);
  3465. } else {
  3466. size_t i;
  3467. for (i = 0; i < stale->num_p2p_clients; i++) {
  3468. if (os_memcmp(stale->p2p_client_list +
  3469. i * ETH_ALEN,
  3470. dev, ETH_ALEN) == 0) {
  3471. os_memmove(stale->p2p_client_list +
  3472. i * ETH_ALEN,
  3473. stale->p2p_client_list +
  3474. (i + 1) * ETH_ALEN,
  3475. (stale->num_p2p_clients -
  3476. i - 1) * ETH_ALEN);
  3477. break;
  3478. }
  3479. }
  3480. stale->num_p2p_clients--;
  3481. }
  3482. save_config = 1;
  3483. }
  3484. if (save_config)
  3485. p2p_config_write(wpa_s);
  3486. if (s) {
  3487. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3488. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3489. if (persistent_go && s != persistent_go &&
  3490. !persistent_go->num_p2p_clients) {
  3491. /* remove empty persistent GO */
  3492. wpa_config_remove_network(wpa_s->conf,
  3493. persistent_go->id);
  3494. /* Save config */
  3495. }
  3496. wpa_msg_global(wpa_s, MSG_INFO,
  3497. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3498. " status=%d"
  3499. " adv_id=%x adv_mac=" MACSTR
  3500. " session=%x mac=" MACSTR
  3501. " persist=%d%s",
  3502. MAC2STR(dev), status,
  3503. adv_id, MAC2STR(adv_mac),
  3504. ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
  3505. return;
  3506. }
  3507. if (conncap == P2PS_SETUP_GROUP_OWNER) {
  3508. /*
  3509. * We need to copy the interface name. Simply saving a
  3510. * pointer isn't enough, since if we use pending_interface_name
  3511. * it will be overwritten when the group is added.
  3512. */
  3513. char go_ifname[100];
  3514. go_ifname[0] = '\0';
  3515. if (!go_wpa_s) {
  3516. wpa_s->global->pending_p2ps_group = 1;
  3517. wpa_s->global->pending_p2ps_group_freq = freq;
  3518. if (!wpas_p2p_create_iface(wpa_s))
  3519. os_memcpy(go_ifname, wpa_s->ifname,
  3520. sizeof(go_ifname));
  3521. else if (wpa_s->pending_interface_name[0])
  3522. os_memcpy(go_ifname,
  3523. wpa_s->pending_interface_name,
  3524. sizeof(go_ifname));
  3525. if (!go_ifname[0]) {
  3526. wpas_p2ps_prov_complete(
  3527. wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
  3528. dev, adv_mac, ses_mac,
  3529. grp_mac, adv_id, ses_id, 0, 0,
  3530. NULL, 0, 0, 0, NULL, NULL, 0, 0,
  3531. NULL, 0);
  3532. return;
  3533. }
  3534. /* If PD Resp complete, start up the GO */
  3535. if (response_done && persistent_go) {
  3536. wpas_p2p_group_add_persistent(
  3537. wpa_s, persistent_go,
  3538. 0, 0, freq, 0, 0, 0, 0, NULL,
  3539. persistent_go->mode ==
  3540. WPAS_MODE_P2P_GO ?
  3541. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  3542. 0, 0);
  3543. } else if (response_done) {
  3544. wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
  3545. }
  3546. if (passwd_id == DEV_PW_P2PS_DEFAULT) {
  3547. os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
  3548. ETH_ALEN);
  3549. wpa_s->p2ps_method_config_any = 1;
  3550. }
  3551. } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
  3552. os_memcpy(go_ifname, go_wpa_s->ifname,
  3553. sizeof(go_ifname));
  3554. if (is_zero_ether_addr(grp_mac)) {
  3555. wpa_dbg(go_wpa_s, MSG_DEBUG,
  3556. "P2P: Setting PIN-1 for ANY");
  3557. wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
  3558. "12345670", NULL, 0,
  3559. 0);
  3560. } else {
  3561. wpa_dbg(go_wpa_s, MSG_DEBUG,
  3562. "P2P: Setting PIN-1 for " MACSTR,
  3563. MAC2STR(grp_mac));
  3564. wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
  3565. "12345670", NULL, 0,
  3566. 0);
  3567. }
  3568. os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
  3569. wpa_s->p2ps_method_config_any = 1;
  3570. }
  3571. wpa_msg_global(wpa_s, MSG_INFO,
  3572. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3573. " status=%d conncap=%x"
  3574. " adv_id=%x adv_mac=" MACSTR
  3575. " session=%x mac=" MACSTR
  3576. " dev_passwd_id=%d go=%s%s",
  3577. MAC2STR(dev), status, conncap,
  3578. adv_id, MAC2STR(adv_mac),
  3579. ses_id, MAC2STR(ses_mac),
  3580. passwd_id, go_ifname, feat_cap_str);
  3581. return;
  3582. }
  3583. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3584. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3585. if (persistent_go && !persistent_go->num_p2p_clients) {
  3586. /* remove empty persistent GO */
  3587. wpa_config_remove_network(wpa_s->conf, persistent_go->id);
  3588. }
  3589. if (conncap == P2PS_SETUP_CLIENT) {
  3590. char ssid_hex[32 * 2 + 1];
  3591. if (group_ssid)
  3592. wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
  3593. group_ssid, group_ssid_len);
  3594. else
  3595. ssid_hex[0] = '\0';
  3596. wpa_msg_global(wpa_s, MSG_INFO,
  3597. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3598. " status=%d conncap=%x"
  3599. " adv_id=%x adv_mac=" MACSTR
  3600. " session=%x mac=" MACSTR
  3601. " dev_passwd_id=%d join=" MACSTR "%s%s%s",
  3602. MAC2STR(dev), status, conncap,
  3603. adv_id, MAC2STR(adv_mac),
  3604. ses_id, MAC2STR(ses_mac),
  3605. passwd_id, MAC2STR(grp_mac), feat_cap_str,
  3606. group_ssid ? " group_ssid=" : "", ssid_hex);
  3607. } else {
  3608. wpa_msg_global(wpa_s, MSG_INFO,
  3609. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3610. " status=%d conncap=%x"
  3611. " adv_id=%x adv_mac=" MACSTR
  3612. " session=%x mac=" MACSTR
  3613. " dev_passwd_id=%d%s",
  3614. MAC2STR(dev), status, conncap,
  3615. adv_id, MAC2STR(adv_mac),
  3616. ses_id, MAC2STR(ses_mac),
  3617. passwd_id, feat_cap_str);
  3618. }
  3619. }
  3620. static int _wpas_p2p_in_progress(void *ctx)
  3621. {
  3622. struct wpa_supplicant *wpa_s = ctx;
  3623. return wpas_p2p_in_progress(wpa_s);
  3624. }
  3625. static int wpas_prov_disc_resp_cb(void *ctx)
  3626. {
  3627. struct wpa_supplicant *wpa_s = ctx;
  3628. struct wpa_ssid *persistent_go;
  3629. unsigned int freq;
  3630. if (!wpa_s->global->pending_p2ps_group)
  3631. return 0;
  3632. freq = wpa_s->global->pending_p2ps_group_freq;
  3633. wpa_s->global->pending_p2ps_group_freq = 0;
  3634. wpa_s->global->pending_p2ps_group = 0;
  3635. if (wpas_p2p_get_go_group(wpa_s))
  3636. return 0;
  3637. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  3638. if (persistent_go) {
  3639. wpas_p2p_group_add_persistent(
  3640. wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
  3641. persistent_go->mode == WPAS_MODE_P2P_GO ?
  3642. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
  3643. } else {
  3644. wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
  3645. }
  3646. return 1;
  3647. }
  3648. static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
  3649. unsigned int *len,
  3650. unsigned int *freq_list)
  3651. {
  3652. struct wpa_supplicant *wpa_s = ctx;
  3653. return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
  3654. WPA_IF_P2P_CLIENT, len, freq_list);
  3655. }
  3656. /**
  3657. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  3658. * @global: Pointer to global data from wpa_supplicant_init()
  3659. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3660. * Returns: 0 on success, -1 on failure
  3661. */
  3662. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  3663. {
  3664. struct p2p_config p2p;
  3665. int i;
  3666. if (wpa_s->conf->p2p_disabled)
  3667. return 0;
  3668. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  3669. return 0;
  3670. if (global->p2p)
  3671. return 0;
  3672. os_memset(&p2p, 0, sizeof(p2p));
  3673. p2p.cb_ctx = wpa_s;
  3674. p2p.debug_print = wpas_p2p_debug_print;
  3675. p2p.p2p_scan = wpas_p2p_scan;
  3676. p2p.send_action = wpas_send_action;
  3677. p2p.send_action_done = wpas_send_action_done;
  3678. p2p.go_neg_completed = wpas_go_neg_completed;
  3679. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  3680. p2p.dev_found = wpas_dev_found;
  3681. p2p.dev_lost = wpas_dev_lost;
  3682. p2p.find_stopped = wpas_find_stopped;
  3683. p2p.start_listen = wpas_start_listen;
  3684. p2p.stop_listen = wpas_stop_listen;
  3685. p2p.send_probe_resp = wpas_send_probe_resp;
  3686. p2p.sd_request = wpas_sd_request;
  3687. p2p.sd_response = wpas_sd_response;
  3688. p2p.prov_disc_req = wpas_prov_disc_req;
  3689. p2p.prov_disc_resp = wpas_prov_disc_resp;
  3690. p2p.prov_disc_fail = wpas_prov_disc_fail;
  3691. p2p.invitation_process = wpas_invitation_process;
  3692. p2p.invitation_received = wpas_invitation_received;
  3693. p2p.invitation_result = wpas_invitation_result;
  3694. p2p.get_noa = wpas_get_noa;
  3695. p2p.go_connected = wpas_go_connected;
  3696. p2p.presence_resp = wpas_presence_resp;
  3697. p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
  3698. p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
  3699. p2p.get_persistent_group = wpas_get_persistent_group;
  3700. p2p.get_go_info = wpas_get_go_info;
  3701. p2p.remove_stale_groups = wpas_remove_stale_groups;
  3702. p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
  3703. p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
  3704. p2p.p2ps_group_capability = p2ps_group_capability;
  3705. p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
  3706. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  3707. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  3708. p2p.dev_name = wpa_s->conf->device_name;
  3709. p2p.manufacturer = wpa_s->conf->manufacturer;
  3710. p2p.model_name = wpa_s->conf->model_name;
  3711. p2p.model_number = wpa_s->conf->model_number;
  3712. p2p.serial_number = wpa_s->conf->serial_number;
  3713. if (wpa_s->wps) {
  3714. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  3715. p2p.config_methods = wpa_s->wps->config_methods;
  3716. }
  3717. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
  3718. wpa_printf(MSG_ERROR,
  3719. "P2P: Failed to configure supported channel list");
  3720. return -1;
  3721. }
  3722. if (wpa_s->conf->p2p_listen_reg_class &&
  3723. wpa_s->conf->p2p_listen_channel) {
  3724. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  3725. p2p.channel = wpa_s->conf->p2p_listen_channel;
  3726. p2p.channel_forced = 1;
  3727. } else {
  3728. /*
  3729. * Pick one of the social channels randomly as the listen
  3730. * channel.
  3731. */
  3732. if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
  3733. &p2p.channel) != 0) {
  3734. wpa_printf(MSG_INFO,
  3735. "P2P: No social channels supported by the driver - do not enable P2P");
  3736. return 0;
  3737. }
  3738. p2p.channel_forced = 0;
  3739. }
  3740. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
  3741. p2p.reg_class, p2p.channel);
  3742. if (wpa_s->conf->p2p_oper_reg_class &&
  3743. wpa_s->conf->p2p_oper_channel) {
  3744. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  3745. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  3746. p2p.cfg_op_channel = 1;
  3747. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  3748. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3749. } else {
  3750. /*
  3751. * Use random operation channel from 2.4 GHz band social
  3752. * channels (1, 6, 11) or band 60 GHz social channel (2) if no
  3753. * other preference is indicated.
  3754. */
  3755. if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
  3756. &p2p.op_channel) != 0) {
  3757. wpa_printf(MSG_ERROR,
  3758. "P2P: Failed to select random social channel as operation channel");
  3759. return -1;
  3760. }
  3761. p2p.cfg_op_channel = 0;
  3762. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  3763. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3764. }
  3765. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  3766. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  3767. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  3768. }
  3769. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3770. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  3771. p2p.country[2] = 0x04;
  3772. } else
  3773. os_memcpy(p2p.country, "XX\x04", 3);
  3774. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  3775. WPS_DEV_TYPE_LEN);
  3776. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  3777. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  3778. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  3779. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  3780. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  3781. p2p.max_peers = 100;
  3782. if (wpa_s->conf->p2p_ssid_postfix) {
  3783. p2p.ssid_postfix_len =
  3784. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  3785. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  3786. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  3787. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  3788. p2p.ssid_postfix_len);
  3789. }
  3790. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  3791. p2p.max_listen = wpa_s->max_remain_on_chan;
  3792. if (wpa_s->conf->p2p_passphrase_len >= 8 &&
  3793. wpa_s->conf->p2p_passphrase_len <= 63)
  3794. p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
  3795. else
  3796. p2p.passphrase_len = 8;
  3797. global->p2p = p2p_init(&p2p);
  3798. if (global->p2p == NULL)
  3799. return -1;
  3800. global->p2p_init_wpa_s = wpa_s;
  3801. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  3802. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  3803. continue;
  3804. p2p_add_wps_vendor_extension(
  3805. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  3806. }
  3807. p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
  3808. return 0;
  3809. }
  3810. /**
  3811. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  3812. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3813. *
  3814. * This function deinitialize per-interface P2P data.
  3815. */
  3816. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  3817. {
  3818. if (wpa_s->driver && wpa_s->drv_priv)
  3819. wpa_drv_probe_req_report(wpa_s, 0);
  3820. if (wpa_s->go_params) {
  3821. /* Clear any stored provisioning info */
  3822. p2p_clear_provisioning_info(
  3823. wpa_s->global->p2p,
  3824. wpa_s->go_params->peer_device_addr);
  3825. }
  3826. os_free(wpa_s->go_params);
  3827. wpa_s->go_params = NULL;
  3828. eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
  3829. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3830. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3831. wpa_s->p2p_long_listen = 0;
  3832. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  3833. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3834. wpas_p2p_remove_pending_group_interface(wpa_s);
  3835. eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
  3836. wpas_p2p_listen_work_done(wpa_s);
  3837. if (wpa_s->p2p_send_action_work) {
  3838. os_free(wpa_s->p2p_send_action_work->ctx);
  3839. radio_work_done(wpa_s->p2p_send_action_work);
  3840. wpa_s->p2p_send_action_work = NULL;
  3841. }
  3842. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
  3843. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  3844. wpa_s->p2p_oob_dev_pw = NULL;
  3845. os_free(wpa_s->p2p_group_common_freqs);
  3846. wpa_s->p2p_group_common_freqs = NULL;
  3847. wpa_s->p2p_group_common_freqs_num = 0;
  3848. /* TODO: remove group interface from the driver if this wpa_s instance
  3849. * is on top of a P2P group interface */
  3850. }
  3851. /**
  3852. * wpas_p2p_deinit_global - Deinitialize global P2P module
  3853. * @global: Pointer to global data from wpa_supplicant_init()
  3854. *
  3855. * This function deinitializes the global (per device) P2P module.
  3856. */
  3857. static void wpas_p2p_deinit_global(struct wpa_global *global)
  3858. {
  3859. struct wpa_supplicant *wpa_s, *tmp;
  3860. wpa_s = global->ifaces;
  3861. wpas_p2p_service_flush(global->p2p_init_wpa_s);
  3862. /* Remove remaining P2P group interfaces */
  3863. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  3864. wpa_s = wpa_s->next;
  3865. while (wpa_s) {
  3866. tmp = global->ifaces;
  3867. while (tmp &&
  3868. (tmp == wpa_s ||
  3869. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  3870. tmp = tmp->next;
  3871. }
  3872. if (tmp == NULL)
  3873. break;
  3874. /* Disconnect from the P2P group and deinit the interface */
  3875. wpas_p2p_disconnect(tmp);
  3876. }
  3877. /*
  3878. * Deinit GO data on any possibly remaining interface (if main
  3879. * interface is used as GO).
  3880. */
  3881. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3882. if (wpa_s->ap_iface)
  3883. wpas_p2p_group_deinit(wpa_s);
  3884. }
  3885. p2p_deinit(global->p2p);
  3886. global->p2p = NULL;
  3887. global->p2p_init_wpa_s = NULL;
  3888. }
  3889. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  3890. {
  3891. if (wpa_s->conf->p2p_no_group_iface)
  3892. return 0; /* separate interface disabled per configuration */
  3893. if (wpa_s->drv_flags &
  3894. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  3895. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  3896. return 1; /* P2P group requires a new interface in every case
  3897. */
  3898. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  3899. return 0; /* driver does not support concurrent operations */
  3900. if (wpa_s->global->ifaces->next)
  3901. return 1; /* more that one interface already in use */
  3902. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  3903. return 1; /* this interface is already in use */
  3904. return 0;
  3905. }
  3906. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  3907. const u8 *peer_addr,
  3908. enum p2p_wps_method wps_method,
  3909. int go_intent, const u8 *own_interface_addr,
  3910. unsigned int force_freq, int persistent_group,
  3911. struct wpa_ssid *ssid, unsigned int pref_freq)
  3912. {
  3913. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3914. persistent_group = 2;
  3915. /*
  3916. * Increase GO config timeout if HT40 is used since it takes some time
  3917. * to scan channels for coex purposes before the BSS can be started.
  3918. */
  3919. p2p_set_config_timeout(wpa_s->global->p2p,
  3920. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  3921. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  3922. go_intent, own_interface_addr, force_freq,
  3923. persistent_group, ssid ? ssid->ssid : NULL,
  3924. ssid ? ssid->ssid_len : 0,
  3925. wpa_s->p2p_pd_before_go_neg, pref_freq,
  3926. wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
  3927. 0);
  3928. }
  3929. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  3930. const u8 *peer_addr,
  3931. enum p2p_wps_method wps_method,
  3932. int go_intent, const u8 *own_interface_addr,
  3933. unsigned int force_freq, int persistent_group,
  3934. struct wpa_ssid *ssid, unsigned int pref_freq)
  3935. {
  3936. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3937. persistent_group = 2;
  3938. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  3939. go_intent, own_interface_addr, force_freq,
  3940. persistent_group, ssid ? ssid->ssid : NULL,
  3941. ssid ? ssid->ssid_len : 0, pref_freq,
  3942. wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
  3943. 0);
  3944. }
  3945. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  3946. {
  3947. wpa_s->p2p_join_scan_count++;
  3948. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  3949. wpa_s->p2p_join_scan_count);
  3950. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  3951. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  3952. " for join operationg - stop join attempt",
  3953. MAC2STR(wpa_s->pending_join_iface_addr));
  3954. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3955. if (wpa_s->p2p_auto_pd) {
  3956. wpa_s->p2p_auto_pd = 0;
  3957. wpa_msg_global(wpa_s, MSG_INFO,
  3958. P2P_EVENT_PROV_DISC_FAILURE
  3959. " p2p_dev_addr=" MACSTR " status=N/A",
  3960. MAC2STR(wpa_s->pending_join_dev_addr));
  3961. return;
  3962. }
  3963. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  3964. P2P_EVENT_GROUP_FORMATION_FAILURE);
  3965. wpas_notify_p2p_group_formation_failure(wpa_s, "");
  3966. }
  3967. }
  3968. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  3969. {
  3970. int res;
  3971. unsigned int num, i;
  3972. struct wpa_used_freq_data *freqs;
  3973. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  3974. /* Multiple channels are supported and not all are in use */
  3975. return 0;
  3976. }
  3977. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  3978. sizeof(struct wpa_used_freq_data));
  3979. if (!freqs)
  3980. return 1;
  3981. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3982. wpa_s->num_multichan_concurrent);
  3983. for (i = 0; i < num; i++) {
  3984. if (freqs[i].freq == freq) {
  3985. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  3986. freq);
  3987. res = 0;
  3988. goto exit_free;
  3989. }
  3990. }
  3991. wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
  3992. res = 1;
  3993. exit_free:
  3994. os_free(freqs);
  3995. return res;
  3996. }
  3997. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  3998. const u8 *peer_dev_addr)
  3999. {
  4000. struct wpa_bss *bss;
  4001. int updated;
  4002. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  4003. if (bss == NULL)
  4004. return -1;
  4005. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  4006. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  4007. "last scan");
  4008. return 0;
  4009. }
  4010. updated = os_reltime_before(&wpa_s->p2p_auto_started,
  4011. &bss->last_update);
  4012. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  4013. "%ld.%06ld (%supdated in last scan)",
  4014. bss->last_update.sec, bss->last_update.usec,
  4015. updated ? "": "not ");
  4016. return updated;
  4017. }
  4018. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  4019. struct wpa_scan_results *scan_res)
  4020. {
  4021. struct wpa_bss *bss = NULL;
  4022. int freq;
  4023. u8 iface_addr[ETH_ALEN];
  4024. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4025. if (wpa_s->global->p2p_disabled)
  4026. return;
  4027. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  4028. scan_res ? (int) scan_res->num : -1,
  4029. wpa_s->p2p_auto_join ? "auto_" : "");
  4030. if (scan_res)
  4031. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  4032. if (wpa_s->p2p_auto_pd) {
  4033. int join = wpas_p2p_peer_go(wpa_s,
  4034. wpa_s->pending_join_dev_addr);
  4035. if (join == 0 &&
  4036. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  4037. wpa_s->auto_pd_scan_retry++;
  4038. bss = wpa_bss_get_bssid_latest(
  4039. wpa_s, wpa_s->pending_join_dev_addr);
  4040. if (bss) {
  4041. freq = bss->freq;
  4042. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  4043. "the peer " MACSTR " at %d MHz",
  4044. wpa_s->auto_pd_scan_retry,
  4045. MAC2STR(wpa_s->
  4046. pending_join_dev_addr),
  4047. freq);
  4048. wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
  4049. return;
  4050. }
  4051. }
  4052. if (join < 0)
  4053. join = 0;
  4054. wpa_s->p2p_auto_pd = 0;
  4055. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  4056. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  4057. MAC2STR(wpa_s->pending_join_dev_addr), join);
  4058. if (p2p_prov_disc_req(wpa_s->global->p2p,
  4059. wpa_s->pending_join_dev_addr, NULL,
  4060. wpa_s->pending_pd_config_methods, join,
  4061. 0, wpa_s->user_initiated_pd) < 0) {
  4062. wpa_s->p2p_auto_pd = 0;
  4063. wpa_msg_global(wpa_s, MSG_INFO,
  4064. P2P_EVENT_PROV_DISC_FAILURE
  4065. " p2p_dev_addr=" MACSTR " status=N/A",
  4066. MAC2STR(wpa_s->pending_join_dev_addr));
  4067. }
  4068. return;
  4069. }
  4070. if (wpa_s->p2p_auto_join) {
  4071. int join = wpas_p2p_peer_go(wpa_s,
  4072. wpa_s->pending_join_dev_addr);
  4073. if (join < 0) {
  4074. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  4075. "running a GO -> use GO Negotiation");
  4076. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  4077. P2P_EVENT_FALLBACK_TO_GO_NEG
  4078. "reason=peer-not-running-GO");
  4079. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  4080. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  4081. wpa_s->p2p_persistent_group, 0, 0, 0,
  4082. wpa_s->p2p_go_intent,
  4083. wpa_s->p2p_connect_freq,
  4084. wpa_s->p2p_go_vht_center_freq2,
  4085. wpa_s->p2p_persistent_id,
  4086. wpa_s->p2p_pd_before_go_neg,
  4087. wpa_s->p2p_go_ht40,
  4088. wpa_s->p2p_go_vht,
  4089. wpa_s->p2p_go_max_oper_chwidth,
  4090. NULL, 0);
  4091. return;
  4092. }
  4093. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  4094. "try to join the group", join ? "" :
  4095. " in older scan");
  4096. if (!join) {
  4097. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  4098. P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
  4099. wpa_s->p2p_fallback_to_go_neg = 1;
  4100. }
  4101. }
  4102. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  4103. wpa_s->pending_join_iface_addr);
  4104. if (freq < 0 &&
  4105. p2p_get_interface_addr(wpa_s->global->p2p,
  4106. wpa_s->pending_join_dev_addr,
  4107. iface_addr) == 0 &&
  4108. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
  4109. && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
  4110. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  4111. "address for join from " MACSTR " to " MACSTR
  4112. " based on newly discovered P2P peer entry",
  4113. MAC2STR(wpa_s->pending_join_iface_addr),
  4114. MAC2STR(iface_addr));
  4115. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  4116. ETH_ALEN);
  4117. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  4118. wpa_s->pending_join_iface_addr);
  4119. }
  4120. if (freq >= 0) {
  4121. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  4122. "from P2P peer table: %d MHz", freq);
  4123. }
  4124. if (wpa_s->p2p_join_ssid_len) {
  4125. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  4126. MACSTR " and SSID %s",
  4127. MAC2STR(wpa_s->pending_join_iface_addr),
  4128. wpa_ssid_txt(wpa_s->p2p_join_ssid,
  4129. wpa_s->p2p_join_ssid_len));
  4130. bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
  4131. wpa_s->p2p_join_ssid,
  4132. wpa_s->p2p_join_ssid_len);
  4133. } else if (!bss) {
  4134. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  4135. MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
  4136. bss = wpa_bss_get_bssid_latest(wpa_s,
  4137. wpa_s->pending_join_iface_addr);
  4138. }
  4139. if (bss) {
  4140. u8 dev_addr[ETH_ALEN];
  4141. freq = bss->freq;
  4142. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  4143. "from BSS table: %d MHz (SSID %s)", freq,
  4144. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  4145. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  4146. dev_addr) == 0 &&
  4147. os_memcmp(wpa_s->pending_join_dev_addr,
  4148. wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
  4149. os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
  4150. ETH_ALEN) != 0) {
  4151. wpa_printf(MSG_DEBUG,
  4152. "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
  4153. MAC2STR(dev_addr),
  4154. MAC2STR(wpa_s->pending_join_dev_addr));
  4155. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
  4156. ETH_ALEN);
  4157. }
  4158. }
  4159. if (freq > 0) {
  4160. u16 method;
  4161. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  4162. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  4163. P2P_EVENT_GROUP_FORMATION_FAILURE
  4164. "reason=FREQ_CONFLICT");
  4165. wpas_notify_p2p_group_formation_failure(
  4166. wpa_s, "FREQ_CONFLICT");
  4167. return;
  4168. }
  4169. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  4170. "prior to joining an existing group (GO " MACSTR
  4171. " freq=%u MHz)",
  4172. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  4173. wpa_s->pending_pd_before_join = 1;
  4174. switch (wpa_s->pending_join_wps_method) {
  4175. case WPS_PIN_DISPLAY:
  4176. method = WPS_CONFIG_KEYPAD;
  4177. break;
  4178. case WPS_PIN_KEYPAD:
  4179. method = WPS_CONFIG_DISPLAY;
  4180. break;
  4181. case WPS_PBC:
  4182. method = WPS_CONFIG_PUSHBUTTON;
  4183. break;
  4184. case WPS_P2PS:
  4185. method = WPS_CONFIG_P2PS;
  4186. break;
  4187. default:
  4188. method = 0;
  4189. break;
  4190. }
  4191. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  4192. wpa_s->pending_join_dev_addr) ==
  4193. method)) {
  4194. /*
  4195. * We have already performed provision discovery for
  4196. * joining the group. Proceed directly to join
  4197. * operation without duplicated provision discovery. */
  4198. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  4199. "with " MACSTR " already done - proceed to "
  4200. "join",
  4201. MAC2STR(wpa_s->pending_join_dev_addr));
  4202. wpa_s->pending_pd_before_join = 0;
  4203. goto start;
  4204. }
  4205. if (p2p_prov_disc_req(wpa_s->global->p2p,
  4206. wpa_s->pending_join_dev_addr,
  4207. NULL, method, 1,
  4208. freq, wpa_s->user_initiated_pd) < 0) {
  4209. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  4210. "Discovery Request before joining an "
  4211. "existing group");
  4212. wpa_s->pending_pd_before_join = 0;
  4213. goto start;
  4214. }
  4215. return;
  4216. }
  4217. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  4218. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4219. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  4220. wpas_p2p_check_join_scan_limit(wpa_s);
  4221. return;
  4222. start:
  4223. /* Start join operation immediately */
  4224. wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
  4225. wpa_s->p2p_join_ssid_len);
  4226. }
  4227. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  4228. const u8 *ssid, size_t ssid_len)
  4229. {
  4230. int ret;
  4231. struct wpa_driver_scan_params params;
  4232. struct wpabuf *wps_ie, *ies;
  4233. size_t ielen;
  4234. int freqs[2] = { 0, 0 };
  4235. unsigned int bands;
  4236. os_memset(&params, 0, sizeof(params));
  4237. /* P2P Wildcard SSID */
  4238. params.num_ssids = 1;
  4239. if (ssid && ssid_len) {
  4240. params.ssids[0].ssid = ssid;
  4241. params.ssids[0].ssid_len = ssid_len;
  4242. os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
  4243. wpa_s->p2p_join_ssid_len = ssid_len;
  4244. } else {
  4245. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  4246. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  4247. wpa_s->p2p_join_ssid_len = 0;
  4248. }
  4249. wpa_s->wps->dev.p2p = 1;
  4250. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  4251. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  4252. NULL);
  4253. if (wps_ie == NULL) {
  4254. wpas_p2p_scan_res_join(wpa_s, NULL);
  4255. return;
  4256. }
  4257. if (!freq) {
  4258. int oper_freq;
  4259. /*
  4260. * If freq is not provided, check the operating freq of the GO
  4261. * and use a single channel scan on if possible.
  4262. */
  4263. oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
  4264. wpa_s->pending_join_iface_addr);
  4265. if (oper_freq > 0)
  4266. freq = oper_freq;
  4267. }
  4268. if (freq > 0) {
  4269. freqs[0] = freq;
  4270. params.freqs = freqs;
  4271. }
  4272. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  4273. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  4274. if (ies == NULL) {
  4275. wpabuf_free(wps_ie);
  4276. wpas_p2p_scan_res_join(wpa_s, NULL);
  4277. return;
  4278. }
  4279. wpabuf_put_buf(ies, wps_ie);
  4280. wpabuf_free(wps_ie);
  4281. bands = wpas_get_bands(wpa_s, freqs);
  4282. p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
  4283. params.p2p_probe = 1;
  4284. params.extra_ies = wpabuf_head(ies);
  4285. params.extra_ies_len = wpabuf_len(ies);
  4286. if (wpa_s->clear_driver_scan_cache) {
  4287. wpa_printf(MSG_DEBUG,
  4288. "Request driver to clear scan cache due to local BSS flush");
  4289. params.only_new_results = 1;
  4290. }
  4291. /*
  4292. * Run a scan to update BSS table and start Provision Discovery once
  4293. * the new scan results become available.
  4294. */
  4295. ret = wpa_drv_scan(wpa_s, &params);
  4296. if (!ret) {
  4297. os_get_reltime(&wpa_s->scan_trigger_time);
  4298. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  4299. wpa_s->own_scan_requested = 1;
  4300. wpa_s->clear_driver_scan_cache = 0;
  4301. }
  4302. wpabuf_free(ies);
  4303. if (ret) {
  4304. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  4305. "try again later");
  4306. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4307. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  4308. wpas_p2p_check_join_scan_limit(wpa_s);
  4309. }
  4310. }
  4311. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  4312. {
  4313. struct wpa_supplicant *wpa_s = eloop_ctx;
  4314. wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
  4315. }
  4316. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  4317. const u8 *dev_addr, enum p2p_wps_method wps_method,
  4318. int auto_join, int op_freq,
  4319. const u8 *ssid, size_t ssid_len)
  4320. {
  4321. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  4322. MACSTR " dev " MACSTR " op_freq=%d)%s",
  4323. MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
  4324. auto_join ? " (auto_join)" : "");
  4325. if (ssid && ssid_len) {
  4326. wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
  4327. wpa_ssid_txt(ssid, ssid_len));
  4328. }
  4329. wpa_s->p2p_auto_pd = 0;
  4330. wpa_s->p2p_auto_join = !!auto_join;
  4331. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  4332. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  4333. wpa_s->pending_join_wps_method = wps_method;
  4334. /* Make sure we are not running find during connection establishment */
  4335. wpas_p2p_stop_find(wpa_s);
  4336. wpa_s->p2p_join_scan_count = 0;
  4337. wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
  4338. return 0;
  4339. }
  4340. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
  4341. const u8 *ssid, size_t ssid_len)
  4342. {
  4343. struct wpa_supplicant *group;
  4344. struct p2p_go_neg_results res;
  4345. struct wpa_bss *bss;
  4346. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  4347. if (group == NULL)
  4348. return -1;
  4349. if (group != wpa_s) {
  4350. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  4351. sizeof(group->p2p_pin));
  4352. group->p2p_wps_method = wpa_s->p2p_wps_method;
  4353. } else {
  4354. /*
  4355. * Need to mark the current interface for p2p_group_formation
  4356. * when a separate group interface is not used. This is needed
  4357. * to allow p2p_cancel stop a pending p2p_connect-join.
  4358. * wpas_p2p_init_group_interface() addresses this for the case
  4359. * where a separate group interface is used.
  4360. */
  4361. wpa_s->global->p2p_group_formation = wpa_s;
  4362. }
  4363. group->p2p_in_provisioning = 1;
  4364. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  4365. os_memset(&res, 0, sizeof(res));
  4366. os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
  4367. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  4368. ETH_ALEN);
  4369. res.wps_method = wpa_s->pending_join_wps_method;
  4370. if (freq && ssid && ssid_len) {
  4371. res.freq = freq;
  4372. res.ssid_len = ssid_len;
  4373. os_memcpy(res.ssid, ssid, ssid_len);
  4374. } else {
  4375. if (ssid && ssid_len) {
  4376. bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
  4377. ssid, ssid_len);
  4378. } else {
  4379. bss = wpa_bss_get_bssid_latest(
  4380. wpa_s, wpa_s->pending_join_iface_addr);
  4381. }
  4382. if (bss) {
  4383. res.freq = bss->freq;
  4384. res.ssid_len = bss->ssid_len;
  4385. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  4386. wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
  4387. bss->freq,
  4388. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  4389. } else if (ssid && ssid_len) {
  4390. res.ssid_len = ssid_len;
  4391. os_memcpy(res.ssid, ssid, ssid_len);
  4392. wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
  4393. wpa_ssid_txt(ssid, ssid_len));
  4394. }
  4395. }
  4396. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  4397. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  4398. "starting client");
  4399. wpa_drv_cancel_remain_on_channel(wpa_s);
  4400. wpa_s->off_channel_freq = 0;
  4401. wpa_s->roc_waiting_drv_freq = 0;
  4402. }
  4403. wpas_start_wps_enrollee(group, &res);
  4404. /*
  4405. * Allow a longer timeout for join-a-running-group than normal 15
  4406. * second group formation timeout since the GO may not have authorized
  4407. * our connection yet.
  4408. */
  4409. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  4410. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  4411. wpa_s, NULL);
  4412. return 0;
  4413. }
  4414. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  4415. int *force_freq, int *pref_freq, int go,
  4416. unsigned int *pref_freq_list,
  4417. unsigned int *num_pref_freq)
  4418. {
  4419. struct wpa_used_freq_data *freqs;
  4420. int res, best_freq, num_unused;
  4421. unsigned int freq_in_use = 0, num, i, max_pref_freq;
  4422. max_pref_freq = *num_pref_freq;
  4423. *num_pref_freq = 0;
  4424. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  4425. sizeof(struct wpa_used_freq_data));
  4426. if (!freqs)
  4427. return -1;
  4428. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  4429. wpa_s->num_multichan_concurrent);
  4430. /*
  4431. * It is possible that the total number of used frequencies is bigger
  4432. * than the number of frequencies used for P2P, so get the system wide
  4433. * number of unused frequencies.
  4434. */
  4435. num_unused = wpas_p2p_num_unused_channels(wpa_s);
  4436. wpa_printf(MSG_DEBUG,
  4437. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
  4438. freq, wpa_s->num_multichan_concurrent, num, num_unused);
  4439. if (freq > 0) {
  4440. int ret;
  4441. if (go)
  4442. ret = p2p_supported_freq(wpa_s->global->p2p, freq);
  4443. else
  4444. ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
  4445. if (!ret) {
  4446. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4447. ieee80211_is_dfs(freq)) {
  4448. /*
  4449. * If freq is a DFS channel and DFS is offloaded
  4450. * to the driver, allow P2P GO to use it.
  4451. */
  4452. wpa_printf(MSG_DEBUG,
  4453. "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
  4454. freq);
  4455. } else {
  4456. wpa_printf(MSG_DEBUG,
  4457. "P2P: The forced channel (%u MHz) is not supported for P2P uses",
  4458. freq);
  4459. res = -3;
  4460. goto exit_free;
  4461. }
  4462. }
  4463. for (i = 0; i < num; i++) {
  4464. if (freqs[i].freq == freq)
  4465. freq_in_use = 1;
  4466. }
  4467. if (num_unused <= 0 && !freq_in_use) {
  4468. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  4469. freq);
  4470. res = -2;
  4471. goto exit_free;
  4472. }
  4473. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  4474. "requested channel (%u MHz)", freq);
  4475. *force_freq = freq;
  4476. goto exit_ok;
  4477. }
  4478. best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  4479. if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
  4480. enum wpa_driver_if_type iface_type;
  4481. if (go)
  4482. iface_type = WPA_IF_P2P_GO;
  4483. else
  4484. iface_type = WPA_IF_P2P_CLIENT;
  4485. wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
  4486. best_freq, go);
  4487. res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
  4488. &max_pref_freq,
  4489. pref_freq_list);
  4490. if (!res && max_pref_freq > 0) {
  4491. *num_pref_freq = max_pref_freq;
  4492. i = 0;
  4493. while (i < *num_pref_freq &&
  4494. (!p2p_supported_freq(wpa_s->global->p2p,
  4495. pref_freq_list[i]) ||
  4496. wpas_p2p_disallowed_freq(wpa_s->global,
  4497. pref_freq_list[i]))) {
  4498. wpa_printf(MSG_DEBUG,
  4499. "P2P: preferred_freq_list[%d]=%d is disallowed",
  4500. i, pref_freq_list[i]);
  4501. i++;
  4502. }
  4503. if (i != *num_pref_freq) {
  4504. best_freq = pref_freq_list[i];
  4505. wpa_printf(MSG_DEBUG,
  4506. "P2P: Using preferred_freq_list[%d]=%d",
  4507. i, best_freq);
  4508. } else {
  4509. wpa_printf(MSG_DEBUG,
  4510. "P2P: All driver preferred frequencies are disallowed for P2P use");
  4511. *num_pref_freq = 0;
  4512. }
  4513. } else {
  4514. wpa_printf(MSG_DEBUG,
  4515. "P2P: No preferred frequency list available");
  4516. }
  4517. }
  4518. /* We have a candidate frequency to use */
  4519. if (best_freq > 0) {
  4520. if (*pref_freq == 0 && num_unused > 0) {
  4521. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
  4522. best_freq);
  4523. *pref_freq = best_freq;
  4524. } else {
  4525. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  4526. best_freq);
  4527. *force_freq = best_freq;
  4528. }
  4529. } else if (num_unused > 0) {
  4530. wpa_printf(MSG_DEBUG,
  4531. "P2P: Current operating channels are not available for P2P. Try to use another channel");
  4532. *force_freq = 0;
  4533. } else {
  4534. wpa_printf(MSG_DEBUG,
  4535. "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  4536. res = -2;
  4537. goto exit_free;
  4538. }
  4539. exit_ok:
  4540. res = 0;
  4541. exit_free:
  4542. os_free(freqs);
  4543. return res;
  4544. }
  4545. /**
  4546. * wpas_p2p_connect - Request P2P Group Formation to be started
  4547. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4548. * @peer_addr: Address of the peer P2P Device
  4549. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  4550. * @persistent_group: Whether to create a persistent group
  4551. * @auto_join: Whether to select join vs. GO Negotiation automatically
  4552. * @join: Whether to join an existing group (as a client) instead of starting
  4553. * Group Owner negotiation; @peer_addr is BSSID in that case
  4554. * @auth: Whether to only authorize the connection instead of doing that and
  4555. * initiating Group Owner negotiation
  4556. * @go_intent: GO Intent or -1 to use default
  4557. * @freq: Frequency for the group or 0 for auto-selection
  4558. * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
  4559. * @persistent_id: Persistent group credentials to use for forcing GO
  4560. * parameters or -1 to generate new values (SSID/passphrase)
  4561. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  4562. * interoperability workaround when initiating group formation
  4563. * @ht40: Start GO with 40 MHz channel width
  4564. * @vht: Start GO with VHT support
  4565. * @vht_chwidth: Channel width supported by GO operating with VHT support
  4566. * (VHT_CHANWIDTH_*).
  4567. * @group_ssid: Specific Group SSID for join or %NULL if not set
  4568. * @group_ssid_len: Length of @group_ssid in octets
  4569. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  4570. * failure, -2 on failure due to channel not currently available,
  4571. * -3 if forced channel is not supported
  4572. */
  4573. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4574. const char *pin, enum p2p_wps_method wps_method,
  4575. int persistent_group, int auto_join, int join, int auth,
  4576. int go_intent, int freq, unsigned int vht_center_freq2,
  4577. int persistent_id, int pd, int ht40, int vht,
  4578. unsigned int vht_chwidth, const u8 *group_ssid,
  4579. size_t group_ssid_len)
  4580. {
  4581. int force_freq = 0, pref_freq = 0;
  4582. int ret = 0, res;
  4583. enum wpa_driver_if_type iftype;
  4584. const u8 *if_addr;
  4585. struct wpa_ssid *ssid = NULL;
  4586. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  4587. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4588. return -1;
  4589. if (persistent_id >= 0) {
  4590. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  4591. if (ssid == NULL || ssid->disabled != 2 ||
  4592. ssid->mode != WPAS_MODE_P2P_GO)
  4593. return -1;
  4594. }
  4595. os_free(wpa_s->global->add_psk);
  4596. wpa_s->global->add_psk = NULL;
  4597. wpa_s->global->p2p_fail_on_wps_complete = 0;
  4598. wpa_s->global->pending_p2ps_group = 0;
  4599. wpa_s->global->pending_p2ps_group_freq = 0;
  4600. wpa_s->p2ps_method_config_any = 0;
  4601. if (go_intent < 0)
  4602. go_intent = wpa_s->conf->p2p_go_intent;
  4603. if (!auth)
  4604. wpa_s->p2p_long_listen = 0;
  4605. wpa_s->p2p_wps_method = wps_method;
  4606. wpa_s->p2p_persistent_group = !!persistent_group;
  4607. wpa_s->p2p_persistent_id = persistent_id;
  4608. wpa_s->p2p_go_intent = go_intent;
  4609. wpa_s->p2p_connect_freq = freq;
  4610. wpa_s->p2p_fallback_to_go_neg = 0;
  4611. wpa_s->p2p_pd_before_go_neg = !!pd;
  4612. wpa_s->p2p_go_ht40 = !!ht40;
  4613. wpa_s->p2p_go_vht = !!vht;
  4614. wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
  4615. wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
  4616. if (pin)
  4617. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  4618. else if (wps_method == WPS_PIN_DISPLAY) {
  4619. if (wps_generate_pin((unsigned int *) &ret) < 0)
  4620. return -1;
  4621. res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
  4622. "%08d", ret);
  4623. if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
  4624. wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
  4625. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  4626. wpa_s->p2p_pin);
  4627. } else if (wps_method == WPS_P2PS) {
  4628. /* Force the P2Ps default PIN to be used */
  4629. os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
  4630. } else
  4631. wpa_s->p2p_pin[0] = '\0';
  4632. if (join || auto_join) {
  4633. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  4634. if (auth) {
  4635. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  4636. "connect a running group from " MACSTR,
  4637. MAC2STR(peer_addr));
  4638. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4639. return ret;
  4640. }
  4641. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  4642. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  4643. iface_addr) < 0) {
  4644. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  4645. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  4646. dev_addr);
  4647. }
  4648. if (auto_join) {
  4649. os_get_reltime(&wpa_s->p2p_auto_started);
  4650. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  4651. "%ld.%06ld",
  4652. wpa_s->p2p_auto_started.sec,
  4653. wpa_s->p2p_auto_started.usec);
  4654. }
  4655. wpa_s->user_initiated_pd = 1;
  4656. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  4657. auto_join, freq,
  4658. group_ssid, group_ssid_len) < 0)
  4659. return -1;
  4660. return ret;
  4661. }
  4662. size = P2P_MAX_PREF_CHANNELS;
  4663. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  4664. go_intent == 15, pref_freq_list, &size);
  4665. if (res)
  4666. return res;
  4667. wpas_p2p_set_own_freq_preference(wpa_s,
  4668. force_freq ? force_freq : pref_freq);
  4669. p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
  4670. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  4671. if (wpa_s->create_p2p_iface) {
  4672. /* Prepare to add a new interface for the group */
  4673. iftype = WPA_IF_P2P_GROUP;
  4674. if (go_intent == 15)
  4675. iftype = WPA_IF_P2P_GO;
  4676. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  4677. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  4678. "interface for the group");
  4679. return -1;
  4680. }
  4681. if_addr = wpa_s->pending_interface_addr;
  4682. } else {
  4683. if (wpa_s->p2p_mgmt)
  4684. if_addr = wpa_s->parent->own_addr;
  4685. else
  4686. if_addr = wpa_s->own_addr;
  4687. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  4688. }
  4689. if (auth) {
  4690. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  4691. go_intent, if_addr,
  4692. force_freq, persistent_group, ssid,
  4693. pref_freq) < 0)
  4694. return -1;
  4695. return ret;
  4696. }
  4697. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  4698. go_intent, if_addr, force_freq,
  4699. persistent_group, ssid, pref_freq) < 0) {
  4700. if (wpa_s->create_p2p_iface)
  4701. wpas_p2p_remove_pending_group_interface(wpa_s);
  4702. return -1;
  4703. }
  4704. return ret;
  4705. }
  4706. /**
  4707. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  4708. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4709. * @freq: Frequency of the channel in MHz
  4710. * @duration: Duration of the stay on the channel in milliseconds
  4711. *
  4712. * This callback is called when the driver indicates that it has started the
  4713. * requested remain-on-channel duration.
  4714. */
  4715. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4716. unsigned int freq, unsigned int duration)
  4717. {
  4718. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4719. return;
  4720. wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
  4721. wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
  4722. wpa_s->roc_waiting_drv_freq, freq, duration);
  4723. if (wpa_s->off_channel_freq &&
  4724. wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  4725. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  4726. wpa_s->pending_listen_duration);
  4727. wpa_s->pending_listen_freq = 0;
  4728. } else {
  4729. wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
  4730. wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
  4731. freq, duration);
  4732. }
  4733. }
  4734. int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4735. {
  4736. /* Limit maximum Listen state time based on driver limitation. */
  4737. if (timeout > wpa_s->max_remain_on_chan)
  4738. timeout = wpa_s->max_remain_on_chan;
  4739. return p2p_listen(wpa_s->global->p2p, timeout);
  4740. }
  4741. /**
  4742. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  4743. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4744. * @freq: Frequency of the channel in MHz
  4745. *
  4746. * This callback is called when the driver indicates that a remain-on-channel
  4747. * operation has been completed, i.e., the duration on the requested channel
  4748. * has timed out.
  4749. */
  4750. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4751. unsigned int freq)
  4752. {
  4753. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  4754. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  4755. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  4756. wpas_p2p_listen_work_done(wpa_s);
  4757. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4758. return;
  4759. if (wpa_s->p2p_long_listen > 0)
  4760. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  4761. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  4762. return; /* P2P module started a new operation */
  4763. if (offchannel_pending_action_tx(wpa_s))
  4764. return;
  4765. if (wpa_s->p2p_long_listen > 0) {
  4766. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  4767. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  4768. } else {
  4769. /*
  4770. * When listen duration is over, stop listen & update p2p_state
  4771. * to IDLE.
  4772. */
  4773. p2p_stop_listen(wpa_s->global->p2p);
  4774. }
  4775. }
  4776. /**
  4777. * wpas_p2p_group_remove - Remove a P2P group
  4778. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4779. * @ifname: Network interface name of the group interface or "*" to remove all
  4780. * groups
  4781. * Returns: 0 on success, -1 on failure
  4782. *
  4783. * This function is used to remove a P2P group. This can be used to disconnect
  4784. * from a group in which the local end is a P2P Client or to end a P2P Group in
  4785. * case the local end is the Group Owner. If a virtual network interface was
  4786. * created for this group, that interface will be removed. Otherwise, only the
  4787. * configured P2P group network will be removed from the interface.
  4788. */
  4789. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  4790. {
  4791. struct wpa_global *global = wpa_s->global;
  4792. struct wpa_supplicant *calling_wpa_s = wpa_s;
  4793. if (os_strcmp(ifname, "*") == 0) {
  4794. struct wpa_supplicant *prev;
  4795. wpa_s = global->ifaces;
  4796. while (wpa_s) {
  4797. prev = wpa_s;
  4798. wpa_s = wpa_s->next;
  4799. if (prev->p2p_group_interface !=
  4800. NOT_P2P_GROUP_INTERFACE ||
  4801. (prev->current_ssid &&
  4802. prev->current_ssid->p2p_group))
  4803. wpas_p2p_disconnect_safely(prev, calling_wpa_s);
  4804. }
  4805. return 0;
  4806. }
  4807. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4808. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4809. break;
  4810. }
  4811. return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
  4812. }
  4813. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  4814. {
  4815. unsigned int r;
  4816. if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
  4817. unsigned int i, size = P2P_MAX_PREF_CHANNELS;
  4818. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
  4819. int res;
  4820. res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
  4821. &size, pref_freq_list);
  4822. if (!res && size > 0) {
  4823. i = 0;
  4824. while (i < size &&
  4825. (!p2p_supported_freq(wpa_s->global->p2p,
  4826. pref_freq_list[i]) ||
  4827. wpas_p2p_disallowed_freq(wpa_s->global,
  4828. pref_freq_list[i]))) {
  4829. wpa_printf(MSG_DEBUG,
  4830. "P2P: preferred_freq_list[%d]=%d is disallowed",
  4831. i, pref_freq_list[i]);
  4832. i++;
  4833. }
  4834. if (i != size) {
  4835. freq = pref_freq_list[i];
  4836. wpa_printf(MSG_DEBUG,
  4837. "P2P: Using preferred_freq_list[%d]=%d",
  4838. i, freq);
  4839. } else {
  4840. wpa_printf(MSG_DEBUG,
  4841. "P2P: All driver preferred frequencies are disallowed for P2P use");
  4842. }
  4843. } else {
  4844. wpa_printf(MSG_DEBUG,
  4845. "P2P: No preferred frequency list available");
  4846. }
  4847. }
  4848. if (freq == 2) {
  4849. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  4850. "band");
  4851. if (wpa_s->best_24_freq > 0 &&
  4852. p2p_supported_freq_go(wpa_s->global->p2p,
  4853. wpa_s->best_24_freq)) {
  4854. freq = wpa_s->best_24_freq;
  4855. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  4856. "channel: %d MHz", freq);
  4857. } else {
  4858. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4859. return -1;
  4860. freq = 2412 + (r % 3) * 25;
  4861. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  4862. "channel: %d MHz", freq);
  4863. }
  4864. }
  4865. if (freq == 5) {
  4866. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  4867. "band");
  4868. if (wpa_s->best_5_freq > 0 &&
  4869. p2p_supported_freq_go(wpa_s->global->p2p,
  4870. wpa_s->best_5_freq)) {
  4871. freq = wpa_s->best_5_freq;
  4872. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  4873. "channel: %d MHz", freq);
  4874. } else {
  4875. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4876. return -1;
  4877. freq = 5180 + (r % 4) * 20;
  4878. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4879. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  4880. "5 GHz channel for P2P group");
  4881. return -1;
  4882. }
  4883. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  4884. "channel: %d MHz", freq);
  4885. }
  4886. }
  4887. if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4888. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4889. ieee80211_is_dfs(freq)) {
  4890. /*
  4891. * If freq is a DFS channel and DFS is offloaded to the
  4892. * driver, allow P2P GO to use it.
  4893. */
  4894. wpa_printf(MSG_DEBUG, "P2P: "
  4895. "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
  4896. __func__, freq);
  4897. return freq;
  4898. }
  4899. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  4900. "(%u MHz) is not supported for P2P uses",
  4901. freq);
  4902. return -1;
  4903. }
  4904. return freq;
  4905. }
  4906. static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
  4907. const struct p2p_channels *channels,
  4908. int freq)
  4909. {
  4910. if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
  4911. p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
  4912. freq_included(wpa_s, channels, freq))
  4913. return 1;
  4914. return 0;
  4915. }
  4916. static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
  4917. struct p2p_go_neg_results *params,
  4918. const struct p2p_channels *channels)
  4919. {
  4920. unsigned int i, r;
  4921. /* first try some random selection of the social channels */
  4922. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4923. return;
  4924. for (i = 0; i < 3; i++) {
  4925. params->freq = 2412 + ((r + i) % 3) * 25;
  4926. if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
  4927. goto out;
  4928. }
  4929. /* try all other channels in operating class 81 */
  4930. for (i = 0; i < 11; i++) {
  4931. params->freq = 2412 + i * 5;
  4932. /* skip social channels; covered in the previous loop */
  4933. if (params->freq == 2412 ||
  4934. params->freq == 2437 ||
  4935. params->freq == 2462)
  4936. continue;
  4937. if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
  4938. goto out;
  4939. }
  4940. /* try all channels in operating class 115 */
  4941. for (i = 0; i < 4; i++) {
  4942. params->freq = 5180 + i * 20;
  4943. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4944. freq_included(wpa_s, channels, params->freq) &&
  4945. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4946. goto out;
  4947. }
  4948. /* try all channels in operating class 124 */
  4949. for (i = 0; i < 4; i++) {
  4950. params->freq = 5745 + i * 20;
  4951. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4952. freq_included(wpa_s, channels, params->freq) &&
  4953. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4954. goto out;
  4955. }
  4956. /* try social channel class 180 channel 2 */
  4957. params->freq = 58320 + 1 * 2160;
  4958. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4959. freq_included(wpa_s, channels, params->freq) &&
  4960. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4961. goto out;
  4962. /* try all channels in reg. class 180 */
  4963. for (i = 0; i < 4; i++) {
  4964. params->freq = 58320 + i * 2160;
  4965. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4966. freq_included(wpa_s, channels, params->freq) &&
  4967. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4968. goto out;
  4969. }
  4970. params->freq = 0;
  4971. wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
  4972. return;
  4973. out:
  4974. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
  4975. params->freq);
  4976. }
  4977. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  4978. struct p2p_go_neg_results *params,
  4979. int freq, int vht_center_freq2, int ht40,
  4980. int vht, int max_oper_chwidth,
  4981. const struct p2p_channels *channels)
  4982. {
  4983. struct wpa_used_freq_data *freqs;
  4984. unsigned int cand;
  4985. unsigned int num, i;
  4986. int ignore_no_freqs = 0;
  4987. int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
  4988. os_memset(params, 0, sizeof(*params));
  4989. params->role_go = 1;
  4990. params->ht40 = ht40;
  4991. params->vht = vht;
  4992. params->max_oper_chwidth = max_oper_chwidth;
  4993. params->vht_center_freq2 = vht_center_freq2;
  4994. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  4995. sizeof(struct wpa_used_freq_data));
  4996. if (!freqs)
  4997. return -1;
  4998. num = get_shared_radio_freqs_data(wpa_s, freqs,
  4999. wpa_s->num_multichan_concurrent);
  5000. if (wpa_s->current_ssid &&
  5001. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
  5002. wpa_s->wpa_state == WPA_COMPLETED) {
  5003. wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
  5004. __func__);
  5005. /*
  5006. * If the frequency selection is done for an active P2P GO that
  5007. * is not sharing a frequency, allow to select a new frequency
  5008. * even if there are no unused frequencies as we are about to
  5009. * move the P2P GO so its frequency can be re-used.
  5010. */
  5011. for (i = 0; i < num; i++) {
  5012. if (freqs[i].freq == wpa_s->current_ssid->frequency &&
  5013. freqs[i].flags == 0) {
  5014. ignore_no_freqs = 1;
  5015. break;
  5016. }
  5017. }
  5018. }
  5019. /* try using the forced freq */
  5020. if (freq) {
  5021. if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
  5022. wpa_printf(MSG_DEBUG,
  5023. "P2P: Forced GO freq %d MHz not accepted",
  5024. freq);
  5025. goto fail;
  5026. }
  5027. for (i = 0; i < num; i++) {
  5028. if (freqs[i].freq == freq) {
  5029. wpa_printf(MSG_DEBUG,
  5030. "P2P: forced freq (%d MHz) is also shared",
  5031. freq);
  5032. params->freq = freq;
  5033. goto success;
  5034. }
  5035. }
  5036. if (!ignore_no_freqs && !unused_channels) {
  5037. wpa_printf(MSG_DEBUG,
  5038. "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
  5039. freq);
  5040. goto fail;
  5041. }
  5042. wpa_printf(MSG_DEBUG,
  5043. "P2P: force GO freq (%d MHz) on a free channel",
  5044. freq);
  5045. params->freq = freq;
  5046. goto success;
  5047. }
  5048. /* consider using one of the shared frequencies */
  5049. if (num &&
  5050. (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
  5051. cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  5052. if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  5053. wpa_printf(MSG_DEBUG,
  5054. "P2P: Use shared freq (%d MHz) for GO",
  5055. cand);
  5056. params->freq = cand;
  5057. goto success;
  5058. }
  5059. /* try using one of the shared freqs */
  5060. for (i = 0; i < num; i++) {
  5061. if (wpas_p2p_supported_freq_go(wpa_s, channels,
  5062. freqs[i].freq)) {
  5063. wpa_printf(MSG_DEBUG,
  5064. "P2P: Use shared freq (%d MHz) for GO",
  5065. freqs[i].freq);
  5066. params->freq = freqs[i].freq;
  5067. goto success;
  5068. }
  5069. }
  5070. }
  5071. if (!ignore_no_freqs && !unused_channels) {
  5072. wpa_printf(MSG_DEBUG,
  5073. "P2P: Cannot force GO on any of the channels we are already using");
  5074. goto fail;
  5075. }
  5076. /* try using the setting from the configuration file */
  5077. if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  5078. wpa_s->conf->p2p_oper_channel >= 1 &&
  5079. wpa_s->conf->p2p_oper_channel <= 11 &&
  5080. wpas_p2p_supported_freq_go(
  5081. wpa_s, channels,
  5082. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  5083. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  5084. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  5085. "frequency %d MHz", params->freq);
  5086. goto success;
  5087. }
  5088. if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  5089. wpa_s->conf->p2p_oper_reg_class == 116 ||
  5090. wpa_s->conf->p2p_oper_reg_class == 117 ||
  5091. wpa_s->conf->p2p_oper_reg_class == 124 ||
  5092. wpa_s->conf->p2p_oper_reg_class == 125 ||
  5093. wpa_s->conf->p2p_oper_reg_class == 126 ||
  5094. wpa_s->conf->p2p_oper_reg_class == 127) &&
  5095. wpas_p2p_supported_freq_go(wpa_s, channels,
  5096. 5000 +
  5097. 5 * wpa_s->conf->p2p_oper_channel)) {
  5098. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  5099. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  5100. "frequency %d MHz", params->freq);
  5101. goto success;
  5102. }
  5103. /* Try using best channels */
  5104. if (wpa_s->conf->p2p_oper_channel == 0 &&
  5105. wpa_s->best_overall_freq > 0 &&
  5106. wpas_p2p_supported_freq_go(wpa_s, channels,
  5107. wpa_s->best_overall_freq)) {
  5108. params->freq = wpa_s->best_overall_freq;
  5109. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  5110. "channel %d MHz", params->freq);
  5111. goto success;
  5112. }
  5113. if (wpa_s->conf->p2p_oper_channel == 0 &&
  5114. wpa_s->best_24_freq > 0 &&
  5115. wpas_p2p_supported_freq_go(wpa_s, channels,
  5116. wpa_s->best_24_freq)) {
  5117. params->freq = wpa_s->best_24_freq;
  5118. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  5119. "channel %d MHz", params->freq);
  5120. goto success;
  5121. }
  5122. if (wpa_s->conf->p2p_oper_channel == 0 &&
  5123. wpa_s->best_5_freq > 0 &&
  5124. wpas_p2p_supported_freq_go(wpa_s, channels,
  5125. wpa_s->best_5_freq)) {
  5126. params->freq = wpa_s->best_5_freq;
  5127. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  5128. "channel %d MHz", params->freq);
  5129. goto success;
  5130. }
  5131. /* try using preferred channels */
  5132. cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
  5133. if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  5134. params->freq = cand;
  5135. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  5136. "channels", params->freq);
  5137. goto success;
  5138. }
  5139. /* Try using one of the group common freqs */
  5140. if (wpa_s->p2p_group_common_freqs) {
  5141. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
  5142. cand = wpa_s->p2p_group_common_freqs[i];
  5143. if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
  5144. params->freq = cand;
  5145. wpa_printf(MSG_DEBUG,
  5146. "P2P: Use freq %d MHz common with the peer",
  5147. params->freq);
  5148. goto success;
  5149. }
  5150. }
  5151. }
  5152. /* no preference, select some channel */
  5153. wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
  5154. if (params->freq == 0) {
  5155. wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
  5156. goto fail;
  5157. }
  5158. success:
  5159. os_free(freqs);
  5160. return 0;
  5161. fail:
  5162. os_free(freqs);
  5163. return -1;
  5164. }
  5165. static struct wpa_supplicant *
  5166. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  5167. int go)
  5168. {
  5169. struct wpa_supplicant *group_wpa_s;
  5170. if (!wpas_p2p_create_iface(wpa_s)) {
  5171. if (wpa_s->p2p_mgmt) {
  5172. /*
  5173. * We may be called on the p2p_dev interface which
  5174. * cannot be used for group operations, so always use
  5175. * the primary interface.
  5176. */
  5177. wpa_s->parent->p2pdev = wpa_s;
  5178. wpa_s = wpa_s->parent;
  5179. }
  5180. wpa_dbg(wpa_s, MSG_DEBUG,
  5181. "P2P: Use primary interface for group operations");
  5182. wpa_s->p2p_first_connection_timeout = 0;
  5183. if (wpa_s != wpa_s->p2pdev)
  5184. wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
  5185. return wpa_s;
  5186. }
  5187. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  5188. WPA_IF_P2P_CLIENT) < 0) {
  5189. wpa_msg_global(wpa_s, MSG_ERROR,
  5190. "P2P: Failed to add group interface");
  5191. return NULL;
  5192. }
  5193. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  5194. if (group_wpa_s == NULL) {
  5195. wpa_msg_global(wpa_s, MSG_ERROR,
  5196. "P2P: Failed to initialize group interface");
  5197. wpas_p2p_remove_pending_group_interface(wpa_s);
  5198. return NULL;
  5199. }
  5200. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  5201. group_wpa_s->ifname);
  5202. group_wpa_s->p2p_first_connection_timeout = 0;
  5203. return group_wpa_s;
  5204. }
  5205. /**
  5206. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  5207. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  5208. * @persistent_group: Whether to create a persistent group
  5209. * @freq: Frequency for the group or 0 to indicate no hardcoding
  5210. * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
  5211. * @ht40: Start GO with 40 MHz channel width
  5212. * @vht: Start GO with VHT support
  5213. * @vht_chwidth: channel bandwidth for GO operating with VHT support
  5214. * Returns: 0 on success, -1 on failure
  5215. *
  5216. * This function creates a new P2P group with the local end as the Group Owner,
  5217. * i.e., without using Group Owner Negotiation.
  5218. */
  5219. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  5220. int freq, int vht_center_freq2, int ht40, int vht,
  5221. int max_oper_chwidth)
  5222. {
  5223. struct p2p_go_neg_results params;
  5224. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5225. return -1;
  5226. os_free(wpa_s->global->add_psk);
  5227. wpa_s->global->add_psk = NULL;
  5228. /* Make sure we are not running find during connection establishment */
  5229. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  5230. wpas_p2p_stop_find_oper(wpa_s);
  5231. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  5232. if (freq < 0)
  5233. return -1;
  5234. if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
  5235. ht40, vht, max_oper_chwidth, NULL))
  5236. return -1;
  5237. if (params.freq &&
  5238. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  5239. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  5240. ieee80211_is_dfs(params.freq)) {
  5241. /*
  5242. * If freq is a DFS channel and DFS is offloaded to the
  5243. * driver, allow P2P GO to use it.
  5244. */
  5245. wpa_printf(MSG_DEBUG,
  5246. "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
  5247. __func__, params.freq);
  5248. } else {
  5249. wpa_printf(MSG_DEBUG,
  5250. "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
  5251. params.freq);
  5252. return -1;
  5253. }
  5254. }
  5255. p2p_go_params(wpa_s->global->p2p, &params);
  5256. params.persistent_group = persistent_group;
  5257. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  5258. if (wpa_s == NULL)
  5259. return -1;
  5260. wpas_start_wps_go(wpa_s, &params, 0);
  5261. return 0;
  5262. }
  5263. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  5264. struct wpa_ssid *params, int addr_allocated,
  5265. int freq, int force_scan)
  5266. {
  5267. struct wpa_ssid *ssid;
  5268. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  5269. if (wpa_s == NULL)
  5270. return -1;
  5271. if (force_scan)
  5272. os_get_reltime(&wpa_s->scan_min_time);
  5273. wpa_s->p2p_last_4way_hs_fail = NULL;
  5274. wpa_supplicant_ap_deinit(wpa_s);
  5275. ssid = wpa_config_add_network(wpa_s->conf);
  5276. if (ssid == NULL)
  5277. return -1;
  5278. os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
  5279. wpa_config_set_network_defaults(ssid);
  5280. ssid->temporary = 1;
  5281. ssid->proto = WPA_PROTO_RSN;
  5282. ssid->pbss = params->pbss;
  5283. ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
  5284. WPA_CIPHER_CCMP;
  5285. ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
  5286. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  5287. ssid->ssid = os_malloc(params->ssid_len);
  5288. if (ssid->ssid == NULL) {
  5289. wpa_config_remove_network(wpa_s->conf, ssid->id);
  5290. return -1;
  5291. }
  5292. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  5293. ssid->ssid_len = params->ssid_len;
  5294. ssid->p2p_group = 1;
  5295. ssid->export_keys = 1;
  5296. if (params->psk_set) {
  5297. os_memcpy(ssid->psk, params->psk, 32);
  5298. ssid->psk_set = 1;
  5299. }
  5300. if (params->passphrase)
  5301. ssid->passphrase = os_strdup(params->passphrase);
  5302. wpa_s->show_group_started = 1;
  5303. wpa_s->p2p_in_invitation = 1;
  5304. wpa_s->p2p_invite_go_freq = freq;
  5305. wpa_s->p2p_go_group_formation_completed = 0;
  5306. wpa_s->global->p2p_group_formation = wpa_s;
  5307. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
  5308. NULL);
  5309. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5310. wpas_p2p_group_formation_timeout,
  5311. wpa_s->p2pdev, NULL);
  5312. wpa_supplicant_select_network(wpa_s, ssid);
  5313. return 0;
  5314. }
  5315. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  5316. struct wpa_ssid *ssid, int addr_allocated,
  5317. int force_freq, int neg_freq,
  5318. int vht_center_freq2, int ht40,
  5319. int vht, int max_oper_chwidth,
  5320. const struct p2p_channels *channels,
  5321. int connection_timeout, int force_scan)
  5322. {
  5323. struct p2p_go_neg_results params;
  5324. int go = 0, freq;
  5325. if (ssid->disabled != 2 || ssid->ssid == NULL)
  5326. return -1;
  5327. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  5328. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  5329. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  5330. "already running");
  5331. if (go == 0 &&
  5332. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5333. wpa_s->p2pdev, NULL)) {
  5334. /*
  5335. * This can happen if Invitation Response frame was lost
  5336. * and the peer (GO of a persistent group) tries to
  5337. * invite us again. Reschedule the timeout to avoid
  5338. * terminating the wait for the connection too early
  5339. * since we now know that the peer is still trying to
  5340. * invite us instead of having already started the GO.
  5341. */
  5342. wpa_printf(MSG_DEBUG,
  5343. "P2P: Reschedule group formation timeout since peer is still trying to invite us");
  5344. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5345. wpas_p2p_group_formation_timeout,
  5346. wpa_s->p2pdev, NULL);
  5347. }
  5348. return 0;
  5349. }
  5350. os_free(wpa_s->global->add_psk);
  5351. wpa_s->global->add_psk = NULL;
  5352. /* Make sure we are not running find during connection establishment */
  5353. wpas_p2p_stop_find_oper(wpa_s);
  5354. wpa_s->p2p_fallback_to_go_neg = 0;
  5355. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5356. if (force_freq > 0) {
  5357. freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
  5358. if (freq < 0)
  5359. return -1;
  5360. } else {
  5361. freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
  5362. if (freq < 0 ||
  5363. (freq > 0 && !freq_included(wpa_s, channels, freq)))
  5364. freq = 0;
  5365. }
  5366. } else if (ssid->mode == WPAS_MODE_INFRA) {
  5367. freq = neg_freq;
  5368. if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
  5369. struct os_reltime now;
  5370. struct wpa_bss *bss =
  5371. wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
  5372. os_get_reltime(&now);
  5373. if (bss &&
  5374. !os_reltime_expired(&now, &bss->last_update, 5) &&
  5375. freq_included(wpa_s, channels, bss->freq))
  5376. freq = bss->freq;
  5377. else
  5378. freq = 0;
  5379. }
  5380. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
  5381. force_scan);
  5382. } else {
  5383. return -1;
  5384. }
  5385. if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
  5386. ht40, vht, max_oper_chwidth, channels))
  5387. return -1;
  5388. params.role_go = 1;
  5389. params.psk_set = ssid->psk_set;
  5390. if (params.psk_set)
  5391. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  5392. if (ssid->passphrase) {
  5393. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  5394. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  5395. "persistent group");
  5396. return -1;
  5397. }
  5398. os_strlcpy(params.passphrase, ssid->passphrase,
  5399. sizeof(params.passphrase));
  5400. }
  5401. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  5402. params.ssid_len = ssid->ssid_len;
  5403. params.persistent_group = 1;
  5404. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  5405. if (wpa_s == NULL)
  5406. return -1;
  5407. p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
  5408. wpa_s->p2p_first_connection_timeout = connection_timeout;
  5409. wpas_start_wps_go(wpa_s, &params, 0);
  5410. return 0;
  5411. }
  5412. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  5413. struct wpabuf *proberesp_ies)
  5414. {
  5415. struct wpa_supplicant *wpa_s = ctx;
  5416. if (wpa_s->ap_iface) {
  5417. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  5418. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  5419. wpabuf_free(beacon_ies);
  5420. wpabuf_free(proberesp_ies);
  5421. return;
  5422. }
  5423. if (beacon_ies) {
  5424. wpabuf_free(hapd->p2p_beacon_ie);
  5425. hapd->p2p_beacon_ie = beacon_ies;
  5426. }
  5427. wpabuf_free(hapd->p2p_probe_resp_ie);
  5428. hapd->p2p_probe_resp_ie = proberesp_ies;
  5429. } else {
  5430. wpabuf_free(beacon_ies);
  5431. wpabuf_free(proberesp_ies);
  5432. }
  5433. wpa_supplicant_ap_update_beacon(wpa_s);
  5434. }
  5435. static void wpas_p2p_idle_update(void *ctx, int idle)
  5436. {
  5437. struct wpa_supplicant *wpa_s = ctx;
  5438. if (!wpa_s->ap_iface)
  5439. return;
  5440. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  5441. if (idle) {
  5442. if (wpa_s->global->p2p_fail_on_wps_complete &&
  5443. wpa_s->p2p_in_provisioning) {
  5444. wpas_p2p_grpform_fail_after_wps(wpa_s);
  5445. return;
  5446. }
  5447. wpas_p2p_set_group_idle_timeout(wpa_s);
  5448. } else
  5449. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  5450. }
  5451. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  5452. struct wpa_ssid *ssid)
  5453. {
  5454. struct p2p_group *group;
  5455. struct p2p_group_config *cfg;
  5456. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  5457. !ssid->p2p_group)
  5458. return NULL;
  5459. cfg = os_zalloc(sizeof(*cfg));
  5460. if (cfg == NULL)
  5461. return NULL;
  5462. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  5463. cfg->persistent_group = 2;
  5464. else if (ssid->p2p_persistent_group)
  5465. cfg->persistent_group = 1;
  5466. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  5467. if (wpa_s->max_stations &&
  5468. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  5469. cfg->max_clients = wpa_s->max_stations;
  5470. else
  5471. cfg->max_clients = wpa_s->conf->max_num_sta;
  5472. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  5473. cfg->ssid_len = ssid->ssid_len;
  5474. cfg->freq = ssid->frequency;
  5475. cfg->cb_ctx = wpa_s;
  5476. cfg->ie_update = wpas_p2p_ie_update;
  5477. cfg->idle_update = wpas_p2p_idle_update;
  5478. cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
  5479. != 0;
  5480. group = p2p_group_init(wpa_s->global->p2p, cfg);
  5481. if (group == NULL)
  5482. os_free(cfg);
  5483. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  5484. p2p_group_notif_formation_done(group);
  5485. wpa_s->p2p_group = group;
  5486. return group;
  5487. }
  5488. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5489. int registrar)
  5490. {
  5491. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5492. if (!wpa_s->p2p_in_provisioning) {
  5493. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  5494. "provisioning not in progress");
  5495. return;
  5496. }
  5497. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  5498. u8 go_dev_addr[ETH_ALEN];
  5499. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  5500. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  5501. ssid->ssid_len);
  5502. /* Clear any stored provisioning info */
  5503. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  5504. }
  5505. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
  5506. NULL);
  5507. wpa_s->p2p_go_group_formation_completed = 1;
  5508. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  5509. /*
  5510. * Use a separate timeout for initial data connection to
  5511. * complete to allow the group to be removed automatically if
  5512. * something goes wrong in this step before the P2P group idle
  5513. * timeout mechanism is taken into use.
  5514. */
  5515. wpa_dbg(wpa_s, MSG_DEBUG,
  5516. "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
  5517. P2P_MAX_INITIAL_CONN_WAIT);
  5518. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  5519. wpas_p2p_group_formation_timeout,
  5520. wpa_s->p2pdev, NULL);
  5521. /* Complete group formation on successful data connection. */
  5522. wpa_s->p2p_go_group_formation_completed = 0;
  5523. } else if (ssid) {
  5524. /*
  5525. * Use a separate timeout for initial data connection to
  5526. * complete to allow the group to be removed automatically if
  5527. * the client does not complete data connection successfully.
  5528. */
  5529. wpa_dbg(wpa_s, MSG_DEBUG,
  5530. "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
  5531. P2P_MAX_INITIAL_CONN_WAIT_GO);
  5532. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
  5533. wpas_p2p_group_formation_timeout,
  5534. wpa_s->p2pdev, NULL);
  5535. /*
  5536. * Complete group formation on first successful data connection
  5537. */
  5538. wpa_s->p2p_go_group_formation_completed = 0;
  5539. }
  5540. if (wpa_s->global->p2p)
  5541. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  5542. wpas_group_formation_completed(wpa_s, 1, 0);
  5543. }
  5544. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  5545. struct wps_event_fail *fail)
  5546. {
  5547. if (!wpa_s->p2p_in_provisioning) {
  5548. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  5549. "provisioning not in progress");
  5550. return;
  5551. }
  5552. if (wpa_s->go_params) {
  5553. p2p_clear_provisioning_info(
  5554. wpa_s->global->p2p,
  5555. wpa_s->go_params->peer_device_addr);
  5556. }
  5557. wpas_notify_p2p_wps_failed(wpa_s, fail);
  5558. if (wpa_s == wpa_s->global->p2p_group_formation) {
  5559. /*
  5560. * Allow some time for the failed WPS negotiation exchange to
  5561. * complete, but remove the group since group formation cannot
  5562. * succeed after provisioning failure.
  5563. */
  5564. wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
  5565. wpa_s->global->p2p_fail_on_wps_complete = 1;
  5566. eloop_deplete_timeout(0, 50000,
  5567. wpas_p2p_group_formation_timeout,
  5568. wpa_s->p2pdev, NULL);
  5569. }
  5570. }
  5571. int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  5572. {
  5573. if (!wpa_s->global->p2p_fail_on_wps_complete ||
  5574. !wpa_s->p2p_in_provisioning)
  5575. return 0;
  5576. wpas_p2p_grpform_fail_after_wps(wpa_s);
  5577. return 1;
  5578. }
  5579. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5580. const char *config_method,
  5581. enum wpas_p2p_prov_disc_use use,
  5582. struct p2ps_provision *p2ps_prov)
  5583. {
  5584. u16 config_methods;
  5585. wpa_s->global->pending_p2ps_group = 0;
  5586. wpa_s->global->pending_p2ps_group_freq = 0;
  5587. wpa_s->p2p_fallback_to_go_neg = 0;
  5588. wpa_s->pending_pd_use = NORMAL_PD;
  5589. if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
  5590. p2ps_prov->conncap = p2ps_group_capability(
  5591. wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
  5592. &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
  5593. wpa_printf(MSG_DEBUG,
  5594. "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
  5595. __func__, p2ps_prov->conncap,
  5596. p2ps_prov->adv_id, p2ps_prov->conncap,
  5597. p2ps_prov->status, p2ps_prov->info);
  5598. config_methods = 0;
  5599. } else if (os_strncmp(config_method, "display", 7) == 0)
  5600. config_methods = WPS_CONFIG_DISPLAY;
  5601. else if (os_strncmp(config_method, "keypad", 6) == 0)
  5602. config_methods = WPS_CONFIG_KEYPAD;
  5603. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  5604. os_strncmp(config_method, "pushbutton", 10) == 0)
  5605. config_methods = WPS_CONFIG_PUSHBUTTON;
  5606. else {
  5607. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  5608. os_free(p2ps_prov);
  5609. return -1;
  5610. }
  5611. if (use == WPAS_P2P_PD_AUTO) {
  5612. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  5613. wpa_s->pending_pd_config_methods = config_methods;
  5614. wpa_s->p2p_auto_pd = 1;
  5615. wpa_s->p2p_auto_join = 0;
  5616. wpa_s->pending_pd_before_join = 0;
  5617. wpa_s->auto_pd_scan_retry = 0;
  5618. wpas_p2p_stop_find(wpa_s);
  5619. wpa_s->p2p_join_scan_count = 0;
  5620. os_get_reltime(&wpa_s->p2p_auto_started);
  5621. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  5622. wpa_s->p2p_auto_started.sec,
  5623. wpa_s->p2p_auto_started.usec);
  5624. wpas_p2p_join_scan(wpa_s, NULL);
  5625. return 0;
  5626. }
  5627. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
  5628. os_free(p2ps_prov);
  5629. return -1;
  5630. }
  5631. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
  5632. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  5633. 0, 1);
  5634. }
  5635. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  5636. char *end)
  5637. {
  5638. return p2p_scan_result_text(ies, ies_len, buf, end);
  5639. }
  5640. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  5641. {
  5642. if (!offchannel_pending_action_tx(wpa_s))
  5643. return;
  5644. if (wpa_s->p2p_send_action_work) {
  5645. wpas_p2p_free_send_action_work(wpa_s);
  5646. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  5647. wpa_s, NULL);
  5648. offchannel_send_action_done(wpa_s);
  5649. }
  5650. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  5651. "operation request");
  5652. offchannel_clear_pending_action_tx(wpa_s);
  5653. }
  5654. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  5655. enum p2p_discovery_type type,
  5656. unsigned int num_req_dev_types, const u8 *req_dev_types,
  5657. const u8 *dev_id, unsigned int search_delay,
  5658. u8 seek_cnt, const char **seek_string, int freq)
  5659. {
  5660. wpas_p2p_clear_pending_action_tx(wpa_s);
  5661. wpa_s->p2p_long_listen = 0;
  5662. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  5663. wpa_s->p2p_in_provisioning)
  5664. return -1;
  5665. wpa_supplicant_cancel_sched_scan(wpa_s);
  5666. return p2p_find(wpa_s->global->p2p, timeout, type,
  5667. num_req_dev_types, req_dev_types, dev_id,
  5668. search_delay, seek_cnt, seek_string, freq);
  5669. }
  5670. static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
  5671. struct wpa_scan_results *scan_res)
  5672. {
  5673. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  5674. if (wpa_s->p2p_scan_work) {
  5675. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  5676. wpa_s->p2p_scan_work = NULL;
  5677. radio_work_done(work);
  5678. }
  5679. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5680. return;
  5681. /*
  5682. * Indicate that results have been processed so that the P2P module can
  5683. * continue pending tasks.
  5684. */
  5685. p2p_scan_res_handled(wpa_s->global->p2p);
  5686. }
  5687. static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  5688. {
  5689. wpas_p2p_clear_pending_action_tx(wpa_s);
  5690. wpa_s->p2p_long_listen = 0;
  5691. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  5692. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  5693. if (wpa_s->global->p2p)
  5694. p2p_stop_find(wpa_s->global->p2p);
  5695. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
  5696. wpa_printf(MSG_DEBUG,
  5697. "P2P: Do not consider the scan results after stop_find");
  5698. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
  5699. }
  5700. }
  5701. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  5702. {
  5703. wpas_p2p_stop_find_oper(wpa_s);
  5704. if (!wpa_s->global->pending_group_iface_for_p2ps)
  5705. wpas_p2p_remove_pending_group_interface(wpa_s);
  5706. }
  5707. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  5708. {
  5709. struct wpa_supplicant *wpa_s = eloop_ctx;
  5710. wpa_s->p2p_long_listen = 0;
  5711. }
  5712. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  5713. {
  5714. int res;
  5715. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5716. return -1;
  5717. if (wpa_s->p2p_lo_started) {
  5718. wpa_printf(MSG_DEBUG,
  5719. "P2P: Cannot start P2P listen, it is offloaded");
  5720. return -1;
  5721. }
  5722. wpa_supplicant_cancel_sched_scan(wpa_s);
  5723. wpas_p2p_clear_pending_action_tx(wpa_s);
  5724. if (timeout == 0) {
  5725. /*
  5726. * This is a request for unlimited Listen state. However, at
  5727. * least for now, this is mapped to a Listen state for one
  5728. * hour.
  5729. */
  5730. timeout = 3600;
  5731. }
  5732. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  5733. wpa_s->p2p_long_listen = 0;
  5734. /*
  5735. * Stop previous find/listen operation to avoid trying to request a new
  5736. * remain-on-channel operation while the driver is still running the
  5737. * previous one.
  5738. */
  5739. if (wpa_s->global->p2p)
  5740. p2p_stop_find(wpa_s->global->p2p);
  5741. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  5742. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  5743. wpa_s->p2p_long_listen = timeout * 1000;
  5744. eloop_register_timeout(timeout, 0,
  5745. wpas_p2p_long_listen_timeout,
  5746. wpa_s, NULL);
  5747. }
  5748. return res;
  5749. }
  5750. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  5751. u8 *buf, size_t len, int p2p_group)
  5752. {
  5753. struct wpabuf *p2p_ie;
  5754. int ret;
  5755. if (wpa_s->global->p2p_disabled)
  5756. return -1;
  5757. /*
  5758. * Advertize mandatory cross connection capability even on
  5759. * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
  5760. */
  5761. if (wpa_s->conf->p2p_disabled && p2p_group)
  5762. return -1;
  5763. if (wpa_s->global->p2p == NULL)
  5764. return -1;
  5765. if (bss == NULL)
  5766. return -1;
  5767. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  5768. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  5769. p2p_group, p2p_ie);
  5770. wpabuf_free(p2p_ie);
  5771. return ret;
  5772. }
  5773. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  5774. const u8 *dst, const u8 *bssid,
  5775. const u8 *ie, size_t ie_len,
  5776. unsigned int rx_freq, int ssi_signal)
  5777. {
  5778. if (wpa_s->global->p2p_disabled)
  5779. return 0;
  5780. if (wpa_s->global->p2p == NULL)
  5781. return 0;
  5782. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  5783. ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
  5784. case P2P_PREQ_NOT_P2P:
  5785. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  5786. ssi_signal);
  5787. /* fall through */
  5788. case P2P_PREQ_MALFORMED:
  5789. case P2P_PREQ_NOT_LISTEN:
  5790. case P2P_PREQ_NOT_PROCESSED:
  5791. default: /* make gcc happy */
  5792. return 0;
  5793. case P2P_PREQ_PROCESSED:
  5794. return 1;
  5795. }
  5796. }
  5797. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  5798. const u8 *sa, const u8 *bssid,
  5799. u8 category, const u8 *data, size_t len, int freq)
  5800. {
  5801. if (wpa_s->global->p2p_disabled)
  5802. return;
  5803. if (wpa_s->global->p2p == NULL)
  5804. return;
  5805. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  5806. freq);
  5807. }
  5808. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  5809. {
  5810. unsigned int bands;
  5811. if (wpa_s->global->p2p_disabled)
  5812. return;
  5813. if (wpa_s->global->p2p == NULL)
  5814. return;
  5815. bands = wpas_get_bands(wpa_s, NULL);
  5816. p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
  5817. }
  5818. static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  5819. {
  5820. p2p_group_deinit(wpa_s->p2p_group);
  5821. wpa_s->p2p_group = NULL;
  5822. wpa_s->ap_configured_cb = NULL;
  5823. wpa_s->ap_configured_cb_ctx = NULL;
  5824. wpa_s->ap_configured_cb_data = NULL;
  5825. wpa_s->connect_without_scan = NULL;
  5826. }
  5827. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  5828. {
  5829. wpa_s->p2p_long_listen = 0;
  5830. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5831. return -1;
  5832. return p2p_reject(wpa_s->global->p2p, addr);
  5833. }
  5834. /* Invite to reinvoke a persistent group */
  5835. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5836. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  5837. int vht_center_freq2, int ht40, int vht, int max_chwidth,
  5838. int pref_freq)
  5839. {
  5840. enum p2p_invite_role role;
  5841. u8 *bssid = NULL;
  5842. int force_freq = 0;
  5843. int res;
  5844. int no_pref_freq_given = pref_freq == 0;
  5845. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  5846. wpa_s->global->p2p_invite_group = NULL;
  5847. if (peer_addr)
  5848. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  5849. else
  5850. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5851. wpa_s->p2p_persistent_go_freq = freq;
  5852. wpa_s->p2p_go_ht40 = !!ht40;
  5853. wpa_s->p2p_go_vht = !!vht;
  5854. wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
  5855. wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
  5856. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5857. role = P2P_INVITE_ROLE_GO;
  5858. if (peer_addr == NULL) {
  5859. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  5860. "address in invitation command");
  5861. return -1;
  5862. }
  5863. if (wpas_p2p_create_iface(wpa_s)) {
  5864. if (wpas_p2p_add_group_interface(wpa_s,
  5865. WPA_IF_P2P_GO) < 0) {
  5866. wpa_printf(MSG_ERROR, "P2P: Failed to "
  5867. "allocate a new interface for the "
  5868. "group");
  5869. return -1;
  5870. }
  5871. bssid = wpa_s->pending_interface_addr;
  5872. } else if (wpa_s->p2p_mgmt)
  5873. bssid = wpa_s->parent->own_addr;
  5874. else
  5875. bssid = wpa_s->own_addr;
  5876. } else {
  5877. role = P2P_INVITE_ROLE_CLIENT;
  5878. peer_addr = ssid->bssid;
  5879. }
  5880. wpa_s->pending_invite_ssid_id = ssid->id;
  5881. size = P2P_MAX_PREF_CHANNELS;
  5882. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  5883. role == P2P_INVITE_ROLE_GO,
  5884. pref_freq_list, &size);
  5885. if (res)
  5886. return res;
  5887. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5888. return -1;
  5889. p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
  5890. if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
  5891. no_pref_freq_given && pref_freq > 0 &&
  5892. wpa_s->num_multichan_concurrent > 1 &&
  5893. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  5894. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
  5895. pref_freq);
  5896. pref_freq = 0;
  5897. }
  5898. /*
  5899. * Stop any find/listen operations before invitation and possibly
  5900. * connection establishment.
  5901. */
  5902. wpas_p2p_stop_find_oper(wpa_s);
  5903. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  5904. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  5905. 1, pref_freq, -1);
  5906. }
  5907. /* Invite to join an active group */
  5908. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  5909. const u8 *peer_addr, const u8 *go_dev_addr)
  5910. {
  5911. struct wpa_global *global = wpa_s->global;
  5912. enum p2p_invite_role role;
  5913. u8 *bssid = NULL;
  5914. struct wpa_ssid *ssid;
  5915. int persistent;
  5916. int freq = 0, force_freq = 0, pref_freq = 0;
  5917. int res;
  5918. unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
  5919. wpa_s->p2p_persistent_go_freq = 0;
  5920. wpa_s->p2p_go_ht40 = 0;
  5921. wpa_s->p2p_go_vht = 0;
  5922. wpa_s->p2p_go_vht_center_freq2 = 0;
  5923. wpa_s->p2p_go_max_oper_chwidth = 0;
  5924. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5925. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  5926. break;
  5927. }
  5928. if (wpa_s == NULL) {
  5929. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  5930. return -1;
  5931. }
  5932. ssid = wpa_s->current_ssid;
  5933. if (ssid == NULL) {
  5934. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  5935. "invitation");
  5936. return -1;
  5937. }
  5938. wpa_s->global->p2p_invite_group = wpa_s;
  5939. persistent = ssid->p2p_persistent_group &&
  5940. wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
  5941. ssid->ssid, ssid->ssid_len);
  5942. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5943. role = P2P_INVITE_ROLE_ACTIVE_GO;
  5944. bssid = wpa_s->own_addr;
  5945. if (go_dev_addr == NULL)
  5946. go_dev_addr = wpa_s->global->p2p_dev_addr;
  5947. freq = ssid->frequency;
  5948. } else {
  5949. role = P2P_INVITE_ROLE_CLIENT;
  5950. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  5951. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  5952. "invite to current group");
  5953. return -1;
  5954. }
  5955. bssid = wpa_s->bssid;
  5956. if (go_dev_addr == NULL &&
  5957. !is_zero_ether_addr(wpa_s->go_dev_addr))
  5958. go_dev_addr = wpa_s->go_dev_addr;
  5959. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  5960. (int) wpa_s->assoc_freq;
  5961. }
  5962. wpa_s->p2pdev->pending_invite_ssid_id = -1;
  5963. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5964. return -1;
  5965. size = P2P_MAX_PREF_CHANNELS;
  5966. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  5967. role == P2P_INVITE_ROLE_ACTIVE_GO,
  5968. pref_freq_list, &size);
  5969. if (res)
  5970. return res;
  5971. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  5972. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  5973. ssid->ssid, ssid->ssid_len, force_freq,
  5974. go_dev_addr, persistent, pref_freq, -1);
  5975. }
  5976. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  5977. {
  5978. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5979. u8 go_dev_addr[ETH_ALEN];
  5980. int persistent;
  5981. int freq;
  5982. u8 ip[3 * 4];
  5983. char ip_addr[100];
  5984. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  5985. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5986. wpa_s->p2pdev, NULL);
  5987. }
  5988. if (!wpa_s->show_group_started || !ssid)
  5989. return;
  5990. wpa_s->show_group_started = 0;
  5991. if (!wpa_s->p2p_go_group_formation_completed &&
  5992. wpa_s->global->p2p_group_formation == wpa_s) {
  5993. wpa_dbg(wpa_s, MSG_DEBUG,
  5994. "P2P: Marking group formation completed on client on data connection");
  5995. wpa_s->p2p_go_group_formation_completed = 1;
  5996. wpa_s->global->p2p_group_formation = NULL;
  5997. wpa_s->p2p_in_provisioning = 0;
  5998. wpa_s->p2p_in_invitation = 0;
  5999. }
  6000. os_memset(go_dev_addr, 0, ETH_ALEN);
  6001. if (ssid->bssid_set)
  6002. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  6003. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  6004. ssid->ssid_len);
  6005. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  6006. if (wpa_s->global->p2p_group_formation == wpa_s)
  6007. wpa_s->global->p2p_group_formation = NULL;
  6008. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  6009. (int) wpa_s->assoc_freq;
  6010. ip_addr[0] = '\0';
  6011. if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
  6012. int res;
  6013. res = os_snprintf(ip_addr, sizeof(ip_addr),
  6014. " ip_addr=%u.%u.%u.%u "
  6015. "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
  6016. ip[0], ip[1], ip[2], ip[3],
  6017. ip[4], ip[5], ip[6], ip[7],
  6018. ip[8], ip[9], ip[10], ip[11]);
  6019. if (os_snprintf_error(sizeof(ip_addr), res))
  6020. ip_addr[0] = '\0';
  6021. }
  6022. wpas_p2p_group_started(wpa_s, 0, ssid, freq,
  6023. ssid->passphrase == NULL && ssid->psk_set ?
  6024. ssid->psk : NULL,
  6025. ssid->passphrase, go_dev_addr, persistent,
  6026. ip_addr);
  6027. if (persistent)
  6028. wpas_p2p_store_persistent_group(wpa_s->p2pdev,
  6029. ssid, go_dev_addr);
  6030. wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
  6031. }
  6032. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  6033. u32 interval1, u32 duration2, u32 interval2)
  6034. {
  6035. int ret;
  6036. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6037. return -1;
  6038. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  6039. wpa_s->current_ssid == NULL ||
  6040. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  6041. return -1;
  6042. ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  6043. wpa_s->own_addr, wpa_s->assoc_freq,
  6044. duration1, interval1, duration2, interval2);
  6045. if (ret == 0)
  6046. wpa_s->waiting_presence_resp = 1;
  6047. return ret;
  6048. }
  6049. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  6050. unsigned int interval)
  6051. {
  6052. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6053. return -1;
  6054. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  6055. }
  6056. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  6057. {
  6058. if (wpa_s->current_ssid == NULL) {
  6059. /*
  6060. * current_ssid can be cleared when P2P client interface gets
  6061. * disconnected, so assume this interface was used as P2P
  6062. * client.
  6063. */
  6064. return 1;
  6065. }
  6066. return wpa_s->current_ssid->p2p_group &&
  6067. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  6068. }
  6069. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  6070. {
  6071. struct wpa_supplicant *wpa_s = eloop_ctx;
  6072. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  6073. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  6074. "disabled");
  6075. return;
  6076. }
  6077. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  6078. "group");
  6079. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  6080. }
  6081. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  6082. {
  6083. int timeout;
  6084. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  6085. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  6086. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  6087. return;
  6088. timeout = wpa_s->conf->p2p_group_idle;
  6089. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  6090. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  6091. timeout = P2P_MAX_CLIENT_IDLE;
  6092. if (timeout == 0)
  6093. return;
  6094. if (timeout < 0) {
  6095. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  6096. timeout = 0; /* special client mode no-timeout */
  6097. else
  6098. return;
  6099. }
  6100. if (wpa_s->p2p_in_provisioning) {
  6101. /*
  6102. * Use the normal group formation timeout during the
  6103. * provisioning phase to avoid terminating this process too
  6104. * early due to group idle timeout.
  6105. */
  6106. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  6107. "during provisioning");
  6108. return;
  6109. }
  6110. if (wpa_s->show_group_started) {
  6111. /*
  6112. * Use the normal group formation timeout between the end of
  6113. * the provisioning phase and completion of 4-way handshake to
  6114. * avoid terminating this process too early due to group idle
  6115. * timeout.
  6116. */
  6117. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  6118. "while waiting for initial 4-way handshake to "
  6119. "complete");
  6120. return;
  6121. }
  6122. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  6123. timeout);
  6124. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  6125. wpa_s, NULL);
  6126. }
  6127. /* Returns 1 if the interface was removed */
  6128. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  6129. u16 reason_code, const u8 *ie, size_t ie_len,
  6130. int locally_generated)
  6131. {
  6132. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6133. return 0;
  6134. if (!locally_generated)
  6135. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  6136. ie_len);
  6137. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  6138. wpa_s->current_ssid &&
  6139. wpa_s->current_ssid->p2p_group &&
  6140. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  6141. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  6142. "session is ending");
  6143. if (wpas_p2p_group_delete(wpa_s,
  6144. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  6145. > 0)
  6146. return 1;
  6147. }
  6148. return 0;
  6149. }
  6150. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  6151. u16 reason_code, const u8 *ie, size_t ie_len,
  6152. int locally_generated)
  6153. {
  6154. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6155. return;
  6156. if (!locally_generated)
  6157. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  6158. ie_len);
  6159. }
  6160. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  6161. {
  6162. struct p2p_data *p2p = wpa_s->global->p2p;
  6163. if (p2p == NULL)
  6164. return;
  6165. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  6166. return;
  6167. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  6168. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  6169. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  6170. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  6171. if (wpa_s->wps &&
  6172. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  6173. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  6174. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  6175. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  6176. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  6177. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  6178. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  6179. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  6180. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  6181. }
  6182. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  6183. p2p_set_sec_dev_types(p2p,
  6184. (void *) wpa_s->conf->sec_device_type,
  6185. wpa_s->conf->num_sec_device_types);
  6186. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  6187. int i;
  6188. p2p_remove_wps_vendor_extensions(p2p);
  6189. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  6190. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  6191. continue;
  6192. p2p_add_wps_vendor_extension(
  6193. p2p, wpa_s->conf->wps_vendor_ext[i]);
  6194. }
  6195. }
  6196. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  6197. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  6198. char country[3];
  6199. country[0] = wpa_s->conf->country[0];
  6200. country[1] = wpa_s->conf->country[1];
  6201. country[2] = 0x04;
  6202. p2p_set_country(p2p, country);
  6203. }
  6204. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  6205. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  6206. wpa_s->conf->p2p_ssid_postfix ?
  6207. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  6208. 0);
  6209. }
  6210. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  6211. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  6212. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  6213. u8 reg_class, channel;
  6214. int ret;
  6215. unsigned int r;
  6216. u8 channel_forced;
  6217. if (wpa_s->conf->p2p_listen_reg_class &&
  6218. wpa_s->conf->p2p_listen_channel) {
  6219. reg_class = wpa_s->conf->p2p_listen_reg_class;
  6220. channel = wpa_s->conf->p2p_listen_channel;
  6221. channel_forced = 1;
  6222. } else {
  6223. reg_class = 81;
  6224. /*
  6225. * Pick one of the social channels randomly as the
  6226. * listen channel.
  6227. */
  6228. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  6229. channel = 1;
  6230. else
  6231. channel = 1 + (r % 3) * 5;
  6232. channel_forced = 0;
  6233. }
  6234. ret = p2p_set_listen_channel(p2p, reg_class, channel,
  6235. channel_forced);
  6236. if (ret)
  6237. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  6238. "failed: %d", ret);
  6239. }
  6240. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  6241. u8 op_reg_class, op_channel, cfg_op_channel;
  6242. int ret = 0;
  6243. unsigned int r;
  6244. if (wpa_s->conf->p2p_oper_reg_class &&
  6245. wpa_s->conf->p2p_oper_channel) {
  6246. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  6247. op_channel = wpa_s->conf->p2p_oper_channel;
  6248. cfg_op_channel = 1;
  6249. } else {
  6250. op_reg_class = 81;
  6251. /*
  6252. * Use random operation channel from (1, 6, 11)
  6253. *if no other preference is indicated.
  6254. */
  6255. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  6256. op_channel = 1;
  6257. else
  6258. op_channel = 1 + (r % 3) * 5;
  6259. cfg_op_channel = 0;
  6260. }
  6261. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  6262. cfg_op_channel);
  6263. if (ret)
  6264. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  6265. "failed: %d", ret);
  6266. }
  6267. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  6268. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  6269. wpa_s->conf->p2p_pref_chan) < 0) {
  6270. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  6271. "update failed");
  6272. }
  6273. if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
  6274. wpa_printf(MSG_ERROR, "P2P: No GO channel list "
  6275. "update failed");
  6276. }
  6277. }
  6278. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
  6279. p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
  6280. }
  6281. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  6282. int duration)
  6283. {
  6284. if (!wpa_s->ap_iface)
  6285. return -1;
  6286. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  6287. duration);
  6288. }
  6289. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  6290. {
  6291. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6292. return -1;
  6293. wpa_s->global->cross_connection = enabled;
  6294. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  6295. if (!enabled) {
  6296. struct wpa_supplicant *iface;
  6297. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  6298. {
  6299. if (iface->cross_connect_enabled == 0)
  6300. continue;
  6301. iface->cross_connect_enabled = 0;
  6302. iface->cross_connect_in_use = 0;
  6303. wpa_msg_global(iface->p2pdev, MSG_INFO,
  6304. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  6305. iface->ifname,
  6306. iface->cross_connect_uplink);
  6307. }
  6308. }
  6309. return 0;
  6310. }
  6311. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  6312. {
  6313. struct wpa_supplicant *iface;
  6314. if (!uplink->global->cross_connection)
  6315. return;
  6316. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  6317. if (!iface->cross_connect_enabled)
  6318. continue;
  6319. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  6320. 0)
  6321. continue;
  6322. if (iface->ap_iface == NULL)
  6323. continue;
  6324. if (iface->cross_connect_in_use)
  6325. continue;
  6326. iface->cross_connect_in_use = 1;
  6327. wpa_msg_global(iface->p2pdev, MSG_INFO,
  6328. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  6329. iface->ifname, iface->cross_connect_uplink);
  6330. }
  6331. }
  6332. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  6333. {
  6334. struct wpa_supplicant *iface;
  6335. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  6336. if (!iface->cross_connect_enabled)
  6337. continue;
  6338. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  6339. 0)
  6340. continue;
  6341. if (!iface->cross_connect_in_use)
  6342. continue;
  6343. wpa_msg_global(iface->p2pdev, MSG_INFO,
  6344. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  6345. iface->ifname, iface->cross_connect_uplink);
  6346. iface->cross_connect_in_use = 0;
  6347. }
  6348. }
  6349. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  6350. {
  6351. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  6352. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  6353. wpa_s->cross_connect_disallowed)
  6354. wpas_p2p_disable_cross_connect(wpa_s);
  6355. else
  6356. wpas_p2p_enable_cross_connect(wpa_s);
  6357. if (!wpa_s->ap_iface &&
  6358. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  6359. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  6360. }
  6361. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  6362. {
  6363. wpas_p2p_disable_cross_connect(wpa_s);
  6364. if (!wpa_s->ap_iface &&
  6365. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  6366. wpa_s, NULL))
  6367. wpas_p2p_set_group_idle_timeout(wpa_s);
  6368. }
  6369. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  6370. {
  6371. struct wpa_supplicant *iface;
  6372. if (!wpa_s->global->cross_connection)
  6373. return;
  6374. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6375. if (iface == wpa_s)
  6376. continue;
  6377. if (iface->drv_flags &
  6378. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  6379. continue;
  6380. if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
  6381. iface != wpa_s->parent)
  6382. continue;
  6383. wpa_s->cross_connect_enabled = 1;
  6384. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  6385. sizeof(wpa_s->cross_connect_uplink));
  6386. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  6387. "%s to %s whenever uplink is available",
  6388. wpa_s->ifname, wpa_s->cross_connect_uplink);
  6389. if (iface->ap_iface || iface->current_ssid == NULL ||
  6390. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  6391. iface->cross_connect_disallowed ||
  6392. iface->wpa_state != WPA_COMPLETED)
  6393. break;
  6394. wpa_s->cross_connect_in_use = 1;
  6395. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  6396. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  6397. wpa_s->ifname, wpa_s->cross_connect_uplink);
  6398. break;
  6399. }
  6400. }
  6401. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  6402. {
  6403. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  6404. !wpa_s->p2p_in_provisioning)
  6405. return 0; /* not P2P client operation */
  6406. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  6407. "session overlap");
  6408. if (wpa_s != wpa_s->p2pdev)
  6409. wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
  6410. wpas_p2p_group_formation_failed(wpa_s, 0);
  6411. return 1;
  6412. }
  6413. void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
  6414. {
  6415. struct wpa_supplicant *wpa_s = eloop_ctx;
  6416. wpas_p2p_notif_pbc_overlap(wpa_s);
  6417. }
  6418. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
  6419. enum wpas_p2p_channel_update_trig trig)
  6420. {
  6421. struct p2p_channels chan, cli_chan;
  6422. struct wpa_used_freq_data *freqs = NULL;
  6423. unsigned int num = wpa_s->num_multichan_concurrent;
  6424. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  6425. return;
  6426. freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
  6427. if (!freqs)
  6428. return;
  6429. num = get_shared_radio_freqs_data(wpa_s, freqs, num);
  6430. os_memset(&chan, 0, sizeof(chan));
  6431. os_memset(&cli_chan, 0, sizeof(cli_chan));
  6432. if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
  6433. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  6434. "channel list");
  6435. return;
  6436. }
  6437. p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
  6438. wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
  6439. /*
  6440. * The used frequencies map changed, so it is possible that a GO is
  6441. * using a channel that is no longer valid for P2P use. It is also
  6442. * possible that due to policy consideration, it would be preferable to
  6443. * move it to a frequency already used by other station interfaces.
  6444. */
  6445. wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
  6446. os_free(freqs);
  6447. }
  6448. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  6449. struct wpa_scan_results *scan_res)
  6450. {
  6451. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  6452. }
  6453. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  6454. {
  6455. struct wpa_global *global = wpa_s->global;
  6456. int found = 0;
  6457. const u8 *peer;
  6458. if (global->p2p == NULL)
  6459. return -1;
  6460. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  6461. if (wpa_s->pending_interface_name[0] &&
  6462. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  6463. found = 1;
  6464. peer = p2p_get_go_neg_peer(global->p2p);
  6465. if (peer) {
  6466. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  6467. MACSTR, MAC2STR(peer));
  6468. p2p_unauthorize(global->p2p, peer);
  6469. found = 1;
  6470. }
  6471. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  6472. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  6473. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  6474. found = 1;
  6475. }
  6476. if (wpa_s->pending_pd_before_join) {
  6477. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  6478. wpa_s->pending_pd_before_join = 0;
  6479. found = 1;
  6480. }
  6481. wpas_p2p_stop_find(wpa_s);
  6482. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6483. if (wpa_s == global->p2p_group_formation &&
  6484. (wpa_s->p2p_in_provisioning ||
  6485. wpa_s->parent->pending_interface_type ==
  6486. WPA_IF_P2P_CLIENT)) {
  6487. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  6488. "formation found - cancelling",
  6489. wpa_s->ifname);
  6490. found = 1;
  6491. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6492. wpa_s->p2pdev, NULL);
  6493. if (wpa_s->p2p_in_provisioning) {
  6494. wpas_group_formation_completed(wpa_s, 0, 0);
  6495. break;
  6496. }
  6497. wpas_p2p_group_delete(wpa_s,
  6498. P2P_GROUP_REMOVAL_REQUESTED);
  6499. break;
  6500. } else if (wpa_s->p2p_in_invitation) {
  6501. wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
  6502. wpa_s->ifname);
  6503. found = 1;
  6504. wpas_p2p_group_formation_failed(wpa_s, 0);
  6505. break;
  6506. }
  6507. }
  6508. if (!found) {
  6509. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  6510. return -1;
  6511. }
  6512. return 0;
  6513. }
  6514. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  6515. {
  6516. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  6517. return;
  6518. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  6519. "being available anymore");
  6520. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  6521. }
  6522. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  6523. int freq_24, int freq_5, int freq_overall)
  6524. {
  6525. struct p2p_data *p2p = wpa_s->global->p2p;
  6526. if (p2p == NULL)
  6527. return;
  6528. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  6529. }
  6530. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  6531. {
  6532. u8 peer[ETH_ALEN];
  6533. struct p2p_data *p2p = wpa_s->global->p2p;
  6534. if (p2p == NULL)
  6535. return -1;
  6536. if (hwaddr_aton(addr, peer))
  6537. return -1;
  6538. return p2p_unauthorize(p2p, peer);
  6539. }
  6540. /**
  6541. * wpas_p2p_disconnect - Disconnect from a P2P Group
  6542. * @wpa_s: Pointer to wpa_supplicant data
  6543. * Returns: 0 on success, -1 on failure
  6544. *
  6545. * This can be used to disconnect from a group in which the local end is a P2P
  6546. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  6547. * virtual network interface was created for this group, that interface will be
  6548. * removed. Otherwise, only the configured P2P group network will be removed
  6549. * from the interface.
  6550. */
  6551. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  6552. {
  6553. if (wpa_s == NULL)
  6554. return -1;
  6555. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  6556. -1 : 0;
  6557. }
  6558. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  6559. {
  6560. int ret;
  6561. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6562. return 0;
  6563. ret = p2p_in_progress(wpa_s->global->p2p);
  6564. if (ret == 0) {
  6565. /*
  6566. * Check whether there is an ongoing WPS provisioning step (or
  6567. * other parts of group formation) on another interface since
  6568. * p2p_in_progress() does not report this to avoid issues for
  6569. * scans during such provisioning step.
  6570. */
  6571. if (wpa_s->global->p2p_group_formation &&
  6572. wpa_s->global->p2p_group_formation != wpa_s) {
  6573. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  6574. "in group formation",
  6575. wpa_s->global->p2p_group_formation->ifname);
  6576. ret = 1;
  6577. }
  6578. }
  6579. if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
  6580. struct os_reltime now;
  6581. os_get_reltime(&now);
  6582. if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
  6583. P2P_MAX_INITIAL_CONN_WAIT_GO)) {
  6584. /* Wait for the first client has expired */
  6585. wpa_s->global->p2p_go_wait_client.sec = 0;
  6586. } else {
  6587. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
  6588. ret = 1;
  6589. }
  6590. }
  6591. return ret;
  6592. }
  6593. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  6594. struct wpa_ssid *ssid)
  6595. {
  6596. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  6597. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6598. wpa_s->p2pdev, NULL) > 0) {
  6599. /**
  6600. * Remove the network by scheduling the group formation
  6601. * timeout to happen immediately. The teardown code
  6602. * needs to be scheduled to run asynch later so that we
  6603. * don't delete data from under ourselves unexpectedly.
  6604. * Calling wpas_p2p_group_formation_timeout directly
  6605. * causes a series of crashes in WPS failure scenarios.
  6606. */
  6607. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  6608. "P2P group network getting removed");
  6609. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  6610. wpa_s->p2pdev, NULL);
  6611. }
  6612. }
  6613. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  6614. const u8 *addr, const u8 *ssid,
  6615. size_t ssid_len)
  6616. {
  6617. struct wpa_ssid *s;
  6618. size_t i;
  6619. for (s = wpa_s->conf->ssid; s; s = s->next) {
  6620. if (s->disabled != 2)
  6621. continue;
  6622. if (ssid &&
  6623. (ssid_len != s->ssid_len ||
  6624. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  6625. continue;
  6626. if (addr == NULL) {
  6627. if (s->mode == WPAS_MODE_P2P_GO)
  6628. return s;
  6629. continue;
  6630. }
  6631. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  6632. return s; /* peer is GO in the persistent group */
  6633. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  6634. continue;
  6635. for (i = 0; i < s->num_p2p_clients; i++) {
  6636. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
  6637. addr, ETH_ALEN) == 0)
  6638. return s; /* peer is P2P client in persistent
  6639. * group */
  6640. }
  6641. }
  6642. return NULL;
  6643. }
  6644. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  6645. const u8 *addr)
  6646. {
  6647. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6648. wpa_s->p2pdev, NULL) > 0) {
  6649. /*
  6650. * This can happen if WPS provisioning step is not terminated
  6651. * cleanly (e.g., P2P Client does not send WSC_Done). Since the
  6652. * peer was able to connect, there is no need to time out group
  6653. * formation after this, though. In addition, this is used with
  6654. * the initial connection wait on the GO as a separate formation
  6655. * timeout and as such, expected to be hit after the initial WPS
  6656. * provisioning step.
  6657. */
  6658. wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
  6659. if (!wpa_s->p2p_go_group_formation_completed &&
  6660. !wpa_s->group_formation_reported) {
  6661. /*
  6662. * GO has not yet notified group formation success since
  6663. * the WPS step was not completed cleanly. Do that
  6664. * notification now since the P2P Client was able to
  6665. * connect and as such, must have received the
  6666. * credential from the WPS step.
  6667. */
  6668. if (wpa_s->global->p2p)
  6669. p2p_wps_success_cb(wpa_s->global->p2p, addr);
  6670. wpas_group_formation_completed(wpa_s, 1, 0);
  6671. }
  6672. }
  6673. if (!wpa_s->p2p_go_group_formation_completed) {
  6674. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
  6675. wpa_s->p2p_go_group_formation_completed = 1;
  6676. wpa_s->global->p2p_group_formation = NULL;
  6677. wpa_s->p2p_in_provisioning = 0;
  6678. wpa_s->p2p_in_invitation = 0;
  6679. }
  6680. wpa_s->global->p2p_go_wait_client.sec = 0;
  6681. if (addr == NULL)
  6682. return;
  6683. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  6684. }
  6685. static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  6686. int group_added)
  6687. {
  6688. struct wpa_supplicant *group = wpa_s;
  6689. int ret = 0;
  6690. if (wpa_s->global->p2p_group_formation)
  6691. group = wpa_s->global->p2p_group_formation;
  6692. wpa_s = wpa_s->global->p2p_init_wpa_s;
  6693. offchannel_send_action_done(wpa_s);
  6694. if (group_added)
  6695. ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  6696. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  6697. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  6698. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  6699. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  6700. wpa_s->p2p_go_vht_center_freq2,
  6701. wpa_s->p2p_persistent_id,
  6702. wpa_s->p2p_pd_before_go_neg,
  6703. wpa_s->p2p_go_ht40,
  6704. wpa_s->p2p_go_vht,
  6705. wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
  6706. return ret;
  6707. }
  6708. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  6709. {
  6710. int res;
  6711. if (!wpa_s->p2p_fallback_to_go_neg ||
  6712. wpa_s->p2p_in_provisioning <= 5)
  6713. return 0;
  6714. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  6715. return 0; /* peer operating as a GO */
  6716. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  6717. "fallback to GO Negotiation");
  6718. wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
  6719. "reason=GO-not-found");
  6720. res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  6721. return res == 1 ? 2 : 1;
  6722. }
  6723. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  6724. {
  6725. struct wpa_supplicant *ifs;
  6726. if (wpa_s->wpa_state > WPA_SCANNING) {
  6727. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  6728. "concurrent operation",
  6729. wpa_s->conf->p2p_search_delay);
  6730. return wpa_s->conf->p2p_search_delay;
  6731. }
  6732. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  6733. radio_list) {
  6734. if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
  6735. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  6736. "delay due to concurrent operation on "
  6737. "interface %s",
  6738. wpa_s->conf->p2p_search_delay,
  6739. ifs->ifname);
  6740. return wpa_s->conf->p2p_search_delay;
  6741. }
  6742. }
  6743. return 0;
  6744. }
  6745. static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
  6746. struct wpa_ssid *s, const u8 *addr,
  6747. int iface_addr)
  6748. {
  6749. struct psk_list_entry *psk, *tmp;
  6750. int changed = 0;
  6751. dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
  6752. list) {
  6753. if ((iface_addr && !psk->p2p &&
  6754. os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
  6755. (!iface_addr && psk->p2p &&
  6756. os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
  6757. wpa_dbg(wpa_s, MSG_DEBUG,
  6758. "P2P: Remove persistent group PSK list entry for "
  6759. MACSTR " p2p=%u",
  6760. MAC2STR(psk->addr), psk->p2p);
  6761. dl_list_del(&psk->list);
  6762. os_free(psk);
  6763. changed++;
  6764. }
  6765. }
  6766. return changed;
  6767. }
  6768. void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
  6769. const u8 *p2p_dev_addr,
  6770. const u8 *psk, size_t psk_len)
  6771. {
  6772. struct wpa_ssid *ssid = wpa_s->current_ssid;
  6773. struct wpa_ssid *persistent;
  6774. struct psk_list_entry *p, *last;
  6775. if (psk_len != sizeof(p->psk))
  6776. return;
  6777. if (p2p_dev_addr) {
  6778. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
  6779. " p2p_dev_addr=" MACSTR,
  6780. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  6781. if (is_zero_ether_addr(p2p_dev_addr))
  6782. p2p_dev_addr = NULL;
  6783. } else {
  6784. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
  6785. MAC2STR(mac_addr));
  6786. }
  6787. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  6788. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
  6789. /* To be added to persistent group once created */
  6790. if (wpa_s->global->add_psk == NULL) {
  6791. wpa_s->global->add_psk = os_zalloc(sizeof(*p));
  6792. if (wpa_s->global->add_psk == NULL)
  6793. return;
  6794. }
  6795. p = wpa_s->global->add_psk;
  6796. if (p2p_dev_addr) {
  6797. p->p2p = 1;
  6798. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  6799. } else {
  6800. p->p2p = 0;
  6801. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  6802. }
  6803. os_memcpy(p->psk, psk, psk_len);
  6804. return;
  6805. }
  6806. if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
  6807. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
  6808. return;
  6809. }
  6810. persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
  6811. ssid->ssid_len);
  6812. if (!persistent) {
  6813. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
  6814. return;
  6815. }
  6816. p = os_zalloc(sizeof(*p));
  6817. if (p == NULL)
  6818. return;
  6819. if (p2p_dev_addr) {
  6820. p->p2p = 1;
  6821. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  6822. } else {
  6823. p->p2p = 0;
  6824. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  6825. }
  6826. os_memcpy(p->psk, psk, psk_len);
  6827. if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
  6828. (last = dl_list_last(&persistent->psk_list,
  6829. struct psk_list_entry, list))) {
  6830. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
  6831. MACSTR " (p2p=%u) to make room for a new one",
  6832. MAC2STR(last->addr), last->p2p);
  6833. dl_list_del(&last->list);
  6834. os_free(last);
  6835. }
  6836. wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
  6837. p2p_dev_addr ? p2p_dev_addr : mac_addr,
  6838. p2p_dev_addr == NULL);
  6839. if (p2p_dev_addr) {
  6840. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
  6841. MACSTR, MAC2STR(p2p_dev_addr));
  6842. } else {
  6843. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
  6844. MAC2STR(mac_addr));
  6845. }
  6846. dl_list_add(&persistent->psk_list, &p->list);
  6847. if (wpa_s->p2pdev->conf->update_config &&
  6848. wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
  6849. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  6850. }
  6851. static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
  6852. struct wpa_ssid *s, const u8 *addr,
  6853. int iface_addr)
  6854. {
  6855. int res;
  6856. res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
  6857. if (res > 0 && wpa_s->conf->update_config &&
  6858. wpa_config_write(wpa_s->confname, wpa_s->conf))
  6859. wpa_dbg(wpa_s, MSG_DEBUG,
  6860. "P2P: Failed to update configuration");
  6861. }
  6862. static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
  6863. const u8 *peer, int iface_addr)
  6864. {
  6865. struct hostapd_data *hapd;
  6866. struct hostapd_wpa_psk *psk, *prev, *rem;
  6867. struct sta_info *sta;
  6868. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
  6869. wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
  6870. return;
  6871. /* Remove per-station PSK entry */
  6872. hapd = wpa_s->ap_iface->bss[0];
  6873. prev = NULL;
  6874. psk = hapd->conf->ssid.wpa_psk;
  6875. while (psk) {
  6876. if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
  6877. (!iface_addr &&
  6878. os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
  6879. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
  6880. MACSTR " iface_addr=%d",
  6881. MAC2STR(peer), iface_addr);
  6882. if (prev)
  6883. prev->next = psk->next;
  6884. else
  6885. hapd->conf->ssid.wpa_psk = psk->next;
  6886. rem = psk;
  6887. psk = psk->next;
  6888. os_free(rem);
  6889. } else {
  6890. prev = psk;
  6891. psk = psk->next;
  6892. }
  6893. }
  6894. /* Disconnect from group */
  6895. if (iface_addr)
  6896. sta = ap_get_sta(hapd, peer);
  6897. else
  6898. sta = ap_get_sta_p2p(hapd, peer);
  6899. if (sta) {
  6900. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
  6901. " (iface_addr=%d) from group",
  6902. MAC2STR(peer), iface_addr);
  6903. hostapd_drv_sta_deauth(hapd, sta->addr,
  6904. WLAN_REASON_DEAUTH_LEAVING);
  6905. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
  6906. }
  6907. }
  6908. void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
  6909. int iface_addr)
  6910. {
  6911. struct wpa_ssid *s;
  6912. struct wpa_supplicant *w;
  6913. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  6914. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
  6915. /* Remove from any persistent group */
  6916. for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
  6917. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  6918. continue;
  6919. if (!iface_addr)
  6920. wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
  6921. wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
  6922. }
  6923. /* Remove from any operating group */
  6924. for (w = wpa_s->global->ifaces; w; w = w->next)
  6925. wpas_p2p_remove_client_go(w, peer, iface_addr);
  6926. }
  6927. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
  6928. {
  6929. struct wpa_supplicant *wpa_s = eloop_ctx;
  6930. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
  6931. }
  6932. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
  6933. {
  6934. struct wpa_supplicant *wpa_s = eloop_ctx;
  6935. wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
  6936. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
  6937. }
  6938. int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
  6939. struct wpa_ssid *ssid)
  6940. {
  6941. struct wpa_supplicant *iface;
  6942. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6943. if (!iface->current_ssid ||
  6944. iface->current_ssid->frequency == freq ||
  6945. (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  6946. !iface->current_ssid->p2p_group))
  6947. continue;
  6948. /* Remove the connection with least priority */
  6949. if (!wpas_is_p2p_prioritized(iface)) {
  6950. /* STA connection has priority over existing
  6951. * P2P connection, so remove the interface. */
  6952. wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
  6953. eloop_register_timeout(0, 0,
  6954. wpas_p2p_group_freq_conflict,
  6955. iface, NULL);
  6956. /* If connection in progress is P2P connection, do not
  6957. * proceed for the connection. */
  6958. if (wpa_s == iface)
  6959. return -1;
  6960. else
  6961. return 0;
  6962. } else {
  6963. /* P2P connection has priority, disable the STA network
  6964. */
  6965. wpa_supplicant_disable_network(wpa_s->global->ifaces,
  6966. ssid);
  6967. wpa_msg(wpa_s->global->ifaces, MSG_INFO,
  6968. WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
  6969. os_memset(wpa_s->global->ifaces->pending_bssid, 0,
  6970. ETH_ALEN);
  6971. /* If P2P connection is in progress, continue
  6972. * connecting...*/
  6973. if (wpa_s == iface)
  6974. return 0;
  6975. else
  6976. return -1;
  6977. }
  6978. }
  6979. return 0;
  6980. }
  6981. int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
  6982. {
  6983. struct wpa_ssid *ssid = wpa_s->current_ssid;
  6984. if (ssid == NULL || !ssid->p2p_group)
  6985. return 0;
  6986. if (wpa_s->p2p_last_4way_hs_fail &&
  6987. wpa_s->p2p_last_4way_hs_fail == ssid) {
  6988. u8 go_dev_addr[ETH_ALEN];
  6989. struct wpa_ssid *persistent;
  6990. if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
  6991. ssid->ssid,
  6992. ssid->ssid_len) <= 0) {
  6993. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
  6994. goto disconnect;
  6995. }
  6996. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
  6997. MACSTR, MAC2STR(go_dev_addr));
  6998. persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
  6999. ssid->ssid,
  7000. ssid->ssid_len);
  7001. if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
  7002. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
  7003. goto disconnect;
  7004. }
  7005. wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
  7006. P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
  7007. persistent->id);
  7008. disconnect:
  7009. wpa_s->p2p_last_4way_hs_fail = NULL;
  7010. /*
  7011. * Remove the group from a timeout to avoid issues with caller
  7012. * continuing to use the interface if this is on a P2P group
  7013. * interface.
  7014. */
  7015. eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
  7016. wpa_s, NULL);
  7017. return 1;
  7018. }
  7019. wpa_s->p2p_last_4way_hs_fail = ssid;
  7020. return 0;
  7021. }
  7022. #ifdef CONFIG_WPS_NFC
  7023. static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
  7024. struct wpabuf *p2p)
  7025. {
  7026. struct wpabuf *ret;
  7027. size_t wsc_len;
  7028. if (p2p == NULL) {
  7029. wpabuf_free(wsc);
  7030. wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
  7031. return NULL;
  7032. }
  7033. wsc_len = wsc ? wpabuf_len(wsc) : 0;
  7034. ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
  7035. if (ret == NULL) {
  7036. wpabuf_free(wsc);
  7037. wpabuf_free(p2p);
  7038. return NULL;
  7039. }
  7040. wpabuf_put_be16(ret, wsc_len);
  7041. if (wsc)
  7042. wpabuf_put_buf(ret, wsc);
  7043. wpabuf_put_be16(ret, wpabuf_len(p2p));
  7044. wpabuf_put_buf(ret, p2p);
  7045. wpabuf_free(wsc);
  7046. wpabuf_free(p2p);
  7047. wpa_hexdump_buf(MSG_DEBUG,
  7048. "P2P: Generated NFC connection handover message", ret);
  7049. if (ndef && ret) {
  7050. struct wpabuf *tmp;
  7051. tmp = ndef_build_p2p(ret);
  7052. wpabuf_free(ret);
  7053. if (tmp == NULL) {
  7054. wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
  7055. return NULL;
  7056. }
  7057. ret = tmp;
  7058. }
  7059. return ret;
  7060. }
  7061. static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
  7062. struct wpa_ssid **ssid, u8 *go_dev_addr)
  7063. {
  7064. struct wpa_supplicant *iface;
  7065. if (go_dev_addr)
  7066. os_memset(go_dev_addr, 0, ETH_ALEN);
  7067. if (ssid)
  7068. *ssid = NULL;
  7069. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  7070. if (iface->wpa_state < WPA_ASSOCIATING ||
  7071. iface->current_ssid == NULL || iface->assoc_freq == 0 ||
  7072. !iface->current_ssid->p2p_group ||
  7073. iface->current_ssid->mode != WPAS_MODE_INFRA)
  7074. continue;
  7075. if (ssid)
  7076. *ssid = iface->current_ssid;
  7077. if (go_dev_addr)
  7078. os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
  7079. return iface->assoc_freq;
  7080. }
  7081. return 0;
  7082. }
  7083. struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
  7084. int ndef)
  7085. {
  7086. struct wpabuf *wsc, *p2p;
  7087. struct wpa_ssid *ssid;
  7088. u8 go_dev_addr[ETH_ALEN];
  7089. int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
  7090. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
  7091. wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
  7092. return NULL;
  7093. }
  7094. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  7095. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  7096. &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
  7097. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
  7098. return NULL;
  7099. }
  7100. if (cli_freq == 0) {
  7101. wsc = wps_build_nfc_handover_req_p2p(
  7102. wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
  7103. } else
  7104. wsc = NULL;
  7105. p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
  7106. go_dev_addr, ssid ? ssid->ssid : NULL,
  7107. ssid ? ssid->ssid_len : 0);
  7108. return wpas_p2p_nfc_handover(ndef, wsc, p2p);
  7109. }
  7110. struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  7111. int ndef, int tag)
  7112. {
  7113. struct wpabuf *wsc, *p2p;
  7114. struct wpa_ssid *ssid;
  7115. u8 go_dev_addr[ETH_ALEN];
  7116. int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
  7117. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7118. return NULL;
  7119. if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  7120. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  7121. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  7122. return NULL;
  7123. if (cli_freq == 0) {
  7124. wsc = wps_build_nfc_handover_sel_p2p(
  7125. wpa_s->parent->wps,
  7126. tag ? wpa_s->conf->wps_nfc_dev_pw_id :
  7127. DEV_PW_NFC_CONNECTION_HANDOVER,
  7128. wpa_s->conf->wps_nfc_dh_pubkey,
  7129. tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
  7130. } else
  7131. wsc = NULL;
  7132. p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
  7133. go_dev_addr, ssid ? ssid->ssid : NULL,
  7134. ssid ? ssid->ssid_len : 0);
  7135. return wpas_p2p_nfc_handover(ndef, wsc, p2p);
  7136. }
  7137. static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
  7138. struct p2p_nfc_params *params)
  7139. {
  7140. wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
  7141. "connection handover (freq=%d)",
  7142. params->go_freq);
  7143. if (params->go_freq && params->go_ssid_len) {
  7144. wpa_s->p2p_wps_method = WPS_NFC;
  7145. wpa_s->pending_join_wps_method = WPS_NFC;
  7146. os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
  7147. os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
  7148. ETH_ALEN);
  7149. return wpas_p2p_join_start(wpa_s, params->go_freq,
  7150. params->go_ssid,
  7151. params->go_ssid_len);
  7152. }
  7153. return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  7154. WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
  7155. params->go_freq, wpa_s->p2p_go_vht_center_freq2,
  7156. -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
  7157. params->go_ssid_len ? params->go_ssid : NULL,
  7158. params->go_ssid_len);
  7159. }
  7160. static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
  7161. struct p2p_nfc_params *params, int tag)
  7162. {
  7163. int res, persistent;
  7164. struct wpa_ssid *ssid;
  7165. wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
  7166. "connection handover");
  7167. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  7168. ssid = wpa_s->current_ssid;
  7169. if (ssid == NULL)
  7170. continue;
  7171. if (ssid->mode != WPAS_MODE_P2P_GO)
  7172. continue;
  7173. if (wpa_s->ap_iface == NULL)
  7174. continue;
  7175. break;
  7176. }
  7177. if (wpa_s == NULL) {
  7178. wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
  7179. return -1;
  7180. }
  7181. if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
  7182. DEV_PW_NFC_CONNECTION_HANDOVER &&
  7183. !wpa_s->p2pdev->p2p_oob_dev_pw) {
  7184. wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
  7185. return -1;
  7186. }
  7187. res = wpas_ap_wps_add_nfc_pw(
  7188. wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
  7189. wpa_s->p2pdev->p2p_oob_dev_pw,
  7190. wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
  7191. wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
  7192. if (res)
  7193. return res;
  7194. if (!tag) {
  7195. wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
  7196. return 0;
  7197. }
  7198. if (!params->peer ||
  7199. !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
  7200. return 0;
  7201. wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
  7202. " to join", MAC2STR(params->peer->p2p_device_addr));
  7203. wpa_s->global->p2p_invite_group = wpa_s;
  7204. persistent = ssid->p2p_persistent_group &&
  7205. wpas_p2p_get_persistent(wpa_s->p2pdev,
  7206. params->peer->p2p_device_addr,
  7207. ssid->ssid, ssid->ssid_len);
  7208. wpa_s->p2pdev->pending_invite_ssid_id = -1;
  7209. return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
  7210. P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
  7211. ssid->ssid, ssid->ssid_len, ssid->frequency,
  7212. wpa_s->global->p2p_dev_addr, persistent, 0,
  7213. wpa_s->p2pdev->p2p_oob_dev_pw_id);
  7214. }
  7215. static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
  7216. struct p2p_nfc_params *params,
  7217. int forced_freq)
  7218. {
  7219. wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
  7220. "connection handover");
  7221. return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  7222. WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
  7223. forced_freq, wpa_s->p2p_go_vht_center_freq2,
  7224. -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
  7225. NULL, 0);
  7226. }
  7227. static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
  7228. struct p2p_nfc_params *params,
  7229. int forced_freq)
  7230. {
  7231. int res;
  7232. wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
  7233. "connection handover");
  7234. res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  7235. WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
  7236. forced_freq, wpa_s->p2p_go_vht_center_freq2,
  7237. -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
  7238. NULL, 0);
  7239. if (res)
  7240. return res;
  7241. res = wpas_p2p_listen(wpa_s, 60);
  7242. if (res) {
  7243. p2p_unauthorize(wpa_s->global->p2p,
  7244. params->peer->p2p_device_addr);
  7245. }
  7246. return res;
  7247. }
  7248. static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
  7249. const struct wpabuf *data,
  7250. int sel, int tag, int forced_freq)
  7251. {
  7252. const u8 *pos, *end;
  7253. u16 len, id;
  7254. struct p2p_nfc_params params;
  7255. int res;
  7256. os_memset(&params, 0, sizeof(params));
  7257. params.sel = sel;
  7258. wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
  7259. pos = wpabuf_head(data);
  7260. end = pos + wpabuf_len(data);
  7261. if (end - pos < 2) {
  7262. wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
  7263. "attributes");
  7264. return -1;
  7265. }
  7266. len = WPA_GET_BE16(pos);
  7267. pos += 2;
  7268. if (len > end - pos) {
  7269. wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
  7270. "attributes");
  7271. return -1;
  7272. }
  7273. params.wsc_attr = pos;
  7274. params.wsc_len = len;
  7275. pos += len;
  7276. if (end - pos < 2) {
  7277. wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
  7278. "attributes");
  7279. return -1;
  7280. }
  7281. len = WPA_GET_BE16(pos);
  7282. pos += 2;
  7283. if (len > end - pos) {
  7284. wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
  7285. "attributes");
  7286. return -1;
  7287. }
  7288. params.p2p_attr = pos;
  7289. params.p2p_len = len;
  7290. pos += len;
  7291. wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
  7292. params.wsc_attr, params.wsc_len);
  7293. wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
  7294. params.p2p_attr, params.p2p_len);
  7295. if (pos < end) {
  7296. wpa_hexdump(MSG_DEBUG,
  7297. "P2P: Ignored extra data after P2P attributes",
  7298. pos, end - pos);
  7299. }
  7300. res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
  7301. if (res)
  7302. return res;
  7303. if (params.next_step == NO_ACTION)
  7304. return 0;
  7305. if (params.next_step == BOTH_GO) {
  7306. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
  7307. MAC2STR(params.peer->p2p_device_addr));
  7308. return 0;
  7309. }
  7310. if (params.next_step == PEER_CLIENT) {
  7311. if (!is_zero_ether_addr(params.go_dev_addr)) {
  7312. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
  7313. "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
  7314. " ssid=\"%s\"",
  7315. MAC2STR(params.peer->p2p_device_addr),
  7316. params.go_freq,
  7317. MAC2STR(params.go_dev_addr),
  7318. wpa_ssid_txt(params.go_ssid,
  7319. params.go_ssid_len));
  7320. } else {
  7321. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
  7322. "peer=" MACSTR " freq=%d",
  7323. MAC2STR(params.peer->p2p_device_addr),
  7324. params.go_freq);
  7325. }
  7326. return 0;
  7327. }
  7328. if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
  7329. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
  7330. MACSTR, MAC2STR(params.peer->p2p_device_addr));
  7331. return 0;
  7332. }
  7333. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  7334. wpa_s->p2p_oob_dev_pw = NULL;
  7335. if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
  7336. wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
  7337. "received");
  7338. return -1;
  7339. }
  7340. id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
  7341. wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
  7342. wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
  7343. params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
  7344. os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
  7345. params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
  7346. wpa_s->p2p_peer_oob_pk_hash_known = 1;
  7347. if (tag) {
  7348. if (id < 0x10) {
  7349. wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
  7350. "peer OOB Device Password Id %u", id);
  7351. return -1;
  7352. }
  7353. wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
  7354. "Device Password Id %u", id);
  7355. wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
  7356. params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
  7357. params.oob_dev_pw_len -
  7358. WPS_OOB_PUBKEY_HASH_LEN - 2);
  7359. wpa_s->p2p_oob_dev_pw_id = id;
  7360. wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
  7361. params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
  7362. params.oob_dev_pw_len -
  7363. WPS_OOB_PUBKEY_HASH_LEN - 2);
  7364. if (wpa_s->p2p_oob_dev_pw == NULL)
  7365. return -1;
  7366. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  7367. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  7368. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  7369. return -1;
  7370. } else {
  7371. wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
  7372. "without Device Password");
  7373. wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
  7374. }
  7375. switch (params.next_step) {
  7376. case NO_ACTION:
  7377. case BOTH_GO:
  7378. case PEER_CLIENT:
  7379. /* already covered above */
  7380. return 0;
  7381. case JOIN_GROUP:
  7382. return wpas_p2p_nfc_join_group(wpa_s, &params);
  7383. case AUTH_JOIN:
  7384. return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
  7385. case INIT_GO_NEG:
  7386. return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
  7387. case RESP_GO_NEG:
  7388. /* TODO: use own OOB Dev Pw */
  7389. return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
  7390. }
  7391. return -1;
  7392. }
  7393. int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
  7394. const struct wpabuf *data, int forced_freq)
  7395. {
  7396. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7397. return -1;
  7398. return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
  7399. }
  7400. int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
  7401. const struct wpabuf *req,
  7402. const struct wpabuf *sel, int forced_freq)
  7403. {
  7404. struct wpabuf *tmp;
  7405. int ret;
  7406. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7407. return -1;
  7408. wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
  7409. wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
  7410. wpabuf_head(req), wpabuf_len(req));
  7411. wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
  7412. wpabuf_head(sel), wpabuf_len(sel));
  7413. if (forced_freq)
  7414. wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
  7415. tmp = ndef_parse_p2p(init ? sel : req);
  7416. if (tmp == NULL) {
  7417. wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
  7418. return -1;
  7419. }
  7420. ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
  7421. forced_freq);
  7422. wpabuf_free(tmp);
  7423. return ret;
  7424. }
  7425. int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
  7426. {
  7427. const u8 *if_addr;
  7428. int go_intent = wpa_s->conf->p2p_go_intent;
  7429. struct wpa_supplicant *iface;
  7430. if (wpa_s->global->p2p == NULL)
  7431. return -1;
  7432. if (!enabled) {
  7433. wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
  7434. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  7435. {
  7436. if (!iface->ap_iface)
  7437. continue;
  7438. hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
  7439. }
  7440. p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
  7441. 0, NULL);
  7442. if (wpa_s->p2p_nfc_tag_enabled)
  7443. wpas_p2p_remove_pending_group_interface(wpa_s);
  7444. wpa_s->p2p_nfc_tag_enabled = 0;
  7445. return 0;
  7446. }
  7447. if (wpa_s->global->p2p_disabled)
  7448. return -1;
  7449. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
  7450. wpa_s->conf->wps_nfc_dh_privkey == NULL ||
  7451. wpa_s->conf->wps_nfc_dev_pw == NULL ||
  7452. wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
  7453. wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
  7454. "to allow static handover cases");
  7455. return -1;
  7456. }
  7457. wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
  7458. wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  7459. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  7460. wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
  7461. if (wpa_s->p2p_oob_dev_pw == NULL)
  7462. return -1;
  7463. wpa_s->p2p_peer_oob_pk_hash_known = 0;
  7464. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
  7465. wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
  7466. /*
  7467. * P2P Group Interface present and the command came on group
  7468. * interface, so enable the token for the current interface.
  7469. */
  7470. wpa_s->create_p2p_iface = 0;
  7471. } else {
  7472. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  7473. }
  7474. if (wpa_s->create_p2p_iface) {
  7475. enum wpa_driver_if_type iftype;
  7476. /* Prepare to add a new interface for the group */
  7477. iftype = WPA_IF_P2P_GROUP;
  7478. if (go_intent == 15)
  7479. iftype = WPA_IF_P2P_GO;
  7480. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  7481. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  7482. "interface for the group");
  7483. return -1;
  7484. }
  7485. if_addr = wpa_s->pending_interface_addr;
  7486. } else if (wpa_s->p2p_mgmt)
  7487. if_addr = wpa_s->parent->own_addr;
  7488. else
  7489. if_addr = wpa_s->own_addr;
  7490. wpa_s->p2p_nfc_tag_enabled = enabled;
  7491. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  7492. struct hostapd_data *hapd;
  7493. if (iface->ap_iface == NULL)
  7494. continue;
  7495. hapd = iface->ap_iface->bss[0];
  7496. wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
  7497. hapd->conf->wps_nfc_dh_pubkey =
  7498. wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
  7499. wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
  7500. hapd->conf->wps_nfc_dh_privkey =
  7501. wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
  7502. wpabuf_free(hapd->conf->wps_nfc_dev_pw);
  7503. hapd->conf->wps_nfc_dev_pw =
  7504. wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
  7505. hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  7506. if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
  7507. wpa_dbg(iface, MSG_DEBUG,
  7508. "P2P: Failed to enable NFC Tag for GO");
  7509. }
  7510. }
  7511. p2p_set_authorized_oob_dev_pw_id(
  7512. wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
  7513. if_addr);
  7514. return 0;
  7515. }
  7516. #endif /* CONFIG_WPS_NFC */
  7517. static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
  7518. struct wpa_used_freq_data *freqs,
  7519. unsigned int num)
  7520. {
  7521. u8 curr_chan, cand, chan;
  7522. unsigned int i;
  7523. /*
  7524. * If possible, optimize the Listen channel to be a channel that is
  7525. * already used by one of the other interfaces.
  7526. */
  7527. if (!wpa_s->conf->p2p_optimize_listen_chan)
  7528. return;
  7529. if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
  7530. return;
  7531. curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
  7532. for (i = 0, cand = 0; i < num; i++) {
  7533. ieee80211_freq_to_chan(freqs[i].freq, &chan);
  7534. if (curr_chan == chan) {
  7535. cand = 0;
  7536. break;
  7537. }
  7538. if (chan == 1 || chan == 6 || chan == 11)
  7539. cand = chan;
  7540. }
  7541. if (cand) {
  7542. wpa_dbg(wpa_s, MSG_DEBUG,
  7543. "P2P: Update Listen channel to %u based on operating channel",
  7544. cand);
  7545. p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
  7546. }
  7547. }
  7548. static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
  7549. {
  7550. struct hostapd_config *conf;
  7551. struct p2p_go_neg_results params;
  7552. struct csa_settings csa_settings;
  7553. struct wpa_ssid *current_ssid = wpa_s->current_ssid;
  7554. int old_freq = current_ssid->frequency;
  7555. int ret;
  7556. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
  7557. wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
  7558. return -1;
  7559. }
  7560. /*
  7561. * TODO: This function may not always work correctly. For example,
  7562. * when we have a running GO and a BSS on a DFS channel.
  7563. */
  7564. if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
  7565. wpa_dbg(wpa_s, MSG_DEBUG,
  7566. "P2P CSA: Failed to select new frequency for GO");
  7567. return -1;
  7568. }
  7569. if (current_ssid->frequency == params.freq) {
  7570. wpa_dbg(wpa_s, MSG_DEBUG,
  7571. "P2P CSA: Selected same frequency - not moving GO");
  7572. return 0;
  7573. }
  7574. conf = hostapd_config_defaults();
  7575. if (!conf) {
  7576. wpa_dbg(wpa_s, MSG_DEBUG,
  7577. "P2P CSA: Failed to allocate default config");
  7578. return -1;
  7579. }
  7580. current_ssid->frequency = params.freq;
  7581. if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
  7582. wpa_dbg(wpa_s, MSG_DEBUG,
  7583. "P2P CSA: Failed to create new GO config");
  7584. ret = -1;
  7585. goto out;
  7586. }
  7587. if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
  7588. wpa_dbg(wpa_s, MSG_DEBUG,
  7589. "P2P CSA: CSA to a different band is not supported");
  7590. ret = -1;
  7591. goto out;
  7592. }
  7593. os_memset(&csa_settings, 0, sizeof(csa_settings));
  7594. csa_settings.cs_count = P2P_GO_CSA_COUNT;
  7595. csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
  7596. csa_settings.freq_params.freq = params.freq;
  7597. csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
  7598. csa_settings.freq_params.ht_enabled = conf->ieee80211n;
  7599. csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
  7600. if (conf->ieee80211ac) {
  7601. int freq1 = 0, freq2 = 0;
  7602. u8 chan, opclass;
  7603. if (ieee80211_freq_to_channel_ext(params.freq,
  7604. conf->secondary_channel,
  7605. conf->vht_oper_chwidth,
  7606. &opclass, &chan) ==
  7607. NUM_HOSTAPD_MODES) {
  7608. wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
  7609. ret = -1;
  7610. goto out;
  7611. }
  7612. if (conf->vht_oper_centr_freq_seg0_idx)
  7613. freq1 = ieee80211_chan_to_freq(
  7614. NULL, opclass,
  7615. conf->vht_oper_centr_freq_seg0_idx);
  7616. if (conf->vht_oper_centr_freq_seg1_idx)
  7617. freq2 = ieee80211_chan_to_freq(
  7618. NULL, opclass,
  7619. conf->vht_oper_centr_freq_seg1_idx);
  7620. if (freq1 < 0 || freq2 < 0) {
  7621. wpa_dbg(wpa_s, MSG_DEBUG,
  7622. "P2P CSA: Selected invalid VHT center freqs");
  7623. ret = -1;
  7624. goto out;
  7625. }
  7626. csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
  7627. csa_settings.freq_params.center_freq1 = freq1;
  7628. csa_settings.freq_params.center_freq2 = freq2;
  7629. switch (conf->vht_oper_chwidth) {
  7630. case VHT_CHANWIDTH_80MHZ:
  7631. case VHT_CHANWIDTH_80P80MHZ:
  7632. csa_settings.freq_params.bandwidth = 80;
  7633. break;
  7634. case VHT_CHANWIDTH_160MHZ:
  7635. csa_settings.freq_params.bandwidth = 160;
  7636. break;
  7637. }
  7638. }
  7639. ret = ap_switch_channel(wpa_s, &csa_settings);
  7640. out:
  7641. current_ssid->frequency = old_freq;
  7642. hostapd_config_free(conf);
  7643. return ret;
  7644. }
  7645. static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
  7646. {
  7647. struct p2p_go_neg_results params;
  7648. struct wpa_ssid *current_ssid = wpa_s->current_ssid;
  7649. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
  7650. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
  7651. current_ssid->frequency);
  7652. /* Stop the AP functionality */
  7653. /* TODO: Should do this in a way that does not indicated to possible
  7654. * P2P Clients in the group that the group is terminated. */
  7655. wpa_supplicant_ap_deinit(wpa_s);
  7656. /* Reselect the GO frequency */
  7657. if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
  7658. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
  7659. wpas_p2p_group_delete(wpa_s,
  7660. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
  7661. return;
  7662. }
  7663. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
  7664. params.freq);
  7665. if (params.freq &&
  7666. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  7667. wpa_printf(MSG_DEBUG,
  7668. "P2P: Selected freq (%u MHz) is not valid for P2P",
  7669. params.freq);
  7670. wpas_p2p_group_delete(wpa_s,
  7671. P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
  7672. return;
  7673. }
  7674. /* Update the frequency */
  7675. current_ssid->frequency = params.freq;
  7676. wpa_s->connect_without_scan = current_ssid;
  7677. wpa_s->reassociate = 1;
  7678. wpa_s->disconnected = 0;
  7679. wpa_supplicant_req_scan(wpa_s, 0, 0);
  7680. }
  7681. static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
  7682. {
  7683. struct wpa_supplicant *wpa_s = eloop_ctx;
  7684. if (!wpa_s->ap_iface || !wpa_s->current_ssid)
  7685. return;
  7686. wpas_p2p_go_update_common_freqs(wpa_s);
  7687. /* Do not move GO in the middle of a CSA */
  7688. if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
  7689. wpa_printf(MSG_DEBUG,
  7690. "P2P: CSA is in progress - not moving GO");
  7691. return;
  7692. }
  7693. /*
  7694. * First, try a channel switch flow. If it is not supported or fails,
  7695. * take down the GO and bring it up again.
  7696. */
  7697. if (wpas_p2p_move_go_csa(wpa_s) < 0)
  7698. wpas_p2p_move_go_no_csa(wpa_s);
  7699. }
  7700. static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
  7701. {
  7702. struct wpa_supplicant *wpa_s = eloop_ctx;
  7703. struct wpa_used_freq_data *freqs = NULL;
  7704. unsigned int num = wpa_s->num_multichan_concurrent;
  7705. freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
  7706. if (!freqs)
  7707. return;
  7708. num = get_shared_radio_freqs_data(wpa_s, freqs, num);
  7709. /* Previous attempt to move a GO was not possible -- try again. */
  7710. wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
  7711. WPAS_P2P_CHANNEL_UPDATE_ANY);
  7712. os_free(freqs);
  7713. }
  7714. /*
  7715. * Consider moving a GO from its currently used frequency:
  7716. * 1. It is possible that due to regulatory consideration the frequency
  7717. * can no longer be used and there is a need to evacuate the GO.
  7718. * 2. It is possible that due to MCC considerations, it would be preferable
  7719. * to move the GO to a channel that is currently used by some other
  7720. * station interface.
  7721. *
  7722. * In case a frequency that became invalid is once again valid, cancel a
  7723. * previously initiated GO frequency change.
  7724. */
  7725. static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
  7726. struct wpa_used_freq_data *freqs,
  7727. unsigned int num)
  7728. {
  7729. unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
  7730. unsigned int timeout;
  7731. int freq;
  7732. wpas_p2p_go_update_common_freqs(wpa_s);
  7733. freq = wpa_s->current_ssid->frequency;
  7734. for (i = 0, invalid_freq = 0; i < num; i++) {
  7735. if (freqs[i].freq == freq) {
  7736. flags = freqs[i].flags;
  7737. /* The channel is invalid, must change it */
  7738. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  7739. wpa_dbg(wpa_s, MSG_DEBUG,
  7740. "P2P: Freq=%d MHz no longer valid for GO",
  7741. freq);
  7742. invalid_freq = 1;
  7743. }
  7744. } else if (freqs[i].flags == 0) {
  7745. /* Freq is not used by any other station interface */
  7746. continue;
  7747. } else if (!p2p_supported_freq(wpa_s->global->p2p,
  7748. freqs[i].freq)) {
  7749. /* Freq is not valid for P2P use cases */
  7750. continue;
  7751. } else if (wpa_s->conf->p2p_go_freq_change_policy ==
  7752. P2P_GO_FREQ_MOVE_SCM) {
  7753. policy_move = 1;
  7754. } else if (wpa_s->conf->p2p_go_freq_change_policy ==
  7755. P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
  7756. wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
  7757. policy_move = 1;
  7758. } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
  7759. P2P_GO_FREQ_MOVE_SCM_ECSA) &&
  7760. wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
  7761. if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
  7762. policy_move = 1;
  7763. } else if ((wpa_s->drv_flags &
  7764. WPA_DRIVER_FLAGS_AP_CSA) &&
  7765. wpas_p2p_go_clients_support_ecsa(wpa_s)) {
  7766. u8 chan;
  7767. /*
  7768. * We do not support CSA between bands, so move
  7769. * GO only within the same band.
  7770. */
  7771. if (wpa_s->ap_iface->current_mode->mode ==
  7772. ieee80211_freq_to_chan(freqs[i].freq,
  7773. &chan))
  7774. policy_move = 1;
  7775. }
  7776. }
  7777. }
  7778. wpa_dbg(wpa_s, MSG_DEBUG,
  7779. "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
  7780. invalid_freq, policy_move, flags);
  7781. /*
  7782. * The channel is valid, or we are going to have a policy move, so
  7783. * cancel timeout.
  7784. */
  7785. if (!invalid_freq || policy_move) {
  7786. wpa_dbg(wpa_s, MSG_DEBUG,
  7787. "P2P: Cancel a GO move from freq=%d MHz", freq);
  7788. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  7789. if (wpas_p2p_in_progress(wpa_s)) {
  7790. wpa_dbg(wpa_s, MSG_DEBUG,
  7791. "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
  7792. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
  7793. wpa_s, NULL);
  7794. eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
  7795. wpas_p2p_reconsider_moving_go,
  7796. wpa_s, NULL);
  7797. return;
  7798. }
  7799. }
  7800. if (!invalid_freq && (!policy_move || flags != 0)) {
  7801. wpa_dbg(wpa_s, MSG_DEBUG,
  7802. "P2P: Not initiating a GO frequency change");
  7803. return;
  7804. }
  7805. /*
  7806. * Do not consider moving GO if it is in the middle of a CSA. When the
  7807. * CSA is finished this flow should be retriggered.
  7808. */
  7809. if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
  7810. wpa_dbg(wpa_s, MSG_DEBUG,
  7811. "P2P: Not initiating a GO frequency change - CSA is in progress");
  7812. return;
  7813. }
  7814. if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
  7815. timeout = P2P_GO_FREQ_CHANGE_TIME;
  7816. else
  7817. timeout = 0;
  7818. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
  7819. freq, timeout);
  7820. eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
  7821. eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
  7822. }
  7823. static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
  7824. struct wpa_used_freq_data *freqs,
  7825. unsigned int num,
  7826. enum wpas_p2p_channel_update_trig trig)
  7827. {
  7828. struct wpa_supplicant *ifs;
  7829. eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
  7830. NULL);
  7831. /*
  7832. * Travers all the radio interfaces, and for each GO interface, check
  7833. * if there is a need to move the GO from the frequency it is using,
  7834. * or in case the frequency is valid again, cancel the evacuation flow.
  7835. */
  7836. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  7837. radio_list) {
  7838. if (ifs->current_ssid == NULL ||
  7839. ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
  7840. continue;
  7841. /*
  7842. * The GO was just started or completed channel switch, no need
  7843. * to move it.
  7844. */
  7845. if (wpa_s == ifs &&
  7846. (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
  7847. trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
  7848. wpa_dbg(wpa_s, MSG_DEBUG,
  7849. "P2P: GO move - schedule re-consideration");
  7850. eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
  7851. wpas_p2p_reconsider_moving_go,
  7852. wpa_s, NULL);
  7853. continue;
  7854. }
  7855. wpas_p2p_consider_moving_one_go(ifs, freqs, num);
  7856. }
  7857. }
  7858. void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
  7859. {
  7860. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  7861. return;
  7862. wpas_p2p_update_channel_list(wpa_s,
  7863. WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
  7864. }
  7865. void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
  7866. {
  7867. if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
  7868. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
  7869. "the management interface is being removed");
  7870. wpas_p2p_deinit_global(wpa_s->global);
  7871. }
  7872. }
  7873. void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
  7874. {
  7875. if (wpa_s->ap_iface->bss)
  7876. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  7877. wpas_p2p_group_deinit(wpa_s);
  7878. }
  7879. int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
  7880. unsigned int period, unsigned int interval,
  7881. unsigned int count)
  7882. {
  7883. struct p2p_data *p2p = wpa_s->global->p2p;
  7884. u8 *device_types;
  7885. size_t dev_types_len;
  7886. struct wpabuf *buf;
  7887. int ret;
  7888. if (wpa_s->p2p_lo_started) {
  7889. wpa_dbg(wpa_s, MSG_DEBUG,
  7890. "P2P Listen offload is already started");
  7891. return 0;
  7892. }
  7893. if (wpa_s->global->p2p == NULL ||
  7894. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
  7895. wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
  7896. return -1;
  7897. }
  7898. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  7899. wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
  7900. freq);
  7901. return -1;
  7902. }
  7903. /* Get device type */
  7904. dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
  7905. WPS_DEV_TYPE_LEN;
  7906. device_types = os_malloc(dev_types_len);
  7907. if (!device_types)
  7908. return -1;
  7909. os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
  7910. os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
  7911. wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
  7912. /* Get Probe Response IE(s) */
  7913. buf = p2p_build_probe_resp_template(p2p, freq);
  7914. if (!buf) {
  7915. os_free(device_types);
  7916. return -1;
  7917. }
  7918. ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
  7919. device_types, dev_types_len,
  7920. wpabuf_mhead_u8(buf), wpabuf_len(buf));
  7921. if (ret < 0)
  7922. wpa_dbg(wpa_s, MSG_DEBUG,
  7923. "P2P: Failed to start P2P listen offload");
  7924. os_free(device_types);
  7925. wpabuf_free(buf);
  7926. if (ret == 0) {
  7927. wpa_s->p2p_lo_started = 1;
  7928. /* Stop current P2P listen if any */
  7929. wpas_stop_listen(wpa_s);
  7930. }
  7931. return ret;
  7932. }
  7933. int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
  7934. {
  7935. int ret;
  7936. if (!wpa_s->p2p_lo_started)
  7937. return 0;
  7938. ret = wpa_drv_p2p_lo_stop(wpa_s);
  7939. if (ret < 0)
  7940. wpa_dbg(wpa_s, MSG_DEBUG,
  7941. "P2P: Failed to stop P2P listen offload");
  7942. wpa_s->p2p_lo_started = 0;
  7943. return ret;
  7944. }