driver_nl80211.c 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298
  1. /*
  2. * Driver interaction with Linux nl80211/cfg80211
  3. * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2003-2004, Instant802 Networks, Inc.
  5. * Copyright (c) 2005-2006, Devicescape Software, Inc.
  6. * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright (c) 2009-2010, Atheros Communications
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Alternatively, this software may be distributed under the terms of BSD
  14. * license.
  15. *
  16. * See README and COPYING for more details.
  17. */
  18. #include "includes.h"
  19. #include <sys/ioctl.h>
  20. #include <net/if.h>
  21. #include <netlink/genl/genl.h>
  22. #include <netlink/genl/family.h>
  23. #include <netlink/genl/ctrl.h>
  24. #include <netpacket/packet.h>
  25. #include <linux/filter.h>
  26. #include "nl80211_copy.h"
  27. #include "common.h"
  28. #include "eloop.h"
  29. #include "common/ieee802_11_defs.h"
  30. #include "netlink.h"
  31. #include "linux_ioctl.h"
  32. #include "radiotap.h"
  33. #include "radiotap_iter.h"
  34. #include "driver.h"
  35. #ifdef CONFIG_LIBNL20
  36. /* libnl 2.0 compatibility code */
  37. #define nl_handle nl_sock
  38. #define nl_handle_alloc_cb nl_socket_alloc_cb
  39. #define nl_handle_destroy nl_socket_free
  40. #endif /* CONFIG_LIBNL20 */
  41. #ifndef IFF_LOWER_UP
  42. #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
  43. #endif
  44. #ifndef IFF_DORMANT
  45. #define IFF_DORMANT 0x20000 /* driver signals dormant */
  46. #endif
  47. #ifndef IF_OPER_DORMANT
  48. #define IF_OPER_DORMANT 5
  49. #endif
  50. #ifndef IF_OPER_UP
  51. #define IF_OPER_UP 6
  52. #endif
  53. struct i802_bss {
  54. struct wpa_driver_nl80211_data *drv;
  55. struct i802_bss *next;
  56. int ifindex;
  57. char ifname[IFNAMSIZ + 1];
  58. unsigned int beacon_set:1;
  59. };
  60. struct wpa_driver_nl80211_data {
  61. void *ctx;
  62. struct netlink_data *netlink;
  63. int ioctl_sock; /* socket for ioctl() use */
  64. char brname[IFNAMSIZ];
  65. int ifindex;
  66. int if_removed;
  67. struct wpa_driver_capa capa;
  68. int has_capability;
  69. int operstate;
  70. int scan_complete_events;
  71. struct nl_handle *nl_handle;
  72. struct nl_handle *nl_handle_event;
  73. struct nl_cache *nl_cache;
  74. struct nl_cache *nl_cache_event;
  75. struct nl_cb *nl_cb;
  76. struct genl_family *nl80211;
  77. u8 auth_bssid[ETH_ALEN];
  78. u8 bssid[ETH_ALEN];
  79. int associated;
  80. u8 ssid[32];
  81. size_t ssid_len;
  82. int nlmode;
  83. int ap_scan_as_station;
  84. unsigned int assoc_freq;
  85. int monitor_sock;
  86. int monitor_ifidx;
  87. int probe_req_report;
  88. int disable_11b_rates;
  89. unsigned int pending_remain_on_chan:1;
  90. unsigned int pending_send_action:1;
  91. unsigned int added_bridge:1;
  92. unsigned int added_if_into_bridge:1;
  93. u64 remain_on_chan_cookie;
  94. u64 send_action_cookie;
  95. struct wpa_driver_scan_filter *filter_ssids;
  96. size_t num_filter_ssids;
  97. struct i802_bss first_bss;
  98. #ifdef HOSTAPD
  99. int eapol_sock; /* socket for EAPOL frames */
  100. int default_if_indices[16];
  101. int *if_indices;
  102. int num_if_indices;
  103. int last_freq;
  104. int last_freq_ht;
  105. #endif /* HOSTAPD */
  106. };
  107. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
  108. void *timeout_ctx);
  109. static int wpa_driver_nl80211_set_mode(void *priv, int mode);
  110. static int
  111. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
  112. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  113. const u8 *addr, int cmd, u16 reason_code,
  114. int local_state_change);
  115. static void nl80211_remove_monitor_interface(
  116. struct wpa_driver_nl80211_data *drv);
  117. #ifdef HOSTAPD
  118. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  119. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  120. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
  121. static int wpa_driver_nl80211_if_remove(void *priv,
  122. enum wpa_driver_if_type type,
  123. const char *ifname);
  124. #endif /* HOSTAPD */
  125. static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx,
  126. void *timeout_ctx);
  127. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  128. int ifindex, int disabled);
  129. /* nl80211 code */
  130. static int ack_handler(struct nl_msg *msg, void *arg)
  131. {
  132. int *err = arg;
  133. *err = 0;
  134. return NL_STOP;
  135. }
  136. static int finish_handler(struct nl_msg *msg, void *arg)
  137. {
  138. int *ret = arg;
  139. *ret = 0;
  140. return NL_SKIP;
  141. }
  142. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  143. void *arg)
  144. {
  145. int *ret = arg;
  146. *ret = err->error;
  147. return NL_SKIP;
  148. }
  149. static int no_seq_check(struct nl_msg *msg, void *arg)
  150. {
  151. return NL_OK;
  152. }
  153. static int send_and_recv(struct wpa_driver_nl80211_data *drv,
  154. struct nl_handle *nl_handle, struct nl_msg *msg,
  155. int (*valid_handler)(struct nl_msg *, void *),
  156. void *valid_data)
  157. {
  158. struct nl_cb *cb;
  159. int err = -ENOMEM;
  160. cb = nl_cb_clone(drv->nl_cb);
  161. if (!cb)
  162. goto out;
  163. err = nl_send_auto_complete(nl_handle, msg);
  164. if (err < 0)
  165. goto out;
  166. err = 1;
  167. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  168. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  169. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  170. if (valid_handler)
  171. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  172. valid_handler, valid_data);
  173. while (err > 0)
  174. nl_recvmsgs(nl_handle, cb);
  175. out:
  176. nl_cb_put(cb);
  177. nlmsg_free(msg);
  178. return err;
  179. }
  180. static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
  181. struct nl_msg *msg,
  182. int (*valid_handler)(struct nl_msg *, void *),
  183. void *valid_data)
  184. {
  185. return send_and_recv(drv, drv->nl_handle, msg, valid_handler,
  186. valid_data);
  187. }
  188. struct family_data {
  189. const char *group;
  190. int id;
  191. };
  192. static int family_handler(struct nl_msg *msg, void *arg)
  193. {
  194. struct family_data *res = arg;
  195. struct nlattr *tb[CTRL_ATTR_MAX + 1];
  196. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  197. struct nlattr *mcgrp;
  198. int i;
  199. nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  200. genlmsg_attrlen(gnlh, 0), NULL);
  201. if (!tb[CTRL_ATTR_MCAST_GROUPS])
  202. return NL_SKIP;
  203. nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
  204. struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
  205. nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
  206. nla_len(mcgrp), NULL);
  207. if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
  208. !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
  209. os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
  210. res->group,
  211. nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
  212. continue;
  213. res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
  214. break;
  215. };
  216. return NL_SKIP;
  217. }
  218. static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv,
  219. const char *family, const char *group)
  220. {
  221. struct nl_msg *msg;
  222. int ret = -1;
  223. struct family_data res = { group, -ENOENT };
  224. msg = nlmsg_alloc();
  225. if (!msg)
  226. return -ENOMEM;
  227. genlmsg_put(msg, 0, 0, genl_ctrl_resolve(drv->nl_handle, "nlctrl"),
  228. 0, 0, CTRL_CMD_GETFAMILY, 0);
  229. NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
  230. ret = send_and_recv_msgs(drv, msg, family_handler, &res);
  231. msg = NULL;
  232. if (ret == 0)
  233. ret = res.id;
  234. nla_put_failure:
  235. nlmsg_free(msg);
  236. return ret;
  237. }
  238. static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
  239. {
  240. struct i802_bss *bss = priv;
  241. struct wpa_driver_nl80211_data *drv = bss->drv;
  242. if (!drv->associated)
  243. return -1;
  244. os_memcpy(bssid, drv->bssid, ETH_ALEN);
  245. return 0;
  246. }
  247. static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
  248. {
  249. struct i802_bss *bss = priv;
  250. struct wpa_driver_nl80211_data *drv = bss->drv;
  251. if (!drv->associated)
  252. return -1;
  253. os_memcpy(ssid, drv->ssid, drv->ssid_len);
  254. return drv->ssid_len;
  255. }
  256. static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
  257. char *buf, size_t len, int del)
  258. {
  259. union wpa_event_data event;
  260. os_memset(&event, 0, sizeof(event));
  261. if (len > sizeof(event.interface_status.ifname))
  262. len = sizeof(event.interface_status.ifname) - 1;
  263. os_memcpy(event.interface_status.ifname, buf, len);
  264. event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
  265. EVENT_INTERFACE_ADDED;
  266. wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
  267. del ? "DEL" : "NEW",
  268. event.interface_status.ifname,
  269. del ? "removed" : "added");
  270. if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
  271. if (del)
  272. drv->if_removed = 1;
  273. else
  274. drv->if_removed = 0;
  275. }
  276. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  277. }
  278. static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
  279. u8 *buf, size_t len)
  280. {
  281. int attrlen, rta_len;
  282. struct rtattr *attr;
  283. attrlen = len;
  284. attr = (struct rtattr *) buf;
  285. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  286. while (RTA_OK(attr, attrlen)) {
  287. if (attr->rta_type == IFLA_IFNAME) {
  288. if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
  289. == 0)
  290. return 1;
  291. else
  292. break;
  293. }
  294. attr = RTA_NEXT(attr, attrlen);
  295. }
  296. return 0;
  297. }
  298. static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
  299. int ifindex, u8 *buf, size_t len)
  300. {
  301. if (drv->ifindex == ifindex)
  302. return 1;
  303. if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
  304. drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
  305. wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
  306. "interface");
  307. wpa_driver_nl80211_finish_drv_init(drv);
  308. return 1;
  309. }
  310. return 0;
  311. }
  312. static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
  313. struct ifinfomsg *ifi,
  314. u8 *buf, size_t len)
  315. {
  316. struct wpa_driver_nl80211_data *drv = ctx;
  317. int attrlen, rta_len;
  318. struct rtattr *attr;
  319. if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, buf, len)) {
  320. wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d",
  321. ifi->ifi_index);
  322. return;
  323. }
  324. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
  325. "(%s%s%s%s)",
  326. drv->operstate, ifi->ifi_flags,
  327. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  328. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  329. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  330. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  331. /*
  332. * Some drivers send the association event before the operup event--in
  333. * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
  334. * fails. This will hit us when wpa_supplicant does not need to do
  335. * IEEE 802.1X authentication
  336. */
  337. if (drv->operstate == 1 &&
  338. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  339. !(ifi->ifi_flags & IFF_RUNNING))
  340. netlink_send_oper_ifla(drv->netlink, drv->ifindex,
  341. -1, IF_OPER_UP);
  342. attrlen = len;
  343. attr = (struct rtattr *) buf;
  344. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  345. while (RTA_OK(attr, attrlen)) {
  346. if (attr->rta_type == IFLA_IFNAME) {
  347. wpa_driver_nl80211_event_link(
  348. drv,
  349. ((char *) attr) + rta_len,
  350. attr->rta_len - rta_len, 0);
  351. }
  352. attr = RTA_NEXT(attr, attrlen);
  353. }
  354. }
  355. static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
  356. struct ifinfomsg *ifi,
  357. u8 *buf, size_t len)
  358. {
  359. struct wpa_driver_nl80211_data *drv = ctx;
  360. int attrlen, rta_len;
  361. struct rtattr *attr;
  362. attrlen = len;
  363. attr = (struct rtattr *) buf;
  364. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  365. while (RTA_OK(attr, attrlen)) {
  366. if (attr->rta_type == IFLA_IFNAME) {
  367. wpa_driver_nl80211_event_link(
  368. drv,
  369. ((char *) attr) + rta_len,
  370. attr->rta_len - rta_len, 1);
  371. }
  372. attr = RTA_NEXT(attr, attrlen);
  373. }
  374. }
  375. static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
  376. const u8 *frame, size_t len)
  377. {
  378. const struct ieee80211_mgmt *mgmt;
  379. union wpa_event_data event;
  380. mgmt = (const struct ieee80211_mgmt *) frame;
  381. if (len < 24 + sizeof(mgmt->u.auth)) {
  382. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  383. "frame");
  384. return;
  385. }
  386. os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
  387. os_memset(&event, 0, sizeof(event));
  388. os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
  389. event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
  390. event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
  391. if (len > 24 + sizeof(mgmt->u.auth)) {
  392. event.auth.ies = mgmt->u.auth.variable;
  393. event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
  394. }
  395. wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
  396. }
  397. static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
  398. const u8 *frame, size_t len)
  399. {
  400. const struct ieee80211_mgmt *mgmt;
  401. union wpa_event_data event;
  402. u16 status;
  403. mgmt = (const struct ieee80211_mgmt *) frame;
  404. if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
  405. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  406. "frame");
  407. return;
  408. }
  409. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  410. if (status != WLAN_STATUS_SUCCESS) {
  411. os_memset(&event, 0, sizeof(event));
  412. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  413. event.assoc_reject.resp_ies =
  414. (u8 *) mgmt->u.assoc_resp.variable;
  415. event.assoc_reject.resp_ies_len =
  416. len - 24 - sizeof(mgmt->u.assoc_resp);
  417. }
  418. event.assoc_reject.status_code = status;
  419. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  420. return;
  421. }
  422. drv->associated = 1;
  423. os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
  424. os_memset(&event, 0, sizeof(event));
  425. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  426. event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
  427. event.assoc_info.resp_ies_len =
  428. len - 24 - sizeof(mgmt->u.assoc_resp);
  429. }
  430. event.assoc_info.freq = drv->assoc_freq;
  431. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  432. }
  433. static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
  434. enum nl80211_commands cmd, struct nlattr *status,
  435. struct nlattr *addr, struct nlattr *req_ie,
  436. struct nlattr *resp_ie)
  437. {
  438. union wpa_event_data event;
  439. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  440. /*
  441. * Avoid reporting two association events that would confuse
  442. * the core code.
  443. */
  444. wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
  445. "when using userspace SME", cmd);
  446. return;
  447. }
  448. os_memset(&event, 0, sizeof(event));
  449. if (cmd == NL80211_CMD_CONNECT &&
  450. nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
  451. if (resp_ie) {
  452. event.assoc_reject.resp_ies = nla_data(resp_ie);
  453. event.assoc_reject.resp_ies_len = nla_len(resp_ie);
  454. }
  455. event.assoc_reject.status_code = nla_get_u16(status);
  456. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  457. return;
  458. }
  459. drv->associated = 1;
  460. if (addr)
  461. os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
  462. if (req_ie) {
  463. event.assoc_info.req_ies = nla_data(req_ie);
  464. event.assoc_info.req_ies_len = nla_len(req_ie);
  465. }
  466. if (resp_ie) {
  467. event.assoc_info.resp_ies = nla_data(resp_ie);
  468. event.assoc_info.resp_ies_len = nla_len(resp_ie);
  469. }
  470. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  471. }
  472. static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
  473. enum nl80211_commands cmd, struct nlattr *addr)
  474. {
  475. union wpa_event_data event;
  476. enum wpa_event_type ev;
  477. if (nla_len(addr) != ETH_ALEN)
  478. return;
  479. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
  480. cmd, MAC2STR((u8 *) nla_data(addr)));
  481. if (cmd == NL80211_CMD_AUTHENTICATE)
  482. ev = EVENT_AUTH_TIMED_OUT;
  483. else if (cmd == NL80211_CMD_ASSOCIATE)
  484. ev = EVENT_ASSOC_TIMED_OUT;
  485. else
  486. return;
  487. os_memset(&event, 0, sizeof(event));
  488. os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
  489. wpa_supplicant_event(drv->ctx, ev, &event);
  490. }
  491. static void mlme_event_action(struct wpa_driver_nl80211_data *drv,
  492. struct nlattr *freq, const u8 *frame, size_t len)
  493. {
  494. const struct ieee80211_mgmt *mgmt;
  495. union wpa_event_data event;
  496. u16 fc, stype;
  497. mgmt = (const struct ieee80211_mgmt *) frame;
  498. if (len < 24) {
  499. wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
  500. return;
  501. }
  502. fc = le_to_host16(mgmt->frame_control);
  503. stype = WLAN_FC_GET_STYPE(fc);
  504. os_memset(&event, 0, sizeof(event));
  505. event.rx_action.da = mgmt->da;
  506. event.rx_action.sa = mgmt->sa;
  507. event.rx_action.bssid = mgmt->bssid;
  508. event.rx_action.category = mgmt->u.action.category;
  509. event.rx_action.data = &mgmt->u.action.category + 1;
  510. event.rx_action.len = frame + len - event.rx_action.data;
  511. if (freq)
  512. event.rx_action.freq = nla_get_u32(freq);
  513. wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
  514. }
  515. static void mlme_event_action_tx_status(struct wpa_driver_nl80211_data *drv,
  516. struct nlattr *cookie, const u8 *frame,
  517. size_t len, struct nlattr *ack)
  518. {
  519. union wpa_event_data event;
  520. const struct ieee80211_hdr *hdr;
  521. u16 fc;
  522. u64 cookie_val;
  523. if (!cookie)
  524. return;
  525. cookie_val = nla_get_u64(cookie);
  526. wpa_printf(MSG_DEBUG, "nl80211: Action TX status: cookie=0%llx%s",
  527. (long long unsigned int) cookie_val,
  528. cookie_val == drv->send_action_cookie ?
  529. " (match)" : " (unknown)");
  530. if (cookie_val != drv->send_action_cookie)
  531. return;
  532. hdr = (const struct ieee80211_hdr *) frame;
  533. fc = le_to_host16(hdr->frame_control);
  534. os_memset(&event, 0, sizeof(event));
  535. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  536. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  537. event.tx_status.dst = hdr->addr1;
  538. event.tx_status.data = frame;
  539. event.tx_status.data_len = len;
  540. event.tx_status.ack = ack != NULL;
  541. wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
  542. }
  543. static void mlme_event(struct wpa_driver_nl80211_data *drv,
  544. enum nl80211_commands cmd, struct nlattr *frame,
  545. struct nlattr *addr, struct nlattr *timed_out,
  546. struct nlattr *freq, struct nlattr *ack,
  547. struct nlattr *cookie)
  548. {
  549. if (timed_out && addr) {
  550. mlme_timeout_event(drv, cmd, addr);
  551. return;
  552. }
  553. if (frame == NULL) {
  554. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
  555. "data", cmd);
  556. return;
  557. }
  558. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
  559. wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
  560. nla_data(frame), nla_len(frame));
  561. switch (cmd) {
  562. case NL80211_CMD_AUTHENTICATE:
  563. mlme_event_auth(drv, nla_data(frame), nla_len(frame));
  564. break;
  565. case NL80211_CMD_ASSOCIATE:
  566. mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
  567. break;
  568. case NL80211_CMD_DEAUTHENTICATE:
  569. drv->associated = 0;
  570. wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, NULL);
  571. break;
  572. case NL80211_CMD_DISASSOCIATE:
  573. drv->associated = 0;
  574. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
  575. break;
  576. case NL80211_CMD_ACTION:
  577. mlme_event_action(drv, freq, nla_data(frame), nla_len(frame));
  578. break;
  579. case NL80211_CMD_ACTION_TX_STATUS:
  580. mlme_event_action_tx_status(drv, cookie, nla_data(frame),
  581. nla_len(frame), ack);
  582. break;
  583. default:
  584. break;
  585. }
  586. }
  587. static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
  588. struct nlattr *tb[])
  589. {
  590. union wpa_event_data data;
  591. wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
  592. os_memset(&data, 0, sizeof(data));
  593. if (tb[NL80211_ATTR_MAC]) {
  594. wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
  595. nla_data(tb[NL80211_ATTR_MAC]),
  596. nla_len(tb[NL80211_ATTR_MAC]));
  597. data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
  598. }
  599. if (tb[NL80211_ATTR_KEY_SEQ]) {
  600. wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
  601. nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  602. nla_len(tb[NL80211_ATTR_KEY_SEQ]));
  603. }
  604. if (tb[NL80211_ATTR_KEY_TYPE]) {
  605. enum nl80211_key_type key_type =
  606. nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
  607. wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
  608. if (key_type == NL80211_KEYTYPE_PAIRWISE)
  609. data.michael_mic_failure.unicast = 1;
  610. } else
  611. data.michael_mic_failure.unicast = 1;
  612. if (tb[NL80211_ATTR_KEY_IDX]) {
  613. u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
  614. wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
  615. }
  616. wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  617. }
  618. static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
  619. struct nlattr *tb[])
  620. {
  621. if (tb[NL80211_ATTR_MAC] == NULL) {
  622. wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
  623. "event");
  624. return;
  625. }
  626. os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  627. drv->associated = 1;
  628. wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
  629. MAC2STR(drv->bssid));
  630. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
  631. }
  632. static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
  633. int cancel_event, struct nlattr *tb[])
  634. {
  635. unsigned int freq, chan_type, duration;
  636. union wpa_event_data data;
  637. u64 cookie;
  638. if (tb[NL80211_ATTR_WIPHY_FREQ])
  639. freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
  640. else
  641. freq = 0;
  642. if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
  643. chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
  644. else
  645. chan_type = 0;
  646. if (tb[NL80211_ATTR_DURATION])
  647. duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
  648. else
  649. duration = 0;
  650. if (tb[NL80211_ATTR_COOKIE])
  651. cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  652. else
  653. cookie = 0;
  654. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
  655. "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
  656. cancel_event, freq, chan_type, duration,
  657. (long long unsigned int) cookie,
  658. cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
  659. if (cookie != drv->remain_on_chan_cookie)
  660. return; /* not for us */
  661. drv->pending_remain_on_chan = !cancel_event;
  662. os_memset(&data, 0, sizeof(data));
  663. data.remain_on_channel.freq = freq;
  664. data.remain_on_channel.duration = duration;
  665. wpa_supplicant_event(drv->ctx, cancel_event ?
  666. EVENT_CANCEL_REMAIN_ON_CHANNEL :
  667. EVENT_REMAIN_ON_CHANNEL, &data);
  668. }
  669. static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
  670. struct nlattr *tb[])
  671. {
  672. union wpa_event_data event;
  673. struct nlattr *nl;
  674. int rem;
  675. struct scan_info *info;
  676. #define MAX_REPORT_FREQS 50
  677. int freqs[MAX_REPORT_FREQS];
  678. int num_freqs = 0;
  679. os_memset(&event, 0, sizeof(event));
  680. info = &event.scan_info;
  681. info->aborted = aborted;
  682. if (tb[NL80211_ATTR_SCAN_SSIDS]) {
  683. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
  684. struct wpa_driver_scan_ssid *s =
  685. &info->ssids[info->num_ssids];
  686. s->ssid = nla_data(nl);
  687. s->ssid_len = nla_len(nl);
  688. info->num_ssids++;
  689. if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
  690. break;
  691. }
  692. }
  693. if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
  694. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
  695. {
  696. freqs[num_freqs] = nla_get_u32(nl);
  697. num_freqs++;
  698. if (num_freqs == MAX_REPORT_FREQS - 1)
  699. break;
  700. }
  701. info->freqs = freqs;
  702. info->num_freqs = num_freqs;
  703. }
  704. wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
  705. }
  706. static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
  707. struct nlattr *tb[])
  708. {
  709. static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
  710. [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
  711. [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
  712. [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
  713. };
  714. struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
  715. enum nl80211_cqm_rssi_threshold_event event;
  716. union wpa_event_data ed;
  717. if (tb[NL80211_ATTR_CQM] == NULL ||
  718. nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
  719. cqm_policy)) {
  720. wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
  721. return;
  722. }
  723. if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
  724. return;
  725. event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
  726. os_memset(&ed, 0, sizeof(ed));
  727. if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
  728. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  729. "event: RSSI high");
  730. ed.signal_change.above_threshold = 1;
  731. } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
  732. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  733. "event: RSSI low");
  734. ed.signal_change.above_threshold = 0;
  735. } else
  736. return;
  737. wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
  738. }
  739. static int process_event(struct nl_msg *msg, void *arg)
  740. {
  741. struct wpa_driver_nl80211_data *drv = arg;
  742. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  743. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  744. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  745. genlmsg_attrlen(gnlh, 0), NULL);
  746. if (tb[NL80211_ATTR_IFINDEX]) {
  747. int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  748. if (ifindex != drv->ifindex) {
  749. wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
  750. " for foreign interface (ifindex %d)",
  751. gnlh->cmd, ifindex);
  752. return NL_SKIP;
  753. }
  754. }
  755. if (drv->ap_scan_as_station &&
  756. (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
  757. gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
  758. wpa_driver_nl80211_set_mode(&drv->first_bss,
  759. IEEE80211_MODE_AP);
  760. drv->ap_scan_as_station = 0;
  761. }
  762. switch (gnlh->cmd) {
  763. case NL80211_CMD_TRIGGER_SCAN:
  764. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
  765. break;
  766. case NL80211_CMD_NEW_SCAN_RESULTS:
  767. wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
  768. drv->scan_complete_events = 1;
  769. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  770. drv->ctx);
  771. send_scan_event(drv, 0, tb);
  772. break;
  773. case NL80211_CMD_SCAN_ABORTED:
  774. wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
  775. /*
  776. * Need to indicate that scan results are available in order
  777. * not to make wpa_supplicant stop its scanning.
  778. */
  779. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  780. drv->ctx);
  781. send_scan_event(drv, 1, tb);
  782. break;
  783. case NL80211_CMD_AUTHENTICATE:
  784. case NL80211_CMD_ASSOCIATE:
  785. case NL80211_CMD_DEAUTHENTICATE:
  786. case NL80211_CMD_DISASSOCIATE:
  787. case NL80211_CMD_ACTION:
  788. case NL80211_CMD_ACTION_TX_STATUS:
  789. mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
  790. tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
  791. tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
  792. tb[NL80211_ATTR_COOKIE]);
  793. break;
  794. case NL80211_CMD_CONNECT:
  795. case NL80211_CMD_ROAM:
  796. mlme_event_connect(drv, gnlh->cmd,
  797. tb[NL80211_ATTR_STATUS_CODE],
  798. tb[NL80211_ATTR_MAC],
  799. tb[NL80211_ATTR_REQ_IE],
  800. tb[NL80211_ATTR_RESP_IE]);
  801. break;
  802. case NL80211_CMD_DISCONNECT:
  803. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  804. /*
  805. * Avoid reporting two disassociation events that could
  806. * confuse the core code.
  807. */
  808. wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
  809. "event when using userspace SME");
  810. break;
  811. }
  812. drv->associated = 0;
  813. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
  814. break;
  815. case NL80211_CMD_MICHAEL_MIC_FAILURE:
  816. mlme_event_michael_mic_failure(drv, tb);
  817. break;
  818. case NL80211_CMD_JOIN_IBSS:
  819. mlme_event_join_ibss(drv, tb);
  820. break;
  821. case NL80211_CMD_REMAIN_ON_CHANNEL:
  822. mlme_event_remain_on_channel(drv, 0, tb);
  823. break;
  824. case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
  825. mlme_event_remain_on_channel(drv, 1, tb);
  826. break;
  827. case NL80211_CMD_NOTIFY_CQM:
  828. nl80211_cqm_event(drv, tb);
  829. break;
  830. default:
  831. wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
  832. "(cmd=%d)", gnlh->cmd);
  833. break;
  834. }
  835. return NL_SKIP;
  836. }
  837. static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
  838. void *sock_ctx)
  839. {
  840. struct nl_cb *cb;
  841. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  842. wpa_printf(MSG_DEBUG, "nl80211: Event message available");
  843. cb = nl_cb_clone(drv->nl_cb);
  844. if (!cb)
  845. return;
  846. nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
  847. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv);
  848. nl_recvmsgs(drv->nl_handle_event, cb);
  849. nl_cb_put(cb);
  850. }
  851. /**
  852. * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
  853. * @priv: driver_nl80211 private data
  854. * @alpha2_arg: country to which to switch to
  855. * Returns: 0 on success, -1 on failure
  856. *
  857. * This asks nl80211 to set the regulatory domain for given
  858. * country ISO / IEC alpha2.
  859. */
  860. static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
  861. {
  862. struct i802_bss *bss = priv;
  863. struct wpa_driver_nl80211_data *drv = bss->drv;
  864. char alpha2[3];
  865. struct nl_msg *msg;
  866. msg = nlmsg_alloc();
  867. if (!msg)
  868. return -ENOMEM;
  869. alpha2[0] = alpha2_arg[0];
  870. alpha2[1] = alpha2_arg[1];
  871. alpha2[2] = '\0';
  872. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  873. 0, NL80211_CMD_REQ_SET_REG, 0);
  874. NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
  875. if (send_and_recv_msgs(drv, msg, NULL, NULL))
  876. return -EINVAL;
  877. return 0;
  878. nla_put_failure:
  879. return -EINVAL;
  880. }
  881. #ifndef HOSTAPD
  882. struct wiphy_info_data {
  883. int max_scan_ssids;
  884. int ap_supported;
  885. int auth_supported;
  886. int connect_supported;
  887. };
  888. static int wiphy_info_handler(struct nl_msg *msg, void *arg)
  889. {
  890. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  891. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  892. struct wiphy_info_data *info = arg;
  893. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  894. genlmsg_attrlen(gnlh, 0), NULL);
  895. if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
  896. info->max_scan_ssids =
  897. nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
  898. if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
  899. struct nlattr *nl_mode;
  900. int i;
  901. nla_for_each_nested(nl_mode,
  902. tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
  903. if (nl_mode->nla_type == NL80211_IFTYPE_AP) {
  904. info->ap_supported = 1;
  905. break;
  906. }
  907. }
  908. }
  909. if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
  910. struct nlattr *nl_cmd;
  911. int i;
  912. nla_for_each_nested(nl_cmd,
  913. tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
  914. u32 cmd = nla_get_u32(nl_cmd);
  915. if (cmd == NL80211_CMD_AUTHENTICATE)
  916. info->auth_supported = 1;
  917. else if (cmd == NL80211_CMD_CONNECT)
  918. info->connect_supported = 1;
  919. }
  920. }
  921. return NL_SKIP;
  922. }
  923. static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
  924. struct wiphy_info_data *info)
  925. {
  926. struct nl_msg *msg;
  927. os_memset(info, 0, sizeof(*info));
  928. msg = nlmsg_alloc();
  929. if (!msg)
  930. return -1;
  931. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  932. 0, NL80211_CMD_GET_WIPHY, 0);
  933. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
  934. if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
  935. return 0;
  936. msg = NULL;
  937. nla_put_failure:
  938. nlmsg_free(msg);
  939. return -1;
  940. }
  941. static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
  942. {
  943. struct wiphy_info_data info;
  944. if (wpa_driver_nl80211_get_info(drv, &info))
  945. return -1;
  946. drv->has_capability = 1;
  947. /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
  948. drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  949. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  950. WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  951. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
  952. drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
  953. WPA_DRIVER_CAPA_ENC_WEP104 |
  954. WPA_DRIVER_CAPA_ENC_TKIP |
  955. WPA_DRIVER_CAPA_ENC_CCMP;
  956. drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
  957. WPA_DRIVER_AUTH_SHARED |
  958. WPA_DRIVER_AUTH_LEAP;
  959. drv->capa.max_scan_ssids = info.max_scan_ssids;
  960. if (info.ap_supported)
  961. drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
  962. if (info.auth_supported)
  963. drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
  964. else if (!info.connect_supported) {
  965. wpa_printf(MSG_INFO, "nl80211: Driver does not support "
  966. "authentication/association or connect commands");
  967. return -1;
  968. }
  969. drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
  970. drv->capa.max_remain_on_chan = 5000;
  971. return 0;
  972. }
  973. #endif /* HOSTAPD */
  974. static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv,
  975. void *ctx)
  976. {
  977. int ret;
  978. /* Initialize generic netlink and nl80211 */
  979. drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  980. if (drv->nl_cb == NULL) {
  981. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  982. "callbacks");
  983. goto err1;
  984. }
  985. drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
  986. if (drv->nl_handle == NULL) {
  987. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  988. "callbacks");
  989. goto err2;
  990. }
  991. drv->nl_handle_event = nl_handle_alloc_cb(drv->nl_cb);
  992. if (drv->nl_handle_event == NULL) {
  993. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  994. "callbacks (event)");
  995. goto err2b;
  996. }
  997. if (genl_connect(drv->nl_handle)) {
  998. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  999. "netlink");
  1000. goto err3;
  1001. }
  1002. if (genl_connect(drv->nl_handle_event)) {
  1003. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  1004. "netlink (event)");
  1005. goto err3;
  1006. }
  1007. #ifdef CONFIG_LIBNL20
  1008. if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
  1009. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1010. "netlink cache");
  1011. goto err3;
  1012. }
  1013. if (genl_ctrl_alloc_cache(drv->nl_handle_event, &drv->nl_cache_event) <
  1014. 0) {
  1015. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1016. "netlink cache (event)");
  1017. goto err3b;
  1018. }
  1019. #else /* CONFIG_LIBNL20 */
  1020. drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
  1021. if (drv->nl_cache == NULL) {
  1022. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1023. "netlink cache");
  1024. goto err3;
  1025. }
  1026. drv->nl_cache_event = genl_ctrl_alloc_cache(drv->nl_handle_event);
  1027. if (drv->nl_cache_event == NULL) {
  1028. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1029. "netlink cache (event)");
  1030. goto err3b;
  1031. }
  1032. #endif /* CONFIG_LIBNL20 */
  1033. drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
  1034. if (drv->nl80211 == NULL) {
  1035. wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
  1036. "found");
  1037. goto err4;
  1038. }
  1039. ret = nl_get_multicast_id(drv, "nl80211", "scan");
  1040. if (ret >= 0)
  1041. ret = nl_socket_add_membership(drv->nl_handle_event, ret);
  1042. if (ret < 0) {
  1043. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  1044. "membership for scan events: %d (%s)",
  1045. ret, strerror(-ret));
  1046. goto err4;
  1047. }
  1048. ret = nl_get_multicast_id(drv, "nl80211", "mlme");
  1049. if (ret >= 0)
  1050. ret = nl_socket_add_membership(drv->nl_handle_event, ret);
  1051. if (ret < 0) {
  1052. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  1053. "membership for mlme events: %d (%s)",
  1054. ret, strerror(-ret));
  1055. goto err4;
  1056. }
  1057. eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_event),
  1058. wpa_driver_nl80211_event_receive, drv, ctx);
  1059. return 0;
  1060. err4:
  1061. nl_cache_free(drv->nl_cache_event);
  1062. err3b:
  1063. nl_cache_free(drv->nl_cache);
  1064. err3:
  1065. nl_handle_destroy(drv->nl_handle_event);
  1066. err2b:
  1067. nl_handle_destroy(drv->nl_handle);
  1068. err2:
  1069. nl_cb_put(drv->nl_cb);
  1070. err1:
  1071. return -1;
  1072. }
  1073. /**
  1074. * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  1075. * @ctx: context to be used when calling wpa_supplicant functions,
  1076. * e.g., wpa_supplicant_event()
  1077. * @ifname: interface name, e.g., wlan0
  1078. * Returns: Pointer to private data, %NULL on failure
  1079. */
  1080. static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
  1081. {
  1082. struct wpa_driver_nl80211_data *drv;
  1083. struct netlink_config *cfg;
  1084. struct i802_bss *bss;
  1085. drv = os_zalloc(sizeof(*drv));
  1086. if (drv == NULL)
  1087. return NULL;
  1088. drv->ctx = ctx;
  1089. bss = &drv->first_bss;
  1090. bss->drv = drv;
  1091. os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
  1092. drv->monitor_ifidx = -1;
  1093. drv->monitor_sock = -1;
  1094. drv->ioctl_sock = -1;
  1095. if (wpa_driver_nl80211_init_nl(drv, ctx)) {
  1096. os_free(drv);
  1097. return NULL;
  1098. }
  1099. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  1100. if (drv->ioctl_sock < 0) {
  1101. perror("socket(PF_INET,SOCK_DGRAM)");
  1102. goto failed;
  1103. }
  1104. cfg = os_zalloc(sizeof(*cfg));
  1105. if (cfg == NULL)
  1106. goto failed;
  1107. cfg->ctx = drv;
  1108. cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
  1109. cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
  1110. drv->netlink = netlink_init(cfg);
  1111. if (drv->netlink == NULL) {
  1112. os_free(cfg);
  1113. goto failed;
  1114. }
  1115. if (wpa_driver_nl80211_finish_drv_init(drv))
  1116. goto failed;
  1117. return bss;
  1118. failed:
  1119. netlink_deinit(drv->netlink);
  1120. if (drv->ioctl_sock >= 0)
  1121. close(drv->ioctl_sock);
  1122. genl_family_put(drv->nl80211);
  1123. nl_cache_free(drv->nl_cache);
  1124. nl_handle_destroy(drv->nl_handle);
  1125. nl_cb_put(drv->nl_cb);
  1126. eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
  1127. os_free(drv);
  1128. return NULL;
  1129. }
  1130. static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv,
  1131. const u8 *match, size_t match_len)
  1132. {
  1133. struct nl_msg *msg;
  1134. int ret = -1;
  1135. msg = nlmsg_alloc();
  1136. if (!msg)
  1137. return -1;
  1138. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1139. NL80211_CMD_REGISTER_ACTION, 0);
  1140. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1141. NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
  1142. ret = send_and_recv(drv, drv->nl_handle_event, msg, NULL, NULL);
  1143. msg = NULL;
  1144. if (ret) {
  1145. wpa_printf(MSG_DEBUG, "nl80211: Register Action command "
  1146. "failed: ret=%d (%s)", ret, strerror(-ret));
  1147. wpa_hexdump(MSG_DEBUG, "nl80211: Register Action match",
  1148. match, match_len);
  1149. goto nla_put_failure;
  1150. }
  1151. ret = 0;
  1152. nla_put_failure:
  1153. nlmsg_free(msg);
  1154. return ret;
  1155. }
  1156. static int nl80211_register_action_frames(struct wpa_driver_nl80211_data *drv)
  1157. {
  1158. /* FT Action frames */
  1159. if (nl80211_register_action_frame(drv, (u8 *) "\x06", 1) < 0)
  1160. return -1;
  1161. else
  1162. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
  1163. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
  1164. return 0;
  1165. }
  1166. static int
  1167. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
  1168. {
  1169. struct i802_bss *bss = &drv->first_bss;
  1170. drv->ifindex = if_nametoindex(bss->ifname);
  1171. drv->first_bss.ifindex = drv->ifindex;
  1172. #ifndef HOSTAPD
  1173. if (wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_INFRA) < 0) {
  1174. wpa_printf(MSG_DEBUG, "nl80211: Could not configure driver to "
  1175. "use managed mode");
  1176. }
  1177. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) {
  1178. wpa_printf(MSG_ERROR, "Could not set interface '%s' UP",
  1179. bss->ifname);
  1180. return -1;
  1181. }
  1182. if (wpa_driver_nl80211_capa(drv))
  1183. return -1;
  1184. netlink_send_oper_ifla(drv->netlink, drv->ifindex,
  1185. 1, IF_OPER_DORMANT);
  1186. #endif /* HOSTAPD */
  1187. if (nl80211_register_action_frames(drv) < 0) {
  1188. wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
  1189. "frame processing - ignore for now");
  1190. /*
  1191. * Older kernel versions did not support this, so ignore the
  1192. * error for now. Some functionality may not be available
  1193. * because of this.
  1194. */
  1195. }
  1196. return 0;
  1197. }
  1198. static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
  1199. {
  1200. struct nl_msg *msg;
  1201. msg = nlmsg_alloc();
  1202. if (!msg)
  1203. return -ENOMEM;
  1204. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1205. 0, NL80211_CMD_DEL_BEACON, 0);
  1206. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1207. return send_and_recv_msgs(drv, msg, NULL, NULL);
  1208. nla_put_failure:
  1209. return -ENOBUFS;
  1210. }
  1211. /**
  1212. * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  1213. * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
  1214. *
  1215. * Shut down driver interface and processing of driver events. Free
  1216. * private data buffer if one was allocated in wpa_driver_nl80211_init().
  1217. */
  1218. static void wpa_driver_nl80211_deinit(void *priv)
  1219. {
  1220. struct i802_bss *bss = priv;
  1221. struct wpa_driver_nl80211_data *drv = bss->drv;
  1222. if (drv->added_if_into_bridge) {
  1223. if (linux_br_del_if(drv->ioctl_sock, drv->brname, bss->ifname)
  1224. < 0)
  1225. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  1226. "interface %s from bridge %s: %s",
  1227. bss->ifname, drv->brname, strerror(errno));
  1228. }
  1229. if (drv->added_bridge) {
  1230. if (linux_br_del(drv->ioctl_sock, drv->brname) < 0)
  1231. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  1232. "bridge %s: %s",
  1233. drv->brname, strerror(errno));
  1234. }
  1235. nl80211_remove_monitor_interface(drv);
  1236. if (drv->nlmode == NL80211_IFTYPE_AP)
  1237. wpa_driver_nl80211_del_beacon(drv);
  1238. #ifdef HOSTAPD
  1239. if (drv->last_freq_ht) {
  1240. /* Clear HT flags from the driver */
  1241. struct hostapd_freq_params freq;
  1242. os_memset(&freq, 0, sizeof(freq));
  1243. freq.freq = drv->last_freq;
  1244. i802_set_freq(priv, &freq);
  1245. }
  1246. if (drv->eapol_sock >= 0) {
  1247. eloop_unregister_read_sock(drv->eapol_sock);
  1248. close(drv->eapol_sock);
  1249. }
  1250. if (drv->if_indices != drv->default_if_indices)
  1251. os_free(drv->if_indices);
  1252. #endif /* HOSTAPD */
  1253. if (drv->disable_11b_rates)
  1254. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  1255. netlink_send_oper_ifla(drv->netlink, drv->ifindex, 0, IF_OPER_UP);
  1256. netlink_deinit(drv->netlink);
  1257. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  1258. (void) linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0);
  1259. wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_INFRA);
  1260. if (drv->ioctl_sock >= 0)
  1261. close(drv->ioctl_sock);
  1262. eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
  1263. genl_family_put(drv->nl80211);
  1264. nl_cache_free(drv->nl_cache);
  1265. nl_cache_free(drv->nl_cache_event);
  1266. nl_handle_destroy(drv->nl_handle);
  1267. nl_handle_destroy(drv->nl_handle_event);
  1268. nl_cb_put(drv->nl_cb);
  1269. eloop_cancel_timeout(wpa_driver_nl80211_probe_req_report_timeout,
  1270. drv, NULL);
  1271. os_free(drv->filter_ssids);
  1272. os_free(drv);
  1273. }
  1274. /**
  1275. * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
  1276. * @eloop_ctx: Driver private data
  1277. * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
  1278. *
  1279. * This function can be used as registered timeout when starting a scan to
  1280. * generate a scan completed event if the driver does not report this.
  1281. */
  1282. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  1283. {
  1284. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  1285. if (drv->ap_scan_as_station) {
  1286. wpa_driver_nl80211_set_mode(&drv->first_bss,
  1287. IEEE80211_MODE_AP);
  1288. drv->ap_scan_as_station = 0;
  1289. }
  1290. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  1291. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  1292. }
  1293. /**
  1294. * wpa_driver_nl80211_scan - Request the driver to initiate scan
  1295. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  1296. * @params: Scan parameters
  1297. * Returns: 0 on success, -1 on failure
  1298. */
  1299. static int wpa_driver_nl80211_scan(void *priv,
  1300. struct wpa_driver_scan_params *params)
  1301. {
  1302. struct i802_bss *bss = priv;
  1303. struct wpa_driver_nl80211_data *drv = bss->drv;
  1304. int ret = 0, timeout;
  1305. struct nl_msg *msg, *ssids, *freqs;
  1306. size_t i;
  1307. msg = nlmsg_alloc();
  1308. ssids = nlmsg_alloc();
  1309. freqs = nlmsg_alloc();
  1310. if (!msg || !ssids || !freqs) {
  1311. nlmsg_free(msg);
  1312. nlmsg_free(ssids);
  1313. nlmsg_free(freqs);
  1314. return -1;
  1315. }
  1316. os_free(drv->filter_ssids);
  1317. drv->filter_ssids = params->filter_ssids;
  1318. params->filter_ssids = NULL;
  1319. drv->num_filter_ssids = params->num_filter_ssids;
  1320. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1321. NL80211_CMD_TRIGGER_SCAN, 0);
  1322. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1323. for (i = 0; i < params->num_ssids; i++) {
  1324. wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
  1325. params->ssids[i].ssid,
  1326. params->ssids[i].ssid_len);
  1327. NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
  1328. params->ssids[i].ssid);
  1329. }
  1330. if (params->num_ssids)
  1331. nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
  1332. if (params->extra_ies) {
  1333. wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan extra IEs",
  1334. params->extra_ies, params->extra_ies_len);
  1335. NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
  1336. params->extra_ies);
  1337. }
  1338. if (params->freqs) {
  1339. for (i = 0; params->freqs[i]; i++) {
  1340. wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
  1341. "MHz", params->freqs[i]);
  1342. NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
  1343. }
  1344. nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
  1345. }
  1346. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1347. msg = NULL;
  1348. if (ret) {
  1349. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
  1350. "(%s)", ret, strerror(-ret));
  1351. #ifdef HOSTAPD
  1352. if (drv->nlmode == NL80211_IFTYPE_AP) {
  1353. /*
  1354. * mac80211 does not allow scan requests in AP mode, so
  1355. * try to do this in station mode.
  1356. */
  1357. if (wpa_driver_nl80211_set_mode(bss,
  1358. IEEE80211_MODE_INFRA))
  1359. goto nla_put_failure;
  1360. if (wpa_driver_nl80211_scan(drv, params)) {
  1361. wpa_driver_nl80211_set_mode(bss,
  1362. IEEE80211_MODE_AP);
  1363. goto nla_put_failure;
  1364. }
  1365. /* Restore AP mode when processing scan results */
  1366. drv->ap_scan_as_station = 1;
  1367. ret = 0;
  1368. } else
  1369. goto nla_put_failure;
  1370. #else /* HOSTAPD */
  1371. goto nla_put_failure;
  1372. #endif /* HOSTAPD */
  1373. }
  1374. /* Not all drivers generate "scan completed" wireless event, so try to
  1375. * read results after a timeout. */
  1376. timeout = 10;
  1377. if (drv->scan_complete_events) {
  1378. /*
  1379. * The driver seems to deliver events to notify when scan is
  1380. * complete, so use longer timeout to avoid race conditions
  1381. * with scanning and following association request.
  1382. */
  1383. timeout = 30;
  1384. }
  1385. wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
  1386. "seconds", ret, timeout);
  1387. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  1388. eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
  1389. drv, drv->ctx);
  1390. nla_put_failure:
  1391. nlmsg_free(ssids);
  1392. nlmsg_free(msg);
  1393. nlmsg_free(freqs);
  1394. return ret;
  1395. }
  1396. static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
  1397. {
  1398. const u8 *end, *pos;
  1399. if (ies == NULL)
  1400. return NULL;
  1401. pos = ies;
  1402. end = ies + ies_len;
  1403. while (pos + 1 < end) {
  1404. if (pos + 2 + pos[1] > end)
  1405. break;
  1406. if (pos[0] == ie)
  1407. return pos;
  1408. pos += 2 + pos[1];
  1409. }
  1410. return NULL;
  1411. }
  1412. static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
  1413. const u8 *ie, size_t ie_len)
  1414. {
  1415. const u8 *ssid;
  1416. size_t i;
  1417. if (drv->filter_ssids == NULL)
  1418. return 0;
  1419. ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
  1420. if (ssid == NULL)
  1421. return 1;
  1422. for (i = 0; i < drv->num_filter_ssids; i++) {
  1423. if (ssid[1] == drv->filter_ssids[i].ssid_len &&
  1424. os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
  1425. 0)
  1426. return 0;
  1427. }
  1428. return 1;
  1429. }
  1430. struct nl80211_bss_info_arg {
  1431. struct wpa_driver_nl80211_data *drv;
  1432. struct wpa_scan_results *res;
  1433. };
  1434. static int bss_info_handler(struct nl_msg *msg, void *arg)
  1435. {
  1436. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1437. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1438. struct nlattr *bss[NL80211_BSS_MAX + 1];
  1439. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  1440. [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
  1441. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  1442. [NL80211_BSS_TSF] = { .type = NLA_U64 },
  1443. [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
  1444. [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
  1445. [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
  1446. [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
  1447. [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
  1448. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  1449. [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
  1450. [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
  1451. };
  1452. struct nl80211_bss_info_arg *_arg = arg;
  1453. struct wpa_scan_results *res = _arg->res;
  1454. struct wpa_scan_res **tmp;
  1455. struct wpa_scan_res *r;
  1456. const u8 *ie, *beacon_ie;
  1457. size_t ie_len, beacon_ie_len;
  1458. u8 *pos;
  1459. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1460. genlmsg_attrlen(gnlh, 0), NULL);
  1461. if (!tb[NL80211_ATTR_BSS])
  1462. return NL_SKIP;
  1463. if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  1464. bss_policy))
  1465. return NL_SKIP;
  1466. if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
  1467. ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1468. ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  1469. } else {
  1470. ie = NULL;
  1471. ie_len = 0;
  1472. }
  1473. if (bss[NL80211_BSS_BEACON_IES]) {
  1474. beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
  1475. beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
  1476. } else {
  1477. beacon_ie = NULL;
  1478. beacon_ie_len = 0;
  1479. }
  1480. if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
  1481. ie ? ie_len : beacon_ie_len))
  1482. return NL_SKIP;
  1483. r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
  1484. if (r == NULL)
  1485. return NL_SKIP;
  1486. if (bss[NL80211_BSS_BSSID])
  1487. os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
  1488. ETH_ALEN);
  1489. if (bss[NL80211_BSS_FREQUENCY])
  1490. r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  1491. if (bss[NL80211_BSS_BEACON_INTERVAL])
  1492. r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
  1493. if (bss[NL80211_BSS_CAPABILITY])
  1494. r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
  1495. r->flags |= WPA_SCAN_NOISE_INVALID;
  1496. if (bss[NL80211_BSS_SIGNAL_MBM]) {
  1497. r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
  1498. r->level /= 100; /* mBm to dBm */
  1499. r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
  1500. } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
  1501. r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
  1502. r->flags |= WPA_SCAN_LEVEL_INVALID;
  1503. } else
  1504. r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
  1505. if (bss[NL80211_BSS_TSF])
  1506. r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
  1507. if (bss[NL80211_BSS_SEEN_MS_AGO])
  1508. r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
  1509. r->ie_len = ie_len;
  1510. pos = (u8 *) (r + 1);
  1511. if (ie) {
  1512. os_memcpy(pos, ie, ie_len);
  1513. pos += ie_len;
  1514. }
  1515. r->beacon_ie_len = beacon_ie_len;
  1516. if (beacon_ie)
  1517. os_memcpy(pos, beacon_ie, beacon_ie_len);
  1518. if (bss[NL80211_BSS_STATUS]) {
  1519. enum nl80211_bss_status status;
  1520. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  1521. switch (status) {
  1522. case NL80211_BSS_STATUS_AUTHENTICATED:
  1523. r->flags |= WPA_SCAN_AUTHENTICATED;
  1524. break;
  1525. case NL80211_BSS_STATUS_ASSOCIATED:
  1526. r->flags |= WPA_SCAN_ASSOCIATED;
  1527. break;
  1528. default:
  1529. break;
  1530. }
  1531. }
  1532. tmp = os_realloc(res->res,
  1533. (res->num + 1) * sizeof(struct wpa_scan_res *));
  1534. if (tmp == NULL) {
  1535. os_free(r);
  1536. return NL_SKIP;
  1537. }
  1538. tmp[res->num++] = r;
  1539. res->res = tmp;
  1540. return NL_SKIP;
  1541. }
  1542. static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
  1543. const u8 *addr)
  1544. {
  1545. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  1546. wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
  1547. "mismatch (" MACSTR ")", MAC2STR(addr));
  1548. wpa_driver_nl80211_mlme(drv, addr,
  1549. NL80211_CMD_DEAUTHENTICATE,
  1550. WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
  1551. }
  1552. }
  1553. static void wpa_driver_nl80211_check_bss_status(
  1554. struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
  1555. {
  1556. size_t i;
  1557. for (i = 0; i < res->num; i++) {
  1558. struct wpa_scan_res *r = res->res[i];
  1559. if (r->flags & WPA_SCAN_AUTHENTICATED) {
  1560. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  1561. "indicates BSS status with " MACSTR
  1562. " as authenticated",
  1563. MAC2STR(r->bssid));
  1564. if (drv->nlmode == NL80211_IFTYPE_STATION &&
  1565. os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
  1566. os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
  1567. 0) {
  1568. wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
  1569. " in local state (auth=" MACSTR
  1570. " assoc=" MACSTR ")",
  1571. MAC2STR(drv->auth_bssid),
  1572. MAC2STR(drv->bssid));
  1573. clear_state_mismatch(drv, r->bssid);
  1574. }
  1575. }
  1576. if (r->flags & WPA_SCAN_ASSOCIATED) {
  1577. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  1578. "indicate BSS status with " MACSTR
  1579. " as associated",
  1580. MAC2STR(r->bssid));
  1581. if (drv->nlmode == NL80211_IFTYPE_STATION &&
  1582. !drv->associated) {
  1583. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  1584. "(not associated) does not match "
  1585. "with BSS state");
  1586. clear_state_mismatch(drv, r->bssid);
  1587. } else if (drv->nlmode == NL80211_IFTYPE_STATION &&
  1588. os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
  1589. 0) {
  1590. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  1591. "(associated with " MACSTR ") does "
  1592. "not match with BSS state",
  1593. MAC2STR(drv->bssid));
  1594. clear_state_mismatch(drv, r->bssid);
  1595. clear_state_mismatch(drv, drv->bssid);
  1596. }
  1597. }
  1598. }
  1599. }
  1600. static void wpa_scan_results_free(struct wpa_scan_results *res)
  1601. {
  1602. size_t i;
  1603. if (res == NULL)
  1604. return;
  1605. for (i = 0; i < res->num; i++)
  1606. os_free(res->res[i]);
  1607. os_free(res->res);
  1608. os_free(res);
  1609. }
  1610. static struct wpa_scan_results *
  1611. nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
  1612. {
  1613. struct nl_msg *msg;
  1614. struct wpa_scan_results *res;
  1615. int ret;
  1616. struct nl80211_bss_info_arg arg;
  1617. res = os_zalloc(sizeof(*res));
  1618. if (res == NULL)
  1619. return NULL;
  1620. msg = nlmsg_alloc();
  1621. if (!msg)
  1622. goto nla_put_failure;
  1623. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP,
  1624. NL80211_CMD_GET_SCAN, 0);
  1625. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1626. arg.drv = drv;
  1627. arg.res = res;
  1628. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  1629. msg = NULL;
  1630. if (ret == 0) {
  1631. wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)",
  1632. (unsigned long) res->num);
  1633. return res;
  1634. }
  1635. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  1636. "(%s)", ret, strerror(-ret));
  1637. nla_put_failure:
  1638. nlmsg_free(msg);
  1639. wpa_scan_results_free(res);
  1640. return NULL;
  1641. }
  1642. /**
  1643. * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
  1644. * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
  1645. * Returns: Scan results on success, -1 on failure
  1646. */
  1647. static struct wpa_scan_results *
  1648. wpa_driver_nl80211_get_scan_results(void *priv)
  1649. {
  1650. struct i802_bss *bss = priv;
  1651. struct wpa_driver_nl80211_data *drv = bss->drv;
  1652. struct wpa_scan_results *res;
  1653. res = nl80211_get_scan_results(drv);
  1654. if (res)
  1655. wpa_driver_nl80211_check_bss_status(drv, res);
  1656. return res;
  1657. }
  1658. static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
  1659. {
  1660. struct wpa_scan_results *res;
  1661. size_t i;
  1662. res = nl80211_get_scan_results(drv);
  1663. if (res == NULL) {
  1664. wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
  1665. return;
  1666. }
  1667. wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
  1668. for (i = 0; i < res->num; i++) {
  1669. struct wpa_scan_res *r = res->res[i];
  1670. wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
  1671. (int) i, (int) res->num, MAC2STR(r->bssid),
  1672. r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
  1673. r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
  1674. }
  1675. wpa_scan_results_free(res);
  1676. }
  1677. static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
  1678. enum wpa_alg alg, const u8 *addr,
  1679. int key_idx, int set_tx,
  1680. const u8 *seq, size_t seq_len,
  1681. const u8 *key, size_t key_len)
  1682. {
  1683. struct i802_bss *bss = priv;
  1684. struct wpa_driver_nl80211_data *drv = bss->drv;
  1685. int ifindex = if_nametoindex(ifname);
  1686. struct nl_msg *msg;
  1687. int ret;
  1688. wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
  1689. "set_tx=%d seq_len=%lu key_len=%lu",
  1690. __func__, ifindex, alg, addr, key_idx, set_tx,
  1691. (unsigned long) seq_len, (unsigned long) key_len);
  1692. msg = nlmsg_alloc();
  1693. if (!msg)
  1694. return -ENOMEM;
  1695. if (alg == WPA_ALG_NONE) {
  1696. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1697. 0, NL80211_CMD_DEL_KEY, 0);
  1698. } else {
  1699. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1700. 0, NL80211_CMD_NEW_KEY, 0);
  1701. NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
  1702. switch (alg) {
  1703. case WPA_ALG_WEP:
  1704. if (key_len == 5)
  1705. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1706. WLAN_CIPHER_SUITE_WEP40);
  1707. else
  1708. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1709. WLAN_CIPHER_SUITE_WEP104);
  1710. break;
  1711. case WPA_ALG_TKIP:
  1712. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1713. WLAN_CIPHER_SUITE_TKIP);
  1714. break;
  1715. case WPA_ALG_CCMP:
  1716. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1717. WLAN_CIPHER_SUITE_CCMP);
  1718. break;
  1719. case WPA_ALG_IGTK:
  1720. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  1721. WLAN_CIPHER_SUITE_AES_CMAC);
  1722. break;
  1723. default:
  1724. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  1725. "algorithm %d", __func__, alg);
  1726. nlmsg_free(msg);
  1727. return -1;
  1728. }
  1729. }
  1730. if (seq && seq_len)
  1731. NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
  1732. if (addr && os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  1733. {
  1734. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  1735. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  1736. }
  1737. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  1738. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  1739. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1740. if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
  1741. ret = 0;
  1742. if (ret)
  1743. wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
  1744. ret, strerror(-ret));
  1745. /*
  1746. * If we failed or don't need to set the default TX key (below),
  1747. * we're done here.
  1748. */
  1749. if (ret || !set_tx || alg == WPA_ALG_NONE)
  1750. return ret;
  1751. #ifdef HOSTAPD
  1752. if (addr)
  1753. return ret;
  1754. #else /* HOSTAPD */
  1755. if (drv->nlmode == NL80211_IFTYPE_AP && addr)
  1756. return ret;
  1757. #endif /* HOSTAPD */
  1758. msg = nlmsg_alloc();
  1759. if (!msg)
  1760. return -ENOMEM;
  1761. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1762. 0, NL80211_CMD_SET_KEY, 0);
  1763. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  1764. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  1765. if (alg == WPA_ALG_IGTK)
  1766. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
  1767. else
  1768. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
  1769. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1770. if (ret == -ENOENT)
  1771. ret = 0;
  1772. if (ret)
  1773. wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
  1774. "err=%d %s)", ret, strerror(-ret));
  1775. return ret;
  1776. nla_put_failure:
  1777. return -ENOBUFS;
  1778. }
  1779. static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
  1780. int key_idx, int defkey,
  1781. const u8 *seq, size_t seq_len,
  1782. const u8 *key, size_t key_len)
  1783. {
  1784. struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
  1785. if (!key_attr)
  1786. return -1;
  1787. if (defkey && alg == WPA_ALG_IGTK)
  1788. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
  1789. else if (defkey)
  1790. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  1791. NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
  1792. switch (alg) {
  1793. case WPA_ALG_WEP:
  1794. if (key_len == 5)
  1795. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  1796. WLAN_CIPHER_SUITE_WEP40);
  1797. else
  1798. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  1799. WLAN_CIPHER_SUITE_WEP104);
  1800. break;
  1801. case WPA_ALG_TKIP:
  1802. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
  1803. break;
  1804. case WPA_ALG_CCMP:
  1805. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
  1806. break;
  1807. case WPA_ALG_IGTK:
  1808. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  1809. WLAN_CIPHER_SUITE_AES_CMAC);
  1810. break;
  1811. default:
  1812. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  1813. "algorithm %d", __func__, alg);
  1814. return -1;
  1815. }
  1816. if (seq && seq_len)
  1817. NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
  1818. NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
  1819. nla_nest_end(msg, key_attr);
  1820. return 0;
  1821. nla_put_failure:
  1822. return -1;
  1823. }
  1824. static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
  1825. struct nl_msg *msg)
  1826. {
  1827. int i, privacy = 0;
  1828. struct nlattr *nl_keys, *nl_key;
  1829. for (i = 0; i < 4; i++) {
  1830. if (!params->wep_key[i])
  1831. continue;
  1832. privacy = 1;
  1833. break;
  1834. }
  1835. if (!privacy)
  1836. return 0;
  1837. NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
  1838. nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
  1839. if (!nl_keys)
  1840. goto nla_put_failure;
  1841. for (i = 0; i < 4; i++) {
  1842. if (!params->wep_key[i])
  1843. continue;
  1844. nl_key = nla_nest_start(msg, i);
  1845. if (!nl_key)
  1846. goto nla_put_failure;
  1847. NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
  1848. params->wep_key[i]);
  1849. if (params->wep_key_len[i] == 5)
  1850. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  1851. WLAN_CIPHER_SUITE_WEP40);
  1852. else
  1853. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  1854. WLAN_CIPHER_SUITE_WEP104);
  1855. NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
  1856. if (i == params->wep_tx_keyidx)
  1857. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  1858. nla_nest_end(msg, nl_key);
  1859. }
  1860. nla_nest_end(msg, nl_keys);
  1861. return 0;
  1862. nla_put_failure:
  1863. return -ENOBUFS;
  1864. }
  1865. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  1866. const u8 *addr, int cmd, u16 reason_code,
  1867. int local_state_change)
  1868. {
  1869. int ret = -1;
  1870. struct nl_msg *msg;
  1871. msg = nlmsg_alloc();
  1872. if (!msg)
  1873. return -1;
  1874. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, cmd, 0);
  1875. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1876. NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
  1877. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  1878. if (local_state_change)
  1879. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  1880. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1881. msg = NULL;
  1882. if (ret) {
  1883. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  1884. "(%s)", ret, strerror(-ret));
  1885. goto nla_put_failure;
  1886. }
  1887. ret = 0;
  1888. nla_put_failure:
  1889. nlmsg_free(msg);
  1890. return ret;
  1891. }
  1892. static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
  1893. const u8 *addr, int reason_code)
  1894. {
  1895. wpa_printf(MSG_DEBUG, "%s", __func__);
  1896. drv->associated = 0;
  1897. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
  1898. reason_code, 0);
  1899. }
  1900. static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
  1901. int reason_code)
  1902. {
  1903. struct i802_bss *bss = priv;
  1904. struct wpa_driver_nl80211_data *drv = bss->drv;
  1905. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  1906. return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
  1907. wpa_printf(MSG_DEBUG, "%s", __func__);
  1908. drv->associated = 0;
  1909. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
  1910. reason_code, 0);
  1911. }
  1912. static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
  1913. int reason_code)
  1914. {
  1915. struct i802_bss *bss = priv;
  1916. struct wpa_driver_nl80211_data *drv = bss->drv;
  1917. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  1918. return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
  1919. wpa_printf(MSG_DEBUG, "%s", __func__);
  1920. drv->associated = 0;
  1921. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
  1922. reason_code, 0);
  1923. }
  1924. static int wpa_driver_nl80211_authenticate(
  1925. void *priv, struct wpa_driver_auth_params *params)
  1926. {
  1927. struct i802_bss *bss = priv;
  1928. struct wpa_driver_nl80211_data *drv = bss->drv;
  1929. int ret = -1, i;
  1930. struct nl_msg *msg;
  1931. enum nl80211_auth_type type;
  1932. int count = 0;
  1933. drv->associated = 0;
  1934. os_memset(drv->auth_bssid, 0, ETH_ALEN);
  1935. /* FIX: IBSS mode */
  1936. if (drv->nlmode != NL80211_IFTYPE_STATION)
  1937. wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA);
  1938. if (wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA) < 0)
  1939. return -1;
  1940. retry:
  1941. msg = nlmsg_alloc();
  1942. if (!msg)
  1943. return -1;
  1944. wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
  1945. drv->ifindex);
  1946. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1947. NL80211_CMD_AUTHENTICATE, 0);
  1948. for (i = 0; i < 4; i++) {
  1949. if (!params->wep_key[i])
  1950. continue;
  1951. wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
  1952. NULL, i,
  1953. i == params->wep_tx_keyidx, NULL, 0,
  1954. params->wep_key[i],
  1955. params->wep_key_len[i]);
  1956. if (params->wep_tx_keyidx != i)
  1957. continue;
  1958. if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
  1959. params->wep_key[i], params->wep_key_len[i])) {
  1960. nlmsg_free(msg);
  1961. return -1;
  1962. }
  1963. }
  1964. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1965. if (params->bssid) {
  1966. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  1967. MAC2STR(params->bssid));
  1968. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  1969. }
  1970. if (params->freq) {
  1971. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  1972. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  1973. }
  1974. if (params->ssid) {
  1975. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  1976. params->ssid, params->ssid_len);
  1977. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  1978. params->ssid);
  1979. }
  1980. wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
  1981. if (params->ie)
  1982. NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
  1983. /*
  1984. * TODO: if multiple auth_alg options enabled, try them one by one if
  1985. * the AP rejects authentication due to unknown auth alg
  1986. */
  1987. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  1988. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  1989. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  1990. type = NL80211_AUTHTYPE_SHARED_KEY;
  1991. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  1992. type = NL80211_AUTHTYPE_NETWORK_EAP;
  1993. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  1994. type = NL80211_AUTHTYPE_FT;
  1995. else
  1996. goto nla_put_failure;
  1997. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  1998. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  1999. if (params->local_state_change) {
  2000. wpa_printf(MSG_DEBUG, " * Local state change only");
  2001. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  2002. }
  2003. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2004. msg = NULL;
  2005. if (ret) {
  2006. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  2007. "(%s)", ret, strerror(-ret));
  2008. count++;
  2009. if (ret == -EALREADY && count == 1 && params->bssid &&
  2010. !params->local_state_change) {
  2011. /*
  2012. * mac80211 does not currently accept new
  2013. * authentication if we are already authenticated. As a
  2014. * workaround, force deauthentication and try again.
  2015. */
  2016. wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
  2017. "after forced deauthentication");
  2018. wpa_driver_nl80211_deauthenticate(
  2019. bss, params->bssid,
  2020. WLAN_REASON_PREV_AUTH_NOT_VALID);
  2021. nlmsg_free(msg);
  2022. goto retry;
  2023. }
  2024. goto nla_put_failure;
  2025. }
  2026. ret = 0;
  2027. wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
  2028. "successfully");
  2029. nla_put_failure:
  2030. nlmsg_free(msg);
  2031. return ret;
  2032. }
  2033. struct phy_info_arg {
  2034. u16 *num_modes;
  2035. struct hostapd_hw_modes *modes;
  2036. };
  2037. static int phy_info_handler(struct nl_msg *msg, void *arg)
  2038. {
  2039. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  2040. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2041. struct phy_info_arg *phy_info = arg;
  2042. struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
  2043. struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
  2044. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  2045. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  2046. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  2047. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  2048. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  2049. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  2050. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  2051. };
  2052. struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
  2053. static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
  2054. [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
  2055. [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
  2056. };
  2057. struct nlattr *nl_band;
  2058. struct nlattr *nl_freq;
  2059. struct nlattr *nl_rate;
  2060. int rem_band, rem_freq, rem_rate;
  2061. struct hostapd_hw_modes *mode;
  2062. int idx, mode_is_set;
  2063. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2064. genlmsg_attrlen(gnlh, 0), NULL);
  2065. if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
  2066. return NL_SKIP;
  2067. nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
  2068. mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
  2069. if (!mode)
  2070. return NL_SKIP;
  2071. phy_info->modes = mode;
  2072. mode_is_set = 0;
  2073. mode = &phy_info->modes[*(phy_info->num_modes)];
  2074. memset(mode, 0, sizeof(*mode));
  2075. *(phy_info->num_modes) += 1;
  2076. nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
  2077. nla_len(nl_band), NULL);
  2078. if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
  2079. mode->ht_capab = nla_get_u16(
  2080. tb_band[NL80211_BAND_ATTR_HT_CAPA]);
  2081. }
  2082. if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
  2083. mode->a_mpdu_params |= nla_get_u8(
  2084. tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
  2085. 0x03;
  2086. }
  2087. if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
  2088. mode->a_mpdu_params |= nla_get_u8(
  2089. tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
  2090. 2;
  2091. }
  2092. if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
  2093. nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
  2094. u8 *mcs;
  2095. mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
  2096. os_memcpy(mode->mcs_set, mcs, 16);
  2097. }
  2098. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  2099. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  2100. nla_len(nl_freq), freq_policy);
  2101. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  2102. continue;
  2103. mode->num_channels++;
  2104. }
  2105. mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
  2106. if (!mode->channels)
  2107. return NL_SKIP;
  2108. idx = 0;
  2109. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  2110. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  2111. nla_len(nl_freq), freq_policy);
  2112. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  2113. continue;
  2114. mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
  2115. mode->channels[idx].flag = 0;
  2116. if (!mode_is_set) {
  2117. /* crude heuristic */
  2118. if (mode->channels[idx].freq < 4000)
  2119. mode->mode = HOSTAPD_MODE_IEEE80211B;
  2120. else
  2121. mode->mode = HOSTAPD_MODE_IEEE80211A;
  2122. mode_is_set = 1;
  2123. }
  2124. /* crude heuristic */
  2125. if (mode->channels[idx].freq < 4000)
  2126. if (mode->channels[idx].freq == 2484)
  2127. mode->channels[idx].chan = 14;
  2128. else
  2129. mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
  2130. else
  2131. mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
  2132. if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  2133. mode->channels[idx].flag |=
  2134. HOSTAPD_CHAN_DISABLED;
  2135. if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
  2136. mode->channels[idx].flag |=
  2137. HOSTAPD_CHAN_PASSIVE_SCAN;
  2138. if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
  2139. mode->channels[idx].flag |=
  2140. HOSTAPD_CHAN_NO_IBSS;
  2141. if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
  2142. mode->channels[idx].flag |=
  2143. HOSTAPD_CHAN_RADAR;
  2144. if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
  2145. !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  2146. mode->channels[idx].max_tx_power =
  2147. nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
  2148. idx++;
  2149. }
  2150. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  2151. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  2152. nla_len(nl_rate), rate_policy);
  2153. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  2154. continue;
  2155. mode->num_rates++;
  2156. }
  2157. mode->rates = os_zalloc(mode->num_rates * sizeof(int));
  2158. if (!mode->rates)
  2159. return NL_SKIP;
  2160. idx = 0;
  2161. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  2162. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  2163. nla_len(nl_rate), rate_policy);
  2164. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  2165. continue;
  2166. mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
  2167. /* crude heuristic */
  2168. if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
  2169. mode->rates[idx] > 200)
  2170. mode->mode = HOSTAPD_MODE_IEEE80211G;
  2171. idx++;
  2172. }
  2173. }
  2174. return NL_SKIP;
  2175. }
  2176. static struct hostapd_hw_modes *
  2177. wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
  2178. {
  2179. u16 m;
  2180. struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
  2181. int i, mode11g_idx = -1;
  2182. /* If only 802.11g mode is included, use it to construct matching
  2183. * 802.11b mode data. */
  2184. for (m = 0; m < *num_modes; m++) {
  2185. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
  2186. return modes; /* 802.11b already included */
  2187. if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  2188. mode11g_idx = m;
  2189. }
  2190. if (mode11g_idx < 0)
  2191. return modes; /* 2.4 GHz band not supported at all */
  2192. nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
  2193. if (nmodes == NULL)
  2194. return modes; /* Could not add 802.11b mode */
  2195. mode = &nmodes[*num_modes];
  2196. os_memset(mode, 0, sizeof(*mode));
  2197. (*num_modes)++;
  2198. modes = nmodes;
  2199. mode->mode = HOSTAPD_MODE_IEEE80211B;
  2200. mode11g = &modes[mode11g_idx];
  2201. mode->num_channels = mode11g->num_channels;
  2202. mode->channels = os_malloc(mode11g->num_channels *
  2203. sizeof(struct hostapd_channel_data));
  2204. if (mode->channels == NULL) {
  2205. (*num_modes)--;
  2206. return modes; /* Could not add 802.11b mode */
  2207. }
  2208. os_memcpy(mode->channels, mode11g->channels,
  2209. mode11g->num_channels * sizeof(struct hostapd_channel_data));
  2210. mode->num_rates = 0;
  2211. mode->rates = os_malloc(4 * sizeof(int));
  2212. if (mode->rates == NULL) {
  2213. os_free(mode->channels);
  2214. (*num_modes)--;
  2215. return modes; /* Could not add 802.11b mode */
  2216. }
  2217. for (i = 0; i < mode11g->num_rates; i++) {
  2218. if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
  2219. mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
  2220. continue;
  2221. mode->rates[mode->num_rates] = mode11g->rates[i];
  2222. mode->num_rates++;
  2223. if (mode->num_rates == 4)
  2224. break;
  2225. }
  2226. if (mode->num_rates == 0) {
  2227. os_free(mode->channels);
  2228. os_free(mode->rates);
  2229. (*num_modes)--;
  2230. return modes; /* No 802.11b rates */
  2231. }
  2232. wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
  2233. "information");
  2234. return modes;
  2235. }
  2236. static struct hostapd_hw_modes *
  2237. wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  2238. {
  2239. struct i802_bss *bss = priv;
  2240. struct wpa_driver_nl80211_data *drv = bss->drv;
  2241. struct nl_msg *msg;
  2242. struct phy_info_arg result = {
  2243. .num_modes = num_modes,
  2244. .modes = NULL,
  2245. };
  2246. *num_modes = 0;
  2247. *flags = 0;
  2248. msg = nlmsg_alloc();
  2249. if (!msg)
  2250. return NULL;
  2251. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2252. 0, NL80211_CMD_GET_WIPHY, 0);
  2253. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2254. if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
  2255. return wpa_driver_nl80211_add_11b(result.modes, num_modes);
  2256. nla_put_failure:
  2257. return NULL;
  2258. }
  2259. static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
  2260. const void *data, size_t len,
  2261. int encrypt)
  2262. {
  2263. __u8 rtap_hdr[] = {
  2264. 0x00, 0x00, /* radiotap version */
  2265. 0x0e, 0x00, /* radiotap length */
  2266. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  2267. IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
  2268. 0x00, /* padding */
  2269. 0x00, 0x00, /* RX and TX flags to indicate that */
  2270. 0x00, 0x00, /* this is the injected frame directly */
  2271. };
  2272. struct iovec iov[2] = {
  2273. {
  2274. .iov_base = &rtap_hdr,
  2275. .iov_len = sizeof(rtap_hdr),
  2276. },
  2277. {
  2278. .iov_base = (void *) data,
  2279. .iov_len = len,
  2280. }
  2281. };
  2282. struct msghdr msg = {
  2283. .msg_name = NULL,
  2284. .msg_namelen = 0,
  2285. .msg_iov = iov,
  2286. .msg_iovlen = 2,
  2287. .msg_control = NULL,
  2288. .msg_controllen = 0,
  2289. .msg_flags = 0,
  2290. };
  2291. if (encrypt)
  2292. rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
  2293. return sendmsg(drv->monitor_sock, &msg, 0);
  2294. }
  2295. static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
  2296. size_t data_len)
  2297. {
  2298. struct i802_bss *bss = priv;
  2299. struct wpa_driver_nl80211_data *drv = bss->drv;
  2300. struct ieee80211_mgmt *mgmt;
  2301. int encrypt = 1;
  2302. u16 fc;
  2303. mgmt = (struct ieee80211_mgmt *) data;
  2304. fc = le_to_host16(mgmt->frame_control);
  2305. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2306. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
  2307. /*
  2308. * Only one of the authentication frame types is encrypted.
  2309. * In order for static WEP encryption to work properly (i.e.,
  2310. * to not encrypt the frame), we need to tell mac80211 about
  2311. * the frames that must not be encrypted.
  2312. */
  2313. u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  2314. u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
  2315. if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
  2316. encrypt = 0;
  2317. }
  2318. return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
  2319. }
  2320. static int wpa_driver_nl80211_set_beacon(void *priv,
  2321. const u8 *head, size_t head_len,
  2322. const u8 *tail, size_t tail_len,
  2323. int dtim_period, int beacon_int)
  2324. {
  2325. struct i802_bss *bss = priv;
  2326. struct wpa_driver_nl80211_data *drv = bss->drv;
  2327. struct nl_msg *msg;
  2328. u8 cmd = NL80211_CMD_NEW_BEACON;
  2329. int ret;
  2330. int beacon_set;
  2331. int ifindex = if_nametoindex(bss->ifname);
  2332. beacon_set = bss->beacon_set;
  2333. msg = nlmsg_alloc();
  2334. if (!msg)
  2335. return -ENOMEM;
  2336. wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
  2337. beacon_set);
  2338. if (beacon_set)
  2339. cmd = NL80211_CMD_SET_BEACON;
  2340. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2341. 0, cmd, 0);
  2342. NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
  2343. NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
  2344. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  2345. NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, beacon_int);
  2346. NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
  2347. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2348. if (ret) {
  2349. wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
  2350. ret, strerror(-ret));
  2351. } else {
  2352. bss->beacon_set = 1;
  2353. }
  2354. return ret;
  2355. nla_put_failure:
  2356. return -ENOBUFS;
  2357. }
  2358. static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
  2359. int freq, int ht_enabled,
  2360. int sec_channel_offset)
  2361. {
  2362. struct nl_msg *msg;
  2363. int ret;
  2364. msg = nlmsg_alloc();
  2365. if (!msg)
  2366. return -1;
  2367. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  2368. NL80211_CMD_SET_WIPHY, 0);
  2369. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2370. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  2371. if (ht_enabled) {
  2372. switch (sec_channel_offset) {
  2373. case -1:
  2374. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  2375. NL80211_CHAN_HT40MINUS);
  2376. break;
  2377. case 1:
  2378. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  2379. NL80211_CHAN_HT40PLUS);
  2380. break;
  2381. default:
  2382. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  2383. NL80211_CHAN_HT20);
  2384. break;
  2385. }
  2386. }
  2387. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2388. if (ret == 0)
  2389. return 0;
  2390. wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
  2391. "%d (%s)", freq, ret, strerror(-ret));
  2392. nla_put_failure:
  2393. return -1;
  2394. }
  2395. static int wpa_driver_nl80211_sta_add(void *priv,
  2396. struct hostapd_sta_add_params *params)
  2397. {
  2398. struct i802_bss *bss = priv;
  2399. struct wpa_driver_nl80211_data *drv = bss->drv;
  2400. struct nl_msg *msg;
  2401. int ret = -ENOBUFS;
  2402. msg = nlmsg_alloc();
  2403. if (!msg)
  2404. return -ENOMEM;
  2405. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2406. 0, NL80211_CMD_NEW_STATION, 0);
  2407. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  2408. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
  2409. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
  2410. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
  2411. params->supp_rates);
  2412. NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  2413. params->listen_interval);
  2414. if (params->ht_capabilities) {
  2415. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
  2416. sizeof(*params->ht_capabilities),
  2417. params->ht_capabilities);
  2418. }
  2419. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2420. if (ret)
  2421. wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
  2422. "result: %d (%s)", ret, strerror(-ret));
  2423. if (ret == -EEXIST)
  2424. ret = 0;
  2425. nla_put_failure:
  2426. return ret;
  2427. }
  2428. static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
  2429. {
  2430. struct i802_bss *bss = priv;
  2431. struct wpa_driver_nl80211_data *drv = bss->drv;
  2432. struct nl_msg *msg;
  2433. int ret;
  2434. msg = nlmsg_alloc();
  2435. if (!msg)
  2436. return -ENOMEM;
  2437. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2438. 0, NL80211_CMD_DEL_STATION, 0);
  2439. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  2440. if_nametoindex(bss->ifname));
  2441. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  2442. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2443. if (ret == -ENOENT)
  2444. return 0;
  2445. return ret;
  2446. nla_put_failure:
  2447. return -ENOBUFS;
  2448. }
  2449. static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
  2450. int ifidx)
  2451. {
  2452. struct nl_msg *msg;
  2453. wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
  2454. #ifdef HOSTAPD
  2455. /* stop listening for EAPOL on this interface */
  2456. del_ifidx(drv, ifidx);
  2457. #endif /* HOSTAPD */
  2458. msg = nlmsg_alloc();
  2459. if (!msg)
  2460. goto nla_put_failure;
  2461. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2462. 0, NL80211_CMD_DEL_INTERFACE, 0);
  2463. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
  2464. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  2465. return;
  2466. nla_put_failure:
  2467. wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
  2468. }
  2469. static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
  2470. const char *ifname,
  2471. enum nl80211_iftype iftype,
  2472. const u8 *addr, int wds)
  2473. {
  2474. struct nl_msg *msg, *flags = NULL;
  2475. int ifidx;
  2476. int ret = -ENOBUFS;
  2477. msg = nlmsg_alloc();
  2478. if (!msg)
  2479. return -1;
  2480. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2481. 0, NL80211_CMD_NEW_INTERFACE, 0);
  2482. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2483. NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
  2484. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
  2485. if (iftype == NL80211_IFTYPE_MONITOR) {
  2486. int err;
  2487. flags = nlmsg_alloc();
  2488. if (!flags)
  2489. goto nla_put_failure;
  2490. NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
  2491. err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
  2492. nlmsg_free(flags);
  2493. if (err)
  2494. goto nla_put_failure;
  2495. } else if (wds) {
  2496. NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
  2497. }
  2498. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2499. if (ret) {
  2500. nla_put_failure:
  2501. wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
  2502. ifname, ret, strerror(-ret));
  2503. return ret;
  2504. }
  2505. ifidx = if_nametoindex(ifname);
  2506. wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
  2507. ifname, ifidx);
  2508. if (ifidx <= 0)
  2509. return -1;
  2510. #ifdef HOSTAPD
  2511. /* start listening for EAPOL on this interface */
  2512. add_ifidx(drv, ifidx);
  2513. #endif /* HOSTAPD */
  2514. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  2515. linux_set_ifhwaddr(drv->ioctl_sock, ifname, addr)) {
  2516. nl80211_remove_iface(drv, ifidx);
  2517. return -1;
  2518. }
  2519. return ifidx;
  2520. }
  2521. static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
  2522. const char *ifname, enum nl80211_iftype iftype,
  2523. const u8 *addr, int wds)
  2524. {
  2525. int ret;
  2526. ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
  2527. /* if error occured and interface exists already */
  2528. if (ret == -ENFILE && if_nametoindex(ifname)) {
  2529. wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
  2530. /* Try to remove the interface that was already there. */
  2531. nl80211_remove_iface(drv, if_nametoindex(ifname));
  2532. /* Try to create the interface again */
  2533. ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
  2534. wds);
  2535. }
  2536. if (ret >= 0 && drv->disable_11b_rates)
  2537. nl80211_disable_11b_rates(drv, ret, 1);
  2538. return ret;
  2539. }
  2540. static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
  2541. {
  2542. struct ieee80211_hdr *hdr;
  2543. u16 fc;
  2544. union wpa_event_data event;
  2545. hdr = (struct ieee80211_hdr *) buf;
  2546. fc = le_to_host16(hdr->frame_control);
  2547. os_memset(&event, 0, sizeof(event));
  2548. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  2549. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  2550. event.tx_status.dst = hdr->addr1;
  2551. event.tx_status.data = buf;
  2552. event.tx_status.data_len = len;
  2553. event.tx_status.ack = ok;
  2554. wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
  2555. }
  2556. static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
  2557. u8 *buf, size_t len)
  2558. {
  2559. union wpa_event_data event;
  2560. os_memset(&event, 0, sizeof(event));
  2561. event.rx_from_unknown.frame = buf;
  2562. event.rx_from_unknown.len = len;
  2563. wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
  2564. }
  2565. static void handle_frame(struct wpa_driver_nl80211_data *drv,
  2566. u8 *buf, size_t len, int datarate, int ssi_signal)
  2567. {
  2568. struct ieee80211_hdr *hdr;
  2569. u16 fc;
  2570. union wpa_event_data event;
  2571. hdr = (struct ieee80211_hdr *) buf;
  2572. fc = le_to_host16(hdr->frame_control);
  2573. switch (WLAN_FC_GET_TYPE(fc)) {
  2574. case WLAN_FC_TYPE_MGMT:
  2575. os_memset(&event, 0, sizeof(event));
  2576. event.rx_mgmt.frame = buf;
  2577. event.rx_mgmt.frame_len = len;
  2578. event.rx_mgmt.datarate = datarate;
  2579. event.rx_mgmt.ssi_signal = ssi_signal;
  2580. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  2581. break;
  2582. case WLAN_FC_TYPE_CTRL:
  2583. /* can only get here with PS-Poll frames */
  2584. wpa_printf(MSG_DEBUG, "CTRL");
  2585. from_unknown_sta(drv, buf, len);
  2586. break;
  2587. case WLAN_FC_TYPE_DATA:
  2588. from_unknown_sta(drv, buf, len);
  2589. break;
  2590. }
  2591. }
  2592. static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
  2593. {
  2594. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  2595. int len;
  2596. unsigned char buf[3000];
  2597. struct ieee80211_radiotap_iterator iter;
  2598. int ret;
  2599. int datarate = 0, ssi_signal = 0;
  2600. int injected = 0, failed = 0, rxflags = 0;
  2601. len = recv(sock, buf, sizeof(buf), 0);
  2602. if (len < 0) {
  2603. perror("recv");
  2604. return;
  2605. }
  2606. if (drv->nlmode == NL80211_IFTYPE_STATION && !drv->probe_req_report) {
  2607. wpa_printf(MSG_DEBUG, "nl80211: Ignore monitor interface "
  2608. "frame since Probe Request reporting is disabled");
  2609. return;
  2610. }
  2611. if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
  2612. printf("received invalid radiotap frame\n");
  2613. return;
  2614. }
  2615. while (1) {
  2616. ret = ieee80211_radiotap_iterator_next(&iter);
  2617. if (ret == -ENOENT)
  2618. break;
  2619. if (ret) {
  2620. printf("received invalid radiotap frame (%d)\n", ret);
  2621. return;
  2622. }
  2623. switch (iter.this_arg_index) {
  2624. case IEEE80211_RADIOTAP_FLAGS:
  2625. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  2626. len -= 4;
  2627. break;
  2628. case IEEE80211_RADIOTAP_RX_FLAGS:
  2629. rxflags = 1;
  2630. break;
  2631. case IEEE80211_RADIOTAP_TX_FLAGS:
  2632. injected = 1;
  2633. failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
  2634. IEEE80211_RADIOTAP_F_TX_FAIL;
  2635. break;
  2636. case IEEE80211_RADIOTAP_DATA_RETRIES:
  2637. break;
  2638. case IEEE80211_RADIOTAP_CHANNEL:
  2639. /* TODO: convert from freq/flags to channel number */
  2640. break;
  2641. case IEEE80211_RADIOTAP_RATE:
  2642. datarate = *iter.this_arg * 5;
  2643. break;
  2644. case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
  2645. ssi_signal = *iter.this_arg;
  2646. break;
  2647. }
  2648. }
  2649. if (rxflags && injected)
  2650. return;
  2651. if (!injected)
  2652. handle_frame(drv, buf + iter.max_length,
  2653. len - iter.max_length, datarate, ssi_signal);
  2654. else
  2655. handle_tx_callback(drv->ctx, buf + iter.max_length,
  2656. len - iter.max_length, !failed);
  2657. }
  2658. /*
  2659. * we post-process the filter code later and rewrite
  2660. * this to the offset to the last instruction
  2661. */
  2662. #define PASS 0xFF
  2663. #define FAIL 0xFE
  2664. static struct sock_filter msock_filter_insns[] = {
  2665. /*
  2666. * do a little-endian load of the radiotap length field
  2667. */
  2668. /* load lower byte into A */
  2669. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
  2670. /* put it into X (== index register) */
  2671. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  2672. /* load upper byte into A */
  2673. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
  2674. /* left-shift it by 8 */
  2675. BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
  2676. /* or with X */
  2677. BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
  2678. /* put result into X */
  2679. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  2680. /*
  2681. * Allow management frames through, this also gives us those
  2682. * management frames that we sent ourselves with status
  2683. */
  2684. /* load the lower byte of the IEEE 802.11 frame control field */
  2685. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  2686. /* mask off frame type and version */
  2687. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
  2688. /* accept frame if it's both 0, fall through otherwise */
  2689. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
  2690. /*
  2691. * TODO: add a bit to radiotap RX flags that indicates
  2692. * that the sending station is not associated, then
  2693. * add a filter here that filters on our DA and that flag
  2694. * to allow us to deauth frames to that bad station.
  2695. *
  2696. * Not a regression -- we didn't do it before either.
  2697. */
  2698. #if 0
  2699. /*
  2700. * drop non-data frames
  2701. */
  2702. /* load the lower byte of the frame control field */
  2703. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  2704. /* mask off QoS bit */
  2705. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
  2706. /* drop non-data frames */
  2707. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
  2708. #endif
  2709. /* load the upper byte of the frame control field */
  2710. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
  2711. /* mask off toDS/fromDS */
  2712. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
  2713. /* accept WDS frames */
  2714. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
  2715. /*
  2716. * add header length to index
  2717. */
  2718. /* load the lower byte of the frame control field */
  2719. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  2720. /* mask off QoS bit */
  2721. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
  2722. /* right shift it by 6 to give 0 or 2 */
  2723. BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
  2724. /* add data frame header length */
  2725. BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
  2726. /* add index, was start of 802.11 header */
  2727. BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
  2728. /* move to index, now start of LL header */
  2729. BPF_STMT(BPF_MISC | BPF_TAX, 0),
  2730. /*
  2731. * Accept empty data frames, we use those for
  2732. * polling activity.
  2733. */
  2734. BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
  2735. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
  2736. /*
  2737. * Accept EAPOL frames
  2738. */
  2739. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
  2740. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
  2741. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
  2742. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
  2743. /* keep these last two statements or change the code below */
  2744. /* return 0 == "DROP" */
  2745. BPF_STMT(BPF_RET | BPF_K, 0),
  2746. /* return ~0 == "keep all" */
  2747. BPF_STMT(BPF_RET | BPF_K, ~0),
  2748. };
  2749. static struct sock_fprog msock_filter = {
  2750. .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
  2751. .filter = msock_filter_insns,
  2752. };
  2753. static int add_monitor_filter(int s)
  2754. {
  2755. int idx;
  2756. /* rewrite all PASS/FAIL jump offsets */
  2757. for (idx = 0; idx < msock_filter.len; idx++) {
  2758. struct sock_filter *insn = &msock_filter_insns[idx];
  2759. if (BPF_CLASS(insn->code) == BPF_JMP) {
  2760. if (insn->code == (BPF_JMP|BPF_JA)) {
  2761. if (insn->k == PASS)
  2762. insn->k = msock_filter.len - idx - 2;
  2763. else if (insn->k == FAIL)
  2764. insn->k = msock_filter.len - idx - 3;
  2765. }
  2766. if (insn->jt == PASS)
  2767. insn->jt = msock_filter.len - idx - 2;
  2768. else if (insn->jt == FAIL)
  2769. insn->jt = msock_filter.len - idx - 3;
  2770. if (insn->jf == PASS)
  2771. insn->jf = msock_filter.len - idx - 2;
  2772. else if (insn->jf == FAIL)
  2773. insn->jf = msock_filter.len - idx - 3;
  2774. }
  2775. }
  2776. if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
  2777. &msock_filter, sizeof(msock_filter))) {
  2778. perror("SO_ATTACH_FILTER");
  2779. return -1;
  2780. }
  2781. return 0;
  2782. }
  2783. static void nl80211_remove_monitor_interface(
  2784. struct wpa_driver_nl80211_data *drv)
  2785. {
  2786. if (drv->monitor_ifidx >= 0) {
  2787. nl80211_remove_iface(drv, drv->monitor_ifidx);
  2788. drv->monitor_ifidx = -1;
  2789. }
  2790. if (drv->monitor_sock >= 0) {
  2791. eloop_unregister_read_sock(drv->monitor_sock);
  2792. close(drv->monitor_sock);
  2793. drv->monitor_sock = -1;
  2794. }
  2795. }
  2796. static int
  2797. nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
  2798. {
  2799. char buf[IFNAMSIZ];
  2800. struct sockaddr_ll ll;
  2801. int optval;
  2802. socklen_t optlen;
  2803. snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
  2804. buf[IFNAMSIZ - 1] = '\0';
  2805. drv->monitor_ifidx =
  2806. nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
  2807. 0);
  2808. if (drv->monitor_ifidx < 0)
  2809. return -1;
  2810. if (linux_set_iface_flags(drv->ioctl_sock, buf, 1))
  2811. goto error;
  2812. memset(&ll, 0, sizeof(ll));
  2813. ll.sll_family = AF_PACKET;
  2814. ll.sll_ifindex = drv->monitor_ifidx;
  2815. drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  2816. if (drv->monitor_sock < 0) {
  2817. perror("socket[PF_PACKET,SOCK_RAW]");
  2818. goto error;
  2819. }
  2820. if (add_monitor_filter(drv->monitor_sock)) {
  2821. wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
  2822. "interface; do filtering in user space");
  2823. /* This works, but will cost in performance. */
  2824. }
  2825. if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  2826. perror("monitor socket bind");
  2827. goto error;
  2828. }
  2829. optlen = sizeof(optval);
  2830. optval = 20;
  2831. if (setsockopt
  2832. (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
  2833. perror("Failed to set socket priority");
  2834. goto error;
  2835. }
  2836. if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
  2837. drv, NULL)) {
  2838. printf("Could not register monitor read socket\n");
  2839. goto error;
  2840. }
  2841. return 0;
  2842. error:
  2843. nl80211_remove_monitor_interface(drv);
  2844. return -1;
  2845. }
  2846. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  2847. static int wpa_driver_nl80211_hapd_send_eapol(
  2848. void *priv, const u8 *addr, const u8 *data,
  2849. size_t data_len, int encrypt, const u8 *own_addr)
  2850. {
  2851. struct i802_bss *bss = priv;
  2852. struct wpa_driver_nl80211_data *drv = bss->drv;
  2853. struct ieee80211_hdr *hdr;
  2854. size_t len;
  2855. u8 *pos;
  2856. int res;
  2857. #if 0 /* FIX */
  2858. int qos = sta->flags & WPA_STA_WMM;
  2859. #else
  2860. int qos = 0;
  2861. #endif
  2862. len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
  2863. data_len;
  2864. hdr = os_zalloc(len);
  2865. if (hdr == NULL) {
  2866. printf("malloc() failed for i802_send_data(len=%lu)\n",
  2867. (unsigned long) len);
  2868. return -1;
  2869. }
  2870. hdr->frame_control =
  2871. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  2872. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  2873. if (encrypt)
  2874. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  2875. #if 0 /* To be enabled if qos determination is added above */
  2876. if (qos) {
  2877. hdr->frame_control |=
  2878. host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
  2879. }
  2880. #endif
  2881. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  2882. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  2883. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  2884. pos = (u8 *) (hdr + 1);
  2885. #if 0 /* To be enabled if qos determination is added above */
  2886. if (qos) {
  2887. /* add an empty QoS header if needed */
  2888. pos[0] = 0;
  2889. pos[1] = 0;
  2890. pos += 2;
  2891. }
  2892. #endif
  2893. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  2894. pos += sizeof(rfc1042_header);
  2895. WPA_PUT_BE16(pos, ETH_P_PAE);
  2896. pos += 2;
  2897. memcpy(pos, data, data_len);
  2898. res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
  2899. if (res < 0) {
  2900. wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
  2901. "failed: %d (%s)",
  2902. (unsigned long) len, errno, strerror(errno));
  2903. }
  2904. os_free(hdr);
  2905. return res;
  2906. }
  2907. static u32 sta_flags_nl80211(int flags)
  2908. {
  2909. u32 f = 0;
  2910. if (flags & WPA_STA_AUTHORIZED)
  2911. f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  2912. if (flags & WPA_STA_WMM)
  2913. f |= BIT(NL80211_STA_FLAG_WME);
  2914. if (flags & WPA_STA_SHORT_PREAMBLE)
  2915. f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  2916. if (flags & WPA_STA_MFP)
  2917. f |= BIT(NL80211_STA_FLAG_MFP);
  2918. return f;
  2919. }
  2920. static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
  2921. int total_flags,
  2922. int flags_or, int flags_and)
  2923. {
  2924. struct i802_bss *bss = priv;
  2925. struct wpa_driver_nl80211_data *drv = bss->drv;
  2926. struct nl_msg *msg, *flags = NULL;
  2927. struct nl80211_sta_flag_update upd;
  2928. msg = nlmsg_alloc();
  2929. if (!msg)
  2930. return -ENOMEM;
  2931. flags = nlmsg_alloc();
  2932. if (!flags) {
  2933. nlmsg_free(msg);
  2934. return -ENOMEM;
  2935. }
  2936. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2937. 0, NL80211_CMD_SET_STATION, 0);
  2938. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  2939. if_nametoindex(bss->ifname));
  2940. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  2941. /*
  2942. * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
  2943. * can be removed eventually.
  2944. */
  2945. if (total_flags & WPA_STA_AUTHORIZED)
  2946. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
  2947. if (total_flags & WPA_STA_WMM)
  2948. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
  2949. if (total_flags & WPA_STA_SHORT_PREAMBLE)
  2950. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
  2951. if (total_flags & WPA_STA_MFP)
  2952. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
  2953. if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
  2954. goto nla_put_failure;
  2955. os_memset(&upd, 0, sizeof(upd));
  2956. upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
  2957. upd.set = sta_flags_nl80211(flags_or);
  2958. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  2959. nlmsg_free(flags);
  2960. return send_and_recv_msgs(drv, msg, NULL, NULL);
  2961. nla_put_failure:
  2962. nlmsg_free(flags);
  2963. return -ENOBUFS;
  2964. }
  2965. static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
  2966. struct wpa_driver_associate_params *params)
  2967. {
  2968. if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode) ||
  2969. wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
  2970. nl80211_remove_monitor_interface(drv);
  2971. return -1;
  2972. }
  2973. /* TODO: setup monitor interface (and add code somewhere to remove this
  2974. * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
  2975. return 0;
  2976. }
  2977. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
  2978. {
  2979. struct nl_msg *msg;
  2980. int ret = -1;
  2981. msg = nlmsg_alloc();
  2982. if (!msg)
  2983. return -1;
  2984. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  2985. NL80211_CMD_LEAVE_IBSS, 0);
  2986. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2987. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2988. msg = NULL;
  2989. if (ret) {
  2990. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
  2991. "(%s)", ret, strerror(-ret));
  2992. goto nla_put_failure;
  2993. }
  2994. ret = 0;
  2995. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
  2996. nla_put_failure:
  2997. nlmsg_free(msg);
  2998. return ret;
  2999. }
  3000. static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
  3001. struct wpa_driver_associate_params *params)
  3002. {
  3003. struct nl_msg *msg;
  3004. int ret = -1;
  3005. int count = 0;
  3006. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
  3007. if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode)) {
  3008. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  3009. "IBSS mode");
  3010. return -1;
  3011. }
  3012. retry:
  3013. msg = nlmsg_alloc();
  3014. if (!msg)
  3015. return -1;
  3016. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3017. NL80211_CMD_JOIN_IBSS, 0);
  3018. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3019. if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
  3020. goto nla_put_failure;
  3021. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3022. params->ssid, params->ssid_len);
  3023. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  3024. params->ssid);
  3025. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  3026. drv->ssid_len = params->ssid_len;
  3027. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  3028. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  3029. ret = nl80211_set_conn_keys(params, msg);
  3030. if (ret)
  3031. goto nla_put_failure;
  3032. if (params->wpa_ie) {
  3033. wpa_hexdump(MSG_DEBUG,
  3034. " * Extra IEs for Beacon/Probe Response frames",
  3035. params->wpa_ie, params->wpa_ie_len);
  3036. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  3037. params->wpa_ie);
  3038. }
  3039. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3040. msg = NULL;
  3041. if (ret) {
  3042. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
  3043. ret, strerror(-ret));
  3044. count++;
  3045. if (ret == -EALREADY && count == 1) {
  3046. wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
  3047. "forced leave");
  3048. nl80211_leave_ibss(drv);
  3049. nlmsg_free(msg);
  3050. goto retry;
  3051. }
  3052. goto nla_put_failure;
  3053. }
  3054. ret = 0;
  3055. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
  3056. nla_put_failure:
  3057. nlmsg_free(msg);
  3058. return ret;
  3059. }
  3060. static int wpa_driver_nl80211_connect(
  3061. struct wpa_driver_nl80211_data *drv,
  3062. struct wpa_driver_associate_params *params)
  3063. {
  3064. struct nl_msg *msg;
  3065. enum nl80211_auth_type type;
  3066. int ret = 0;
  3067. msg = nlmsg_alloc();
  3068. if (!msg)
  3069. return -1;
  3070. wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
  3071. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3072. NL80211_CMD_CONNECT, 0);
  3073. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3074. if (params->bssid) {
  3075. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  3076. MAC2STR(params->bssid));
  3077. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  3078. }
  3079. if (params->freq) {
  3080. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  3081. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  3082. }
  3083. if (params->ssid) {
  3084. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3085. params->ssid, params->ssid_len);
  3086. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  3087. params->ssid);
  3088. if (params->ssid_len > sizeof(drv->ssid))
  3089. goto nla_put_failure;
  3090. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  3091. drv->ssid_len = params->ssid_len;
  3092. }
  3093. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  3094. if (params->wpa_ie)
  3095. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  3096. params->wpa_ie);
  3097. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  3098. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  3099. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  3100. type = NL80211_AUTHTYPE_SHARED_KEY;
  3101. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  3102. type = NL80211_AUTHTYPE_NETWORK_EAP;
  3103. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  3104. type = NL80211_AUTHTYPE_FT;
  3105. else
  3106. goto nla_put_failure;
  3107. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  3108. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  3109. if (params->wpa_ie && params->wpa_ie_len) {
  3110. enum nl80211_wpa_versions ver;
  3111. if (params->wpa_ie[0] == WLAN_EID_RSN)
  3112. ver = NL80211_WPA_VERSION_2;
  3113. else
  3114. ver = NL80211_WPA_VERSION_1;
  3115. wpa_printf(MSG_DEBUG, " * WPA Version %d", ver);
  3116. NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
  3117. }
  3118. if (params->pairwise_suite != CIPHER_NONE) {
  3119. int cipher;
  3120. switch (params->pairwise_suite) {
  3121. case CIPHER_WEP40:
  3122. cipher = WLAN_CIPHER_SUITE_WEP40;
  3123. break;
  3124. case CIPHER_WEP104:
  3125. cipher = WLAN_CIPHER_SUITE_WEP104;
  3126. break;
  3127. case CIPHER_CCMP:
  3128. cipher = WLAN_CIPHER_SUITE_CCMP;
  3129. break;
  3130. case CIPHER_TKIP:
  3131. default:
  3132. cipher = WLAN_CIPHER_SUITE_TKIP;
  3133. break;
  3134. }
  3135. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  3136. }
  3137. if (params->group_suite != CIPHER_NONE) {
  3138. int cipher;
  3139. switch (params->group_suite) {
  3140. case CIPHER_WEP40:
  3141. cipher = WLAN_CIPHER_SUITE_WEP40;
  3142. break;
  3143. case CIPHER_WEP104:
  3144. cipher = WLAN_CIPHER_SUITE_WEP104;
  3145. break;
  3146. case CIPHER_CCMP:
  3147. cipher = WLAN_CIPHER_SUITE_CCMP;
  3148. break;
  3149. case CIPHER_TKIP:
  3150. default:
  3151. cipher = WLAN_CIPHER_SUITE_TKIP;
  3152. break;
  3153. }
  3154. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  3155. }
  3156. if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
  3157. params->key_mgmt_suite == KEY_MGMT_PSK) {
  3158. int mgmt = WLAN_AKM_SUITE_PSK;
  3159. switch (params->key_mgmt_suite) {
  3160. case KEY_MGMT_802_1X:
  3161. mgmt = WLAN_AKM_SUITE_8021X;
  3162. break;
  3163. case KEY_MGMT_PSK:
  3164. default:
  3165. mgmt = WLAN_AKM_SUITE_PSK;
  3166. break;
  3167. }
  3168. NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
  3169. }
  3170. ret = nl80211_set_conn_keys(params, msg);
  3171. if (ret)
  3172. goto nla_put_failure;
  3173. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3174. msg = NULL;
  3175. if (ret) {
  3176. wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
  3177. "(%s)", ret, strerror(-ret));
  3178. goto nla_put_failure;
  3179. }
  3180. ret = 0;
  3181. wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
  3182. nla_put_failure:
  3183. nlmsg_free(msg);
  3184. return ret;
  3185. }
  3186. static int wpa_driver_nl80211_associate(
  3187. void *priv, struct wpa_driver_associate_params *params)
  3188. {
  3189. struct i802_bss *bss = priv;
  3190. struct wpa_driver_nl80211_data *drv = bss->drv;
  3191. int ret = -1;
  3192. struct nl_msg *msg;
  3193. if (params->mode == IEEE80211_MODE_AP)
  3194. return wpa_driver_nl80211_ap(drv, params);
  3195. if (params->mode == IEEE80211_MODE_IBSS)
  3196. return wpa_driver_nl80211_ibss(drv, params);
  3197. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
  3198. if (wpa_driver_nl80211_set_mode(priv, params->mode) < 0)
  3199. return -1;
  3200. return wpa_driver_nl80211_connect(drv, params);
  3201. }
  3202. drv->associated = 0;
  3203. msg = nlmsg_alloc();
  3204. if (!msg)
  3205. return -1;
  3206. wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
  3207. drv->ifindex);
  3208. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3209. NL80211_CMD_ASSOCIATE, 0);
  3210. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3211. if (params->bssid) {
  3212. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  3213. MAC2STR(params->bssid));
  3214. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  3215. }
  3216. if (params->freq) {
  3217. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  3218. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  3219. drv->assoc_freq = params->freq;
  3220. } else
  3221. drv->assoc_freq = 0;
  3222. if (params->ssid) {
  3223. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3224. params->ssid, params->ssid_len);
  3225. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  3226. params->ssid);
  3227. if (params->ssid_len > sizeof(drv->ssid))
  3228. goto nla_put_failure;
  3229. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  3230. drv->ssid_len = params->ssid_len;
  3231. }
  3232. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  3233. if (params->wpa_ie)
  3234. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  3235. params->wpa_ie);
  3236. #ifdef CONFIG_IEEE80211W
  3237. if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
  3238. NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
  3239. #endif /* CONFIG_IEEE80211W */
  3240. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
  3241. if (params->prev_bssid) {
  3242. wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
  3243. MAC2STR(params->prev_bssid));
  3244. NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
  3245. params->prev_bssid);
  3246. }
  3247. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3248. msg = NULL;
  3249. if (ret) {
  3250. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  3251. "(%s)", ret, strerror(-ret));
  3252. nl80211_dump_scan(drv);
  3253. goto nla_put_failure;
  3254. }
  3255. ret = 0;
  3256. wpa_printf(MSG_DEBUG, "nl80211: Association request send "
  3257. "successfully");
  3258. nla_put_failure:
  3259. nlmsg_free(msg);
  3260. return ret;
  3261. }
  3262. static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
  3263. int ifindex, int mode)
  3264. {
  3265. struct nl_msg *msg;
  3266. int ret = -ENOBUFS;
  3267. msg = nlmsg_alloc();
  3268. if (!msg)
  3269. return -ENOMEM;
  3270. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3271. 0, NL80211_CMD_SET_INTERFACE, 0);
  3272. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  3273. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
  3274. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3275. if (!ret)
  3276. return 0;
  3277. nla_put_failure:
  3278. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
  3279. " %d (%s)", ifindex, mode, ret, strerror(-ret));
  3280. return ret;
  3281. }
  3282. static int wpa_driver_nl80211_set_mode(void *priv, int mode)
  3283. {
  3284. struct i802_bss *bss = priv;
  3285. struct wpa_driver_nl80211_data *drv = bss->drv;
  3286. int ret = -1;
  3287. int nlmode;
  3288. switch (mode) {
  3289. case 0:
  3290. nlmode = NL80211_IFTYPE_STATION;
  3291. break;
  3292. case 1:
  3293. nlmode = NL80211_IFTYPE_ADHOC;
  3294. break;
  3295. case 2:
  3296. nlmode = NL80211_IFTYPE_AP;
  3297. break;
  3298. default:
  3299. return -1;
  3300. }
  3301. if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
  3302. drv->nlmode = nlmode;
  3303. ret = 0;
  3304. goto done;
  3305. }
  3306. if (nlmode == drv->nlmode) {
  3307. wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
  3308. "requested mode - ignore error");
  3309. ret = 0;
  3310. goto done; /* Already in the requested mode */
  3311. }
  3312. /* mac80211 doesn't allow mode changes while the device is up, so
  3313. * take the device down, try to set the mode again, and bring the
  3314. * device back up.
  3315. */
  3316. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0) == 0) {
  3317. /* Try to set the mode again while the interface is down */
  3318. ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
  3319. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1))
  3320. ret = -1;
  3321. }
  3322. if (!ret) {
  3323. wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
  3324. "interface is down");
  3325. drv->nlmode = nlmode;
  3326. }
  3327. done:
  3328. if (!ret && nlmode == NL80211_IFTYPE_AP) {
  3329. /* Setup additional AP mode functionality if needed */
  3330. if (drv->monitor_ifidx < 0 &&
  3331. nl80211_create_monitor_interface(drv))
  3332. return -1;
  3333. } else if (!ret && nlmode != NL80211_IFTYPE_AP) {
  3334. /* Remove additional AP mode functionality */
  3335. nl80211_remove_monitor_interface(drv);
  3336. bss->beacon_set = 0;
  3337. }
  3338. if (ret)
  3339. wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
  3340. "from %d failed", nlmode, drv->nlmode);
  3341. return ret;
  3342. }
  3343. static int wpa_driver_nl80211_get_capa(void *priv,
  3344. struct wpa_driver_capa *capa)
  3345. {
  3346. struct i802_bss *bss = priv;
  3347. struct wpa_driver_nl80211_data *drv = bss->drv;
  3348. if (!drv->has_capability)
  3349. return -1;
  3350. os_memcpy(capa, &drv->capa, sizeof(*capa));
  3351. return 0;
  3352. }
  3353. static int wpa_driver_nl80211_set_operstate(void *priv, int state)
  3354. {
  3355. struct i802_bss *bss = priv;
  3356. struct wpa_driver_nl80211_data *drv = bss->drv;
  3357. wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
  3358. __func__, drv->operstate, state, state ? "UP" : "DORMANT");
  3359. drv->operstate = state;
  3360. return netlink_send_oper_ifla(drv->netlink, drv->ifindex, -1,
  3361. state ? IF_OPER_UP : IF_OPER_DORMANT);
  3362. }
  3363. static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
  3364. {
  3365. struct i802_bss *bss = priv;
  3366. struct wpa_driver_nl80211_data *drv = bss->drv;
  3367. struct nl_msg *msg;
  3368. struct nl80211_sta_flag_update upd;
  3369. msg = nlmsg_alloc();
  3370. if (!msg)
  3371. return -ENOMEM;
  3372. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3373. 0, NL80211_CMD_SET_STATION, 0);
  3374. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  3375. if_nametoindex(bss->ifname));
  3376. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  3377. os_memset(&upd, 0, sizeof(upd));
  3378. upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
  3379. if (authorized)
  3380. upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
  3381. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  3382. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3383. nla_put_failure:
  3384. return -ENOBUFS;
  3385. }
  3386. #ifdef HOSTAPD
  3387. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  3388. {
  3389. int i;
  3390. int *old;
  3391. wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
  3392. ifidx);
  3393. for (i = 0; i < drv->num_if_indices; i++) {
  3394. if (drv->if_indices[i] == 0) {
  3395. drv->if_indices[i] = ifidx;
  3396. return;
  3397. }
  3398. }
  3399. if (drv->if_indices != drv->default_if_indices)
  3400. old = drv->if_indices;
  3401. else
  3402. old = NULL;
  3403. drv->if_indices = os_realloc(old,
  3404. sizeof(int) * (drv->num_if_indices + 1));
  3405. if (!drv->if_indices) {
  3406. if (!old)
  3407. drv->if_indices = drv->default_if_indices;
  3408. else
  3409. drv->if_indices = old;
  3410. wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
  3411. "interfaces");
  3412. wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
  3413. return;
  3414. }
  3415. drv->if_indices[drv->num_if_indices] = ifidx;
  3416. drv->num_if_indices++;
  3417. }
  3418. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  3419. {
  3420. int i;
  3421. for (i = 0; i < drv->num_if_indices; i++) {
  3422. if (drv->if_indices[i] == ifidx) {
  3423. drv->if_indices[i] = 0;
  3424. break;
  3425. }
  3426. }
  3427. }
  3428. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  3429. {
  3430. int i;
  3431. for (i = 0; i < drv->num_if_indices; i++)
  3432. if (drv->if_indices[i] == ifidx)
  3433. return 1;
  3434. return 0;
  3435. }
  3436. static inline int min_int(int a, int b)
  3437. {
  3438. if (a < b)
  3439. return a;
  3440. return b;
  3441. }
  3442. static int get_key_handler(struct nl_msg *msg, void *arg)
  3443. {
  3444. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  3445. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  3446. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  3447. genlmsg_attrlen(gnlh, 0), NULL);
  3448. /*
  3449. * TODO: validate the key index and mac address!
  3450. * Otherwise, there's a race condition as soon as
  3451. * the kernel starts sending key notifications.
  3452. */
  3453. if (tb[NL80211_ATTR_KEY_SEQ])
  3454. memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  3455. min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
  3456. return NL_SKIP;
  3457. }
  3458. static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
  3459. int idx, u8 *seq)
  3460. {
  3461. struct i802_bss *bss = priv;
  3462. struct wpa_driver_nl80211_data *drv = bss->drv;
  3463. struct nl_msg *msg;
  3464. msg = nlmsg_alloc();
  3465. if (!msg)
  3466. return -ENOMEM;
  3467. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3468. 0, NL80211_CMD_GET_KEY, 0);
  3469. if (addr)
  3470. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3471. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
  3472. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
  3473. memset(seq, 0, 6);
  3474. return send_and_recv_msgs(drv, msg, get_key_handler, seq);
  3475. nla_put_failure:
  3476. return -ENOBUFS;
  3477. }
  3478. static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
  3479. int mode)
  3480. {
  3481. struct i802_bss *bss = priv;
  3482. struct wpa_driver_nl80211_data *drv = bss->drv;
  3483. struct nl_msg *msg;
  3484. u8 rates[NL80211_MAX_SUPP_RATES];
  3485. u8 rates_len = 0;
  3486. int i;
  3487. msg = nlmsg_alloc();
  3488. if (!msg)
  3489. return -ENOMEM;
  3490. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3491. NL80211_CMD_SET_BSS, 0);
  3492. for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
  3493. rates[rates_len++] = basic_rates[i] / 5;
  3494. NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
  3495. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  3496. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3497. nla_put_failure:
  3498. return -ENOBUFS;
  3499. }
  3500. /* Set kernel driver on given frequency (MHz) */
  3501. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
  3502. {
  3503. struct i802_bss *bss = priv;
  3504. struct wpa_driver_nl80211_data *drv = bss->drv;
  3505. return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled,
  3506. freq->sec_channel_offset);
  3507. }
  3508. static int i802_set_rts(void *priv, int rts)
  3509. {
  3510. struct i802_bss *bss = priv;
  3511. struct wpa_driver_nl80211_data *drv = bss->drv;
  3512. struct nl_msg *msg;
  3513. int ret = -ENOBUFS;
  3514. u32 val;
  3515. msg = nlmsg_alloc();
  3516. if (!msg)
  3517. return -ENOMEM;
  3518. if (rts >= 2347)
  3519. val = (u32) -1;
  3520. else
  3521. val = rts;
  3522. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3523. 0, NL80211_CMD_SET_WIPHY, 0);
  3524. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3525. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
  3526. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3527. if (!ret)
  3528. return 0;
  3529. nla_put_failure:
  3530. wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
  3531. "%d (%s)", rts, ret, strerror(-ret));
  3532. return ret;
  3533. }
  3534. static int i802_set_frag(void *priv, int frag)
  3535. {
  3536. struct i802_bss *bss = priv;
  3537. struct wpa_driver_nl80211_data *drv = bss->drv;
  3538. struct nl_msg *msg;
  3539. int ret = -ENOBUFS;
  3540. u32 val;
  3541. msg = nlmsg_alloc();
  3542. if (!msg)
  3543. return -ENOMEM;
  3544. if (frag >= 2346)
  3545. val = (u32) -1;
  3546. else
  3547. val = frag;
  3548. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3549. 0, NL80211_CMD_SET_WIPHY, 0);
  3550. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3551. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
  3552. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3553. if (!ret)
  3554. return 0;
  3555. nla_put_failure:
  3556. wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
  3557. "%d: %d (%s)", frag, ret, strerror(-ret));
  3558. return ret;
  3559. }
  3560. static int i802_flush(void *priv)
  3561. {
  3562. struct i802_bss *bss = priv;
  3563. struct wpa_driver_nl80211_data *drv = bss->drv;
  3564. struct nl_msg *msg;
  3565. msg = nlmsg_alloc();
  3566. if (!msg)
  3567. return -1;
  3568. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3569. 0, NL80211_CMD_DEL_STATION, 0);
  3570. /*
  3571. * XXX: FIX! this needs to flush all VLANs too
  3572. */
  3573. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  3574. if_nametoindex(bss->ifname));
  3575. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3576. nla_put_failure:
  3577. return -ENOBUFS;
  3578. }
  3579. static int get_sta_handler(struct nl_msg *msg, void *arg)
  3580. {
  3581. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  3582. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  3583. struct hostap_sta_driver_data *data = arg;
  3584. struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
  3585. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  3586. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  3587. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  3588. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  3589. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  3590. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  3591. };
  3592. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  3593. genlmsg_attrlen(gnlh, 0), NULL);
  3594. /*
  3595. * TODO: validate the interface and mac address!
  3596. * Otherwise, there's a race condition as soon as
  3597. * the kernel starts sending station notifications.
  3598. */
  3599. if (!tb[NL80211_ATTR_STA_INFO]) {
  3600. wpa_printf(MSG_DEBUG, "sta stats missing!");
  3601. return NL_SKIP;
  3602. }
  3603. if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
  3604. tb[NL80211_ATTR_STA_INFO],
  3605. stats_policy)) {
  3606. wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
  3607. return NL_SKIP;
  3608. }
  3609. if (stats[NL80211_STA_INFO_INACTIVE_TIME])
  3610. data->inactive_msec =
  3611. nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
  3612. if (stats[NL80211_STA_INFO_RX_BYTES])
  3613. data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
  3614. if (stats[NL80211_STA_INFO_TX_BYTES])
  3615. data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
  3616. if (stats[NL80211_STA_INFO_RX_PACKETS])
  3617. data->rx_packets =
  3618. nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
  3619. if (stats[NL80211_STA_INFO_TX_PACKETS])
  3620. data->tx_packets =
  3621. nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
  3622. return NL_SKIP;
  3623. }
  3624. static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
  3625. const u8 *addr)
  3626. {
  3627. struct i802_bss *bss = priv;
  3628. struct wpa_driver_nl80211_data *drv = bss->drv;
  3629. struct nl_msg *msg;
  3630. os_memset(data, 0, sizeof(*data));
  3631. msg = nlmsg_alloc();
  3632. if (!msg)
  3633. return -ENOMEM;
  3634. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3635. 0, NL80211_CMD_GET_STATION, 0);
  3636. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3637. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  3638. return send_and_recv_msgs(drv, msg, get_sta_handler, data);
  3639. nla_put_failure:
  3640. return -ENOBUFS;
  3641. }
  3642. static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
  3643. int cw_min, int cw_max, int burst_time)
  3644. {
  3645. struct i802_bss *bss = priv;
  3646. struct wpa_driver_nl80211_data *drv = bss->drv;
  3647. struct nl_msg *msg;
  3648. struct nlattr *txq, *params;
  3649. msg = nlmsg_alloc();
  3650. if (!msg)
  3651. return -1;
  3652. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3653. 0, NL80211_CMD_SET_WIPHY, 0);
  3654. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  3655. txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
  3656. if (!txq)
  3657. goto nla_put_failure;
  3658. /* We are only sending parameters for a single TXQ at a time */
  3659. params = nla_nest_start(msg, 1);
  3660. if (!params)
  3661. goto nla_put_failure;
  3662. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
  3663. /* Burst time is configured in units of 0.1 msec and TXOP parameter in
  3664. * 32 usec, so need to convert the value here. */
  3665. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
  3666. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
  3667. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
  3668. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
  3669. nla_nest_end(msg, params);
  3670. nla_nest_end(msg, txq);
  3671. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  3672. return 0;
  3673. nla_put_failure:
  3674. return -1;
  3675. }
  3676. static int i802_set_bss(void *priv, int cts, int preamble, int slot)
  3677. {
  3678. struct i802_bss *bss = priv;
  3679. struct wpa_driver_nl80211_data *drv = bss->drv;
  3680. struct nl_msg *msg;
  3681. msg = nlmsg_alloc();
  3682. if (!msg)
  3683. return -ENOMEM;
  3684. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3685. NL80211_CMD_SET_BSS, 0);
  3686. if (cts >= 0)
  3687. NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
  3688. if (preamble >= 0)
  3689. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
  3690. if (slot >= 0)
  3691. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
  3692. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  3693. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3694. nla_put_failure:
  3695. return -ENOBUFS;
  3696. }
  3697. static int i802_set_cts_protect(void *priv, int value)
  3698. {
  3699. return i802_set_bss(priv, value, -1, -1);
  3700. }
  3701. static int i802_set_preamble(void *priv, int value)
  3702. {
  3703. return i802_set_bss(priv, -1, value, -1);
  3704. }
  3705. static int i802_set_short_slot_time(void *priv, int value)
  3706. {
  3707. return i802_set_bss(priv, -1, -1, value);
  3708. }
  3709. static int i802_set_sta_vlan(void *priv, const u8 *addr,
  3710. const char *ifname, int vlan_id)
  3711. {
  3712. struct i802_bss *bss = priv;
  3713. struct wpa_driver_nl80211_data *drv = bss->drv;
  3714. struct nl_msg *msg;
  3715. msg = nlmsg_alloc();
  3716. if (!msg)
  3717. return -ENOMEM;
  3718. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3719. 0, NL80211_CMD_SET_STATION, 0);
  3720. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  3721. if_nametoindex(bss->ifname));
  3722. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3723. NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
  3724. if_nametoindex(ifname));
  3725. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3726. nla_put_failure:
  3727. return -ENOBUFS;
  3728. }
  3729. static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val)
  3730. {
  3731. struct i802_bss *bss = priv;
  3732. struct wpa_driver_nl80211_data *drv = bss->drv;
  3733. char name[IFNAMSIZ + 1];
  3734. os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
  3735. wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
  3736. " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
  3737. if (val) {
  3738. if (nl80211_create_iface(drv, name, NL80211_IFTYPE_AP_VLAN,
  3739. NULL, 1) < 0)
  3740. return -1;
  3741. linux_set_iface_flags(drv->ioctl_sock, name, 1);
  3742. return i802_set_sta_vlan(priv, addr, name, 0);
  3743. } else {
  3744. i802_set_sta_vlan(priv, addr, bss->ifname, 0);
  3745. return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
  3746. name);
  3747. }
  3748. }
  3749. static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
  3750. {
  3751. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  3752. struct sockaddr_ll lladdr;
  3753. unsigned char buf[3000];
  3754. int len;
  3755. socklen_t fromlen = sizeof(lladdr);
  3756. len = recvfrom(sock, buf, sizeof(buf), 0,
  3757. (struct sockaddr *)&lladdr, &fromlen);
  3758. if (len < 0) {
  3759. perror("recv");
  3760. return;
  3761. }
  3762. if (have_ifidx(drv, lladdr.sll_ifindex))
  3763. drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
  3764. }
  3765. static int i802_get_inact_sec(void *priv, const u8 *addr)
  3766. {
  3767. struct hostap_sta_driver_data data;
  3768. int ret;
  3769. data.inactive_msec = (unsigned long) -1;
  3770. ret = i802_read_sta_data(priv, &data, addr);
  3771. if (ret || data.inactive_msec == (unsigned long) -1)
  3772. return -1;
  3773. return data.inactive_msec / 1000;
  3774. }
  3775. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  3776. {
  3777. #if 0
  3778. /* TODO */
  3779. #endif
  3780. return 0;
  3781. }
  3782. static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  3783. int reason)
  3784. {
  3785. struct i802_bss *bss = priv;
  3786. struct ieee80211_mgmt mgmt;
  3787. memset(&mgmt, 0, sizeof(mgmt));
  3788. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  3789. WLAN_FC_STYPE_DEAUTH);
  3790. memcpy(mgmt.da, addr, ETH_ALEN);
  3791. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  3792. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  3793. mgmt.u.deauth.reason_code = host_to_le16(reason);
  3794. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  3795. IEEE80211_HDRLEN +
  3796. sizeof(mgmt.u.deauth));
  3797. }
  3798. static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  3799. int reason)
  3800. {
  3801. struct i802_bss *bss = priv;
  3802. struct ieee80211_mgmt mgmt;
  3803. memset(&mgmt, 0, sizeof(mgmt));
  3804. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  3805. WLAN_FC_STYPE_DISASSOC);
  3806. memcpy(mgmt.da, addr, ETH_ALEN);
  3807. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  3808. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  3809. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  3810. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  3811. IEEE80211_HDRLEN +
  3812. sizeof(mgmt.u.disassoc));
  3813. }
  3814. static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
  3815. const char *brname, const char *ifname)
  3816. {
  3817. int ifindex;
  3818. char in_br[IFNAMSIZ];
  3819. os_strlcpy(drv->brname, brname, IFNAMSIZ);
  3820. ifindex = if_nametoindex(brname);
  3821. if (ifindex == 0) {
  3822. /*
  3823. * Bridge was configured, but the bridge device does
  3824. * not exist. Try to add it now.
  3825. */
  3826. if (linux_br_add(drv->ioctl_sock, brname) < 0) {
  3827. wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
  3828. "bridge interface %s: %s",
  3829. brname, strerror(errno));
  3830. return -1;
  3831. }
  3832. drv->added_bridge = 1;
  3833. add_ifidx(drv, if_nametoindex(brname));
  3834. }
  3835. if (linux_br_get(in_br, ifname) == 0) {
  3836. if (os_strcmp(in_br, brname) == 0)
  3837. return 0; /* already in the bridge */
  3838. wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
  3839. "bridge %s", ifname, in_br);
  3840. if (linux_br_del_if(drv->ioctl_sock, in_br, ifname) < 0) {
  3841. wpa_printf(MSG_ERROR, "nl80211: Failed to "
  3842. "remove interface %s from bridge "
  3843. "%s: %s",
  3844. ifname, brname, strerror(errno));
  3845. return -1;
  3846. }
  3847. }
  3848. wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
  3849. ifname, brname);
  3850. if (linux_br_add_if(drv->ioctl_sock, brname, ifname) < 0) {
  3851. wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
  3852. "into bridge %s: %s",
  3853. ifname, brname, strerror(errno));
  3854. return -1;
  3855. }
  3856. drv->added_if_into_bridge = 1;
  3857. return 0;
  3858. }
  3859. static void *i802_init(struct hostapd_data *hapd,
  3860. struct wpa_init_params *params)
  3861. {
  3862. struct wpa_driver_nl80211_data *drv;
  3863. struct i802_bss *bss;
  3864. size_t i;
  3865. char brname[IFNAMSIZ];
  3866. int ifindex, br_ifindex;
  3867. int br_added = 0;
  3868. bss = wpa_driver_nl80211_init(hapd, params->ifname);
  3869. if (bss == NULL)
  3870. return NULL;
  3871. drv = bss->drv;
  3872. if (linux_br_get(brname, params->ifname) == 0) {
  3873. wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
  3874. params->ifname, brname);
  3875. br_ifindex = if_nametoindex(brname);
  3876. } else {
  3877. brname[0] = '\0';
  3878. br_ifindex = 0;
  3879. }
  3880. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  3881. drv->if_indices = drv->default_if_indices;
  3882. for (i = 0; i < params->num_bridge; i++) {
  3883. if (params->bridge[i]) {
  3884. ifindex = if_nametoindex(params->bridge[i]);
  3885. if (ifindex)
  3886. add_ifidx(drv, ifindex);
  3887. if (ifindex == br_ifindex)
  3888. br_added = 1;
  3889. }
  3890. }
  3891. if (!br_added && br_ifindex &&
  3892. (params->num_bridge == 0 || !params->bridge[0]))
  3893. add_ifidx(drv, br_ifindex);
  3894. /* start listening for EAPOL on the default AP interface */
  3895. add_ifidx(drv, drv->ifindex);
  3896. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0))
  3897. goto failed;
  3898. if (params->bssid) {
  3899. if (linux_set_ifhwaddr(drv->ioctl_sock, bss->ifname,
  3900. params->bssid))
  3901. goto failed;
  3902. }
  3903. if (wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_AP)) {
  3904. wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
  3905. "into AP mode", bss->ifname);
  3906. goto failed;
  3907. }
  3908. if (params->num_bridge && params->bridge[0] &&
  3909. i802_check_bridge(drv, params->bridge[0], params->ifname) < 0)
  3910. goto failed;
  3911. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1))
  3912. goto failed;
  3913. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  3914. if (drv->eapol_sock < 0) {
  3915. perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
  3916. goto failed;
  3917. }
  3918. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  3919. {
  3920. printf("Could not register read socket for eapol\n");
  3921. goto failed;
  3922. }
  3923. if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, params->own_addr))
  3924. goto failed;
  3925. return bss;
  3926. failed:
  3927. nl80211_remove_monitor_interface(drv);
  3928. if (drv->ioctl_sock >= 0)
  3929. close(drv->ioctl_sock);
  3930. genl_family_put(drv->nl80211);
  3931. nl_cache_free(drv->nl_cache);
  3932. nl_handle_destroy(drv->nl_handle);
  3933. nl_cb_put(drv->nl_cb);
  3934. os_free(drv);
  3935. return NULL;
  3936. }
  3937. static void i802_deinit(void *priv)
  3938. {
  3939. wpa_driver_nl80211_deinit(priv);
  3940. }
  3941. #endif /* HOSTAPD */
  3942. static enum nl80211_iftype wpa_driver_nl80211_if_type(
  3943. enum wpa_driver_if_type type)
  3944. {
  3945. switch (type) {
  3946. case WPA_IF_STATION:
  3947. return NL80211_IFTYPE_STATION;
  3948. case WPA_IF_AP_VLAN:
  3949. return NL80211_IFTYPE_AP_VLAN;
  3950. case WPA_IF_AP_BSS:
  3951. return NL80211_IFTYPE_AP;
  3952. }
  3953. return -1;
  3954. }
  3955. static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
  3956. const char *ifname, const u8 *addr,
  3957. void *bss_ctx, void **drv_priv,
  3958. char *force_ifname, u8 *if_addr)
  3959. {
  3960. struct i802_bss *bss = priv;
  3961. struct wpa_driver_nl80211_data *drv = bss->drv;
  3962. int ifidx;
  3963. #ifdef HOSTAPD
  3964. struct i802_bss *new_bss = NULL;
  3965. if (type == WPA_IF_AP_BSS) {
  3966. new_bss = os_zalloc(sizeof(*new_bss));
  3967. if (new_bss == NULL)
  3968. return -1;
  3969. }
  3970. #endif /* HOSTAPD */
  3971. if (addr)
  3972. os_memcpy(if_addr, addr, ETH_ALEN);
  3973. ifidx = nl80211_create_iface(drv, ifname,
  3974. wpa_driver_nl80211_if_type(type), addr,
  3975. 0);
  3976. if (ifidx < 0) {
  3977. #ifdef HOSTAPD
  3978. os_free(new_bss);
  3979. #endif /* HOSTAPD */
  3980. return -1;
  3981. }
  3982. if (!addr &&
  3983. linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, if_addr) < 0)
  3984. return -1;
  3985. #ifdef HOSTAPD
  3986. if (type == WPA_IF_AP_BSS) {
  3987. if (linux_set_iface_flags(drv->ioctl_sock, ifname, 1)) {
  3988. nl80211_remove_iface(drv, ifidx);
  3989. os_free(new_bss);
  3990. return -1;
  3991. }
  3992. os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
  3993. new_bss->ifindex = ifidx;
  3994. new_bss->drv = drv;
  3995. new_bss->next = drv->first_bss.next;
  3996. drv->first_bss.next = new_bss;
  3997. if (drv_priv)
  3998. *drv_priv = new_bss;
  3999. }
  4000. #endif /* HOSTAPD */
  4001. return 0;
  4002. }
  4003. static int wpa_driver_nl80211_if_remove(void *priv,
  4004. enum wpa_driver_if_type type,
  4005. const char *ifname)
  4006. {
  4007. struct i802_bss *bss = priv;
  4008. struct wpa_driver_nl80211_data *drv = bss->drv;
  4009. int ifindex = if_nametoindex(ifname);
  4010. wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
  4011. __func__, type, ifname, ifindex);
  4012. if (ifindex <= 0)
  4013. return -1;
  4014. nl80211_remove_iface(drv, ifindex);
  4015. #ifdef HOSTAPD
  4016. if (type != WPA_IF_AP_BSS)
  4017. return 0;
  4018. if (bss != &drv->first_bss) {
  4019. struct i802_bss *tbss = &drv->first_bss;
  4020. while (tbss) {
  4021. if (tbss->next != bss)
  4022. continue;
  4023. tbss->next = bss->next;
  4024. os_free(bss);
  4025. break;
  4026. }
  4027. }
  4028. #endif /* HOSTAPD */
  4029. return 0;
  4030. }
  4031. static int cookie_handler(struct nl_msg *msg, void *arg)
  4032. {
  4033. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4034. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4035. u64 *cookie = arg;
  4036. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4037. genlmsg_attrlen(gnlh, 0), NULL);
  4038. if (tb[NL80211_ATTR_COOKIE])
  4039. *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  4040. return NL_SKIP;
  4041. }
  4042. static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
  4043. const u8 *dst, const u8 *src,
  4044. const u8 *bssid,
  4045. const u8 *data, size_t data_len)
  4046. {
  4047. struct i802_bss *bss = priv;
  4048. struct wpa_driver_nl80211_data *drv = bss->drv;
  4049. int ret = -1;
  4050. struct nl_msg *msg;
  4051. u8 *buf;
  4052. struct ieee80211_hdr *hdr;
  4053. u64 cookie;
  4054. wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d)",
  4055. drv->ifindex);
  4056. buf = os_zalloc(24 + data_len);
  4057. if (buf == NULL)
  4058. return ret;
  4059. os_memcpy(buf + 24, data, data_len);
  4060. hdr = (struct ieee80211_hdr *) buf;
  4061. hdr->frame_control =
  4062. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
  4063. os_memcpy(hdr->addr1, dst, ETH_ALEN);
  4064. os_memcpy(hdr->addr2, src, ETH_ALEN);
  4065. os_memcpy(hdr->addr3, bssid, ETH_ALEN);
  4066. if (drv->nlmode == NL80211_IFTYPE_AP) {
  4067. ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len);
  4068. os_free(buf);
  4069. return ret;
  4070. }
  4071. msg = nlmsg_alloc();
  4072. if (!msg) {
  4073. os_free(buf);
  4074. return -1;
  4075. }
  4076. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4077. NL80211_CMD_ACTION, 0);
  4078. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4079. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  4080. NLA_PUT(msg, NL80211_ATTR_FRAME, 24 + data_len, buf);
  4081. os_free(buf);
  4082. buf = NULL;
  4083. cookie = 0;
  4084. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  4085. msg = NULL;
  4086. if (ret) {
  4087. wpa_printf(MSG_DEBUG, "nl80211: Action command failed: ret=%d "
  4088. "(%s)", ret, strerror(-ret));
  4089. goto nla_put_failure;
  4090. }
  4091. wpa_printf(MSG_DEBUG, "nl80211: Action TX command accepted; "
  4092. "cookie 0x%llx", (long long unsigned int) cookie);
  4093. drv->send_action_cookie = cookie;
  4094. drv->pending_send_action = 1;
  4095. ret = 0;
  4096. nla_put_failure:
  4097. os_free(buf);
  4098. nlmsg_free(msg);
  4099. return ret;
  4100. }
  4101. static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
  4102. unsigned int duration)
  4103. {
  4104. struct i802_bss *bss = priv;
  4105. struct wpa_driver_nl80211_data *drv = bss->drv;
  4106. struct nl_msg *msg;
  4107. int ret;
  4108. u64 cookie;
  4109. msg = nlmsg_alloc();
  4110. if (!msg)
  4111. return -1;
  4112. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4113. NL80211_CMD_REMAIN_ON_CHANNEL, 0);
  4114. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4115. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  4116. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
  4117. cookie = 0;
  4118. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  4119. if (ret == 0) {
  4120. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
  4121. "0x%llx for freq=%u MHz duration=%u",
  4122. (long long unsigned int) cookie, freq, duration);
  4123. drv->remain_on_chan_cookie = cookie;
  4124. return 0;
  4125. }
  4126. wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
  4127. "(freq=%d): %d (%s)", freq, ret, strerror(-ret));
  4128. nla_put_failure:
  4129. return -1;
  4130. }
  4131. static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
  4132. {
  4133. struct i802_bss *bss = priv;
  4134. struct wpa_driver_nl80211_data *drv = bss->drv;
  4135. struct nl_msg *msg;
  4136. int ret;
  4137. if (!drv->pending_remain_on_chan) {
  4138. wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
  4139. "to cancel");
  4140. return -1;
  4141. }
  4142. wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
  4143. "0x%llx",
  4144. (long long unsigned int) drv->remain_on_chan_cookie);
  4145. msg = nlmsg_alloc();
  4146. if (!msg)
  4147. return -1;
  4148. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4149. NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL, 0);
  4150. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4151. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
  4152. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4153. if (ret == 0)
  4154. return 0;
  4155. wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
  4156. "%d (%s)", ret, strerror(-ret));
  4157. nla_put_failure:
  4158. return -1;
  4159. }
  4160. static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx,
  4161. void *timeout_ctx)
  4162. {
  4163. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  4164. if (drv->monitor_ifidx < 0)
  4165. return; /* monitor interface already removed */
  4166. if (drv->nlmode != NL80211_IFTYPE_STATION)
  4167. return; /* not in station mode anymore */
  4168. if (drv->probe_req_report)
  4169. return; /* reporting enabled */
  4170. wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface due to no "
  4171. "Probe Request reporting needed anymore");
  4172. nl80211_remove_monitor_interface(drv);
  4173. }
  4174. static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
  4175. {
  4176. struct i802_bss *bss = priv;
  4177. struct wpa_driver_nl80211_data *drv = bss->drv;
  4178. if (drv->nlmode != NL80211_IFTYPE_STATION) {
  4179. wpa_printf(MSG_DEBUG, "nl80211: probe_req_report control only "
  4180. "allowed in station mode (iftype=%d)",
  4181. drv->nlmode);
  4182. return -1;
  4183. }
  4184. drv->probe_req_report = report;
  4185. if (report) {
  4186. eloop_cancel_timeout(
  4187. wpa_driver_nl80211_probe_req_report_timeout,
  4188. drv, NULL);
  4189. if (drv->monitor_ifidx < 0 &&
  4190. nl80211_create_monitor_interface(drv))
  4191. return -1;
  4192. } else {
  4193. /*
  4194. * It takes a while to remove the monitor interface, so try to
  4195. * avoid doing this if it is needed again shortly. Instead,
  4196. * schedule the interface to be removed later if no need for it
  4197. * is seen.
  4198. */
  4199. wpa_printf(MSG_DEBUG, "nl80211: Scheduling monitor interface "
  4200. "to be removed after 10 seconds of no use");
  4201. eloop_register_timeout(
  4202. 10, 0, wpa_driver_nl80211_probe_req_report_timeout,
  4203. drv, NULL);
  4204. }
  4205. return 0;
  4206. }
  4207. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  4208. int ifindex, int disabled)
  4209. {
  4210. struct nl_msg *msg;
  4211. struct nlattr *bands, *band;
  4212. int ret;
  4213. msg = nlmsg_alloc();
  4214. if (!msg)
  4215. return -1;
  4216. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4217. NL80211_CMD_SET_TX_BITRATE_MASK, 0);
  4218. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  4219. bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
  4220. if (!bands)
  4221. goto nla_put_failure;
  4222. /*
  4223. * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
  4224. * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
  4225. * rates. All 5 GHz rates are left enabled.
  4226. */
  4227. band = nla_nest_start(msg, NL80211_BAND_2GHZ);
  4228. if (!band)
  4229. goto nla_put_failure;
  4230. NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
  4231. "\x0c\x12\x18\x24\x30\x48\x60\x6c");
  4232. nla_nest_end(msg, band);
  4233. nla_nest_end(msg, bands);
  4234. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4235. msg = NULL;
  4236. if (ret) {
  4237. wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
  4238. "(%s)", ret, strerror(-ret));
  4239. }
  4240. return ret;
  4241. nla_put_failure:
  4242. nlmsg_free(msg);
  4243. return -1;
  4244. }
  4245. static int wpa_driver_nl80211_disable_11b_rates(void *priv, int disabled)
  4246. {
  4247. struct i802_bss *bss = priv;
  4248. struct wpa_driver_nl80211_data *drv = bss->drv;
  4249. drv->disable_11b_rates = disabled;
  4250. return nl80211_disable_11b_rates(drv, drv->ifindex, disabled);
  4251. }
  4252. static int wpa_driver_nl80211_deinit_ap(void *priv)
  4253. {
  4254. struct i802_bss *bss = priv;
  4255. struct wpa_driver_nl80211_data *drv = bss->drv;
  4256. if (drv->nlmode != NL80211_IFTYPE_AP)
  4257. return -1;
  4258. wpa_driver_nl80211_del_beacon(drv);
  4259. return wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA);
  4260. }
  4261. static void wpa_driver_nl80211_resume(void *priv)
  4262. {
  4263. struct i802_bss *bss = priv;
  4264. struct wpa_driver_nl80211_data *drv = bss->drv;
  4265. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) {
  4266. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
  4267. "resume event");
  4268. }
  4269. }
  4270. static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
  4271. const u8 *ies, size_t ies_len)
  4272. {
  4273. struct i802_bss *bss = priv;
  4274. struct wpa_driver_nl80211_data *drv = bss->drv;
  4275. int ret;
  4276. u8 *data, *pos;
  4277. size_t data_len;
  4278. u8 own_addr[ETH_ALEN];
  4279. if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, own_addr) < 0)
  4280. return -1;
  4281. if (action != 1) {
  4282. wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
  4283. "action %d", action);
  4284. return -1;
  4285. }
  4286. /*
  4287. * Action frame payload:
  4288. * Category[1] = 6 (Fast BSS Transition)
  4289. * Action[1] = 1 (Fast BSS Transition Request)
  4290. * STA Address
  4291. * Target AP Address
  4292. * FT IEs
  4293. */
  4294. data_len = 2 + 2 * ETH_ALEN + ies_len;
  4295. data = os_malloc(data_len);
  4296. if (data == NULL)
  4297. return -1;
  4298. pos = data;
  4299. *pos++ = 0x06; /* FT Action category */
  4300. *pos++ = action;
  4301. os_memcpy(pos, own_addr, ETH_ALEN);
  4302. pos += ETH_ALEN;
  4303. os_memcpy(pos, target_ap, ETH_ALEN);
  4304. pos += ETH_ALEN;
  4305. os_memcpy(pos, ies, ies_len);
  4306. ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, drv->bssid,
  4307. own_addr, drv->bssid,
  4308. data, data_len);
  4309. os_free(data);
  4310. return ret;
  4311. }
  4312. static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
  4313. {
  4314. struct i802_bss *bss = priv;
  4315. struct wpa_driver_nl80211_data *drv = bss->drv;
  4316. struct nl_msg *msg, *cqm = NULL;
  4317. wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
  4318. "hysteresis=%d", threshold, hysteresis);
  4319. msg = nlmsg_alloc();
  4320. if (!msg)
  4321. return -1;
  4322. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4323. 0, NL80211_CMD_SET_CQM, 0);
  4324. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  4325. cqm = nlmsg_alloc();
  4326. if (cqm == NULL)
  4327. return -1;
  4328. NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
  4329. NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
  4330. nla_put_nested(msg, NL80211_ATTR_CQM, cqm);
  4331. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  4332. return 0;
  4333. msg = NULL;
  4334. nla_put_failure:
  4335. if (cqm)
  4336. nlmsg_free(cqm);
  4337. nlmsg_free(msg);
  4338. return -1;
  4339. }
  4340. static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
  4341. int encrypt)
  4342. {
  4343. struct i802_bss *bss = priv;
  4344. struct wpa_driver_nl80211_data *drv = bss->drv;
  4345. return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
  4346. }
  4347. const struct wpa_driver_ops wpa_driver_nl80211_ops = {
  4348. .name = "nl80211",
  4349. .desc = "Linux nl80211/cfg80211",
  4350. .get_bssid = wpa_driver_nl80211_get_bssid,
  4351. .get_ssid = wpa_driver_nl80211_get_ssid,
  4352. .set_key = wpa_driver_nl80211_set_key,
  4353. .scan2 = wpa_driver_nl80211_scan,
  4354. .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
  4355. .deauthenticate = wpa_driver_nl80211_deauthenticate,
  4356. .disassociate = wpa_driver_nl80211_disassociate,
  4357. .authenticate = wpa_driver_nl80211_authenticate,
  4358. .associate = wpa_driver_nl80211_associate,
  4359. .init = wpa_driver_nl80211_init,
  4360. .deinit = wpa_driver_nl80211_deinit,
  4361. .get_capa = wpa_driver_nl80211_get_capa,
  4362. .set_operstate = wpa_driver_nl80211_set_operstate,
  4363. .set_supp_port = wpa_driver_nl80211_set_supp_port,
  4364. .set_country = wpa_driver_nl80211_set_country,
  4365. .set_beacon = wpa_driver_nl80211_set_beacon,
  4366. .if_add = wpa_driver_nl80211_if_add,
  4367. .if_remove = wpa_driver_nl80211_if_remove,
  4368. .send_mlme = wpa_driver_nl80211_send_mlme,
  4369. .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
  4370. .sta_add = wpa_driver_nl80211_sta_add,
  4371. .sta_remove = wpa_driver_nl80211_sta_remove,
  4372. .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
  4373. .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
  4374. #ifdef HOSTAPD
  4375. .hapd_init = i802_init,
  4376. .hapd_deinit = i802_deinit,
  4377. .get_seqnum = i802_get_seqnum,
  4378. .flush = i802_flush,
  4379. .read_sta_data = i802_read_sta_data,
  4380. .sta_deauth = i802_sta_deauth,
  4381. .sta_disassoc = i802_sta_disassoc,
  4382. .get_inact_sec = i802_get_inact_sec,
  4383. .sta_clear_stats = i802_sta_clear_stats,
  4384. .set_freq = i802_set_freq,
  4385. .set_rts = i802_set_rts,
  4386. .set_frag = i802_set_frag,
  4387. .set_rate_sets = i802_set_rate_sets,
  4388. .set_cts_protect = i802_set_cts_protect,
  4389. .set_preamble = i802_set_preamble,
  4390. .set_short_slot_time = i802_set_short_slot_time,
  4391. .set_tx_queue_params = i802_set_tx_queue_params,
  4392. .set_sta_vlan = i802_set_sta_vlan,
  4393. .set_wds_sta = i802_set_wds_sta,
  4394. #endif /* HOSTAPD */
  4395. .send_action = wpa_driver_nl80211_send_action,
  4396. .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
  4397. .cancel_remain_on_channel =
  4398. wpa_driver_nl80211_cancel_remain_on_channel,
  4399. .probe_req_report = wpa_driver_nl80211_probe_req_report,
  4400. .disable_11b_rates = wpa_driver_nl80211_disable_11b_rates,
  4401. .deinit_ap = wpa_driver_nl80211_deinit_ap,
  4402. .resume = wpa_driver_nl80211_resume,
  4403. .send_ft_action = nl80211_send_ft_action,
  4404. .signal_monitor = nl80211_signal_monitor,
  4405. .send_frame = nl80211_send_frame,
  4406. };