p2p_supplicant.c 218 KB

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