p2p_supplicant.c 222 KB

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