p2p_supplicant.c 213 KB

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