p2p_supplicant.c 221 KB

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