p2p_supplicant.c 219 KB

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