p2p_supplicant.c 216 KB

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