p2p_supplicant.c 227 KB

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