p2p_supplicant.c 179 KB

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