p2p_supplicant.c 210 KB

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