p2p_supplicant.c 227 KB

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