p2p_supplicant.c 219 KB

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