p2p_supplicant.c 177 KB

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