p2p_supplicant.c 182 KB

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