p2p_supplicant.c 160 KB

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