p2p_supplicant.c 194 KB

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