p2p_supplicant.c 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eloop.h"
  11. #include "common/ieee802_11_common.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/wpa_ctrl.h"
  14. #include "wps/wps_i.h"
  15. #include "p2p/p2p.h"
  16. #include "ap/hostapd.h"
  17. #include "ap/ap_config.h"
  18. #include "ap/p2p_hostapd.h"
  19. #include "eapol_supp/eapol_supp_sm.h"
  20. #include "rsn_supp/wpa.h"
  21. #include "wpa_supplicant_i.h"
  22. #include "driver_i.h"
  23. #include "ap.h"
  24. #include "config_ssid.h"
  25. #include "config.h"
  26. #include "notify.h"
  27. #include "scan.h"
  28. #include "bss.h"
  29. #include "offchannel.h"
  30. #include "wps_supplicant.h"
  31. #include "p2p_supplicant.h"
  32. /*
  33. * How many times to try to scan to find the GO before giving up on join
  34. * request.
  35. */
  36. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  37. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  38. #ifndef P2P_MAX_CLIENT_IDLE
  39. /*
  40. * How many seconds to try to reconnect to the GO when connection in P2P client
  41. * role has been lost.
  42. */
  43. #define P2P_MAX_CLIENT_IDLE 10
  44. #endif /* P2P_MAX_CLIENT_IDLE */
  45. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  46. /*
  47. * How many seconds to wait for initial 4-way handshake to get completed after
  48. * WPS provisioning step.
  49. */
  50. #define P2P_MAX_INITIAL_CONN_WAIT 10
  51. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  52. #ifndef P2P_CONCURRENT_SEARCH_DELAY
  53. #define P2P_CONCURRENT_SEARCH_DELAY 500
  54. #endif /* P2P_CONCURRENT_SEARCH_DELAY */
  55. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  56. enum p2p_group_removal_reason {
  57. P2P_GROUP_REMOVAL_UNKNOWN,
  58. P2P_GROUP_REMOVAL_SILENT,
  59. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  60. P2P_GROUP_REMOVAL_REQUESTED,
  61. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  62. P2P_GROUP_REMOVAL_UNAVAILABLE,
  63. P2P_GROUP_REMOVAL_GO_ENDING_SESSION
  64. };
  65. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  66. static struct wpa_supplicant *
  67. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  68. int go);
  69. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  70. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
  71. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  72. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  73. const u8 *dev_addr, enum p2p_wps_method wps_method,
  74. int auto_join);
  75. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  76. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  77. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  78. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  79. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  80. void *timeout_ctx);
  81. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  82. int group_added);
  83. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  84. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  85. int freq)
  86. {
  87. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  88. return;
  89. if (freq > 0 &&
  90. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
  91. wpa_s->parent->conf->p2p_ignore_shared_freq)
  92. freq = 0;
  93. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  94. }
  95. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  96. struct wpa_scan_results *scan_res)
  97. {
  98. size_t i;
  99. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  100. return;
  101. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  102. (int) scan_res->num);
  103. for (i = 0; i < scan_res->num; i++) {
  104. struct wpa_scan_res *bss = scan_res->res[i];
  105. struct os_time time_tmp_age, entry_ts;
  106. time_tmp_age.sec = bss->age / 1000;
  107. time_tmp_age.usec = (bss->age % 1000) * 1000;
  108. os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  109. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  110. bss->freq, &entry_ts, bss->level,
  111. (const u8 *) (bss + 1),
  112. bss->ie_len) > 0)
  113. break;
  114. }
  115. p2p_scan_res_handled(wpa_s->global->p2p);
  116. }
  117. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  118. unsigned int num_req_dev_types,
  119. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  120. {
  121. struct wpa_supplicant *wpa_s = ctx;
  122. struct wpa_supplicant *ifs;
  123. struct wpa_driver_scan_params params;
  124. int ret;
  125. struct wpabuf *wps_ie, *ies;
  126. int social_channels[] = { 2412, 2437, 2462, 0, 0 };
  127. size_t ielen;
  128. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  129. return -1;
  130. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  131. if (ifs->sta_scan_pending &&
  132. (wpas_scan_scheduled(ifs) || ifs->scanning) &&
  133. wpas_p2p_in_progress(wpa_s) == 2) {
  134. wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
  135. "pending station mode scan to be "
  136. "completed on interface %s", ifs->ifname);
  137. wpa_s->global->p2p_cb_on_scan_complete = 1;
  138. wpa_supplicant_req_scan(ifs, 0, 0);
  139. return 1;
  140. }
  141. }
  142. os_memset(&params, 0, sizeof(params));
  143. /* P2P Wildcard SSID */
  144. params.num_ssids = 1;
  145. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  146. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  147. wpa_s->wps->dev.p2p = 1;
  148. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  149. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  150. num_req_dev_types, req_dev_types);
  151. if (wps_ie == NULL)
  152. return -1;
  153. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  154. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  155. if (ies == NULL) {
  156. wpabuf_free(wps_ie);
  157. return -1;
  158. }
  159. wpabuf_put_buf(ies, wps_ie);
  160. wpabuf_free(wps_ie);
  161. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
  162. params.p2p_probe = 1;
  163. params.extra_ies = wpabuf_head(ies);
  164. params.extra_ies_len = wpabuf_len(ies);
  165. switch (type) {
  166. case P2P_SCAN_SOCIAL:
  167. params.freqs = social_channels;
  168. break;
  169. case P2P_SCAN_FULL:
  170. break;
  171. case P2P_SCAN_SOCIAL_PLUS_ONE:
  172. social_channels[3] = freq;
  173. params.freqs = social_channels;
  174. break;
  175. }
  176. ret = wpa_drv_scan(wpa_s, &params);
  177. wpabuf_free(ies);
  178. if (ret) {
  179. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  180. if (ifs->scanning ||
  181. ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
  182. wpa_s->global->p2p_cb_on_scan_complete = 1;
  183. ret = 1;
  184. break;
  185. }
  186. }
  187. } else {
  188. os_get_time(&wpa_s->scan_trigger_time);
  189. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  190. }
  191. return ret;
  192. }
  193. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  194. {
  195. switch (p2p_group_interface) {
  196. case P2P_GROUP_INTERFACE_PENDING:
  197. return WPA_IF_P2P_GROUP;
  198. case P2P_GROUP_INTERFACE_GO:
  199. return WPA_IF_P2P_GO;
  200. case P2P_GROUP_INTERFACE_CLIENT:
  201. return WPA_IF_P2P_CLIENT;
  202. }
  203. return WPA_IF_P2P_GROUP;
  204. }
  205. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  206. const u8 *ssid,
  207. size_t ssid_len, int *go)
  208. {
  209. struct wpa_ssid *s;
  210. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  211. for (s = wpa_s->conf->ssid; s; s = s->next) {
  212. if (s->disabled != 0 || !s->p2p_group ||
  213. s->ssid_len != ssid_len ||
  214. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  215. continue;
  216. if (s->mode == WPAS_MODE_P2P_GO &&
  217. s != wpa_s->current_ssid)
  218. continue;
  219. if (go)
  220. *go = s->mode == WPAS_MODE_P2P_GO;
  221. return wpa_s;
  222. }
  223. }
  224. return NULL;
  225. }
  226. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  227. enum p2p_group_removal_reason removal_reason)
  228. {
  229. struct wpa_ssid *ssid;
  230. char *gtype;
  231. const char *reason;
  232. ssid = wpa_s->current_ssid;
  233. if (ssid == NULL) {
  234. /*
  235. * The current SSID was not known, but there may still be a
  236. * pending P2P group interface waiting for provisioning or a
  237. * P2P group that is trying to reconnect.
  238. */
  239. ssid = wpa_s->conf->ssid;
  240. while (ssid) {
  241. if (ssid->p2p_group && ssid->disabled != 2)
  242. break;
  243. ssid = ssid->next;
  244. }
  245. if (ssid == NULL &&
  246. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  247. {
  248. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  249. "not found");
  250. return -1;
  251. }
  252. }
  253. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  254. gtype = "GO";
  255. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  256. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  257. wpa_s->reassociate = 0;
  258. wpa_s->disconnected = 1;
  259. wpa_supplicant_deauthenticate(wpa_s,
  260. WLAN_REASON_DEAUTH_LEAVING);
  261. gtype = "client";
  262. } else
  263. gtype = "GO";
  264. if (wpa_s->cross_connect_in_use) {
  265. wpa_s->cross_connect_in_use = 0;
  266. wpa_msg_global(wpa_s->parent, MSG_INFO,
  267. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  268. wpa_s->ifname, wpa_s->cross_connect_uplink);
  269. }
  270. switch (removal_reason) {
  271. case P2P_GROUP_REMOVAL_REQUESTED:
  272. reason = " reason=REQUESTED";
  273. break;
  274. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  275. reason = " reason=FORMATION_FAILED";
  276. break;
  277. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  278. reason = " reason=IDLE";
  279. break;
  280. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  281. reason = " reason=UNAVAILABLE";
  282. break;
  283. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  284. reason = " reason=GO_ENDING_SESSION";
  285. break;
  286. default:
  287. reason = "";
  288. break;
  289. }
  290. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  291. wpa_msg_global(wpa_s->parent, MSG_INFO,
  292. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  293. wpa_s->ifname, gtype, reason);
  294. }
  295. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  296. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  297. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  298. wpa_s->parent, NULL) > 0) {
  299. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  300. "timeout");
  301. wpa_s->p2p_in_provisioning = 0;
  302. }
  303. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  304. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  305. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  306. struct wpa_global *global;
  307. char *ifname;
  308. enum wpa_driver_if_type type;
  309. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  310. wpa_s->ifname);
  311. global = wpa_s->global;
  312. ifname = os_strdup(wpa_s->ifname);
  313. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  314. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  315. wpa_s = global->ifaces;
  316. if (wpa_s && ifname)
  317. wpa_drv_if_remove(wpa_s, type, ifname);
  318. os_free(ifname);
  319. return 1;
  320. }
  321. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  322. if (ssid && (ssid->p2p_group ||
  323. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  324. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  325. int id = ssid->id;
  326. if (ssid == wpa_s->current_ssid) {
  327. wpa_sm_set_config(wpa_s->wpa, NULL);
  328. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  329. wpa_s->current_ssid = NULL;
  330. }
  331. /*
  332. * Networks objects created during any P2P activities are not
  333. * exposed out as they might/will confuse certain non-P2P aware
  334. * applications since these network objects won't behave like
  335. * regular ones.
  336. *
  337. * Likewise, we don't send out network removed signals for such
  338. * network objects.
  339. */
  340. wpa_config_remove_network(wpa_s->conf, id);
  341. wpa_supplicant_clear_status(wpa_s);
  342. wpa_supplicant_cancel_sched_scan(wpa_s);
  343. wpa_s->sta_scan_pending = 0;
  344. } else {
  345. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  346. "found");
  347. }
  348. if (wpa_s->ap_iface)
  349. wpa_supplicant_ap_deinit(wpa_s);
  350. else
  351. wpa_drv_deinit_p2p_cli(wpa_s);
  352. return 0;
  353. }
  354. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  355. u8 *go_dev_addr,
  356. const u8 *ssid, size_t ssid_len)
  357. {
  358. struct wpa_bss *bss;
  359. const u8 *bssid;
  360. struct wpabuf *p2p;
  361. u8 group_capab;
  362. const u8 *addr;
  363. if (wpa_s->go_params)
  364. bssid = wpa_s->go_params->peer_interface_addr;
  365. else
  366. bssid = wpa_s->bssid;
  367. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  368. if (bss == NULL) {
  369. u8 iface_addr[ETH_ALEN];
  370. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  371. iface_addr) == 0)
  372. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  373. }
  374. if (bss == NULL) {
  375. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  376. "group is persistent - BSS " MACSTR " not found",
  377. MAC2STR(bssid));
  378. return 0;
  379. }
  380. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  381. if (p2p == NULL) {
  382. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  383. "group is persistent - BSS " MACSTR
  384. " did not include P2P IE", MAC2STR(bssid));
  385. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  386. (u8 *) (bss + 1), bss->ie_len);
  387. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  388. ((u8 *) bss + 1) + bss->ie_len,
  389. bss->beacon_ie_len);
  390. return 0;
  391. }
  392. group_capab = p2p_get_group_capab(p2p);
  393. addr = p2p_get_go_dev_addr(p2p);
  394. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  395. "group_capab=0x%x", group_capab);
  396. if (addr) {
  397. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  398. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  399. MAC2STR(addr));
  400. } else
  401. os_memset(go_dev_addr, 0, ETH_ALEN);
  402. wpabuf_free(p2p);
  403. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  404. "go_dev_addr=" MACSTR,
  405. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  406. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  407. }
  408. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  409. struct wpa_ssid *ssid,
  410. const u8 *go_dev_addr)
  411. {
  412. struct wpa_ssid *s;
  413. int changed = 0;
  414. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  415. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  416. for (s = wpa_s->conf->ssid; s; s = s->next) {
  417. if (s->disabled == 2 &&
  418. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  419. s->ssid_len == ssid->ssid_len &&
  420. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  421. break;
  422. }
  423. if (s) {
  424. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  425. "entry");
  426. if (ssid->passphrase && !s->passphrase)
  427. changed = 1;
  428. else if (ssid->passphrase && s->passphrase &&
  429. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  430. changed = 1;
  431. } else {
  432. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  433. "entry");
  434. changed = 1;
  435. s = wpa_config_add_network(wpa_s->conf);
  436. if (s == NULL)
  437. return -1;
  438. /*
  439. * Instead of network_added we emit persistent_group_added
  440. * notification. Also to keep the defense checks in
  441. * persistent_group obj registration method, we set the
  442. * relevant flags in s to designate it as a persistent group.
  443. */
  444. s->p2p_group = 1;
  445. s->p2p_persistent_group = 1;
  446. wpas_notify_persistent_group_added(wpa_s, s);
  447. wpa_config_set_network_defaults(s);
  448. }
  449. s->p2p_group = 1;
  450. s->p2p_persistent_group = 1;
  451. s->disabled = 2;
  452. s->bssid_set = 1;
  453. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  454. s->mode = ssid->mode;
  455. s->auth_alg = WPA_AUTH_ALG_OPEN;
  456. s->key_mgmt = WPA_KEY_MGMT_PSK;
  457. s->proto = WPA_PROTO_RSN;
  458. s->pairwise_cipher = WPA_CIPHER_CCMP;
  459. s->export_keys = 1;
  460. if (ssid->passphrase) {
  461. os_free(s->passphrase);
  462. s->passphrase = os_strdup(ssid->passphrase);
  463. }
  464. if (ssid->psk_set) {
  465. s->psk_set = 1;
  466. os_memcpy(s->psk, ssid->psk, 32);
  467. }
  468. if (s->passphrase && !s->psk_set)
  469. wpa_config_update_psk(s);
  470. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  471. os_free(s->ssid);
  472. s->ssid = os_malloc(ssid->ssid_len);
  473. }
  474. if (s->ssid) {
  475. s->ssid_len = ssid->ssid_len;
  476. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  477. }
  478. #ifndef CONFIG_NO_CONFIG_WRITE
  479. if (changed && wpa_s->conf->update_config &&
  480. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  481. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  482. }
  483. #endif /* CONFIG_NO_CONFIG_WRITE */
  484. return s->id;
  485. }
  486. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  487. const u8 *addr)
  488. {
  489. struct wpa_ssid *ssid, *s;
  490. u8 *n;
  491. size_t i;
  492. int found = 0;
  493. ssid = wpa_s->current_ssid;
  494. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  495. !ssid->p2p_persistent_group)
  496. return;
  497. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  498. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  499. continue;
  500. if (s->ssid_len == ssid->ssid_len &&
  501. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  502. break;
  503. }
  504. if (s == NULL)
  505. return;
  506. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  507. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
  508. ETH_ALEN) != 0)
  509. continue;
  510. if (i == s->num_p2p_clients - 1)
  511. return; /* already the most recent entry */
  512. /* move the entry to mark it most recent */
  513. os_memmove(s->p2p_client_list + i * ETH_ALEN,
  514. s->p2p_client_list + (i + 1) * ETH_ALEN,
  515. (s->num_p2p_clients - i - 1) * ETH_ALEN);
  516. os_memcpy(s->p2p_client_list +
  517. (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
  518. found = 1;
  519. break;
  520. }
  521. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  522. n = os_realloc_array(s->p2p_client_list,
  523. s->num_p2p_clients + 1, ETH_ALEN);
  524. if (n == NULL)
  525. return;
  526. os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
  527. s->p2p_client_list = n;
  528. s->num_p2p_clients++;
  529. } else if (!found) {
  530. /* Not enough room for an additional entry - drop the oldest
  531. * entry */
  532. os_memmove(s->p2p_client_list,
  533. s->p2p_client_list + ETH_ALEN,
  534. (s->num_p2p_clients - 1) * ETH_ALEN);
  535. os_memcpy(s->p2p_client_list +
  536. (s->num_p2p_clients - 1) * ETH_ALEN,
  537. addr, ETH_ALEN);
  538. }
  539. #ifndef CONFIG_NO_CONFIG_WRITE
  540. if (wpa_s->parent->conf->update_config &&
  541. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  542. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  543. #endif /* CONFIG_NO_CONFIG_WRITE */
  544. }
  545. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  546. int success)
  547. {
  548. struct wpa_ssid *ssid;
  549. const char *ssid_txt;
  550. int client;
  551. int persistent;
  552. u8 go_dev_addr[ETH_ALEN];
  553. int network_id = -1;
  554. /*
  555. * This callback is likely called for the main interface. Update wpa_s
  556. * to use the group interface if a new interface was created for the
  557. * group.
  558. */
  559. if (wpa_s->global->p2p_group_formation)
  560. wpa_s = wpa_s->global->p2p_group_formation;
  561. wpa_s->global->p2p_group_formation = NULL;
  562. wpa_s->p2p_in_provisioning = 0;
  563. if (!success) {
  564. wpa_msg_global(wpa_s->parent, MSG_INFO,
  565. P2P_EVENT_GROUP_FORMATION_FAILURE);
  566. wpas_p2p_group_delete(wpa_s,
  567. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  568. return;
  569. }
  570. wpa_msg_global(wpa_s->parent, MSG_INFO,
  571. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  572. ssid = wpa_s->current_ssid;
  573. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  574. ssid->mode = WPAS_MODE_P2P_GO;
  575. p2p_group_notif_formation_done(wpa_s->p2p_group);
  576. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  577. }
  578. persistent = 0;
  579. if (ssid) {
  580. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  581. client = ssid->mode == WPAS_MODE_INFRA;
  582. if (ssid->mode == WPAS_MODE_P2P_GO) {
  583. persistent = ssid->p2p_persistent_group;
  584. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  585. ETH_ALEN);
  586. } else
  587. persistent = wpas_p2p_persistent_group(wpa_s,
  588. go_dev_addr,
  589. ssid->ssid,
  590. ssid->ssid_len);
  591. } else {
  592. ssid_txt = "";
  593. client = wpa_s->p2p_group_interface ==
  594. P2P_GROUP_INTERFACE_CLIENT;
  595. os_memset(go_dev_addr, 0, ETH_ALEN);
  596. }
  597. wpa_s->show_group_started = 0;
  598. if (client) {
  599. /*
  600. * Indicate event only after successfully completed 4-way
  601. * handshake, i.e., when the interface is ready for data
  602. * packets.
  603. */
  604. wpa_s->show_group_started = 1;
  605. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  606. char psk[65];
  607. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  608. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  609. "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  610. MACSTR "%s",
  611. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  612. MAC2STR(go_dev_addr),
  613. persistent ? " [PERSISTENT]" : "");
  614. wpas_p2p_cross_connect_setup(wpa_s);
  615. wpas_p2p_set_group_idle_timeout(wpa_s);
  616. } else {
  617. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  618. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  619. "go_dev_addr=" MACSTR "%s",
  620. wpa_s->ifname, ssid_txt,
  621. ssid ? ssid->frequency : 0,
  622. ssid && ssid->passphrase ? ssid->passphrase : "",
  623. MAC2STR(go_dev_addr),
  624. persistent ? " [PERSISTENT]" : "");
  625. wpas_p2p_cross_connect_setup(wpa_s);
  626. wpas_p2p_set_group_idle_timeout(wpa_s);
  627. }
  628. if (persistent)
  629. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  630. ssid, go_dev_addr);
  631. if (network_id < 0 && ssid)
  632. network_id = ssid->id;
  633. if (!client)
  634. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  635. }
  636. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  637. unsigned int freq,
  638. const u8 *dst, const u8 *src,
  639. const u8 *bssid,
  640. const u8 *data, size_t data_len,
  641. enum offchannel_send_action_result
  642. result)
  643. {
  644. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  645. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  646. return;
  647. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  648. return;
  649. switch (result) {
  650. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  651. res = P2P_SEND_ACTION_SUCCESS;
  652. break;
  653. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  654. res = P2P_SEND_ACTION_NO_ACK;
  655. break;
  656. case OFFCHANNEL_SEND_ACTION_FAILED:
  657. res = P2P_SEND_ACTION_FAILED;
  658. break;
  659. }
  660. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  661. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  662. wpa_s->pending_pd_before_join &&
  663. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  664. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  665. wpa_s->p2p_fallback_to_go_neg) {
  666. wpa_s->pending_pd_before_join = 0;
  667. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  668. "during p2p_connect-auto");
  669. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  670. return;
  671. }
  672. }
  673. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  674. const u8 *src, const u8 *bssid, const u8 *buf,
  675. size_t len, unsigned int wait_time)
  676. {
  677. struct wpa_supplicant *wpa_s = ctx;
  678. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  679. wait_time,
  680. wpas_p2p_send_action_tx_status, 1);
  681. }
  682. static void wpas_send_action_done(void *ctx)
  683. {
  684. struct wpa_supplicant *wpa_s = ctx;
  685. offchannel_send_action_done(wpa_s);
  686. }
  687. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  688. struct p2p_go_neg_results *params)
  689. {
  690. if (wpa_s->go_params == NULL) {
  691. wpa_s->go_params = os_malloc(sizeof(*params));
  692. if (wpa_s->go_params == NULL)
  693. return -1;
  694. }
  695. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  696. return 0;
  697. }
  698. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  699. struct p2p_go_neg_results *res)
  700. {
  701. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
  702. MAC2STR(res->peer_interface_addr));
  703. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  704. res->ssid, res->ssid_len);
  705. wpa_supplicant_ap_deinit(wpa_s);
  706. wpas_copy_go_neg_results(wpa_s, res);
  707. if (res->wps_method == WPS_PBC)
  708. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  709. else {
  710. u16 dev_pw_id = DEV_PW_DEFAULT;
  711. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  712. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  713. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  714. wpa_s->p2p_pin, 1, dev_pw_id);
  715. }
  716. }
  717. static void p2p_go_configured(void *ctx, void *data)
  718. {
  719. struct wpa_supplicant *wpa_s = ctx;
  720. struct p2p_go_neg_results *params = data;
  721. struct wpa_ssid *ssid;
  722. int network_id = -1;
  723. ssid = wpa_s->current_ssid;
  724. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  725. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  726. if (wpa_s->global->p2p_group_formation == wpa_s)
  727. wpa_s->global->p2p_group_formation = NULL;
  728. if (os_strlen(params->passphrase) > 0) {
  729. wpa_msg_global(wpa_s->parent, MSG_INFO,
  730. P2P_EVENT_GROUP_STARTED
  731. "%s GO ssid=\"%s\" freq=%d "
  732. "passphrase=\"%s\" go_dev_addr=" MACSTR
  733. "%s", wpa_s->ifname,
  734. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  735. ssid->frequency, params->passphrase,
  736. MAC2STR(wpa_s->global->p2p_dev_addr),
  737. params->persistent_group ?
  738. " [PERSISTENT]" : "");
  739. } else {
  740. char psk[65];
  741. wpa_snprintf_hex(psk, sizeof(psk), params->psk,
  742. sizeof(params->psk));
  743. wpa_msg_global(wpa_s->parent, MSG_INFO,
  744. P2P_EVENT_GROUP_STARTED
  745. "%s GO ssid=\"%s\" freq=%d psk=%s "
  746. "go_dev_addr=" MACSTR "%s",
  747. wpa_s->ifname,
  748. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  749. ssid->frequency, psk,
  750. MAC2STR(wpa_s->global->p2p_dev_addr),
  751. params->persistent_group ?
  752. " [PERSISTENT]" : "");
  753. }
  754. if (params->persistent_group)
  755. network_id = wpas_p2p_store_persistent_group(
  756. wpa_s->parent, ssid,
  757. wpa_s->global->p2p_dev_addr);
  758. if (network_id < 0)
  759. network_id = ssid->id;
  760. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  761. wpas_p2p_cross_connect_setup(wpa_s);
  762. wpas_p2p_set_group_idle_timeout(wpa_s);
  763. return;
  764. }
  765. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  766. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  767. params->peer_interface_addr)) {
  768. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  769. "filtering");
  770. return;
  771. }
  772. if (params->wps_method == WPS_PBC)
  773. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  774. params->peer_device_addr);
  775. else if (wpa_s->p2p_pin[0])
  776. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  777. wpa_s->p2p_pin, NULL, 0, 0);
  778. os_free(wpa_s->go_params);
  779. wpa_s->go_params = NULL;
  780. }
  781. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  782. struct p2p_go_neg_results *params,
  783. int group_formation)
  784. {
  785. struct wpa_ssid *ssid;
  786. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  787. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  788. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  789. "results");
  790. return;
  791. }
  792. ssid = wpa_config_add_network(wpa_s->conf);
  793. if (ssid == NULL) {
  794. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  795. return;
  796. }
  797. wpa_s->show_group_started = 0;
  798. wpa_config_set_network_defaults(ssid);
  799. ssid->temporary = 1;
  800. ssid->p2p_group = 1;
  801. ssid->p2p_persistent_group = params->persistent_group;
  802. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  803. WPAS_MODE_P2P_GO;
  804. ssid->frequency = params->freq;
  805. ssid->ht40 = params->ht40;
  806. ssid->ssid = os_zalloc(params->ssid_len + 1);
  807. if (ssid->ssid) {
  808. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  809. ssid->ssid_len = params->ssid_len;
  810. }
  811. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  812. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  813. ssid->proto = WPA_PROTO_RSN;
  814. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  815. if (os_strlen(params->passphrase) > 0) {
  816. ssid->passphrase = os_strdup(params->passphrase);
  817. if (ssid->passphrase == NULL) {
  818. wpa_msg_global(wpa_s, MSG_ERROR,
  819. "P2P: Failed to copy passphrase for GO");
  820. wpa_config_remove_network(wpa_s->conf, ssid->id);
  821. return;
  822. }
  823. } else
  824. ssid->passphrase = NULL;
  825. ssid->psk_set = params->psk_set;
  826. if (ssid->psk_set)
  827. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  828. else if (ssid->passphrase)
  829. wpa_config_update_psk(ssid);
  830. ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
  831. wpa_s->ap_configured_cb = p2p_go_configured;
  832. wpa_s->ap_configured_cb_ctx = wpa_s;
  833. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  834. wpa_s->connect_without_scan = ssid;
  835. wpa_s->reassociate = 1;
  836. wpa_s->disconnected = 0;
  837. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  838. "start GO)");
  839. wpa_supplicant_req_scan(wpa_s, 0, 0);
  840. }
  841. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  842. const struct wpa_supplicant *src)
  843. {
  844. struct wpa_config *d;
  845. const struct wpa_config *s;
  846. d = dst->conf;
  847. s = src->conf;
  848. #define C(n) if (s->n) d->n = os_strdup(s->n)
  849. C(device_name);
  850. C(manufacturer);
  851. C(model_name);
  852. C(model_number);
  853. C(serial_number);
  854. C(config_methods);
  855. #undef C
  856. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  857. os_memcpy(d->sec_device_type, s->sec_device_type,
  858. sizeof(d->sec_device_type));
  859. d->num_sec_device_types = s->num_sec_device_types;
  860. d->p2p_group_idle = s->p2p_group_idle;
  861. d->p2p_intra_bss = s->p2p_intra_bss;
  862. d->persistent_reconnect = s->persistent_reconnect;
  863. d->max_num_sta = s->max_num_sta;
  864. d->pbc_in_m1 = s->pbc_in_m1;
  865. d->ignore_old_scan_res = s->ignore_old_scan_res;
  866. d->beacon_int = s->beacon_int;
  867. }
  868. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  869. char *ifname, size_t len)
  870. {
  871. char *ifname_ptr = wpa_s->ifname;
  872. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  873. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  874. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  875. }
  876. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  877. if (os_strlen(ifname) >= IFNAMSIZ &&
  878. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  879. /* Try to avoid going over the IFNAMSIZ length limit */
  880. os_snprintf(ifname, sizeof(ifname), "p2p-%d",
  881. wpa_s->p2p_group_idx);
  882. }
  883. }
  884. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  885. enum wpa_driver_if_type type)
  886. {
  887. char ifname[120], force_ifname[120];
  888. if (wpa_s->pending_interface_name[0]) {
  889. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  890. "- skip creation of a new one");
  891. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  892. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  893. "unknown?! ifname='%s'",
  894. wpa_s->pending_interface_name);
  895. return -1;
  896. }
  897. return 0;
  898. }
  899. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  900. force_ifname[0] = '\0';
  901. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  902. ifname);
  903. wpa_s->p2p_group_idx++;
  904. wpa_s->pending_interface_type = type;
  905. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  906. wpa_s->pending_interface_addr, NULL) < 0) {
  907. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  908. "interface");
  909. return -1;
  910. }
  911. if (force_ifname[0]) {
  912. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  913. force_ifname);
  914. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  915. sizeof(wpa_s->pending_interface_name));
  916. } else
  917. os_strlcpy(wpa_s->pending_interface_name, ifname,
  918. sizeof(wpa_s->pending_interface_name));
  919. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  920. MACSTR, wpa_s->pending_interface_name,
  921. MAC2STR(wpa_s->pending_interface_addr));
  922. return 0;
  923. }
  924. static void wpas_p2p_remove_pending_group_interface(
  925. struct wpa_supplicant *wpa_s)
  926. {
  927. if (!wpa_s->pending_interface_name[0] ||
  928. is_zero_ether_addr(wpa_s->pending_interface_addr))
  929. return; /* No pending virtual interface */
  930. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  931. wpa_s->pending_interface_name);
  932. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  933. wpa_s->pending_interface_name);
  934. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  935. wpa_s->pending_interface_name[0] = '\0';
  936. }
  937. static struct wpa_supplicant *
  938. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  939. {
  940. struct wpa_interface iface;
  941. struct wpa_supplicant *group_wpa_s;
  942. if (!wpa_s->pending_interface_name[0]) {
  943. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  944. if (!wpas_p2p_create_iface(wpa_s))
  945. return NULL;
  946. /*
  947. * Something has forced us to remove the pending interface; try
  948. * to create a new one and hope for the best that we will get
  949. * the same local address.
  950. */
  951. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  952. WPA_IF_P2P_CLIENT) < 0)
  953. return NULL;
  954. }
  955. os_memset(&iface, 0, sizeof(iface));
  956. iface.ifname = wpa_s->pending_interface_name;
  957. iface.driver = wpa_s->driver->name;
  958. if (wpa_s->conf->ctrl_interface == NULL &&
  959. wpa_s->parent != wpa_s &&
  960. wpa_s->p2p_mgmt &&
  961. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  962. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  963. else
  964. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  965. iface.driver_param = wpa_s->conf->driver_param;
  966. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  967. if (group_wpa_s == NULL) {
  968. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  969. "wpa_supplicant interface");
  970. return NULL;
  971. }
  972. wpa_s->pending_interface_name[0] = '\0';
  973. group_wpa_s->parent = wpa_s;
  974. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  975. P2P_GROUP_INTERFACE_CLIENT;
  976. wpa_s->global->p2p_group_formation = group_wpa_s;
  977. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  978. return group_wpa_s;
  979. }
  980. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  981. void *timeout_ctx)
  982. {
  983. struct wpa_supplicant *wpa_s = eloop_ctx;
  984. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  985. if (wpa_s->global->p2p)
  986. p2p_group_formation_failed(wpa_s->global->p2p);
  987. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  988. wpa_drv_p2p_group_formation_failed(wpa_s);
  989. wpas_group_formation_completed(wpa_s, 0);
  990. }
  991. void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  992. {
  993. struct wpa_supplicant *wpa_s = ctx;
  994. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  995. wpa_drv_cancel_remain_on_channel(wpa_s);
  996. wpa_s->off_channel_freq = 0;
  997. wpa_s->roc_waiting_drv_freq = 0;
  998. }
  999. if (res->status) {
  1000. wpa_msg_global(wpa_s, MSG_INFO,
  1001. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1002. res->status);
  1003. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1004. wpas_p2p_remove_pending_group_interface(wpa_s);
  1005. return;
  1006. }
  1007. if (wpa_s->p2p_go_ht40)
  1008. res->ht40 = 1;
  1009. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
  1010. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1011. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1012. struct wpa_ssid *ssid;
  1013. ssid = wpa_config_get_network(wpa_s->conf,
  1014. wpa_s->p2p_persistent_id);
  1015. if (ssid && ssid->disabled == 2 &&
  1016. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1017. size_t len = os_strlen(ssid->passphrase);
  1018. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1019. "on requested persistent group");
  1020. os_memcpy(res->passphrase, ssid->passphrase, len);
  1021. res->passphrase[len] = '\0';
  1022. }
  1023. }
  1024. if (wpa_s->create_p2p_iface) {
  1025. struct wpa_supplicant *group_wpa_s =
  1026. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1027. if (group_wpa_s == NULL) {
  1028. wpas_p2p_remove_pending_group_interface(wpa_s);
  1029. return;
  1030. }
  1031. if (group_wpa_s != wpa_s) {
  1032. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1033. sizeof(group_wpa_s->p2p_pin));
  1034. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1035. }
  1036. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1037. wpa_s->pending_interface_name[0] = '\0';
  1038. group_wpa_s->p2p_in_provisioning = 1;
  1039. if (res->role_go)
  1040. wpas_start_wps_go(group_wpa_s, res, 1);
  1041. else
  1042. wpas_start_wps_enrollee(group_wpa_s, res);
  1043. } else {
  1044. wpa_s->p2p_in_provisioning = 1;
  1045. wpa_s->global->p2p_group_formation = wpa_s;
  1046. if (res->role_go)
  1047. wpas_start_wps_go(wpa_s, res, 1);
  1048. else
  1049. wpas_start_wps_enrollee(ctx, res);
  1050. }
  1051. wpa_s->p2p_long_listen = 0;
  1052. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1053. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1054. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1055. (res->peer_config_timeout % 100) * 10000,
  1056. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1057. }
  1058. void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  1059. {
  1060. struct wpa_supplicant *wpa_s = ctx;
  1061. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1062. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  1063. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  1064. }
  1065. void wpas_dev_found(void *ctx, const u8 *addr,
  1066. const struct p2p_peer_info *info,
  1067. int new_device)
  1068. {
  1069. #ifndef CONFIG_NO_STDOUT_DEBUG
  1070. struct wpa_supplicant *wpa_s = ctx;
  1071. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1072. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  1073. " p2p_dev_addr=" MACSTR
  1074. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1075. "dev_capab=0x%x group_capab=0x%x",
  1076. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  1077. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  1078. sizeof(devtype)),
  1079. info->device_name, info->config_methods,
  1080. info->dev_capab, info->group_capab);
  1081. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1082. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  1083. }
  1084. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  1085. {
  1086. struct wpa_supplicant *wpa_s = ctx;
  1087. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  1088. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  1089. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  1090. }
  1091. static void wpas_find_stopped(void *ctx)
  1092. {
  1093. struct wpa_supplicant *wpa_s = ctx;
  1094. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  1095. }
  1096. static int wpas_start_listen(void *ctx, unsigned int freq,
  1097. unsigned int duration,
  1098. const struct wpabuf *probe_resp_ie)
  1099. {
  1100. struct wpa_supplicant *wpa_s = ctx;
  1101. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
  1102. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1103. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1104. "report received Probe Request frames");
  1105. return -1;
  1106. }
  1107. wpa_s->pending_listen_freq = freq;
  1108. wpa_s->pending_listen_duration = duration;
  1109. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  1110. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1111. "to remain on channel (%u MHz) for Listen "
  1112. "state", freq);
  1113. wpa_s->pending_listen_freq = 0;
  1114. return -1;
  1115. }
  1116. wpa_s->off_channel_freq = 0;
  1117. wpa_s->roc_waiting_drv_freq = freq;
  1118. return 0;
  1119. }
  1120. static void wpas_stop_listen(void *ctx)
  1121. {
  1122. struct wpa_supplicant *wpa_s = ctx;
  1123. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1124. wpa_drv_cancel_remain_on_channel(wpa_s);
  1125. wpa_s->off_channel_freq = 0;
  1126. wpa_s->roc_waiting_drv_freq = 0;
  1127. }
  1128. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  1129. wpa_drv_probe_req_report(wpa_s, 0);
  1130. }
  1131. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  1132. {
  1133. struct wpa_supplicant *wpa_s = ctx;
  1134. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
  1135. }
  1136. /*
  1137. * DNS Header section is used only to calculate compression pointers, so the
  1138. * contents of this data does not matter, but the length needs to be reserved
  1139. * in the virtual packet.
  1140. */
  1141. #define DNS_HEADER_LEN 12
  1142. /*
  1143. * 27-octet in-memory packet from P2P specification containing two implied
  1144. * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
  1145. */
  1146. #define P2P_SD_IN_MEMORY_LEN 27
  1147. static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
  1148. u8 **spos, const u8 *end)
  1149. {
  1150. while (*spos < end) {
  1151. u8 val = ((*spos)[0] & 0xc0) >> 6;
  1152. int len;
  1153. if (val == 1 || val == 2) {
  1154. /* These are reserved values in RFC 1035 */
  1155. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1156. "sequence starting with 0x%x", val);
  1157. return -1;
  1158. }
  1159. if (val == 3) {
  1160. u16 offset;
  1161. u8 *spos_tmp;
  1162. /* Offset */
  1163. if (*spos + 2 > end) {
  1164. wpa_printf(MSG_DEBUG, "P2P: No room for full "
  1165. "DNS offset field");
  1166. return -1;
  1167. }
  1168. offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
  1169. if (offset >= *spos - start) {
  1170. wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
  1171. "pointer offset %u", offset);
  1172. return -1;
  1173. }
  1174. (*spos) += 2;
  1175. spos_tmp = start + offset;
  1176. return p2p_sd_dns_uncompress_label(upos, uend, start,
  1177. &spos_tmp,
  1178. *spos - 2);
  1179. }
  1180. /* Label */
  1181. len = (*spos)[0] & 0x3f;
  1182. if (len == 0)
  1183. return 0;
  1184. (*spos)++;
  1185. if (*spos + len > end) {
  1186. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1187. "sequence - no room for label with length "
  1188. "%u", len);
  1189. return -1;
  1190. }
  1191. if (*upos + len + 2 > uend)
  1192. return -2;
  1193. os_memcpy(*upos, *spos, len);
  1194. *spos += len;
  1195. *upos += len;
  1196. (*upos)[0] = '.';
  1197. (*upos)++;
  1198. (*upos)[0] = '\0';
  1199. }
  1200. return 0;
  1201. }
  1202. /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
  1203. * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
  1204. * not large enough */
  1205. static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
  1206. size_t msg_len, size_t offset)
  1207. {
  1208. /* 27-octet in-memory packet from P2P specification */
  1209. const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
  1210. "\x04_udp\xC0\x11\x00\x0C\x00\x01";
  1211. u8 *tmp, *end, *spos;
  1212. char *upos, *uend;
  1213. int ret = 0;
  1214. if (buf_len < 2)
  1215. return -1;
  1216. if (offset > msg_len)
  1217. return -1;
  1218. tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
  1219. if (tmp == NULL)
  1220. return -1;
  1221. spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
  1222. end = spos + msg_len;
  1223. spos += offset;
  1224. os_memset(tmp, 0, DNS_HEADER_LEN);
  1225. os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
  1226. os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
  1227. upos = buf;
  1228. uend = buf + buf_len;
  1229. ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
  1230. if (ret) {
  1231. os_free(tmp);
  1232. return ret;
  1233. }
  1234. if (upos == buf) {
  1235. upos[0] = '.';
  1236. upos[1] = '\0';
  1237. } else if (upos[-1] == '.')
  1238. upos[-1] = '\0';
  1239. os_free(tmp);
  1240. return 0;
  1241. }
  1242. static struct p2p_srv_bonjour *
  1243. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  1244. const struct wpabuf *query)
  1245. {
  1246. struct p2p_srv_bonjour *bsrv;
  1247. size_t len;
  1248. len = wpabuf_len(query);
  1249. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1250. struct p2p_srv_bonjour, list) {
  1251. if (len == wpabuf_len(bsrv->query) &&
  1252. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  1253. len) == 0)
  1254. return bsrv;
  1255. }
  1256. return NULL;
  1257. }
  1258. static struct p2p_srv_upnp *
  1259. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1260. const char *service)
  1261. {
  1262. struct p2p_srv_upnp *usrv;
  1263. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1264. struct p2p_srv_upnp, list) {
  1265. if (version == usrv->version &&
  1266. os_strcmp(service, usrv->service) == 0)
  1267. return usrv;
  1268. }
  1269. return NULL;
  1270. }
  1271. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  1272. u8 srv_trans_id)
  1273. {
  1274. u8 *len_pos;
  1275. if (wpabuf_tailroom(resp) < 5)
  1276. return;
  1277. /* Length (to be filled) */
  1278. len_pos = wpabuf_put(resp, 2);
  1279. wpabuf_put_u8(resp, srv_proto);
  1280. wpabuf_put_u8(resp, srv_trans_id);
  1281. /* Status Code */
  1282. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  1283. /* Response Data: empty */
  1284. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1285. }
  1286. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  1287. struct wpabuf *resp, u8 srv_trans_id)
  1288. {
  1289. struct p2p_srv_bonjour *bsrv;
  1290. u8 *len_pos;
  1291. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  1292. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1293. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1294. return;
  1295. }
  1296. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1297. struct p2p_srv_bonjour, list) {
  1298. if (wpabuf_tailroom(resp) <
  1299. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  1300. return;
  1301. /* Length (to be filled) */
  1302. len_pos = wpabuf_put(resp, 2);
  1303. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1304. wpabuf_put_u8(resp, srv_trans_id);
  1305. /* Status Code */
  1306. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1307. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1308. wpabuf_head(bsrv->resp),
  1309. wpabuf_len(bsrv->resp));
  1310. /* Response Data */
  1311. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1312. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1313. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1314. 2);
  1315. }
  1316. }
  1317. static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
  1318. size_t query_len)
  1319. {
  1320. char str_rx[256], str_srv[256];
  1321. if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
  1322. return 0; /* Too short to include DNS Type and Version */
  1323. if (os_memcmp(query + query_len - 3,
  1324. wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
  1325. 3) != 0)
  1326. return 0; /* Mismatch in DNS Type or Version */
  1327. if (query_len == wpabuf_len(bsrv->query) &&
  1328. os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
  1329. return 1; /* Binary match */
  1330. if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
  1331. 0))
  1332. return 0; /* Failed to uncompress query */
  1333. if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
  1334. wpabuf_head(bsrv->query),
  1335. wpabuf_len(bsrv->query) - 3, 0))
  1336. return 0; /* Failed to uncompress service */
  1337. return os_strcmp(str_rx, str_srv) == 0;
  1338. }
  1339. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  1340. struct wpabuf *resp, u8 srv_trans_id,
  1341. const u8 *query, size_t query_len)
  1342. {
  1343. struct p2p_srv_bonjour *bsrv;
  1344. u8 *len_pos;
  1345. int matches = 0;
  1346. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1347. query, query_len);
  1348. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1349. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1350. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1351. srv_trans_id);
  1352. return;
  1353. }
  1354. if (query_len == 0) {
  1355. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1356. return;
  1357. }
  1358. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1359. struct p2p_srv_bonjour, list) {
  1360. if (!match_bonjour_query(bsrv, query, query_len))
  1361. continue;
  1362. if (wpabuf_tailroom(resp) <
  1363. 5 + query_len + wpabuf_len(bsrv->resp))
  1364. return;
  1365. matches++;
  1366. /* Length (to be filled) */
  1367. len_pos = wpabuf_put(resp, 2);
  1368. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1369. wpabuf_put_u8(resp, srv_trans_id);
  1370. /* Status Code */
  1371. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1372. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1373. wpabuf_head(bsrv->resp),
  1374. wpabuf_len(bsrv->resp));
  1375. /* Response Data */
  1376. wpabuf_put_data(resp, query, query_len); /* Key */
  1377. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1378. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1379. }
  1380. if (matches == 0) {
  1381. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1382. "available");
  1383. if (wpabuf_tailroom(resp) < 5)
  1384. return;
  1385. /* Length (to be filled) */
  1386. len_pos = wpabuf_put(resp, 2);
  1387. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1388. wpabuf_put_u8(resp, srv_trans_id);
  1389. /* Status Code */
  1390. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1391. /* Response Data: empty */
  1392. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1393. 2);
  1394. }
  1395. }
  1396. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1397. struct wpabuf *resp, u8 srv_trans_id)
  1398. {
  1399. struct p2p_srv_upnp *usrv;
  1400. u8 *len_pos;
  1401. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1402. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1403. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1404. return;
  1405. }
  1406. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1407. struct p2p_srv_upnp, list) {
  1408. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1409. return;
  1410. /* Length (to be filled) */
  1411. len_pos = wpabuf_put(resp, 2);
  1412. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1413. wpabuf_put_u8(resp, srv_trans_id);
  1414. /* Status Code */
  1415. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1416. /* Response Data */
  1417. wpabuf_put_u8(resp, usrv->version);
  1418. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1419. usrv->service);
  1420. wpabuf_put_str(resp, usrv->service);
  1421. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1422. 2);
  1423. }
  1424. }
  1425. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1426. struct wpabuf *resp, u8 srv_trans_id,
  1427. const u8 *query, size_t query_len)
  1428. {
  1429. struct p2p_srv_upnp *usrv;
  1430. u8 *len_pos;
  1431. u8 version;
  1432. char *str;
  1433. int count = 0;
  1434. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1435. query, query_len);
  1436. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1437. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1438. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1439. srv_trans_id);
  1440. return;
  1441. }
  1442. if (query_len == 0) {
  1443. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1444. return;
  1445. }
  1446. if (wpabuf_tailroom(resp) < 5)
  1447. return;
  1448. /* Length (to be filled) */
  1449. len_pos = wpabuf_put(resp, 2);
  1450. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1451. wpabuf_put_u8(resp, srv_trans_id);
  1452. version = query[0];
  1453. str = os_malloc(query_len);
  1454. if (str == NULL)
  1455. return;
  1456. os_memcpy(str, query + 1, query_len - 1);
  1457. str[query_len - 1] = '\0';
  1458. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1459. struct p2p_srv_upnp, list) {
  1460. if (version != usrv->version)
  1461. continue;
  1462. if (os_strcmp(str, "ssdp:all") != 0 &&
  1463. os_strstr(usrv->service, str) == NULL)
  1464. continue;
  1465. if (wpabuf_tailroom(resp) < 2)
  1466. break;
  1467. if (count == 0) {
  1468. /* Status Code */
  1469. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1470. /* Response Data */
  1471. wpabuf_put_u8(resp, version);
  1472. } else
  1473. wpabuf_put_u8(resp, ',');
  1474. count++;
  1475. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1476. usrv->service);
  1477. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1478. break;
  1479. wpabuf_put_str(resp, usrv->service);
  1480. }
  1481. os_free(str);
  1482. if (count == 0) {
  1483. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1484. "available");
  1485. /* Status Code */
  1486. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1487. /* Response Data: empty */
  1488. }
  1489. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1490. }
  1491. #ifdef CONFIG_WIFI_DISPLAY
  1492. static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
  1493. struct wpabuf *resp, u8 srv_trans_id,
  1494. const u8 *query, size_t query_len)
  1495. {
  1496. const u8 *pos;
  1497. u8 role;
  1498. u8 *len_pos;
  1499. wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
  1500. if (!wpa_s->global->wifi_display) {
  1501. wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
  1502. wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
  1503. srv_trans_id);
  1504. return;
  1505. }
  1506. if (query_len < 1) {
  1507. wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
  1508. "Role");
  1509. return;
  1510. }
  1511. if (wpabuf_tailroom(resp) < 5)
  1512. return;
  1513. pos = query;
  1514. role = *pos++;
  1515. wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
  1516. /* TODO: role specific handling */
  1517. /* Length (to be filled) */
  1518. len_pos = wpabuf_put(resp, 2);
  1519. wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
  1520. wpabuf_put_u8(resp, srv_trans_id);
  1521. wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
  1522. while (pos < query + query_len) {
  1523. if (*pos < MAX_WFD_SUBELEMS &&
  1524. wpa_s->global->wfd_subelem[*pos] &&
  1525. wpabuf_tailroom(resp) >=
  1526. wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
  1527. wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
  1528. "subelement %u", *pos);
  1529. wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
  1530. }
  1531. pos++;
  1532. }
  1533. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1534. }
  1535. #endif /* CONFIG_WIFI_DISPLAY */
  1536. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1537. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1538. {
  1539. struct wpa_supplicant *wpa_s = ctx;
  1540. const u8 *pos = tlvs;
  1541. const u8 *end = tlvs + tlvs_len;
  1542. const u8 *tlv_end;
  1543. u16 slen;
  1544. struct wpabuf *resp;
  1545. u8 srv_proto, srv_trans_id;
  1546. size_t buf_len;
  1547. char *buf;
  1548. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1549. tlvs, tlvs_len);
  1550. buf_len = 2 * tlvs_len + 1;
  1551. buf = os_malloc(buf_len);
  1552. if (buf) {
  1553. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1554. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1555. MACSTR " %u %u %s",
  1556. freq, MAC2STR(sa), dialog_token, update_indic,
  1557. buf);
  1558. os_free(buf);
  1559. }
  1560. if (wpa_s->p2p_sd_over_ctrl_iface) {
  1561. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1562. update_indic, tlvs, tlvs_len);
  1563. return; /* to be processed by an external program */
  1564. }
  1565. resp = wpabuf_alloc(10000);
  1566. if (resp == NULL)
  1567. return;
  1568. while (pos + 1 < end) {
  1569. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1570. slen = WPA_GET_LE16(pos);
  1571. pos += 2;
  1572. if (pos + slen > end || slen < 2) {
  1573. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1574. "length");
  1575. wpabuf_free(resp);
  1576. return;
  1577. }
  1578. tlv_end = pos + slen;
  1579. srv_proto = *pos++;
  1580. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1581. srv_proto);
  1582. srv_trans_id = *pos++;
  1583. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1584. srv_trans_id);
  1585. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1586. pos, tlv_end - pos);
  1587. if (wpa_s->force_long_sd) {
  1588. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1589. "response");
  1590. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1591. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1592. goto done;
  1593. }
  1594. switch (srv_proto) {
  1595. case P2P_SERV_ALL_SERVICES:
  1596. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1597. "for all services");
  1598. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1599. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1600. wpa_printf(MSG_DEBUG, "P2P: No service "
  1601. "discovery protocols available");
  1602. wpas_sd_add_proto_not_avail(
  1603. resp, P2P_SERV_ALL_SERVICES,
  1604. srv_trans_id);
  1605. break;
  1606. }
  1607. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1608. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1609. break;
  1610. case P2P_SERV_BONJOUR:
  1611. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1612. pos, tlv_end - pos);
  1613. break;
  1614. case P2P_SERV_UPNP:
  1615. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1616. pos, tlv_end - pos);
  1617. break;
  1618. #ifdef CONFIG_WIFI_DISPLAY
  1619. case P2P_SERV_WIFI_DISPLAY:
  1620. wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
  1621. pos, tlv_end - pos);
  1622. break;
  1623. #endif /* CONFIG_WIFI_DISPLAY */
  1624. default:
  1625. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1626. "protocol %u", srv_proto);
  1627. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1628. srv_trans_id);
  1629. break;
  1630. }
  1631. pos = tlv_end;
  1632. }
  1633. done:
  1634. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1635. update_indic, tlvs, tlvs_len);
  1636. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1637. wpabuf_free(resp);
  1638. }
  1639. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1640. const u8 *tlvs, size_t tlvs_len)
  1641. {
  1642. struct wpa_supplicant *wpa_s = ctx;
  1643. const u8 *pos = tlvs;
  1644. const u8 *end = tlvs + tlvs_len;
  1645. const u8 *tlv_end;
  1646. u16 slen;
  1647. size_t buf_len;
  1648. char *buf;
  1649. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1650. tlvs, tlvs_len);
  1651. if (tlvs_len > 1500) {
  1652. /* TODO: better way for handling this */
  1653. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1654. P2P_EVENT_SERV_DISC_RESP MACSTR
  1655. " %u <long response: %u bytes>",
  1656. MAC2STR(sa), update_indic,
  1657. (unsigned int) tlvs_len);
  1658. } else {
  1659. buf_len = 2 * tlvs_len + 1;
  1660. buf = os_malloc(buf_len);
  1661. if (buf) {
  1662. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1663. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1664. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1665. MAC2STR(sa), update_indic, buf);
  1666. os_free(buf);
  1667. }
  1668. }
  1669. while (pos < end) {
  1670. u8 srv_proto, srv_trans_id, status;
  1671. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1672. slen = WPA_GET_LE16(pos);
  1673. pos += 2;
  1674. if (pos + slen > end || slen < 3) {
  1675. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1676. "length");
  1677. return;
  1678. }
  1679. tlv_end = pos + slen;
  1680. srv_proto = *pos++;
  1681. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1682. srv_proto);
  1683. srv_trans_id = *pos++;
  1684. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1685. srv_trans_id);
  1686. status = *pos++;
  1687. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1688. status);
  1689. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1690. pos, tlv_end - pos);
  1691. pos = tlv_end;
  1692. }
  1693. wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
  1694. }
  1695. u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1696. const struct wpabuf *tlvs)
  1697. {
  1698. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1699. return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
  1700. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1701. return 0;
  1702. return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1703. }
  1704. u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1705. u8 version, const char *query)
  1706. {
  1707. struct wpabuf *tlvs;
  1708. u64 ret;
  1709. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1710. if (tlvs == NULL)
  1711. return 0;
  1712. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1713. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1714. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1715. wpabuf_put_u8(tlvs, version);
  1716. wpabuf_put_str(tlvs, query);
  1717. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1718. wpabuf_free(tlvs);
  1719. return ret;
  1720. }
  1721. #ifdef CONFIG_WIFI_DISPLAY
  1722. static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
  1723. const struct wpabuf *tlvs)
  1724. {
  1725. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1726. return 0;
  1727. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1728. return 0;
  1729. return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
  1730. }
  1731. #define MAX_WFD_SD_SUBELEMS 20
  1732. static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
  1733. const char *subelems)
  1734. {
  1735. u8 *len;
  1736. const char *pos;
  1737. int val;
  1738. int count = 0;
  1739. len = wpabuf_put(tlvs, 2);
  1740. wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
  1741. wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
  1742. wpabuf_put_u8(tlvs, role);
  1743. pos = subelems;
  1744. while (*pos) {
  1745. val = atoi(pos);
  1746. if (val >= 0 && val < 256) {
  1747. wpabuf_put_u8(tlvs, val);
  1748. count++;
  1749. if (count == MAX_WFD_SD_SUBELEMS)
  1750. break;
  1751. }
  1752. pos = os_strchr(pos + 1, ',');
  1753. if (pos == NULL)
  1754. break;
  1755. pos++;
  1756. }
  1757. WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
  1758. }
  1759. u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
  1760. const u8 *dst, const char *role)
  1761. {
  1762. struct wpabuf *tlvs;
  1763. u64 ret;
  1764. const char *subelems;
  1765. u8 id = 1;
  1766. subelems = os_strchr(role, ' ');
  1767. if (subelems == NULL)
  1768. return 0;
  1769. subelems++;
  1770. tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
  1771. if (tlvs == NULL)
  1772. return 0;
  1773. if (os_strstr(role, "[source]"))
  1774. wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
  1775. if (os_strstr(role, "[pri-sink]"))
  1776. wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
  1777. if (os_strstr(role, "[sec-sink]"))
  1778. wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
  1779. if (os_strstr(role, "[source+sink]"))
  1780. wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
  1781. ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
  1782. wpabuf_free(tlvs);
  1783. return ret;
  1784. }
  1785. #endif /* CONFIG_WIFI_DISPLAY */
  1786. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
  1787. {
  1788. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1789. return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
  1790. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1791. return -1;
  1792. return p2p_sd_cancel_request(wpa_s->global->p2p,
  1793. (void *) (uintptr_t) req);
  1794. }
  1795. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1796. const u8 *dst, u8 dialog_token,
  1797. const struct wpabuf *resp_tlvs)
  1798. {
  1799. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1800. wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
  1801. resp_tlvs);
  1802. return;
  1803. }
  1804. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1805. return;
  1806. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1807. resp_tlvs);
  1808. }
  1809. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1810. {
  1811. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1812. wpa_drv_p2p_service_update(wpa_s);
  1813. return;
  1814. }
  1815. if (wpa_s->global->p2p)
  1816. p2p_sd_service_update(wpa_s->global->p2p);
  1817. }
  1818. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1819. {
  1820. dl_list_del(&bsrv->list);
  1821. wpabuf_free(bsrv->query);
  1822. wpabuf_free(bsrv->resp);
  1823. os_free(bsrv);
  1824. }
  1825. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1826. {
  1827. dl_list_del(&usrv->list);
  1828. os_free(usrv->service);
  1829. os_free(usrv);
  1830. }
  1831. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1832. {
  1833. struct p2p_srv_bonjour *bsrv, *bn;
  1834. struct p2p_srv_upnp *usrv, *un;
  1835. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1836. struct p2p_srv_bonjour, list)
  1837. wpas_p2p_srv_bonjour_free(bsrv);
  1838. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1839. struct p2p_srv_upnp, list)
  1840. wpas_p2p_srv_upnp_free(usrv);
  1841. wpas_p2p_sd_service_update(wpa_s);
  1842. }
  1843. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1844. struct wpabuf *query, struct wpabuf *resp)
  1845. {
  1846. struct p2p_srv_bonjour *bsrv;
  1847. bsrv = os_zalloc(sizeof(*bsrv));
  1848. if (bsrv == NULL)
  1849. return -1;
  1850. bsrv->query = query;
  1851. bsrv->resp = resp;
  1852. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  1853. wpas_p2p_sd_service_update(wpa_s);
  1854. return 0;
  1855. }
  1856. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1857. const struct wpabuf *query)
  1858. {
  1859. struct p2p_srv_bonjour *bsrv;
  1860. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1861. if (bsrv == NULL)
  1862. return -1;
  1863. wpas_p2p_srv_bonjour_free(bsrv);
  1864. wpas_p2p_sd_service_update(wpa_s);
  1865. return 0;
  1866. }
  1867. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1868. const char *service)
  1869. {
  1870. struct p2p_srv_upnp *usrv;
  1871. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  1872. return 0; /* Already listed */
  1873. usrv = os_zalloc(sizeof(*usrv));
  1874. if (usrv == NULL)
  1875. return -1;
  1876. usrv->version = version;
  1877. usrv->service = os_strdup(service);
  1878. if (usrv->service == NULL) {
  1879. os_free(usrv);
  1880. return -1;
  1881. }
  1882. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  1883. wpas_p2p_sd_service_update(wpa_s);
  1884. return 0;
  1885. }
  1886. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1887. const char *service)
  1888. {
  1889. struct p2p_srv_upnp *usrv;
  1890. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  1891. if (usrv == NULL)
  1892. return -1;
  1893. wpas_p2p_srv_upnp_free(usrv);
  1894. wpas_p2p_sd_service_update(wpa_s);
  1895. return 0;
  1896. }
  1897. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  1898. const u8 *peer, const char *params,
  1899. unsigned int generated_pin)
  1900. {
  1901. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  1902. " %08d%s", MAC2STR(peer), generated_pin, params);
  1903. }
  1904. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  1905. const u8 *peer, const char *params)
  1906. {
  1907. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  1908. "%s", MAC2STR(peer), params);
  1909. }
  1910. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  1911. const u8 *dev_addr, const u8 *pri_dev_type,
  1912. const char *dev_name, u16 supp_config_methods,
  1913. u8 dev_capab, u8 group_capab, const u8 *group_id,
  1914. size_t group_id_len)
  1915. {
  1916. struct wpa_supplicant *wpa_s = ctx;
  1917. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1918. char params[300];
  1919. u8 empty_dev_type[8];
  1920. unsigned int generated_pin = 0;
  1921. struct wpa_supplicant *group = NULL;
  1922. if (group_id) {
  1923. for (group = wpa_s->global->ifaces; group; group = group->next)
  1924. {
  1925. struct wpa_ssid *s = group->current_ssid;
  1926. if (s != NULL &&
  1927. s->mode == WPAS_MODE_P2P_GO &&
  1928. group_id_len - ETH_ALEN == s->ssid_len &&
  1929. os_memcmp(group_id + ETH_ALEN, s->ssid,
  1930. s->ssid_len) == 0)
  1931. break;
  1932. }
  1933. }
  1934. if (pri_dev_type == NULL) {
  1935. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  1936. pri_dev_type = empty_dev_type;
  1937. }
  1938. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  1939. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1940. "dev_capab=0x%x group_capab=0x%x%s%s",
  1941. MAC2STR(dev_addr),
  1942. wps_dev_type_bin2str(pri_dev_type, devtype,
  1943. sizeof(devtype)),
  1944. dev_name, supp_config_methods, dev_capab, group_capab,
  1945. group ? " group=" : "",
  1946. group ? group->ifname : "");
  1947. params[sizeof(params) - 1] = '\0';
  1948. if (config_methods & WPS_CONFIG_DISPLAY) {
  1949. generated_pin = wps_generate_pin();
  1950. wpas_prov_disc_local_display(wpa_s, peer, params,
  1951. generated_pin);
  1952. } else if (config_methods & WPS_CONFIG_KEYPAD)
  1953. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  1954. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1955. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  1956. MACSTR "%s", MAC2STR(peer), params);
  1957. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  1958. P2P_PROV_DISC_SUCCESS,
  1959. config_methods, generated_pin);
  1960. }
  1961. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  1962. {
  1963. struct wpa_supplicant *wpa_s = ctx;
  1964. unsigned int generated_pin = 0;
  1965. char params[20];
  1966. if (wpa_s->pending_pd_before_join &&
  1967. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  1968. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  1969. wpa_s->pending_pd_before_join = 0;
  1970. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  1971. "join-existing-group operation");
  1972. wpas_p2p_join_start(wpa_s);
  1973. return;
  1974. }
  1975. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  1976. wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
  1977. os_snprintf(params, sizeof(params), " peer_go=%d",
  1978. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  1979. else
  1980. params[0] = '\0';
  1981. if (config_methods & WPS_CONFIG_DISPLAY)
  1982. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  1983. else if (config_methods & WPS_CONFIG_KEYPAD) {
  1984. generated_pin = wps_generate_pin();
  1985. wpas_prov_disc_local_display(wpa_s, peer, params,
  1986. generated_pin);
  1987. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  1988. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  1989. MACSTR "%s", MAC2STR(peer), params);
  1990. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  1991. P2P_PROV_DISC_SUCCESS,
  1992. config_methods, generated_pin);
  1993. }
  1994. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  1995. enum p2p_prov_disc_status status)
  1996. {
  1997. struct wpa_supplicant *wpa_s = ctx;
  1998. if (wpa_s->p2p_fallback_to_go_neg) {
  1999. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2000. "failed - fall back to GO Negotiation");
  2001. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2002. return;
  2003. }
  2004. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2005. wpa_s->pending_pd_before_join = 0;
  2006. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2007. "join-existing-group operation (no ACK for PD "
  2008. "Req attempts)");
  2009. wpas_p2p_join_start(wpa_s);
  2010. return;
  2011. }
  2012. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2013. " p2p_dev_addr=" MACSTR " status=%d",
  2014. MAC2STR(peer), status);
  2015. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2016. status, 0, 0);
  2017. }
  2018. static int freq_included(const struct p2p_channels *channels, unsigned int freq)
  2019. {
  2020. if (channels == NULL)
  2021. return 1; /* Assume no restrictions */
  2022. return p2p_channels_includes_freq(channels, freq);
  2023. }
  2024. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2025. const u8 *go_dev_addr, const u8 *ssid,
  2026. size_t ssid_len, int *go, u8 *group_bssid,
  2027. int *force_freq, int persistent_group,
  2028. const struct p2p_channels *channels)
  2029. {
  2030. struct wpa_supplicant *wpa_s = ctx;
  2031. struct wpa_ssid *s;
  2032. u8 cur_bssid[ETH_ALEN];
  2033. int res;
  2034. struct wpa_supplicant *grp;
  2035. if (!persistent_group) {
  2036. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2037. " to join an active group", MAC2STR(sa));
  2038. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2039. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2040. == 0 ||
  2041. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2042. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2043. "authorized invitation");
  2044. goto accept_inv;
  2045. }
  2046. /*
  2047. * Do not accept the invitation automatically; notify user and
  2048. * request approval.
  2049. */
  2050. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2051. }
  2052. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2053. if (grp) {
  2054. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2055. "running persistent group");
  2056. if (*go)
  2057. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2058. goto accept_inv;
  2059. }
  2060. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2061. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2062. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2063. "invitation to re-invoke a persistent group");
  2064. } else if (!wpa_s->conf->persistent_reconnect)
  2065. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2066. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2067. if (s->disabled == 2 &&
  2068. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2069. s->ssid_len == ssid_len &&
  2070. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2071. break;
  2072. }
  2073. if (!s) {
  2074. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2075. " requested reinvocation of an unknown group",
  2076. MAC2STR(sa));
  2077. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2078. }
  2079. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2080. *go = 1;
  2081. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2082. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2083. "interface is already in use - reject "
  2084. "invitation");
  2085. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2086. }
  2087. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2088. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2089. *go = 1;
  2090. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2091. {
  2092. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2093. "interface address for the group");
  2094. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2095. }
  2096. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2097. ETH_ALEN);
  2098. }
  2099. accept_inv:
  2100. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2101. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
  2102. wpa_s->assoc_freq) {
  2103. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  2104. "the channel we are already using");
  2105. *force_freq = wpa_s->assoc_freq;
  2106. wpas_p2p_set_own_freq_preference(wpa_s, wpa_s->assoc_freq);
  2107. }
  2108. res = wpa_drv_shared_freq(wpa_s);
  2109. if (res > 0) {
  2110. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
  2111. "with the channel we are already using on a "
  2112. "shared interface");
  2113. *force_freq = res;
  2114. wpas_p2p_set_own_freq_preference(wpa_s, res);
  2115. }
  2116. if (*force_freq > 0 &&
  2117. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  2118. if (*go == 0) {
  2119. /* We are the client */
  2120. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2121. "running a GO but we are capable of MCC, "
  2122. "figure out the best channel to use");
  2123. *force_freq = 0;
  2124. } else if (!freq_included(channels, *force_freq)) {
  2125. /* We are the GO, and *force_freq is not in the
  2126. * intersection */
  2127. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2128. "in intersection but we are capable of MCC, "
  2129. "figure out the best channel to use",
  2130. *force_freq);
  2131. *force_freq = 0;
  2132. }
  2133. }
  2134. return P2P_SC_SUCCESS;
  2135. }
  2136. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2137. const u8 *ssid, size_t ssid_len,
  2138. const u8 *go_dev_addr, u8 status,
  2139. int op_freq)
  2140. {
  2141. struct wpa_supplicant *wpa_s = ctx;
  2142. struct wpa_ssid *s;
  2143. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2144. if (s->disabled == 2 &&
  2145. s->ssid_len == ssid_len &&
  2146. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2147. break;
  2148. }
  2149. if (status == P2P_SC_SUCCESS) {
  2150. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2151. " was accepted; op_freq=%d MHz",
  2152. MAC2STR(sa), op_freq);
  2153. if (s) {
  2154. int go = s->mode == WPAS_MODE_P2P_GO;
  2155. wpas_p2p_group_add_persistent(
  2156. wpa_s, s, go, go ? op_freq : 0, 0, NULL);
  2157. } else if (bssid) {
  2158. wpa_s->user_initiated_pd = 0;
  2159. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2160. wpa_s->p2p_wps_method, 0);
  2161. }
  2162. return;
  2163. }
  2164. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2165. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2166. " was rejected (status %u)", MAC2STR(sa), status);
  2167. return;
  2168. }
  2169. if (!s) {
  2170. if (bssid) {
  2171. wpa_msg_global(wpa_s, MSG_INFO,
  2172. P2P_EVENT_INVITATION_RECEIVED
  2173. "sa=" MACSTR " go_dev_addr=" MACSTR
  2174. " bssid=" MACSTR " unknown-network",
  2175. MAC2STR(sa), MAC2STR(go_dev_addr),
  2176. MAC2STR(bssid));
  2177. } else {
  2178. wpa_msg_global(wpa_s, MSG_INFO,
  2179. P2P_EVENT_INVITATION_RECEIVED
  2180. "sa=" MACSTR " go_dev_addr=" MACSTR
  2181. " unknown-network",
  2182. MAC2STR(sa), MAC2STR(go_dev_addr));
  2183. }
  2184. return;
  2185. }
  2186. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2187. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2188. "sa=" MACSTR " persistent=%d freq=%d",
  2189. MAC2STR(sa), s->id, op_freq);
  2190. } else {
  2191. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2192. "sa=" MACSTR " persistent=%d",
  2193. MAC2STR(sa), s->id);
  2194. }
  2195. }
  2196. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2197. struct wpa_ssid *ssid,
  2198. const u8 *peer)
  2199. {
  2200. size_t i;
  2201. if (ssid == NULL)
  2202. return;
  2203. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2204. if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
  2205. ETH_ALEN) == 0)
  2206. break;
  2207. }
  2208. if (i >= ssid->num_p2p_clients) {
  2209. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2210. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2211. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2212. "due to invitation result", ssid->id);
  2213. wpas_notify_network_removed(wpa_s, ssid);
  2214. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2215. return;
  2216. }
  2217. return; /* Peer not found in client list */
  2218. }
  2219. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2220. "group %d client list due to invitation result",
  2221. MAC2STR(peer), ssid->id);
  2222. os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
  2223. ssid->p2p_client_list + (i + 1) * ETH_ALEN,
  2224. (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
  2225. ssid->num_p2p_clients--;
  2226. #ifndef CONFIG_NO_CONFIG_WRITE
  2227. if (wpa_s->parent->conf->update_config &&
  2228. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  2229. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2230. #endif /* CONFIG_NO_CONFIG_WRITE */
  2231. }
  2232. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2233. const u8 *peer)
  2234. {
  2235. struct wpa_ssid *ssid;
  2236. wpa_s = wpa_s->global->p2p_invite_group;
  2237. if (wpa_s == NULL)
  2238. return; /* No known invitation group */
  2239. ssid = wpa_s->current_ssid;
  2240. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2241. !ssid->p2p_persistent_group)
  2242. return; /* Not operating as a GO in persistent group */
  2243. ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
  2244. ssid->ssid, ssid->ssid_len);
  2245. wpas_remove_persistent_peer(wpa_s, ssid, peer);
  2246. }
  2247. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2248. const struct p2p_channels *channels,
  2249. const u8 *peer)
  2250. {
  2251. struct wpa_supplicant *wpa_s = ctx;
  2252. struct wpa_ssid *ssid;
  2253. if (bssid) {
  2254. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2255. "status=%d " MACSTR,
  2256. status, MAC2STR(bssid));
  2257. } else {
  2258. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2259. "status=%d ", status);
  2260. }
  2261. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2262. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2263. status, MAC2STR(peer));
  2264. if (wpa_s->pending_invite_ssid_id == -1) {
  2265. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2266. wpas_remove_persistent_client(wpa_s, peer);
  2267. return; /* Invitation to active group */
  2268. }
  2269. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2270. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2271. "invitation exchange to indicate readiness for "
  2272. "re-invocation");
  2273. }
  2274. if (status != P2P_SC_SUCCESS) {
  2275. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2276. ssid = wpa_config_get_network(
  2277. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2278. wpas_remove_persistent_peer(wpa_s, ssid, peer);
  2279. }
  2280. wpas_p2p_remove_pending_group_interface(wpa_s);
  2281. return;
  2282. }
  2283. ssid = wpa_config_get_network(wpa_s->conf,
  2284. wpa_s->pending_invite_ssid_id);
  2285. if (ssid == NULL) {
  2286. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2287. "data matching with invitation");
  2288. return;
  2289. }
  2290. /*
  2291. * The peer could have missed our ctrl::ack frame for Invitation
  2292. * Response and continue retransmitting the frame. To reduce the
  2293. * likelihood of the peer not getting successful TX status for the
  2294. * Invitation Response frame, wait a short time here before starting
  2295. * the persistent group so that we will remain on the current channel to
  2296. * acknowledge any possible retransmission from the peer.
  2297. */
  2298. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2299. "starting persistent group");
  2300. os_sleep(0, 50000);
  2301. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2302. ssid->mode == WPAS_MODE_P2P_GO,
  2303. wpa_s->p2p_persistent_go_freq,
  2304. wpa_s->p2p_go_ht40, channels);
  2305. }
  2306. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2307. unsigned int freq)
  2308. {
  2309. unsigned int i;
  2310. if (global->p2p_disallow_freq == NULL)
  2311. return 0;
  2312. for (i = 0; i < global->num_p2p_disallow_freq; i++) {
  2313. if (freq >= global->p2p_disallow_freq[i].min &&
  2314. freq <= global->p2p_disallow_freq[i].max)
  2315. return 1;
  2316. }
  2317. return 0;
  2318. }
  2319. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2320. {
  2321. reg->channel[reg->channels] = chan;
  2322. reg->channels++;
  2323. }
  2324. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2325. struct p2p_channels *chan)
  2326. {
  2327. int i, cla = 0;
  2328. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2329. "band");
  2330. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2331. chan->reg_class[cla].reg_class = 81;
  2332. chan->reg_class[cla].channels = 0;
  2333. for (i = 0; i < 11; i++) {
  2334. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2335. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2336. }
  2337. if (chan->reg_class[cla].channels)
  2338. cla++;
  2339. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2340. "band");
  2341. /* Operating class 115 - 5 GHz, channels 36-48 */
  2342. chan->reg_class[cla].reg_class = 115;
  2343. chan->reg_class[cla].channels = 0;
  2344. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2345. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2346. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2347. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2348. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2349. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2350. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2351. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2352. if (chan->reg_class[cla].channels)
  2353. cla++;
  2354. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2355. "band");
  2356. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2357. chan->reg_class[cla].reg_class = 124;
  2358. chan->reg_class[cla].channels = 0;
  2359. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2360. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2361. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2362. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2363. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2364. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2365. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2366. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2367. if (chan->reg_class[cla].channels)
  2368. cla++;
  2369. chan->reg_classes = cla;
  2370. return 0;
  2371. }
  2372. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2373. u16 num_modes,
  2374. enum hostapd_hw_mode mode)
  2375. {
  2376. u16 i;
  2377. for (i = 0; i < num_modes; i++) {
  2378. if (modes[i].mode == mode)
  2379. return &modes[i];
  2380. }
  2381. return NULL;
  2382. }
  2383. static int has_channel(struct wpa_global *global,
  2384. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2385. {
  2386. int i;
  2387. unsigned int freq;
  2388. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2389. chan * 5;
  2390. if (wpas_p2p_disallowed_freq(global, freq))
  2391. return 0;
  2392. for (i = 0; i < mode->num_channels; i++) {
  2393. if (mode->channels[i].chan == chan) {
  2394. if (flags)
  2395. *flags = mode->channels[i].flag;
  2396. return !(mode->channels[i].flag &
  2397. (HOSTAPD_CHAN_DISABLED |
  2398. HOSTAPD_CHAN_PASSIVE_SCAN |
  2399. HOSTAPD_CHAN_NO_IBSS |
  2400. HOSTAPD_CHAN_RADAR));
  2401. }
  2402. }
  2403. return 0;
  2404. }
  2405. struct p2p_oper_class_map {
  2406. enum hostapd_hw_mode mode;
  2407. u8 op_class;
  2408. u8 min_chan;
  2409. u8 max_chan;
  2410. u8 inc;
  2411. enum { BW20, BW40PLUS, BW40MINUS } bw;
  2412. };
  2413. static struct p2p_oper_class_map op_class[] = {
  2414. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2415. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2416. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2417. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2418. #endif
  2419. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2420. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2421. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2422. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2423. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2424. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2425. { -1, 0, 0, 0, 0, BW20 }
  2426. };
  2427. static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2428. struct hostapd_hw_modes *mode,
  2429. u8 channel, u8 bw)
  2430. {
  2431. int flag;
  2432. if (!has_channel(wpa_s->global, mode, channel, &flag))
  2433. return -1;
  2434. if (bw == BW40MINUS &&
  2435. (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
  2436. !has_channel(wpa_s->global, mode, channel - 4, NULL)))
  2437. return 0;
  2438. if (bw == BW40PLUS &&
  2439. (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
  2440. !has_channel(wpa_s->global, mode, channel + 4, NULL)))
  2441. return 0;
  2442. return 1;
  2443. }
  2444. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2445. struct p2p_channels *chan)
  2446. {
  2447. struct hostapd_hw_modes *mode;
  2448. int cla, op;
  2449. if (wpa_s->hw.modes == NULL) {
  2450. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2451. "of all supported channels; assume dualband "
  2452. "support");
  2453. return wpas_p2p_default_channels(wpa_s, chan);
  2454. }
  2455. cla = 0;
  2456. for (op = 0; op_class[op].op_class; op++) {
  2457. struct p2p_oper_class_map *o = &op_class[op];
  2458. u8 ch;
  2459. struct p2p_reg_class *reg = NULL;
  2460. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2461. if (mode == NULL)
  2462. continue;
  2463. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2464. if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
  2465. continue;
  2466. if (reg == NULL) {
  2467. wpa_printf(MSG_DEBUG, "P2P: Add operating "
  2468. "class %u", o->op_class);
  2469. reg = &chan->reg_class[cla];
  2470. cla++;
  2471. reg->reg_class = o->op_class;
  2472. }
  2473. reg->channel[reg->channels] = ch;
  2474. reg->channels++;
  2475. }
  2476. if (reg) {
  2477. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2478. reg->channel, reg->channels);
  2479. }
  2480. }
  2481. chan->reg_classes = cla;
  2482. return 0;
  2483. }
  2484. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2485. struct hostapd_hw_modes *mode, u8 channel)
  2486. {
  2487. int op, ret;
  2488. for (op = 0; op_class[op].op_class; op++) {
  2489. struct p2p_oper_class_map *o = &op_class[op];
  2490. u8 ch;
  2491. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2492. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  2493. o->bw == BW20 || ch != channel)
  2494. continue;
  2495. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2496. if (ret < 0)
  2497. continue;
  2498. else if (ret > 0)
  2499. return (o->bw == BW40MINUS) ? -1 : 1;
  2500. else
  2501. return 0;
  2502. }
  2503. }
  2504. return 0;
  2505. }
  2506. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  2507. size_t buf_len)
  2508. {
  2509. struct wpa_supplicant *wpa_s = ctx;
  2510. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2511. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  2512. break;
  2513. }
  2514. if (wpa_s == NULL)
  2515. return -1;
  2516. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  2517. }
  2518. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  2519. {
  2520. struct wpa_supplicant *wpa_s = ctx;
  2521. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2522. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2523. if (ssid == NULL)
  2524. continue;
  2525. if (ssid->mode != WPAS_MODE_INFRA)
  2526. continue;
  2527. if (wpa_s->wpa_state != WPA_COMPLETED &&
  2528. wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
  2529. continue;
  2530. if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
  2531. return 1;
  2532. }
  2533. return 0;
  2534. }
  2535. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  2536. {
  2537. struct wpa_supplicant *wpa_s = ctx;
  2538. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  2539. }
  2540. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
  2541. {
  2542. struct wpa_interface iface;
  2543. struct wpa_supplicant *p2pdev_wpa_s;
  2544. char ifname[100];
  2545. char force_name[100];
  2546. int ret;
  2547. os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  2548. wpa_s->ifname);
  2549. force_name[0] = '\0';
  2550. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  2551. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  2552. force_name, wpa_s->pending_interface_addr, NULL);
  2553. if (ret < 0) {
  2554. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  2555. return ret;
  2556. }
  2557. os_strlcpy(wpa_s->pending_interface_name, ifname,
  2558. sizeof(wpa_s->pending_interface_name));
  2559. os_memset(&iface, 0, sizeof(iface));
  2560. iface.p2p_mgmt = 1;
  2561. iface.ifname = wpa_s->pending_interface_name;
  2562. iface.driver = wpa_s->driver->name;
  2563. iface.driver_param = wpa_s->conf->driver_param;
  2564. iface.confname = wpa_s->confname;
  2565. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  2566. if (!p2pdev_wpa_s) {
  2567. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  2568. return -1;
  2569. }
  2570. p2pdev_wpa_s->parent = wpa_s;
  2571. wpa_s->pending_interface_name[0] = '\0';
  2572. return 0;
  2573. }
  2574. /**
  2575. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  2576. * @global: Pointer to global data from wpa_supplicant_init()
  2577. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2578. * Returns: 0 on success, -1 on failure
  2579. */
  2580. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  2581. {
  2582. struct p2p_config p2p;
  2583. unsigned int r;
  2584. int i;
  2585. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  2586. return 0;
  2587. if (global->p2p)
  2588. return 0;
  2589. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2590. struct p2p_params params;
  2591. wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
  2592. os_memset(&params, 0, sizeof(params));
  2593. params.dev_name = wpa_s->conf->device_name;
  2594. os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
  2595. WPS_DEV_TYPE_LEN);
  2596. params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2597. os_memcpy(params.sec_dev_type,
  2598. wpa_s->conf->sec_device_type,
  2599. params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2600. if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
  2601. return -1;
  2602. return 0;
  2603. }
  2604. os_memset(&p2p, 0, sizeof(p2p));
  2605. p2p.cb_ctx = wpa_s;
  2606. p2p.debug_print = wpas_p2p_debug_print;
  2607. p2p.p2p_scan = wpas_p2p_scan;
  2608. p2p.send_action = wpas_send_action;
  2609. p2p.send_action_done = wpas_send_action_done;
  2610. p2p.go_neg_completed = wpas_go_neg_completed;
  2611. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  2612. p2p.dev_found = wpas_dev_found;
  2613. p2p.dev_lost = wpas_dev_lost;
  2614. p2p.find_stopped = wpas_find_stopped;
  2615. p2p.start_listen = wpas_start_listen;
  2616. p2p.stop_listen = wpas_stop_listen;
  2617. p2p.send_probe_resp = wpas_send_probe_resp;
  2618. p2p.sd_request = wpas_sd_request;
  2619. p2p.sd_response = wpas_sd_response;
  2620. p2p.prov_disc_req = wpas_prov_disc_req;
  2621. p2p.prov_disc_resp = wpas_prov_disc_resp;
  2622. p2p.prov_disc_fail = wpas_prov_disc_fail;
  2623. p2p.invitation_process = wpas_invitation_process;
  2624. p2p.invitation_received = wpas_invitation_received;
  2625. p2p.invitation_result = wpas_invitation_result;
  2626. p2p.get_noa = wpas_get_noa;
  2627. p2p.go_connected = wpas_go_connected;
  2628. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  2629. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  2630. p2p.dev_name = wpa_s->conf->device_name;
  2631. p2p.manufacturer = wpa_s->conf->manufacturer;
  2632. p2p.model_name = wpa_s->conf->model_name;
  2633. p2p.model_number = wpa_s->conf->model_number;
  2634. p2p.serial_number = wpa_s->conf->serial_number;
  2635. if (wpa_s->wps) {
  2636. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  2637. p2p.config_methods = wpa_s->wps->config_methods;
  2638. }
  2639. if (wpa_s->conf->p2p_listen_reg_class &&
  2640. wpa_s->conf->p2p_listen_channel) {
  2641. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  2642. p2p.channel = wpa_s->conf->p2p_listen_channel;
  2643. } else {
  2644. p2p.reg_class = 81;
  2645. /*
  2646. * Pick one of the social channels randomly as the listen
  2647. * channel.
  2648. */
  2649. os_get_random((u8 *) &r, sizeof(r));
  2650. p2p.channel = 1 + (r % 3) * 5;
  2651. }
  2652. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  2653. if (wpa_s->conf->p2p_oper_reg_class &&
  2654. wpa_s->conf->p2p_oper_channel) {
  2655. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  2656. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  2657. p2p.cfg_op_channel = 1;
  2658. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  2659. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2660. } else {
  2661. p2p.op_reg_class = 81;
  2662. /*
  2663. * Use random operation channel from (1, 6, 11) if no other
  2664. * preference is indicated.
  2665. */
  2666. os_get_random((u8 *) &r, sizeof(r));
  2667. p2p.op_channel = 1 + (r % 3) * 5;
  2668. p2p.cfg_op_channel = 0;
  2669. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  2670. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2671. }
  2672. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  2673. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  2674. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  2675. }
  2676. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2677. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  2678. p2p.country[2] = 0x04;
  2679. } else
  2680. os_memcpy(p2p.country, "XX\x04", 3);
  2681. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
  2682. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  2683. "channel list");
  2684. return -1;
  2685. }
  2686. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  2687. WPS_DEV_TYPE_LEN);
  2688. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2689. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  2690. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2691. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  2692. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  2693. p2p.max_peers = 100;
  2694. if (wpa_s->conf->p2p_ssid_postfix) {
  2695. p2p.ssid_postfix_len =
  2696. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  2697. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  2698. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  2699. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  2700. p2p.ssid_postfix_len);
  2701. }
  2702. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  2703. p2p.max_listen = wpa_s->max_remain_on_chan;
  2704. global->p2p = p2p_init(&p2p);
  2705. if (global->p2p == NULL)
  2706. return -1;
  2707. global->p2p_init_wpa_s = wpa_s;
  2708. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  2709. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  2710. continue;
  2711. p2p_add_wps_vendor_extension(
  2712. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  2713. }
  2714. return 0;
  2715. }
  2716. /**
  2717. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  2718. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2719. *
  2720. * This function deinitialize per-interface P2P data.
  2721. */
  2722. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  2723. {
  2724. if (wpa_s->driver && wpa_s->drv_priv)
  2725. wpa_drv_probe_req_report(wpa_s, 0);
  2726. if (wpa_s->go_params) {
  2727. /* Clear any stored provisioning info */
  2728. p2p_clear_provisioning_info(
  2729. wpa_s->global->p2p,
  2730. wpa_s->go_params->peer_device_addr);
  2731. }
  2732. os_free(wpa_s->go_params);
  2733. wpa_s->go_params = NULL;
  2734. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2735. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2736. wpa_s->p2p_long_listen = 0;
  2737. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2738. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2739. wpas_p2p_remove_pending_group_interface(wpa_s);
  2740. /* TODO: remove group interface from the driver if this wpa_s instance
  2741. * is on top of a P2P group interface */
  2742. }
  2743. /**
  2744. * wpas_p2p_deinit_global - Deinitialize global P2P module
  2745. * @global: Pointer to global data from wpa_supplicant_init()
  2746. *
  2747. * This function deinitializes the global (per device) P2P module.
  2748. */
  2749. void wpas_p2p_deinit_global(struct wpa_global *global)
  2750. {
  2751. struct wpa_supplicant *wpa_s, *tmp;
  2752. wpa_s = global->ifaces;
  2753. if (wpa_s)
  2754. wpas_p2p_service_flush(wpa_s);
  2755. if (global->p2p == NULL)
  2756. return;
  2757. /* Remove remaining P2P group interfaces */
  2758. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  2759. wpa_s = wpa_s->next;
  2760. while (wpa_s) {
  2761. tmp = global->ifaces;
  2762. while (tmp &&
  2763. (tmp == wpa_s ||
  2764. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  2765. tmp = tmp->next;
  2766. }
  2767. if (tmp == NULL)
  2768. break;
  2769. /* Disconnect from the P2P group and deinit the interface */
  2770. wpas_p2p_disconnect(tmp);
  2771. }
  2772. /*
  2773. * Deinit GO data on any possibly remaining interface (if main
  2774. * interface is used as GO).
  2775. */
  2776. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2777. if (wpa_s->ap_iface)
  2778. wpas_p2p_group_deinit(wpa_s);
  2779. }
  2780. p2p_deinit(global->p2p);
  2781. global->p2p = NULL;
  2782. global->p2p_init_wpa_s = NULL;
  2783. }
  2784. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  2785. {
  2786. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  2787. wpa_s->conf->p2p_no_group_iface)
  2788. return 0; /* separate interface disabled per configuration */
  2789. if (wpa_s->drv_flags &
  2790. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  2791. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  2792. return 1; /* P2P group requires a new interface in every case
  2793. */
  2794. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  2795. return 0; /* driver does not support concurrent operations */
  2796. if (wpa_s->global->ifaces->next)
  2797. return 1; /* more that one interface already in use */
  2798. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2799. return 1; /* this interface is already in use */
  2800. return 0;
  2801. }
  2802. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  2803. const u8 *peer_addr,
  2804. enum p2p_wps_method wps_method,
  2805. int go_intent, const u8 *own_interface_addr,
  2806. unsigned int force_freq, int persistent_group,
  2807. struct wpa_ssid *ssid, unsigned int pref_freq)
  2808. {
  2809. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2810. persistent_group = 2;
  2811. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2812. return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
  2813. go_intent, own_interface_addr,
  2814. force_freq, persistent_group);
  2815. }
  2816. /*
  2817. * Increase GO config timeout if HT40 is used since it takes some time
  2818. * to scan channels for coex purposes before the BSS can be started.
  2819. */
  2820. p2p_set_config_timeout(wpa_s->global->p2p,
  2821. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  2822. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  2823. go_intent, own_interface_addr, force_freq,
  2824. persistent_group, ssid ? ssid->ssid : NULL,
  2825. ssid ? ssid->ssid_len : 0,
  2826. wpa_s->p2p_pd_before_go_neg, pref_freq);
  2827. }
  2828. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  2829. const u8 *peer_addr,
  2830. enum p2p_wps_method wps_method,
  2831. int go_intent, const u8 *own_interface_addr,
  2832. unsigned int force_freq, int persistent_group,
  2833. struct wpa_ssid *ssid, unsigned int pref_freq)
  2834. {
  2835. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2836. persistent_group = 2;
  2837. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2838. return -1;
  2839. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  2840. go_intent, own_interface_addr, force_freq,
  2841. persistent_group, ssid ? ssid->ssid : NULL,
  2842. ssid ? ssid->ssid_len : 0, pref_freq);
  2843. }
  2844. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  2845. {
  2846. wpa_s->p2p_join_scan_count++;
  2847. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  2848. wpa_s->p2p_join_scan_count);
  2849. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  2850. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  2851. " for join operationg - stop join attempt",
  2852. MAC2STR(wpa_s->pending_join_iface_addr));
  2853. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2854. if (wpa_s->p2p_auto_pd) {
  2855. wpa_s->p2p_auto_pd = 0;
  2856. wpa_msg_global(wpa_s, MSG_INFO,
  2857. P2P_EVENT_PROV_DISC_FAILURE
  2858. " p2p_dev_addr=" MACSTR " status=N/A",
  2859. MAC2STR(wpa_s->pending_join_dev_addr));
  2860. return;
  2861. }
  2862. wpa_msg_global(wpa_s->parent, MSG_INFO,
  2863. P2P_EVENT_GROUP_FORMATION_FAILURE);
  2864. }
  2865. }
  2866. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  2867. {
  2868. struct wpa_supplicant *iface;
  2869. int shared_freq;
  2870. u8 bssid[ETH_ALEN];
  2871. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
  2872. return 0;
  2873. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  2874. if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
  2875. continue;
  2876. if (iface->current_ssid == NULL || iface->assoc_freq == 0)
  2877. continue;
  2878. if (iface->current_ssid->mode == WPAS_MODE_AP ||
  2879. iface->current_ssid->mode == WPAS_MODE_P2P_GO)
  2880. shared_freq = iface->current_ssid->frequency;
  2881. else if (wpa_drv_get_bssid(iface, bssid) == 0)
  2882. shared_freq = iface->assoc_freq;
  2883. else
  2884. shared_freq = 0;
  2885. if (shared_freq && freq != shared_freq) {
  2886. wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
  2887. "connected on %d MHz - new connection on "
  2888. "%d MHz", iface->ifname, shared_freq, freq);
  2889. return 1;
  2890. }
  2891. }
  2892. shared_freq = wpa_drv_shared_freq(wpa_s);
  2893. if (shared_freq > 0 && shared_freq != freq) {
  2894. wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
  2895. "virtual interface connected on %d MHz - new "
  2896. "connection on %d MHz", shared_freq, freq);
  2897. return 1;
  2898. }
  2899. return 0;
  2900. }
  2901. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  2902. const u8 *peer_dev_addr)
  2903. {
  2904. struct wpa_bss *bss;
  2905. int updated;
  2906. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  2907. if (bss == NULL)
  2908. return -1;
  2909. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  2910. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  2911. "last scan");
  2912. return 0;
  2913. }
  2914. updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
  2915. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  2916. "%ld.%06ld (%supdated in last scan)",
  2917. bss->last_update.sec, bss->last_update.usec,
  2918. updated ? "": "not ");
  2919. return updated;
  2920. }
  2921. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  2922. struct wpa_scan_results *scan_res)
  2923. {
  2924. struct wpa_bss *bss;
  2925. int freq;
  2926. u8 iface_addr[ETH_ALEN];
  2927. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2928. if (wpa_s->global->p2p_disabled)
  2929. return;
  2930. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  2931. scan_res ? (int) scan_res->num : -1,
  2932. wpa_s->p2p_auto_join ? "auto_" : "");
  2933. if (scan_res)
  2934. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  2935. if (wpa_s->p2p_auto_pd) {
  2936. int join = wpas_p2p_peer_go(wpa_s,
  2937. wpa_s->pending_join_dev_addr);
  2938. if (join == 0 &&
  2939. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  2940. wpa_s->auto_pd_scan_retry++;
  2941. bss = wpa_bss_get_bssid_latest(
  2942. wpa_s, wpa_s->pending_join_dev_addr);
  2943. if (bss) {
  2944. freq = bss->freq;
  2945. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  2946. "the peer " MACSTR " at %d MHz",
  2947. wpa_s->auto_pd_scan_retry,
  2948. MAC2STR(wpa_s->
  2949. pending_join_dev_addr),
  2950. freq);
  2951. wpas_p2p_join_scan_req(wpa_s, freq);
  2952. return;
  2953. }
  2954. }
  2955. if (join < 0)
  2956. join = 0;
  2957. wpa_s->p2p_auto_pd = 0;
  2958. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  2959. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  2960. MAC2STR(wpa_s->pending_join_dev_addr), join);
  2961. if (p2p_prov_disc_req(wpa_s->global->p2p,
  2962. wpa_s->pending_join_dev_addr,
  2963. wpa_s->pending_pd_config_methods, join,
  2964. 0, wpa_s->user_initiated_pd) < 0) {
  2965. wpa_s->p2p_auto_pd = 0;
  2966. wpa_msg_global(wpa_s, MSG_INFO,
  2967. P2P_EVENT_PROV_DISC_FAILURE
  2968. " p2p_dev_addr=" MACSTR " status=N/A",
  2969. MAC2STR(wpa_s->pending_join_dev_addr));
  2970. }
  2971. return;
  2972. }
  2973. if (wpa_s->p2p_auto_join) {
  2974. int join = wpas_p2p_peer_go(wpa_s,
  2975. wpa_s->pending_join_dev_addr);
  2976. if (join < 0) {
  2977. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  2978. "running a GO -> use GO Negotiation");
  2979. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  2980. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  2981. wpa_s->p2p_persistent_group, 0, 0, 0,
  2982. wpa_s->p2p_go_intent,
  2983. wpa_s->p2p_connect_freq,
  2984. wpa_s->p2p_persistent_id,
  2985. wpa_s->p2p_pd_before_go_neg,
  2986. wpa_s->p2p_go_ht40);
  2987. return;
  2988. }
  2989. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  2990. "try to join the group", join ? "" :
  2991. " in older scan");
  2992. if (!join)
  2993. wpa_s->p2p_fallback_to_go_neg = 1;
  2994. }
  2995. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  2996. wpa_s->pending_join_iface_addr);
  2997. if (freq < 0 &&
  2998. p2p_get_interface_addr(wpa_s->global->p2p,
  2999. wpa_s->pending_join_dev_addr,
  3000. iface_addr) == 0 &&
  3001. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  3002. {
  3003. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3004. "address for join from " MACSTR " to " MACSTR
  3005. " based on newly discovered P2P peer entry",
  3006. MAC2STR(wpa_s->pending_join_iface_addr),
  3007. MAC2STR(iface_addr));
  3008. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  3009. ETH_ALEN);
  3010. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3011. wpa_s->pending_join_iface_addr);
  3012. }
  3013. if (freq >= 0) {
  3014. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3015. "from P2P peer table: %d MHz", freq);
  3016. }
  3017. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3018. if (bss) {
  3019. freq = bss->freq;
  3020. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3021. "from BSS table: %d MHz", freq);
  3022. }
  3023. if (freq > 0) {
  3024. u16 method;
  3025. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  3026. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3027. P2P_EVENT_GROUP_FORMATION_FAILURE
  3028. "reason=FREQ_CONFLICT");
  3029. return;
  3030. }
  3031. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  3032. "prior to joining an existing group (GO " MACSTR
  3033. " freq=%u MHz)",
  3034. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  3035. wpa_s->pending_pd_before_join = 1;
  3036. switch (wpa_s->pending_join_wps_method) {
  3037. case WPS_PIN_DISPLAY:
  3038. method = WPS_CONFIG_KEYPAD;
  3039. break;
  3040. case WPS_PIN_KEYPAD:
  3041. method = WPS_CONFIG_DISPLAY;
  3042. break;
  3043. case WPS_PBC:
  3044. method = WPS_CONFIG_PUSHBUTTON;
  3045. break;
  3046. default:
  3047. method = 0;
  3048. break;
  3049. }
  3050. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  3051. wpa_s->pending_join_dev_addr) ==
  3052. method)) {
  3053. /*
  3054. * We have already performed provision discovery for
  3055. * joining the group. Proceed directly to join
  3056. * operation without duplicated provision discovery. */
  3057. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  3058. "with " MACSTR " already done - proceed to "
  3059. "join",
  3060. MAC2STR(wpa_s->pending_join_dev_addr));
  3061. wpa_s->pending_pd_before_join = 0;
  3062. goto start;
  3063. }
  3064. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3065. wpa_s->pending_join_dev_addr, method, 1,
  3066. freq, wpa_s->user_initiated_pd) < 0) {
  3067. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  3068. "Discovery Request before joining an "
  3069. "existing group");
  3070. wpa_s->pending_pd_before_join = 0;
  3071. goto start;
  3072. }
  3073. return;
  3074. }
  3075. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  3076. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3077. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3078. wpas_p2p_check_join_scan_limit(wpa_s);
  3079. return;
  3080. start:
  3081. /* Start join operation immediately */
  3082. wpas_p2p_join_start(wpa_s);
  3083. }
  3084. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
  3085. {
  3086. int ret;
  3087. struct wpa_driver_scan_params params;
  3088. struct wpabuf *wps_ie, *ies;
  3089. size_t ielen;
  3090. int freqs[2] = { 0, 0 };
  3091. os_memset(&params, 0, sizeof(params));
  3092. /* P2P Wildcard SSID */
  3093. params.num_ssids = 1;
  3094. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  3095. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  3096. wpa_s->wps->dev.p2p = 1;
  3097. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  3098. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  3099. NULL);
  3100. if (wps_ie == NULL) {
  3101. wpas_p2p_scan_res_join(wpa_s, NULL);
  3102. return;
  3103. }
  3104. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  3105. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  3106. if (ies == NULL) {
  3107. wpabuf_free(wps_ie);
  3108. wpas_p2p_scan_res_join(wpa_s, NULL);
  3109. return;
  3110. }
  3111. wpabuf_put_buf(ies, wps_ie);
  3112. wpabuf_free(wps_ie);
  3113. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  3114. params.p2p_probe = 1;
  3115. params.extra_ies = wpabuf_head(ies);
  3116. params.extra_ies_len = wpabuf_len(ies);
  3117. if (freq > 0) {
  3118. freqs[0] = freq;
  3119. params.freqs = freqs;
  3120. }
  3121. /*
  3122. * Run a scan to update BSS table and start Provision Discovery once
  3123. * the new scan results become available.
  3124. */
  3125. ret = wpa_drv_scan(wpa_s, &params);
  3126. if (!ret) {
  3127. os_get_time(&wpa_s->scan_trigger_time);
  3128. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  3129. }
  3130. wpabuf_free(ies);
  3131. if (ret) {
  3132. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  3133. "try again later");
  3134. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3135. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3136. wpas_p2p_check_join_scan_limit(wpa_s);
  3137. }
  3138. }
  3139. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  3140. {
  3141. struct wpa_supplicant *wpa_s = eloop_ctx;
  3142. wpas_p2p_join_scan_req(wpa_s, 0);
  3143. }
  3144. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  3145. const u8 *dev_addr, enum p2p_wps_method wps_method,
  3146. int auto_join)
  3147. {
  3148. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  3149. MACSTR " dev " MACSTR ")%s",
  3150. MAC2STR(iface_addr), MAC2STR(dev_addr),
  3151. auto_join ? " (auto_join)" : "");
  3152. wpa_s->p2p_auto_pd = 0;
  3153. wpa_s->p2p_auto_join = !!auto_join;
  3154. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  3155. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  3156. wpa_s->pending_join_wps_method = wps_method;
  3157. /* Make sure we are not running find during connection establishment */
  3158. wpas_p2p_stop_find(wpa_s);
  3159. wpa_s->p2p_join_scan_count = 0;
  3160. wpas_p2p_join_scan(wpa_s, NULL);
  3161. return 0;
  3162. }
  3163. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  3164. {
  3165. struct wpa_supplicant *group;
  3166. struct p2p_go_neg_results res;
  3167. struct wpa_bss *bss;
  3168. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  3169. if (group == NULL)
  3170. return -1;
  3171. if (group != wpa_s) {
  3172. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  3173. sizeof(group->p2p_pin));
  3174. group->p2p_wps_method = wpa_s->p2p_wps_method;
  3175. } else {
  3176. /*
  3177. * Need to mark the current interface for p2p_group_formation
  3178. * when a separate group interface is not used. This is needed
  3179. * to allow p2p_cancel stop a pending p2p_connect-join.
  3180. * wpas_p2p_init_group_interface() addresses this for the case
  3181. * where a separate group interface is used.
  3182. */
  3183. wpa_s->global->p2p_group_formation = wpa_s;
  3184. }
  3185. group->p2p_in_provisioning = 1;
  3186. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  3187. os_memset(&res, 0, sizeof(res));
  3188. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  3189. ETH_ALEN);
  3190. res.wps_method = wpa_s->pending_join_wps_method;
  3191. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3192. if (bss) {
  3193. res.freq = bss->freq;
  3194. res.ssid_len = bss->ssid_len;
  3195. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  3196. }
  3197. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  3198. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  3199. "starting client");
  3200. wpa_drv_cancel_remain_on_channel(wpa_s);
  3201. wpa_s->off_channel_freq = 0;
  3202. wpa_s->roc_waiting_drv_freq = 0;
  3203. }
  3204. wpas_start_wps_enrollee(group, &res);
  3205. /*
  3206. * Allow a longer timeout for join-a-running-group than normal 15
  3207. * second group formation timeout since the GO may not have authorized
  3208. * our connection yet.
  3209. */
  3210. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3211. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  3212. wpa_s, NULL);
  3213. return 0;
  3214. }
  3215. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  3216. int *force_freq, int *pref_freq,
  3217. int *oper_freq)
  3218. {
  3219. if (freq > 0) {
  3220. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3221. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  3222. "(%u MHz) is not supported for P2P uses",
  3223. freq);
  3224. return -3;
  3225. }
  3226. if (*oper_freq > 0 && freq != *oper_freq &&
  3227. !(wpa_s->drv_flags &
  3228. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  3229. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
  3230. "on %u MHz while connected on another "
  3231. "channel (%u MHz)", freq, *oper_freq);
  3232. return -2;
  3233. }
  3234. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  3235. "requested channel (%u MHz)", freq);
  3236. *force_freq = freq;
  3237. } else if (*oper_freq > 0 &&
  3238. !p2p_supported_freq(wpa_s->global->p2p, *oper_freq)) {
  3239. if (!(wpa_s->drv_flags &
  3240. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  3241. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
  3242. "while connected on non-P2P supported "
  3243. "channel (%u MHz)", *oper_freq);
  3244. return -2;
  3245. }
  3246. wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
  3247. "(%u MHz) not available for P2P - try to use "
  3248. "another channel", *oper_freq);
  3249. *force_freq = 0;
  3250. } else if (*oper_freq > 0 && *pref_freq == 0 &&
  3251. (wpa_s->drv_flags &
  3252. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  3253. wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
  3254. "are already using (%u MHz) on another interface",
  3255. *oper_freq);
  3256. *pref_freq = *oper_freq;
  3257. } else if (*oper_freq > 0) {
  3258. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  3259. "channel we are already using (%u MHz) on another "
  3260. "interface", *oper_freq);
  3261. *force_freq = *oper_freq;
  3262. }
  3263. return 0;
  3264. }
  3265. /**
  3266. * wpas_p2p_connect - Request P2P Group Formation to be started
  3267. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3268. * @peer_addr: Address of the peer P2P Device
  3269. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  3270. * @persistent_group: Whether to create a persistent group
  3271. * @auto_join: Whether to select join vs. GO Negotiation automatically
  3272. * @join: Whether to join an existing group (as a client) instead of starting
  3273. * Group Owner negotiation; @peer_addr is BSSID in that case
  3274. * @auth: Whether to only authorize the connection instead of doing that and
  3275. * initiating Group Owner negotiation
  3276. * @go_intent: GO Intent or -1 to use default
  3277. * @freq: Frequency for the group or 0 for auto-selection
  3278. * @persistent_id: Persistent group credentials to use for forcing GO
  3279. * parameters or -1 to generate new values (SSID/passphrase)
  3280. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  3281. * interoperability workaround when initiating group formation
  3282. * @ht40: Start GO with 40 MHz channel width
  3283. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  3284. * failure, -2 on failure due to channel not currently available,
  3285. * -3 if forced channel is not supported
  3286. */
  3287. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3288. const char *pin, enum p2p_wps_method wps_method,
  3289. int persistent_group, int auto_join, int join, int auth,
  3290. int go_intent, int freq, int persistent_id, int pd,
  3291. int ht40)
  3292. {
  3293. int force_freq = 0, pref_freq = 0, oper_freq = 0;
  3294. u8 bssid[ETH_ALEN];
  3295. int ret = 0, res;
  3296. enum wpa_driver_if_type iftype;
  3297. const u8 *if_addr;
  3298. struct wpa_ssid *ssid = NULL;
  3299. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3300. return -1;
  3301. if (persistent_id >= 0) {
  3302. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3303. if (ssid == NULL || ssid->disabled != 2 ||
  3304. ssid->mode != WPAS_MODE_P2P_GO)
  3305. return -1;
  3306. }
  3307. if (go_intent < 0)
  3308. go_intent = wpa_s->conf->p2p_go_intent;
  3309. if (!auth)
  3310. wpa_s->p2p_long_listen = 0;
  3311. wpa_s->p2p_wps_method = wps_method;
  3312. wpa_s->p2p_persistent_group = !!persistent_group;
  3313. wpa_s->p2p_persistent_id = persistent_id;
  3314. wpa_s->p2p_go_intent = go_intent;
  3315. wpa_s->p2p_connect_freq = freq;
  3316. wpa_s->p2p_fallback_to_go_neg = 0;
  3317. wpa_s->p2p_pd_before_go_neg = !!pd;
  3318. wpa_s->p2p_go_ht40 = !!ht40;
  3319. if (pin)
  3320. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  3321. else if (wps_method == WPS_PIN_DISPLAY) {
  3322. ret = wps_generate_pin();
  3323. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  3324. ret);
  3325. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  3326. wpa_s->p2p_pin);
  3327. } else
  3328. wpa_s->p2p_pin[0] = '\0';
  3329. if (join || auto_join) {
  3330. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  3331. if (auth) {
  3332. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  3333. "connect a running group from " MACSTR,
  3334. MAC2STR(peer_addr));
  3335. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  3336. return ret;
  3337. }
  3338. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  3339. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  3340. iface_addr) < 0) {
  3341. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  3342. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  3343. dev_addr);
  3344. }
  3345. if (auto_join) {
  3346. os_get_time(&wpa_s->p2p_auto_started);
  3347. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  3348. "%ld.%06ld",
  3349. wpa_s->p2p_auto_started.sec,
  3350. wpa_s->p2p_auto_started.usec);
  3351. }
  3352. wpa_s->user_initiated_pd = 1;
  3353. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  3354. auto_join) < 0)
  3355. return -1;
  3356. return ret;
  3357. }
  3358. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  3359. wpa_s->assoc_freq) {
  3360. oper_freq = wpa_s->assoc_freq;
  3361. } else {
  3362. oper_freq = wpa_drv_shared_freq(wpa_s);
  3363. if (oper_freq < 0)
  3364. oper_freq = 0;
  3365. }
  3366. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  3367. &oper_freq);
  3368. if (res)
  3369. return res;
  3370. wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
  3371. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  3372. if (wpa_s->create_p2p_iface) {
  3373. /* Prepare to add a new interface for the group */
  3374. iftype = WPA_IF_P2P_GROUP;
  3375. if (go_intent == 15)
  3376. iftype = WPA_IF_P2P_GO;
  3377. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  3378. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  3379. "interface for the group");
  3380. return -1;
  3381. }
  3382. if_addr = wpa_s->pending_interface_addr;
  3383. } else
  3384. if_addr = wpa_s->own_addr;
  3385. if (auth) {
  3386. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  3387. go_intent, if_addr,
  3388. force_freq, persistent_group, ssid,
  3389. pref_freq) < 0)
  3390. return -1;
  3391. return ret;
  3392. }
  3393. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  3394. go_intent, if_addr, force_freq,
  3395. persistent_group, ssid, pref_freq) < 0) {
  3396. if (wpa_s->create_p2p_iface)
  3397. wpas_p2p_remove_pending_group_interface(wpa_s);
  3398. return -1;
  3399. }
  3400. return ret;
  3401. }
  3402. /**
  3403. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  3404. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3405. * @freq: Frequency of the channel in MHz
  3406. * @duration: Duration of the stay on the channel in milliseconds
  3407. *
  3408. * This callback is called when the driver indicates that it has started the
  3409. * requested remain-on-channel duration.
  3410. */
  3411. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3412. unsigned int freq, unsigned int duration)
  3413. {
  3414. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3415. return;
  3416. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  3417. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  3418. wpa_s->pending_listen_duration);
  3419. wpa_s->pending_listen_freq = 0;
  3420. }
  3421. }
  3422. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  3423. unsigned int timeout)
  3424. {
  3425. /* Limit maximum Listen state time based on driver limitation. */
  3426. if (timeout > wpa_s->max_remain_on_chan)
  3427. timeout = wpa_s->max_remain_on_chan;
  3428. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3429. return wpa_drv_p2p_listen(wpa_s, timeout);
  3430. return p2p_listen(wpa_s->global->p2p, timeout);
  3431. }
  3432. /**
  3433. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  3434. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3435. * @freq: Frequency of the channel in MHz
  3436. *
  3437. * This callback is called when the driver indicates that a remain-on-channel
  3438. * operation has been completed, i.e., the duration on the requested channel
  3439. * has timed out.
  3440. */
  3441. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3442. unsigned int freq)
  3443. {
  3444. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  3445. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  3446. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  3447. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3448. return;
  3449. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  3450. return; /* P2P module started a new operation */
  3451. if (offchannel_pending_action_tx(wpa_s))
  3452. return;
  3453. if (wpa_s->p2p_long_listen > 0)
  3454. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  3455. if (wpa_s->p2p_long_listen > 0) {
  3456. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  3457. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  3458. }
  3459. }
  3460. /**
  3461. * wpas_p2p_group_remove - Remove a P2P group
  3462. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3463. * @ifname: Network interface name of the group interface or "*" to remove all
  3464. * groups
  3465. * Returns: 0 on success, -1 on failure
  3466. *
  3467. * This function is used to remove a P2P group. This can be used to disconnect
  3468. * from a group in which the local end is a P2P Client or to end a P2P Group in
  3469. * case the local end is the Group Owner. If a virtual network interface was
  3470. * created for this group, that interface will be removed. Otherwise, only the
  3471. * configured P2P group network will be removed from the interface.
  3472. */
  3473. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  3474. {
  3475. struct wpa_global *global = wpa_s->global;
  3476. if (os_strcmp(ifname, "*") == 0) {
  3477. struct wpa_supplicant *prev;
  3478. wpa_s = global->ifaces;
  3479. while (wpa_s) {
  3480. prev = wpa_s;
  3481. wpa_s = wpa_s->next;
  3482. wpas_p2p_disconnect(prev);
  3483. }
  3484. return 0;
  3485. }
  3486. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3487. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3488. break;
  3489. }
  3490. return wpas_p2p_disconnect(wpa_s);
  3491. }
  3492. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  3493. {
  3494. unsigned int r;
  3495. if (freq == 2) {
  3496. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  3497. "band");
  3498. if (wpa_s->best_24_freq > 0 &&
  3499. p2p_supported_freq(wpa_s->global->p2p,
  3500. wpa_s->best_24_freq)) {
  3501. freq = wpa_s->best_24_freq;
  3502. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  3503. "channel: %d MHz", freq);
  3504. } else {
  3505. os_get_random((u8 *) &r, sizeof(r));
  3506. freq = 2412 + (r % 3) * 25;
  3507. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  3508. "channel: %d MHz", freq);
  3509. }
  3510. }
  3511. if (freq == 5) {
  3512. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  3513. "band");
  3514. if (wpa_s->best_5_freq > 0 &&
  3515. p2p_supported_freq(wpa_s->global->p2p,
  3516. wpa_s->best_5_freq)) {
  3517. freq = wpa_s->best_5_freq;
  3518. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  3519. "channel: %d MHz", freq);
  3520. } else {
  3521. os_get_random((u8 *) &r, sizeof(r));
  3522. freq = 5180 + (r % 4) * 20;
  3523. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3524. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  3525. "5 GHz channel for P2P group");
  3526. return -1;
  3527. }
  3528. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  3529. "channel: %d MHz", freq);
  3530. }
  3531. }
  3532. if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3533. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  3534. "(%u MHz) is not supported for P2P uses",
  3535. freq);
  3536. return -1;
  3537. }
  3538. return freq;
  3539. }
  3540. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  3541. struct p2p_go_neg_results *params,
  3542. int freq, int ht40,
  3543. const struct p2p_channels *channels)
  3544. {
  3545. u8 bssid[ETH_ALEN];
  3546. int res;
  3547. unsigned int pref_freq;
  3548. os_memset(params, 0, sizeof(*params));
  3549. params->role_go = 1;
  3550. params->ht40 = ht40;
  3551. if (freq) {
  3552. if (!freq_included(channels, freq)) {
  3553. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  3554. "accepted", freq);
  3555. return -1;
  3556. }
  3557. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  3558. "frequency %d MHz", freq);
  3559. params->freq = freq;
  3560. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  3561. wpa_s->conf->p2p_oper_channel >= 1 &&
  3562. wpa_s->conf->p2p_oper_channel <= 11 &&
  3563. freq_included(channels,
  3564. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3565. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  3566. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3567. "frequency %d MHz", params->freq);
  3568. } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  3569. wpa_s->conf->p2p_oper_reg_class == 116 ||
  3570. wpa_s->conf->p2p_oper_reg_class == 117 ||
  3571. wpa_s->conf->p2p_oper_reg_class == 124 ||
  3572. wpa_s->conf->p2p_oper_reg_class == 126 ||
  3573. wpa_s->conf->p2p_oper_reg_class == 127) &&
  3574. freq_included(channels,
  3575. 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3576. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  3577. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3578. "frequency %d MHz", params->freq);
  3579. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3580. wpa_s->best_overall_freq > 0 &&
  3581. p2p_supported_freq(wpa_s->global->p2p,
  3582. wpa_s->best_overall_freq) &&
  3583. freq_included(channels, wpa_s->best_overall_freq)) {
  3584. params->freq = wpa_s->best_overall_freq;
  3585. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  3586. "channel %d MHz", params->freq);
  3587. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3588. wpa_s->best_24_freq > 0 &&
  3589. p2p_supported_freq(wpa_s->global->p2p,
  3590. wpa_s->best_24_freq) &&
  3591. freq_included(channels, wpa_s->best_24_freq)) {
  3592. params->freq = wpa_s->best_24_freq;
  3593. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  3594. "channel %d MHz", params->freq);
  3595. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3596. wpa_s->best_5_freq > 0 &&
  3597. p2p_supported_freq(wpa_s->global->p2p,
  3598. wpa_s->best_5_freq) &&
  3599. freq_included(channels, wpa_s->best_5_freq)) {
  3600. params->freq = wpa_s->best_5_freq;
  3601. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  3602. "channel %d MHz", params->freq);
  3603. } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
  3604. channels))) {
  3605. params->freq = pref_freq;
  3606. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  3607. "channels", params->freq);
  3608. } else {
  3609. int chan;
  3610. for (chan = 0; chan < 11; chan++) {
  3611. params->freq = 2412 + chan * 5;
  3612. if (!wpas_p2p_disallowed_freq(wpa_s->global,
  3613. params->freq) &&
  3614. freq_included(channels, params->freq))
  3615. break;
  3616. }
  3617. if (chan == 11) {
  3618. wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
  3619. "allowed");
  3620. return -1;
  3621. }
  3622. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  3623. "known)", params->freq);
  3624. }
  3625. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
  3626. wpa_s->assoc_freq && !freq) {
  3627. if (!p2p_supported_freq(wpa_s->global->p2p, wpa_s->assoc_freq)
  3628. || !freq_included(channels, wpa_s->assoc_freq)) {
  3629. if (wpa_s->drv_flags &
  3630. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
  3631. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
  3632. "the channel we are already using "
  3633. "(%u MHz) - allow multi-channel "
  3634. "concurrency", wpa_s->assoc_freq);
  3635. } else {
  3636. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on "
  3637. "the channel we are already using "
  3638. "(%u MHz)", wpa_s->assoc_freq);
  3639. return -1;
  3640. }
  3641. } else {
  3642. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we "
  3643. "are already using (%u MHz)",
  3644. wpa_s->assoc_freq);
  3645. params->freq = wpa_s->assoc_freq;
  3646. }
  3647. }
  3648. res = wpa_drv_shared_freq(wpa_s);
  3649. if (res > 0 && !freq &&
  3650. (!p2p_supported_freq(wpa_s->global->p2p, res) ||
  3651. !freq_included(channels, res))) {
  3652. if (wpa_s->drv_flags &
  3653. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
  3654. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
  3655. "channel we are already using on a shared "
  3656. "interface (%u MHz) - allow multi-channel "
  3657. "concurrency", res);
  3658. } else {
  3659. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on the "
  3660. "channel we are already using on a shared "
  3661. "interface (%u MHz)", res);
  3662. return -1;
  3663. }
  3664. } else if (res > 0 && !freq) {
  3665. wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
  3666. "already using on a shared interface");
  3667. params->freq = res;
  3668. if (!freq_included(channels, params->freq)) {
  3669. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  3670. "accepted", params->freq);
  3671. return -1;
  3672. }
  3673. } else if (res > 0 && freq != res &&
  3674. !(wpa_s->drv_flags &
  3675. WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
  3676. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
  3677. "while connected on another channel (%u MHz)",
  3678. freq, res);
  3679. return -1;
  3680. }
  3681. return 0;
  3682. }
  3683. static struct wpa_supplicant *
  3684. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  3685. int go)
  3686. {
  3687. struct wpa_supplicant *group_wpa_s;
  3688. if (!wpas_p2p_create_iface(wpa_s)) {
  3689. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  3690. "operations");
  3691. return wpa_s;
  3692. }
  3693. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  3694. WPA_IF_P2P_CLIENT) < 0) {
  3695. wpa_msg_global(wpa_s, MSG_ERROR,
  3696. "P2P: Failed to add group interface");
  3697. return NULL;
  3698. }
  3699. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  3700. if (group_wpa_s == NULL) {
  3701. wpa_msg_global(wpa_s, MSG_ERROR,
  3702. "P2P: Failed to initialize group interface");
  3703. wpas_p2p_remove_pending_group_interface(wpa_s);
  3704. return NULL;
  3705. }
  3706. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  3707. group_wpa_s->ifname);
  3708. return group_wpa_s;
  3709. }
  3710. /**
  3711. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  3712. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3713. * @persistent_group: Whether to create a persistent group
  3714. * @freq: Frequency for the group or 0 to indicate no hardcoding
  3715. * Returns: 0 on success, -1 on failure
  3716. *
  3717. * This function creates a new P2P group with the local end as the Group Owner,
  3718. * i.e., without using Group Owner Negotiation.
  3719. */
  3720. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  3721. int freq, int ht40)
  3722. {
  3723. struct p2p_go_neg_results params;
  3724. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3725. return -1;
  3726. /* Make sure we are not running find during connection establishment */
  3727. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  3728. wpas_p2p_stop_find_oper(wpa_s);
  3729. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3730. if (freq < 0)
  3731. return -1;
  3732. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
  3733. return -1;
  3734. if (params.freq &&
  3735. !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
  3736. wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
  3737. "(%u MHz) is not supported for P2P uses",
  3738. params.freq);
  3739. return -1;
  3740. }
  3741. p2p_go_params(wpa_s->global->p2p, &params);
  3742. params.persistent_group = persistent_group;
  3743. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  3744. if (wpa_s == NULL)
  3745. return -1;
  3746. wpas_start_wps_go(wpa_s, &params, 0);
  3747. return 0;
  3748. }
  3749. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  3750. struct wpa_ssid *params, int addr_allocated)
  3751. {
  3752. struct wpa_ssid *ssid;
  3753. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  3754. if (wpa_s == NULL)
  3755. return -1;
  3756. wpa_supplicant_ap_deinit(wpa_s);
  3757. ssid = wpa_config_add_network(wpa_s->conf);
  3758. if (ssid == NULL)
  3759. return -1;
  3760. wpa_config_set_network_defaults(ssid);
  3761. ssid->temporary = 1;
  3762. ssid->proto = WPA_PROTO_RSN;
  3763. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  3764. ssid->group_cipher = WPA_CIPHER_CCMP;
  3765. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  3766. ssid->ssid = os_malloc(params->ssid_len);
  3767. if (ssid->ssid == NULL) {
  3768. wpa_config_remove_network(wpa_s->conf, ssid->id);
  3769. return -1;
  3770. }
  3771. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  3772. ssid->ssid_len = params->ssid_len;
  3773. ssid->p2p_group = 1;
  3774. ssid->export_keys = 1;
  3775. if (params->psk_set) {
  3776. os_memcpy(ssid->psk, params->psk, 32);
  3777. ssid->psk_set = 1;
  3778. }
  3779. if (params->passphrase)
  3780. ssid->passphrase = os_strdup(params->passphrase);
  3781. wpa_supplicant_select_network(wpa_s, ssid);
  3782. wpa_s->show_group_started = 1;
  3783. return 0;
  3784. }
  3785. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  3786. struct wpa_ssid *ssid, int addr_allocated,
  3787. int freq, int ht40,
  3788. const struct p2p_channels *channels)
  3789. {
  3790. struct p2p_go_neg_results params;
  3791. int go = 0;
  3792. if (ssid->disabled != 2 || ssid->ssid == NULL)
  3793. return -1;
  3794. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  3795. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  3796. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  3797. "already running");
  3798. return 0;
  3799. }
  3800. /* Make sure we are not running find during connection establishment */
  3801. wpas_p2p_stop_find_oper(wpa_s);
  3802. wpa_s->p2p_fallback_to_go_neg = 0;
  3803. if (ssid->mode == WPAS_MODE_INFRA)
  3804. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  3805. if (ssid->mode != WPAS_MODE_P2P_GO)
  3806. return -1;
  3807. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3808. if (freq < 0)
  3809. return -1;
  3810. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
  3811. return -1;
  3812. params.role_go = 1;
  3813. params.psk_set = ssid->psk_set;
  3814. if (params.psk_set)
  3815. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  3816. if (ssid->passphrase) {
  3817. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  3818. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  3819. "persistent group");
  3820. return -1;
  3821. }
  3822. os_strlcpy(params.passphrase, ssid->passphrase,
  3823. sizeof(params.passphrase));
  3824. }
  3825. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  3826. params.ssid_len = ssid->ssid_len;
  3827. params.persistent_group = 1;
  3828. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  3829. if (wpa_s == NULL)
  3830. return -1;
  3831. wpas_start_wps_go(wpa_s, &params, 0);
  3832. return 0;
  3833. }
  3834. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  3835. struct wpabuf *proberesp_ies)
  3836. {
  3837. struct wpa_supplicant *wpa_s = ctx;
  3838. if (wpa_s->ap_iface) {
  3839. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  3840. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  3841. wpabuf_free(beacon_ies);
  3842. wpabuf_free(proberesp_ies);
  3843. return;
  3844. }
  3845. if (beacon_ies) {
  3846. wpabuf_free(hapd->p2p_beacon_ie);
  3847. hapd->p2p_beacon_ie = beacon_ies;
  3848. }
  3849. wpabuf_free(hapd->p2p_probe_resp_ie);
  3850. hapd->p2p_probe_resp_ie = proberesp_ies;
  3851. } else {
  3852. wpabuf_free(beacon_ies);
  3853. wpabuf_free(proberesp_ies);
  3854. }
  3855. wpa_supplicant_ap_update_beacon(wpa_s);
  3856. }
  3857. static void wpas_p2p_idle_update(void *ctx, int idle)
  3858. {
  3859. struct wpa_supplicant *wpa_s = ctx;
  3860. if (!wpa_s->ap_iface)
  3861. return;
  3862. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  3863. if (idle)
  3864. wpas_p2p_set_group_idle_timeout(wpa_s);
  3865. else
  3866. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3867. }
  3868. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  3869. struct wpa_ssid *ssid)
  3870. {
  3871. struct p2p_group *group;
  3872. struct p2p_group_config *cfg;
  3873. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3874. return NULL;
  3875. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3876. return NULL;
  3877. cfg = os_zalloc(sizeof(*cfg));
  3878. if (cfg == NULL)
  3879. return NULL;
  3880. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  3881. cfg->persistent_group = 2;
  3882. else if (ssid->p2p_persistent_group)
  3883. cfg->persistent_group = 1;
  3884. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  3885. if (wpa_s->max_stations &&
  3886. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  3887. cfg->max_clients = wpa_s->max_stations;
  3888. else
  3889. cfg->max_clients = wpa_s->conf->max_num_sta;
  3890. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  3891. cfg->ssid_len = ssid->ssid_len;
  3892. cfg->cb_ctx = wpa_s;
  3893. cfg->ie_update = wpas_p2p_ie_update;
  3894. cfg->idle_update = wpas_p2p_idle_update;
  3895. group = p2p_group_init(wpa_s->global->p2p, cfg);
  3896. if (group == NULL)
  3897. os_free(cfg);
  3898. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  3899. p2p_group_notif_formation_done(group);
  3900. wpa_s->p2p_group = group;
  3901. return group;
  3902. }
  3903. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3904. int registrar)
  3905. {
  3906. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3907. if (!wpa_s->p2p_in_provisioning) {
  3908. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  3909. "provisioning not in progress");
  3910. return;
  3911. }
  3912. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  3913. u8 go_dev_addr[ETH_ALEN];
  3914. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  3915. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  3916. ssid->ssid_len);
  3917. /* Clear any stored provisioning info */
  3918. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  3919. }
  3920. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  3921. NULL);
  3922. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  3923. /*
  3924. * Use a separate timeout for initial data connection to
  3925. * complete to allow the group to be removed automatically if
  3926. * something goes wrong in this step before the P2P group idle
  3927. * timeout mechanism is taken into use.
  3928. */
  3929. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  3930. wpas_p2p_group_formation_timeout,
  3931. wpa_s->parent, NULL);
  3932. }
  3933. if (wpa_s->global->p2p)
  3934. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  3935. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3936. wpa_drv_wps_success_cb(wpa_s, peer_addr);
  3937. wpas_group_formation_completed(wpa_s, 1);
  3938. }
  3939. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  3940. struct wps_event_fail *fail)
  3941. {
  3942. if (!wpa_s->p2p_in_provisioning) {
  3943. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  3944. "provisioning not in progress");
  3945. return;
  3946. }
  3947. if (wpa_s->go_params) {
  3948. p2p_clear_provisioning_info(
  3949. wpa_s->global->p2p,
  3950. wpa_s->go_params->peer_device_addr);
  3951. }
  3952. wpas_notify_p2p_wps_failed(wpa_s, fail);
  3953. }
  3954. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3955. const char *config_method,
  3956. enum wpas_p2p_prov_disc_use use)
  3957. {
  3958. u16 config_methods;
  3959. wpa_s->p2p_fallback_to_go_neg = 0;
  3960. wpa_s->pending_pd_use = NORMAL_PD;
  3961. if (os_strncmp(config_method, "display", 7) == 0)
  3962. config_methods = WPS_CONFIG_DISPLAY;
  3963. else if (os_strncmp(config_method, "keypad", 6) == 0)
  3964. config_methods = WPS_CONFIG_KEYPAD;
  3965. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  3966. os_strncmp(config_method, "pushbutton", 10) == 0)
  3967. config_methods = WPS_CONFIG_PUSHBUTTON;
  3968. else {
  3969. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  3970. return -1;
  3971. }
  3972. if (use == WPAS_P2P_PD_AUTO) {
  3973. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  3974. wpa_s->pending_pd_config_methods = config_methods;
  3975. wpa_s->p2p_auto_pd = 1;
  3976. wpa_s->p2p_auto_join = 0;
  3977. wpa_s->pending_pd_before_join = 0;
  3978. wpa_s->auto_pd_scan_retry = 0;
  3979. wpas_p2p_stop_find(wpa_s);
  3980. wpa_s->p2p_join_scan_count = 0;
  3981. os_get_time(&wpa_s->p2p_auto_started);
  3982. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  3983. wpa_s->p2p_auto_started.sec,
  3984. wpa_s->p2p_auto_started.usec);
  3985. wpas_p2p_join_scan(wpa_s, NULL);
  3986. return 0;
  3987. }
  3988. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  3989. return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
  3990. config_methods,
  3991. use == WPAS_P2P_PD_FOR_JOIN);
  3992. }
  3993. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  3994. return -1;
  3995. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  3996. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  3997. 0, 1);
  3998. }
  3999. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  4000. char *end)
  4001. {
  4002. return p2p_scan_result_text(ies, ies_len, buf, end);
  4003. }
  4004. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  4005. {
  4006. if (!offchannel_pending_action_tx(wpa_s))
  4007. return;
  4008. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  4009. "operation request");
  4010. offchannel_clear_pending_action_tx(wpa_s);
  4011. }
  4012. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  4013. enum p2p_discovery_type type,
  4014. unsigned int num_req_dev_types, const u8 *req_dev_types,
  4015. const u8 *dev_id, unsigned int search_delay)
  4016. {
  4017. wpas_p2p_clear_pending_action_tx(wpa_s);
  4018. wpa_s->p2p_long_listen = 0;
  4019. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4020. return wpa_drv_p2p_find(wpa_s, timeout, type);
  4021. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  4022. wpa_s->p2p_in_provisioning)
  4023. return -1;
  4024. wpa_supplicant_cancel_sched_scan(wpa_s);
  4025. return p2p_find(wpa_s->global->p2p, timeout, type,
  4026. num_req_dev_types, req_dev_types, dev_id,
  4027. search_delay);
  4028. }
  4029. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  4030. {
  4031. wpas_p2p_clear_pending_action_tx(wpa_s);
  4032. wpa_s->p2p_long_listen = 0;
  4033. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4034. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4035. wpa_s->global->p2p_cb_on_scan_complete = 0;
  4036. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4037. wpa_drv_p2p_stop_find(wpa_s);
  4038. return 1;
  4039. }
  4040. if (wpa_s->global->p2p)
  4041. p2p_stop_find(wpa_s->global->p2p);
  4042. return 0;
  4043. }
  4044. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  4045. {
  4046. if (wpas_p2p_stop_find_oper(wpa_s) > 0)
  4047. return;
  4048. wpas_p2p_remove_pending_group_interface(wpa_s);
  4049. }
  4050. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  4051. {
  4052. struct wpa_supplicant *wpa_s = eloop_ctx;
  4053. wpa_s->p2p_long_listen = 0;
  4054. }
  4055. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4056. {
  4057. int res;
  4058. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4059. return -1;
  4060. wpa_supplicant_cancel_sched_scan(wpa_s);
  4061. wpas_p2p_clear_pending_action_tx(wpa_s);
  4062. if (timeout == 0) {
  4063. /*
  4064. * This is a request for unlimited Listen state. However, at
  4065. * least for now, this is mapped to a Listen state for one
  4066. * hour.
  4067. */
  4068. timeout = 3600;
  4069. }
  4070. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4071. wpa_s->p2p_long_listen = 0;
  4072. /*
  4073. * Stop previous find/listen operation to avoid trying to request a new
  4074. * remain-on-channel operation while the driver is still running the
  4075. * previous one.
  4076. */
  4077. if (wpa_s->global->p2p)
  4078. p2p_stop_find(wpa_s->global->p2p);
  4079. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  4080. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  4081. wpa_s->p2p_long_listen = timeout * 1000;
  4082. eloop_register_timeout(timeout, 0,
  4083. wpas_p2p_long_listen_timeout,
  4084. wpa_s, NULL);
  4085. }
  4086. return res;
  4087. }
  4088. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  4089. u8 *buf, size_t len, int p2p_group)
  4090. {
  4091. struct wpabuf *p2p_ie;
  4092. int ret;
  4093. if (wpa_s->global->p2p_disabled)
  4094. return -1;
  4095. if (wpa_s->global->p2p == NULL)
  4096. return -1;
  4097. if (bss == NULL)
  4098. return -1;
  4099. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  4100. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  4101. p2p_group, p2p_ie);
  4102. wpabuf_free(p2p_ie);
  4103. return ret;
  4104. }
  4105. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  4106. const u8 *dst, const u8 *bssid,
  4107. const u8 *ie, size_t ie_len, int ssi_signal)
  4108. {
  4109. if (wpa_s->global->p2p_disabled)
  4110. return 0;
  4111. if (wpa_s->global->p2p == NULL)
  4112. return 0;
  4113. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  4114. ie, ie_len)) {
  4115. case P2P_PREQ_NOT_P2P:
  4116. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  4117. ssi_signal);
  4118. /* fall through */
  4119. case P2P_PREQ_MALFORMED:
  4120. case P2P_PREQ_NOT_LISTEN:
  4121. case P2P_PREQ_NOT_PROCESSED:
  4122. default: /* make gcc happy */
  4123. return 0;
  4124. case P2P_PREQ_PROCESSED:
  4125. return 1;
  4126. }
  4127. }
  4128. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  4129. const u8 *sa, const u8 *bssid,
  4130. u8 category, const u8 *data, size_t len, int freq)
  4131. {
  4132. if (wpa_s->global->p2p_disabled)
  4133. return;
  4134. if (wpa_s->global->p2p == NULL)
  4135. return;
  4136. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  4137. freq);
  4138. }
  4139. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  4140. {
  4141. if (wpa_s->global->p2p_disabled)
  4142. return;
  4143. if (wpa_s->global->p2p == NULL)
  4144. return;
  4145. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  4146. }
  4147. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  4148. {
  4149. p2p_group_deinit(wpa_s->p2p_group);
  4150. wpa_s->p2p_group = NULL;
  4151. wpa_s->ap_configured_cb = NULL;
  4152. wpa_s->ap_configured_cb_ctx = NULL;
  4153. wpa_s->ap_configured_cb_data = NULL;
  4154. wpa_s->connect_without_scan = NULL;
  4155. }
  4156. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  4157. {
  4158. wpa_s->p2p_long_listen = 0;
  4159. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4160. return wpa_drv_p2p_reject(wpa_s, addr);
  4161. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4162. return -1;
  4163. return p2p_reject(wpa_s->global->p2p, addr);
  4164. }
  4165. /* Invite to reinvoke a persistent group */
  4166. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4167. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  4168. int ht40, int pref_freq)
  4169. {
  4170. enum p2p_invite_role role;
  4171. u8 *bssid = NULL, bssid_buf[ETH_ALEN];
  4172. int force_freq = 0, oper_freq = 0;
  4173. int res;
  4174. wpa_s->global->p2p_invite_group = NULL;
  4175. if (peer_addr)
  4176. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4177. else
  4178. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4179. wpa_s->p2p_persistent_go_freq = freq;
  4180. wpa_s->p2p_go_ht40 = !!ht40;
  4181. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4182. role = P2P_INVITE_ROLE_GO;
  4183. if (peer_addr == NULL) {
  4184. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  4185. "address in invitation command");
  4186. return -1;
  4187. }
  4188. if (wpas_p2p_create_iface(wpa_s)) {
  4189. if (wpas_p2p_add_group_interface(wpa_s,
  4190. WPA_IF_P2P_GO) < 0) {
  4191. wpa_printf(MSG_ERROR, "P2P: Failed to "
  4192. "allocate a new interface for the "
  4193. "group");
  4194. return -1;
  4195. }
  4196. bssid = wpa_s->pending_interface_addr;
  4197. } else
  4198. bssid = wpa_s->own_addr;
  4199. } else {
  4200. role = P2P_INVITE_ROLE_CLIENT;
  4201. peer_addr = ssid->bssid;
  4202. }
  4203. wpa_s->pending_invite_ssid_id = ssid->id;
  4204. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
  4205. wpa_s->assoc_freq) {
  4206. oper_freq = wpa_s->assoc_freq;
  4207. if (bssid == NULL)
  4208. bssid = bssid_buf;
  4209. } else {
  4210. oper_freq = wpa_drv_shared_freq(wpa_s);
  4211. if (oper_freq < 0)
  4212. oper_freq = 0;
  4213. }
  4214. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  4215. &oper_freq);
  4216. if (res)
  4217. return res;
  4218. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4219. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4220. ssid->ssid, ssid->ssid_len,
  4221. go_dev_addr, 1);
  4222. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4223. return -1;
  4224. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4225. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  4226. 1, pref_freq);
  4227. }
  4228. /* Invite to join an active group */
  4229. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  4230. const u8 *peer_addr, const u8 *go_dev_addr)
  4231. {
  4232. struct wpa_global *global = wpa_s->global;
  4233. enum p2p_invite_role role;
  4234. u8 *bssid = NULL, bssid_buf[ETH_ALEN];
  4235. struct wpa_ssid *ssid;
  4236. int persistent;
  4237. int force_freq = 0, oper_freq = 0, pref_freq = 0;
  4238. int res;
  4239. wpa_s->p2p_persistent_go_freq = 0;
  4240. wpa_s->p2p_go_ht40 = 0;
  4241. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4242. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4243. break;
  4244. }
  4245. if (wpa_s == NULL) {
  4246. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  4247. return -1;
  4248. }
  4249. ssid = wpa_s->current_ssid;
  4250. if (ssid == NULL) {
  4251. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  4252. "invitation");
  4253. return -1;
  4254. }
  4255. wpa_s->global->p2p_invite_group = wpa_s;
  4256. persistent = ssid->p2p_persistent_group &&
  4257. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  4258. ssid->ssid, ssid->ssid_len);
  4259. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4260. role = P2P_INVITE_ROLE_ACTIVE_GO;
  4261. bssid = wpa_s->own_addr;
  4262. if (go_dev_addr == NULL)
  4263. go_dev_addr = wpa_s->global->p2p_dev_addr;
  4264. } else {
  4265. role = P2P_INVITE_ROLE_CLIENT;
  4266. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  4267. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  4268. "invite to current group");
  4269. return -1;
  4270. }
  4271. bssid = wpa_s->bssid;
  4272. if (go_dev_addr == NULL &&
  4273. !is_zero_ether_addr(wpa_s->go_dev_addr))
  4274. go_dev_addr = wpa_s->go_dev_addr;
  4275. }
  4276. wpa_s->parent->pending_invite_ssid_id = -1;
  4277. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4278. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4279. ssid->ssid, ssid->ssid_len,
  4280. go_dev_addr, persistent);
  4281. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4282. return -1;
  4283. if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
  4284. wpa_s->assoc_freq) {
  4285. oper_freq = wpa_s->assoc_freq;
  4286. if (bssid == NULL)
  4287. bssid = bssid_buf;
  4288. } else {
  4289. oper_freq = wpa_drv_shared_freq(wpa_s);
  4290. if (oper_freq < 0)
  4291. oper_freq = 0;
  4292. }
  4293. res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq,
  4294. &oper_freq);
  4295. if (res)
  4296. return res;
  4297. wpas_p2p_set_own_freq_preference(wpa_s, oper_freq);
  4298. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4299. ssid->ssid, ssid->ssid_len, force_freq,
  4300. go_dev_addr, persistent, pref_freq);
  4301. }
  4302. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  4303. {
  4304. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4305. const char *ssid_txt;
  4306. u8 go_dev_addr[ETH_ALEN];
  4307. int network_id = -1;
  4308. int persistent;
  4309. int freq;
  4310. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  4311. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4312. wpa_s->parent, NULL);
  4313. }
  4314. if (!wpa_s->show_group_started || !ssid)
  4315. goto done;
  4316. wpa_s->show_group_started = 0;
  4317. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  4318. os_memset(go_dev_addr, 0, ETH_ALEN);
  4319. if (ssid->bssid_set)
  4320. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  4321. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4322. ssid->ssid_len);
  4323. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  4324. if (wpa_s->global->p2p_group_formation == wpa_s)
  4325. wpa_s->global->p2p_group_formation = NULL;
  4326. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  4327. (int) wpa_s->assoc_freq;
  4328. if (ssid->passphrase == NULL && ssid->psk_set) {
  4329. char psk[65];
  4330. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  4331. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4332. "%s client ssid=\"%s\" freq=%d psk=%s "
  4333. "go_dev_addr=" MACSTR "%s",
  4334. wpa_s->ifname, ssid_txt, freq, psk,
  4335. MAC2STR(go_dev_addr),
  4336. persistent ? " [PERSISTENT]" : "");
  4337. } else {
  4338. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4339. "%s client ssid=\"%s\" freq=%d "
  4340. "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
  4341. wpa_s->ifname, ssid_txt, freq,
  4342. ssid->passphrase ? ssid->passphrase : "",
  4343. MAC2STR(go_dev_addr),
  4344. persistent ? " [PERSISTENT]" : "");
  4345. }
  4346. if (persistent)
  4347. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  4348. ssid, go_dev_addr);
  4349. if (network_id < 0)
  4350. network_id = ssid->id;
  4351. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  4352. done:
  4353. wpas_p2p_continue_after_scan(wpa_s);
  4354. }
  4355. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  4356. u32 interval1, u32 duration2, u32 interval2)
  4357. {
  4358. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4359. return -1;
  4360. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4361. return -1;
  4362. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  4363. wpa_s->current_ssid == NULL ||
  4364. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  4365. return -1;
  4366. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  4367. wpa_s->own_addr, wpa_s->assoc_freq,
  4368. duration1, interval1, duration2, interval2);
  4369. }
  4370. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  4371. unsigned int interval)
  4372. {
  4373. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4374. return -1;
  4375. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4376. return -1;
  4377. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  4378. }
  4379. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  4380. {
  4381. if (wpa_s->current_ssid == NULL) {
  4382. /*
  4383. * current_ssid can be cleared when P2P client interface gets
  4384. * disconnected, so assume this interface was used as P2P
  4385. * client.
  4386. */
  4387. return 1;
  4388. }
  4389. return wpa_s->current_ssid->p2p_group &&
  4390. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  4391. }
  4392. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  4393. {
  4394. struct wpa_supplicant *wpa_s = eloop_ctx;
  4395. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  4396. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  4397. "disabled");
  4398. return;
  4399. }
  4400. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  4401. "group");
  4402. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  4403. }
  4404. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  4405. {
  4406. int timeout;
  4407. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4408. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4409. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4410. return;
  4411. timeout = wpa_s->conf->p2p_group_idle;
  4412. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  4413. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  4414. timeout = P2P_MAX_CLIENT_IDLE;
  4415. if (timeout == 0)
  4416. return;
  4417. if (timeout < 0) {
  4418. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  4419. timeout = 0; /* special client mode no-timeout */
  4420. else
  4421. return;
  4422. }
  4423. if (wpa_s->p2p_in_provisioning) {
  4424. /*
  4425. * Use the normal group formation timeout during the
  4426. * provisioning phase to avoid terminating this process too
  4427. * early due to group idle timeout.
  4428. */
  4429. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4430. "during provisioning");
  4431. return;
  4432. }
  4433. if (wpa_s->show_group_started) {
  4434. /*
  4435. * Use the normal group formation timeout between the end of
  4436. * the provisioning phase and completion of 4-way handshake to
  4437. * avoid terminating this process too early due to group idle
  4438. * timeout.
  4439. */
  4440. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4441. "while waiting for initial 4-way handshake to "
  4442. "complete");
  4443. return;
  4444. }
  4445. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  4446. timeout);
  4447. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  4448. wpa_s, NULL);
  4449. }
  4450. /* Returns 1 if the interface was removed */
  4451. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4452. u16 reason_code, const u8 *ie, size_t ie_len,
  4453. int locally_generated)
  4454. {
  4455. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4456. return 0;
  4457. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4458. return 0;
  4459. if (!locally_generated)
  4460. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4461. ie_len);
  4462. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  4463. wpa_s->current_ssid &&
  4464. wpa_s->current_ssid->p2p_group &&
  4465. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  4466. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  4467. "session is ending");
  4468. if (wpas_p2p_group_delete(wpa_s,
  4469. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  4470. > 0)
  4471. return 1;
  4472. }
  4473. return 0;
  4474. }
  4475. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4476. u16 reason_code, const u8 *ie, size_t ie_len,
  4477. int locally_generated)
  4478. {
  4479. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4480. return;
  4481. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4482. return;
  4483. if (!locally_generated)
  4484. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4485. ie_len);
  4486. }
  4487. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  4488. {
  4489. struct p2p_data *p2p = wpa_s->global->p2p;
  4490. if (p2p == NULL)
  4491. return;
  4492. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  4493. return;
  4494. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  4495. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  4496. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  4497. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  4498. if (wpa_s->wps &&
  4499. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  4500. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  4501. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  4502. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  4503. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  4504. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  4505. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  4506. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  4507. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  4508. }
  4509. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  4510. p2p_set_sec_dev_types(p2p,
  4511. (void *) wpa_s->conf->sec_device_type,
  4512. wpa_s->conf->num_sec_device_types);
  4513. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  4514. int i;
  4515. p2p_remove_wps_vendor_extensions(p2p);
  4516. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  4517. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  4518. continue;
  4519. p2p_add_wps_vendor_extension(
  4520. p2p, wpa_s->conf->wps_vendor_ext[i]);
  4521. }
  4522. }
  4523. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  4524. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  4525. char country[3];
  4526. country[0] = wpa_s->conf->country[0];
  4527. country[1] = wpa_s->conf->country[1];
  4528. country[2] = 0x04;
  4529. p2p_set_country(p2p, country);
  4530. }
  4531. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  4532. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  4533. wpa_s->conf->p2p_ssid_postfix ?
  4534. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  4535. 0);
  4536. }
  4537. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  4538. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  4539. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  4540. u8 reg_class, channel;
  4541. int ret;
  4542. unsigned int r;
  4543. if (wpa_s->conf->p2p_listen_reg_class &&
  4544. wpa_s->conf->p2p_listen_channel) {
  4545. reg_class = wpa_s->conf->p2p_listen_reg_class;
  4546. channel = wpa_s->conf->p2p_listen_channel;
  4547. } else {
  4548. reg_class = 81;
  4549. /*
  4550. * Pick one of the social channels randomly as the
  4551. * listen channel.
  4552. */
  4553. os_get_random((u8 *) &r, sizeof(r));
  4554. channel = 1 + (r % 3) * 5;
  4555. }
  4556. ret = p2p_set_listen_channel(p2p, reg_class, channel);
  4557. if (ret)
  4558. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  4559. "failed: %d", ret);
  4560. }
  4561. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  4562. u8 op_reg_class, op_channel, cfg_op_channel;
  4563. int ret = 0;
  4564. unsigned int r;
  4565. if (wpa_s->conf->p2p_oper_reg_class &&
  4566. wpa_s->conf->p2p_oper_channel) {
  4567. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  4568. op_channel = wpa_s->conf->p2p_oper_channel;
  4569. cfg_op_channel = 1;
  4570. } else {
  4571. op_reg_class = 81;
  4572. /*
  4573. * Use random operation channel from (1, 6, 11)
  4574. *if no other preference is indicated.
  4575. */
  4576. os_get_random((u8 *) &r, sizeof(r));
  4577. op_channel = 1 + (r % 3) * 5;
  4578. cfg_op_channel = 0;
  4579. }
  4580. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  4581. cfg_op_channel);
  4582. if (ret)
  4583. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  4584. "failed: %d", ret);
  4585. }
  4586. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  4587. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  4588. wpa_s->conf->p2p_pref_chan) < 0) {
  4589. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  4590. "update failed");
  4591. }
  4592. }
  4593. }
  4594. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  4595. int duration)
  4596. {
  4597. if (!wpa_s->ap_iface)
  4598. return -1;
  4599. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  4600. duration);
  4601. }
  4602. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  4603. {
  4604. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4605. return -1;
  4606. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4607. return -1;
  4608. wpa_s->global->cross_connection = enabled;
  4609. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  4610. if (!enabled) {
  4611. struct wpa_supplicant *iface;
  4612. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  4613. {
  4614. if (iface->cross_connect_enabled == 0)
  4615. continue;
  4616. iface->cross_connect_enabled = 0;
  4617. iface->cross_connect_in_use = 0;
  4618. wpa_msg_global(iface->parent, MSG_INFO,
  4619. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4620. iface->ifname,
  4621. iface->cross_connect_uplink);
  4622. }
  4623. }
  4624. return 0;
  4625. }
  4626. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  4627. {
  4628. struct wpa_supplicant *iface;
  4629. if (!uplink->global->cross_connection)
  4630. return;
  4631. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4632. if (!iface->cross_connect_enabled)
  4633. continue;
  4634. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4635. 0)
  4636. continue;
  4637. if (iface->ap_iface == NULL)
  4638. continue;
  4639. if (iface->cross_connect_in_use)
  4640. continue;
  4641. iface->cross_connect_in_use = 1;
  4642. wpa_msg_global(iface->parent, MSG_INFO,
  4643. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4644. iface->ifname, iface->cross_connect_uplink);
  4645. }
  4646. }
  4647. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  4648. {
  4649. struct wpa_supplicant *iface;
  4650. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4651. if (!iface->cross_connect_enabled)
  4652. continue;
  4653. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4654. 0)
  4655. continue;
  4656. if (!iface->cross_connect_in_use)
  4657. continue;
  4658. wpa_msg_global(iface->parent, MSG_INFO,
  4659. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4660. iface->ifname, iface->cross_connect_uplink);
  4661. iface->cross_connect_in_use = 0;
  4662. }
  4663. }
  4664. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  4665. {
  4666. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  4667. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  4668. wpa_s->cross_connect_disallowed)
  4669. wpas_p2p_disable_cross_connect(wpa_s);
  4670. else
  4671. wpas_p2p_enable_cross_connect(wpa_s);
  4672. if (!wpa_s->ap_iface &&
  4673. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4674. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4675. }
  4676. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  4677. {
  4678. wpas_p2p_disable_cross_connect(wpa_s);
  4679. if (!wpa_s->ap_iface &&
  4680. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  4681. wpa_s, NULL))
  4682. wpas_p2p_set_group_idle_timeout(wpa_s);
  4683. }
  4684. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  4685. {
  4686. struct wpa_supplicant *iface;
  4687. if (!wpa_s->global->cross_connection)
  4688. return;
  4689. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  4690. if (iface == wpa_s)
  4691. continue;
  4692. if (iface->drv_flags &
  4693. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  4694. continue;
  4695. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  4696. continue;
  4697. wpa_s->cross_connect_enabled = 1;
  4698. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  4699. sizeof(wpa_s->cross_connect_uplink));
  4700. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  4701. "%s to %s whenever uplink is available",
  4702. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4703. if (iface->ap_iface || iface->current_ssid == NULL ||
  4704. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  4705. iface->cross_connect_disallowed ||
  4706. iface->wpa_state != WPA_COMPLETED)
  4707. break;
  4708. wpa_s->cross_connect_in_use = 1;
  4709. wpa_msg_global(wpa_s->parent, MSG_INFO,
  4710. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4711. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4712. break;
  4713. }
  4714. }
  4715. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  4716. {
  4717. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  4718. !wpa_s->p2p_in_provisioning)
  4719. return 0; /* not P2P client operation */
  4720. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  4721. "session overlap");
  4722. if (wpa_s != wpa_s->parent)
  4723. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  4724. if (wpa_s->global->p2p)
  4725. p2p_group_formation_failed(wpa_s->global->p2p);
  4726. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4727. wpa_s->parent, NULL);
  4728. wpas_group_formation_completed(wpa_s, 0);
  4729. return 1;
  4730. }
  4731. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  4732. {
  4733. struct p2p_channels chan;
  4734. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  4735. return;
  4736. os_memset(&chan, 0, sizeof(chan));
  4737. if (wpas_p2p_setup_channels(wpa_s, &chan)) {
  4738. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  4739. "channel list");
  4740. return;
  4741. }
  4742. p2p_update_channel_list(wpa_s->global->p2p, &chan);
  4743. }
  4744. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  4745. struct wpa_scan_results *scan_res)
  4746. {
  4747. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  4748. }
  4749. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  4750. {
  4751. struct wpa_global *global = wpa_s->global;
  4752. int found = 0;
  4753. const u8 *peer;
  4754. if (global->p2p == NULL)
  4755. return -1;
  4756. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  4757. if (wpa_s->pending_interface_name[0] &&
  4758. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  4759. found = 1;
  4760. peer = p2p_get_go_neg_peer(global->p2p);
  4761. if (peer) {
  4762. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  4763. MACSTR, MAC2STR(peer));
  4764. p2p_unauthorize(global->p2p, peer);
  4765. found = 1;
  4766. }
  4767. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  4768. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  4769. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  4770. found = 1;
  4771. }
  4772. if (wpa_s->pending_pd_before_join) {
  4773. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  4774. wpa_s->pending_pd_before_join = 0;
  4775. found = 1;
  4776. }
  4777. wpas_p2p_stop_find(wpa_s);
  4778. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4779. if (wpa_s == global->p2p_group_formation &&
  4780. (wpa_s->p2p_in_provisioning ||
  4781. wpa_s->parent->pending_interface_type ==
  4782. WPA_IF_P2P_CLIENT)) {
  4783. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  4784. "formation found - cancelling",
  4785. wpa_s->ifname);
  4786. found = 1;
  4787. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4788. wpa_s->parent, NULL);
  4789. if (wpa_s->p2p_in_provisioning) {
  4790. wpas_group_formation_completed(wpa_s, 0);
  4791. break;
  4792. }
  4793. wpas_p2p_group_delete(wpa_s,
  4794. P2P_GROUP_REMOVAL_REQUESTED);
  4795. break;
  4796. }
  4797. }
  4798. if (!found) {
  4799. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  4800. return -1;
  4801. }
  4802. return 0;
  4803. }
  4804. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  4805. {
  4806. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4807. return;
  4808. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  4809. "being available anymore");
  4810. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  4811. }
  4812. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  4813. int freq_24, int freq_5, int freq_overall)
  4814. {
  4815. struct p2p_data *p2p = wpa_s->global->p2p;
  4816. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4817. return;
  4818. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  4819. }
  4820. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  4821. {
  4822. u8 peer[ETH_ALEN];
  4823. struct p2p_data *p2p = wpa_s->global->p2p;
  4824. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4825. return -1;
  4826. if (hwaddr_aton(addr, peer))
  4827. return -1;
  4828. return p2p_unauthorize(p2p, peer);
  4829. }
  4830. /**
  4831. * wpas_p2p_disconnect - Disconnect from a P2P Group
  4832. * @wpa_s: Pointer to wpa_supplicant data
  4833. * Returns: 0 on success, -1 on failure
  4834. *
  4835. * This can be used to disconnect from a group in which the local end is a P2P
  4836. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  4837. * virtual network interface was created for this group, that interface will be
  4838. * removed. Otherwise, only the configured P2P group network will be removed
  4839. * from the interface.
  4840. */
  4841. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  4842. {
  4843. if (wpa_s == NULL)
  4844. return -1;
  4845. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  4846. -1 : 0;
  4847. }
  4848. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  4849. {
  4850. int ret;
  4851. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4852. return 0;
  4853. ret = p2p_in_progress(wpa_s->global->p2p);
  4854. if (ret == 0) {
  4855. /*
  4856. * Check whether there is an ongoing WPS provisioning step (or
  4857. * other parts of group formation) on another interface since
  4858. * p2p_in_progress() does not report this to avoid issues for
  4859. * scans during such provisioning step.
  4860. */
  4861. if (wpa_s->global->p2p_group_formation &&
  4862. wpa_s->global->p2p_group_formation != wpa_s) {
  4863. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  4864. "in group formation",
  4865. wpa_s->global->p2p_group_formation->ifname);
  4866. ret = 1;
  4867. }
  4868. }
  4869. return ret;
  4870. }
  4871. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  4872. struct wpa_ssid *ssid)
  4873. {
  4874. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  4875. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4876. wpa_s->parent, NULL) > 0) {
  4877. /**
  4878. * Remove the network by scheduling the group formation
  4879. * timeout to happen immediately. The teardown code
  4880. * needs to be scheduled to run asynch later so that we
  4881. * don't delete data from under ourselves unexpectedly.
  4882. * Calling wpas_p2p_group_formation_timeout directly
  4883. * causes a series of crashes in WPS failure scenarios.
  4884. */
  4885. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  4886. "P2P group network getting removed");
  4887. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  4888. wpa_s->parent, NULL);
  4889. }
  4890. }
  4891. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  4892. const u8 *addr, const u8 *ssid,
  4893. size_t ssid_len)
  4894. {
  4895. struct wpa_ssid *s;
  4896. size_t i;
  4897. for (s = wpa_s->conf->ssid; s; s = s->next) {
  4898. if (s->disabled != 2)
  4899. continue;
  4900. if (ssid &&
  4901. (ssid_len != s->ssid_len ||
  4902. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  4903. continue;
  4904. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  4905. return s; /* peer is GO in the persistent group */
  4906. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  4907. continue;
  4908. for (i = 0; i < s->num_p2p_clients; i++) {
  4909. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
  4910. addr, ETH_ALEN) == 0)
  4911. return s; /* peer is P2P client in persistent
  4912. * group */
  4913. }
  4914. }
  4915. return NULL;
  4916. }
  4917. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  4918. const u8 *addr)
  4919. {
  4920. if (addr == NULL)
  4921. return;
  4922. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  4923. }
  4924. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  4925. int group_added)
  4926. {
  4927. struct wpa_supplicant *group = wpa_s;
  4928. if (wpa_s->global->p2p_group_formation)
  4929. group = wpa_s->global->p2p_group_formation;
  4930. wpa_s = wpa_s->parent;
  4931. offchannel_send_action_done(wpa_s);
  4932. if (group_added)
  4933. wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  4934. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  4935. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  4936. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  4937. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  4938. wpa_s->p2p_persistent_id,
  4939. wpa_s->p2p_pd_before_go_neg,
  4940. wpa_s->p2p_go_ht40);
  4941. }
  4942. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  4943. {
  4944. if (!wpa_s->p2p_fallback_to_go_neg ||
  4945. wpa_s->p2p_in_provisioning <= 5)
  4946. return 0;
  4947. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  4948. return 0; /* peer operating as a GO */
  4949. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  4950. "fallback to GO Negotiation");
  4951. wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  4952. return 1;
  4953. }
  4954. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  4955. {
  4956. const char *rn, *rn2;
  4957. struct wpa_supplicant *ifs;
  4958. if (wpa_s->wpa_state > WPA_SCANNING) {
  4959. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  4960. "concurrent operation",
  4961. P2P_CONCURRENT_SEARCH_DELAY);
  4962. return P2P_CONCURRENT_SEARCH_DELAY;
  4963. }
  4964. if (!wpa_s->driver->get_radio_name)
  4965. return 0;
  4966. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  4967. if (rn == NULL || rn[0] == '\0')
  4968. return 0;
  4969. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  4970. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  4971. continue;
  4972. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  4973. if (!rn2 || os_strcmp(rn, rn2) != 0)
  4974. continue;
  4975. if (ifs->wpa_state > WPA_SCANNING) {
  4976. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  4977. "delay due to concurrent operation on "
  4978. "interface %s",
  4979. P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
  4980. return P2P_CONCURRENT_SEARCH_DELAY;
  4981. }
  4982. }
  4983. return 0;
  4984. }
  4985. void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
  4986. {
  4987. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
  4988. "pending anymore (sta_scan_pending=%d "
  4989. "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
  4990. wpa_s->global->p2p_cb_on_scan_complete);
  4991. wpa_s->sta_scan_pending = 0;
  4992. if (!wpa_s->global->p2p_cb_on_scan_complete)
  4993. return;
  4994. wpa_s->global->p2p_cb_on_scan_complete = 0;
  4995. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4996. return;
  4997. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  4998. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  4999. "continued after successful connection");
  5000. p2p_increase_search_delay(wpa_s->global->p2p,
  5001. wpas_p2p_search_delay(wpa_s));
  5002. }
  5003. }