p2p_supplicant.c 226 KB

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