p2p_supplicant.c 229 KB

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