p2p_supplicant.c 234 KB

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