p2p_supplicant.c 252 KB

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