p2p_supplicant.c 212 KB

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