p2p_supplicant.c 219 KB

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