rndis_wlan.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780
  1. /*
  2. * Driver for RNDIS based wireless USB devices.
  3. *
  4. * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
  5. * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Portions of this file are based on NDISwrapper project,
  22. * Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
  23. * http://ndiswrapper.sourceforge.net/
  24. */
  25. // #define DEBUG // error path messages, extra info
  26. // #define VERBOSE // more; success messages
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/mutex.h>
  34. #include <linux/mii.h>
  35. #include <linux/usb.h>
  36. #include <linux/usb/cdc.h>
  37. #include <linux/ieee80211.h>
  38. #include <linux/if_arp.h>
  39. #include <linux/ctype.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/slab.h>
  42. #include <net/cfg80211.h>
  43. #include <linux/usb/usbnet.h>
  44. #include <linux/usb/rndis_host.h>
  45. /* NOTE: All these are settings for Broadcom chipset */
  46. static char modparam_country[4] = "EU";
  47. module_param_string(country, modparam_country, 4, 0444);
  48. MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
  49. static int modparam_frameburst = 1;
  50. module_param_named(frameburst, modparam_frameburst, int, 0444);
  51. MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
  52. static int modparam_afterburner = 0;
  53. module_param_named(afterburner, modparam_afterburner, int, 0444);
  54. MODULE_PARM_DESC(afterburner,
  55. "enable afterburner aka '125 High Speed Mode' (default: off)");
  56. static int modparam_power_save = 0;
  57. module_param_named(power_save, modparam_power_save, int, 0444);
  58. MODULE_PARM_DESC(power_save,
  59. "set power save mode: 0=off, 1=on, 2=fast (default: off)");
  60. static int modparam_power_output = 3;
  61. module_param_named(power_output, modparam_power_output, int, 0444);
  62. MODULE_PARM_DESC(power_output,
  63. "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
  64. static int modparam_roamtrigger = -70;
  65. module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
  66. MODULE_PARM_DESC(roamtrigger,
  67. "set roaming dBm trigger: -80=optimize for distance, "
  68. "-60=bandwidth (default: -70)");
  69. static int modparam_roamdelta = 1;
  70. module_param_named(roamdelta, modparam_roamdelta, int, 0444);
  71. MODULE_PARM_DESC(roamdelta,
  72. "set roaming tendency: 0=aggressive, 1=moderate, "
  73. "2=conservative (default: moderate)");
  74. static int modparam_workaround_interval;
  75. module_param_named(workaround_interval, modparam_workaround_interval,
  76. int, 0444);
  77. MODULE_PARM_DESC(workaround_interval,
  78. "set stall workaround interval in msecs (0=disabled) (default: 0)");
  79. /* various RNDIS OID defs */
  80. #define OID_GEN_LINK_SPEED cpu_to_le32(0x00010107)
  81. #define OID_GEN_RNDIS_CONFIG_PARAMETER cpu_to_le32(0x0001021b)
  82. #define OID_GEN_XMIT_OK cpu_to_le32(0x00020101)
  83. #define OID_GEN_RCV_OK cpu_to_le32(0x00020102)
  84. #define OID_GEN_XMIT_ERROR cpu_to_le32(0x00020103)
  85. #define OID_GEN_RCV_ERROR cpu_to_le32(0x00020104)
  86. #define OID_GEN_RCV_NO_BUFFER cpu_to_le32(0x00020105)
  87. #define OID_802_3_CURRENT_ADDRESS cpu_to_le32(0x01010102)
  88. #define OID_802_3_MULTICAST_LIST cpu_to_le32(0x01010103)
  89. #define OID_802_3_MAXIMUM_LIST_SIZE cpu_to_le32(0x01010104)
  90. #define OID_802_11_BSSID cpu_to_le32(0x0d010101)
  91. #define OID_802_11_SSID cpu_to_le32(0x0d010102)
  92. #define OID_802_11_INFRASTRUCTURE_MODE cpu_to_le32(0x0d010108)
  93. #define OID_802_11_ADD_WEP cpu_to_le32(0x0d010113)
  94. #define OID_802_11_REMOVE_WEP cpu_to_le32(0x0d010114)
  95. #define OID_802_11_DISASSOCIATE cpu_to_le32(0x0d010115)
  96. #define OID_802_11_AUTHENTICATION_MODE cpu_to_le32(0x0d010118)
  97. #define OID_802_11_PRIVACY_FILTER cpu_to_le32(0x0d010119)
  98. #define OID_802_11_BSSID_LIST_SCAN cpu_to_le32(0x0d01011a)
  99. #define OID_802_11_ENCRYPTION_STATUS cpu_to_le32(0x0d01011b)
  100. #define OID_802_11_ADD_KEY cpu_to_le32(0x0d01011d)
  101. #define OID_802_11_REMOVE_KEY cpu_to_le32(0x0d01011e)
  102. #define OID_802_11_ASSOCIATION_INFORMATION cpu_to_le32(0x0d01011f)
  103. #define OID_802_11_CAPABILITY cpu_to_le32(0x0d010122)
  104. #define OID_802_11_PMKID cpu_to_le32(0x0d010123)
  105. #define OID_802_11_NETWORK_TYPES_SUPPORTED cpu_to_le32(0x0d010203)
  106. #define OID_802_11_NETWORK_TYPE_IN_USE cpu_to_le32(0x0d010204)
  107. #define OID_802_11_TX_POWER_LEVEL cpu_to_le32(0x0d010205)
  108. #define OID_802_11_RSSI cpu_to_le32(0x0d010206)
  109. #define OID_802_11_RSSI_TRIGGER cpu_to_le32(0x0d010207)
  110. #define OID_802_11_FRAGMENTATION_THRESHOLD cpu_to_le32(0x0d010209)
  111. #define OID_802_11_RTS_THRESHOLD cpu_to_le32(0x0d01020a)
  112. #define OID_802_11_SUPPORTED_RATES cpu_to_le32(0x0d01020e)
  113. #define OID_802_11_CONFIGURATION cpu_to_le32(0x0d010211)
  114. #define OID_802_11_POWER_MODE cpu_to_le32(0x0d010216)
  115. #define OID_802_11_BSSID_LIST cpu_to_le32(0x0d010217)
  116. /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
  117. #define WL_NOISE -96 /* typical noise level in dBm */
  118. #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
  119. /* Assume that Broadcom 4320 (only chipset at time of writing known to be
  120. * based on wireless rndis) has default txpower of 13dBm.
  121. * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
  122. * 100% : 20 mW ~ 13dBm
  123. * 75% : 15 mW ~ 12dBm
  124. * 50% : 10 mW ~ 10dBm
  125. * 25% : 5 mW ~ 7dBm
  126. */
  127. #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
  128. #define BCM4320_DEFAULT_TXPOWER_DBM_75 12
  129. #define BCM4320_DEFAULT_TXPOWER_DBM_50 10
  130. #define BCM4320_DEFAULT_TXPOWER_DBM_25 7
  131. /* codes for "status" field of completion messages */
  132. #define RNDIS_STATUS_ADAPTER_NOT_READY cpu_to_le32(0xc0010011)
  133. #define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(0xc0010012)
  134. /* Known device types */
  135. #define RNDIS_UNKNOWN 0
  136. #define RNDIS_BCM4320A 1
  137. #define RNDIS_BCM4320B 2
  138. /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
  139. * slightly modified for datatype endianess, etc
  140. */
  141. #define NDIS_802_11_LENGTH_SSID 32
  142. #define NDIS_802_11_LENGTH_RATES 8
  143. #define NDIS_802_11_LENGTH_RATES_EX 16
  144. enum ndis_80211_net_type {
  145. NDIS_80211_TYPE_FREQ_HOP,
  146. NDIS_80211_TYPE_DIRECT_SEQ,
  147. NDIS_80211_TYPE_OFDM_A,
  148. NDIS_80211_TYPE_OFDM_G
  149. };
  150. enum ndis_80211_net_infra {
  151. NDIS_80211_INFRA_ADHOC,
  152. NDIS_80211_INFRA_INFRA,
  153. NDIS_80211_INFRA_AUTO_UNKNOWN
  154. };
  155. enum ndis_80211_auth_mode {
  156. NDIS_80211_AUTH_OPEN,
  157. NDIS_80211_AUTH_SHARED,
  158. NDIS_80211_AUTH_AUTO_SWITCH,
  159. NDIS_80211_AUTH_WPA,
  160. NDIS_80211_AUTH_WPA_PSK,
  161. NDIS_80211_AUTH_WPA_NONE,
  162. NDIS_80211_AUTH_WPA2,
  163. NDIS_80211_AUTH_WPA2_PSK
  164. };
  165. enum ndis_80211_encr_status {
  166. NDIS_80211_ENCR_WEP_ENABLED,
  167. NDIS_80211_ENCR_DISABLED,
  168. NDIS_80211_ENCR_WEP_KEY_ABSENT,
  169. NDIS_80211_ENCR_NOT_SUPPORTED,
  170. NDIS_80211_ENCR_TKIP_ENABLED,
  171. NDIS_80211_ENCR_TKIP_KEY_ABSENT,
  172. NDIS_80211_ENCR_CCMP_ENABLED,
  173. NDIS_80211_ENCR_CCMP_KEY_ABSENT
  174. };
  175. enum ndis_80211_priv_filter {
  176. NDIS_80211_PRIV_ACCEPT_ALL,
  177. NDIS_80211_PRIV_8021X_WEP
  178. };
  179. enum ndis_80211_status_type {
  180. NDIS_80211_STATUSTYPE_AUTHENTICATION,
  181. NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
  182. NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
  183. NDIS_80211_STATUSTYPE_RADIOSTATE,
  184. };
  185. enum ndis_80211_media_stream_mode {
  186. NDIS_80211_MEDIA_STREAM_OFF,
  187. NDIS_80211_MEDIA_STREAM_ON
  188. };
  189. enum ndis_80211_radio_status {
  190. NDIS_80211_RADIO_STATUS_ON,
  191. NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
  192. NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
  193. };
  194. enum ndis_80211_addkey_bits {
  195. NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
  196. NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
  197. NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
  198. NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  199. };
  200. enum ndis_80211_addwep_bits {
  201. NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
  202. NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  203. };
  204. enum ndis_80211_power_mode {
  205. NDIS_80211_POWER_MODE_CAM,
  206. NDIS_80211_POWER_MODE_MAX_PSP,
  207. NDIS_80211_POWER_MODE_FAST_PSP,
  208. };
  209. enum ndis_80211_pmkid_cand_list_flag_bits {
  210. NDIS_80211_PMKID_CAND_PREAUTH = cpu_to_le32(1 << 0)
  211. };
  212. struct ndis_80211_auth_request {
  213. __le32 length;
  214. u8 bssid[6];
  215. u8 padding[2];
  216. __le32 flags;
  217. } __packed;
  218. struct ndis_80211_pmkid_candidate {
  219. u8 bssid[6];
  220. u8 padding[2];
  221. __le32 flags;
  222. } __packed;
  223. struct ndis_80211_pmkid_cand_list {
  224. __le32 version;
  225. __le32 num_candidates;
  226. struct ndis_80211_pmkid_candidate candidate_list[0];
  227. } __packed;
  228. struct ndis_80211_status_indication {
  229. __le32 status_type;
  230. union {
  231. __le32 media_stream_mode;
  232. __le32 radio_status;
  233. struct ndis_80211_auth_request auth_request[0];
  234. struct ndis_80211_pmkid_cand_list cand_list;
  235. } u;
  236. } __packed;
  237. struct ndis_80211_ssid {
  238. __le32 length;
  239. u8 essid[NDIS_802_11_LENGTH_SSID];
  240. } __packed;
  241. struct ndis_80211_conf_freq_hop {
  242. __le32 length;
  243. __le32 hop_pattern;
  244. __le32 hop_set;
  245. __le32 dwell_time;
  246. } __packed;
  247. struct ndis_80211_conf {
  248. __le32 length;
  249. __le32 beacon_period;
  250. __le32 atim_window;
  251. __le32 ds_config;
  252. struct ndis_80211_conf_freq_hop fh_config;
  253. } __packed;
  254. struct ndis_80211_bssid_ex {
  255. __le32 length;
  256. u8 mac[6];
  257. u8 padding[2];
  258. struct ndis_80211_ssid ssid;
  259. __le32 privacy;
  260. __le32 rssi;
  261. __le32 net_type;
  262. struct ndis_80211_conf config;
  263. __le32 net_infra;
  264. u8 rates[NDIS_802_11_LENGTH_RATES_EX];
  265. __le32 ie_length;
  266. u8 ies[0];
  267. } __packed;
  268. struct ndis_80211_bssid_list_ex {
  269. __le32 num_items;
  270. struct ndis_80211_bssid_ex bssid[0];
  271. } __packed;
  272. struct ndis_80211_fixed_ies {
  273. u8 timestamp[8];
  274. __le16 beacon_interval;
  275. __le16 capabilities;
  276. } __packed;
  277. struct ndis_80211_wep_key {
  278. __le32 size;
  279. __le32 index;
  280. __le32 length;
  281. u8 material[32];
  282. } __packed;
  283. struct ndis_80211_key {
  284. __le32 size;
  285. __le32 index;
  286. __le32 length;
  287. u8 bssid[6];
  288. u8 padding[6];
  289. u8 rsc[8];
  290. u8 material[32];
  291. } __packed;
  292. struct ndis_80211_remove_key {
  293. __le32 size;
  294. __le32 index;
  295. u8 bssid[6];
  296. u8 padding[2];
  297. } __packed;
  298. struct ndis_config_param {
  299. __le32 name_offs;
  300. __le32 name_length;
  301. __le32 type;
  302. __le32 value_offs;
  303. __le32 value_length;
  304. } __packed;
  305. struct ndis_80211_assoc_info {
  306. __le32 length;
  307. __le16 req_ies;
  308. struct req_ie {
  309. __le16 capa;
  310. __le16 listen_interval;
  311. u8 cur_ap_address[6];
  312. } req_ie;
  313. __le32 req_ie_length;
  314. __le32 offset_req_ies;
  315. __le16 resp_ies;
  316. struct resp_ie {
  317. __le16 capa;
  318. __le16 status_code;
  319. __le16 assoc_id;
  320. } resp_ie;
  321. __le32 resp_ie_length;
  322. __le32 offset_resp_ies;
  323. } __packed;
  324. struct ndis_80211_auth_encr_pair {
  325. __le32 auth_mode;
  326. __le32 encr_mode;
  327. } __packed;
  328. struct ndis_80211_capability {
  329. __le32 length;
  330. __le32 version;
  331. __le32 num_pmkids;
  332. __le32 num_auth_encr_pair;
  333. struct ndis_80211_auth_encr_pair auth_encr_pair[0];
  334. } __packed;
  335. struct ndis_80211_bssid_info {
  336. u8 bssid[6];
  337. u8 pmkid[16];
  338. } __packed;
  339. struct ndis_80211_pmkid {
  340. __le32 length;
  341. __le32 bssid_info_count;
  342. struct ndis_80211_bssid_info bssid_info[0];
  343. } __packed;
  344. /*
  345. * private data
  346. */
  347. #define CAP_MODE_80211A 1
  348. #define CAP_MODE_80211B 2
  349. #define CAP_MODE_80211G 4
  350. #define CAP_MODE_MASK 7
  351. #define WORK_LINK_UP (1<<0)
  352. #define WORK_LINK_DOWN (1<<1)
  353. #define WORK_SET_MULTICAST_LIST (1<<2)
  354. #define RNDIS_WLAN_ALG_NONE 0
  355. #define RNDIS_WLAN_ALG_WEP (1<<0)
  356. #define RNDIS_WLAN_ALG_TKIP (1<<1)
  357. #define RNDIS_WLAN_ALG_CCMP (1<<2)
  358. #define RNDIS_WLAN_NUM_KEYS 4
  359. #define RNDIS_WLAN_KEY_MGMT_NONE 0
  360. #define RNDIS_WLAN_KEY_MGMT_802_1X (1<<0)
  361. #define RNDIS_WLAN_KEY_MGMT_PSK (1<<1)
  362. #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
  363. static const struct ieee80211_channel rndis_channels[] = {
  364. { .center_freq = 2412 },
  365. { .center_freq = 2417 },
  366. { .center_freq = 2422 },
  367. { .center_freq = 2427 },
  368. { .center_freq = 2432 },
  369. { .center_freq = 2437 },
  370. { .center_freq = 2442 },
  371. { .center_freq = 2447 },
  372. { .center_freq = 2452 },
  373. { .center_freq = 2457 },
  374. { .center_freq = 2462 },
  375. { .center_freq = 2467 },
  376. { .center_freq = 2472 },
  377. { .center_freq = 2484 },
  378. };
  379. static const struct ieee80211_rate rndis_rates[] = {
  380. { .bitrate = 10 },
  381. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  382. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  383. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  384. { .bitrate = 60 },
  385. { .bitrate = 90 },
  386. { .bitrate = 120 },
  387. { .bitrate = 180 },
  388. { .bitrate = 240 },
  389. { .bitrate = 360 },
  390. { .bitrate = 480 },
  391. { .bitrate = 540 }
  392. };
  393. static const u32 rndis_cipher_suites[] = {
  394. WLAN_CIPHER_SUITE_WEP40,
  395. WLAN_CIPHER_SUITE_WEP104,
  396. WLAN_CIPHER_SUITE_TKIP,
  397. WLAN_CIPHER_SUITE_CCMP,
  398. };
  399. struct rndis_wlan_encr_key {
  400. int len;
  401. u32 cipher;
  402. u8 material[32];
  403. u8 bssid[ETH_ALEN];
  404. bool pairwise;
  405. bool tx_key;
  406. };
  407. /* RNDIS device private data */
  408. struct rndis_wlan_private {
  409. struct usbnet *usbdev;
  410. struct wireless_dev wdev;
  411. struct cfg80211_scan_request *scan_request;
  412. struct workqueue_struct *workqueue;
  413. struct delayed_work dev_poller_work;
  414. struct delayed_work scan_work;
  415. struct work_struct work;
  416. struct mutex command_lock;
  417. unsigned long work_pending;
  418. int last_qual;
  419. s32 cqm_rssi_thold;
  420. u32 cqm_rssi_hyst;
  421. int last_cqm_event_rssi;
  422. struct ieee80211_supported_band band;
  423. struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
  424. struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
  425. u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
  426. int device_type;
  427. int caps;
  428. int multicast_size;
  429. /* module parameters */
  430. char param_country[4];
  431. int param_frameburst;
  432. int param_afterburner;
  433. int param_power_save;
  434. int param_power_output;
  435. int param_roamtrigger;
  436. int param_roamdelta;
  437. u32 param_workaround_interval;
  438. /* hardware state */
  439. bool radio_on;
  440. int power_mode;
  441. int infra_mode;
  442. bool connected;
  443. u8 bssid[ETH_ALEN];
  444. __le32 current_command_oid;
  445. /* encryption stuff */
  446. int encr_tx_key_index;
  447. struct rndis_wlan_encr_key encr_keys[RNDIS_WLAN_NUM_KEYS];
  448. int wpa_version;
  449. u8 command_buffer[COMMAND_BUFFER_SIZE];
  450. };
  451. /*
  452. * cfg80211 ops
  453. */
  454. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  455. struct net_device *dev,
  456. enum nl80211_iftype type, u32 *flags,
  457. struct vif_params *params);
  458. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  459. struct cfg80211_scan_request *request);
  460. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
  461. static int rndis_set_tx_power(struct wiphy *wiphy,
  462. enum nl80211_tx_power_setting type,
  463. int mbm);
  464. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
  465. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  466. struct cfg80211_connect_params *sme);
  467. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  468. u16 reason_code);
  469. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  470. struct cfg80211_ibss_params *params);
  471. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
  472. static int rndis_set_channel(struct wiphy *wiphy, struct net_device *dev,
  473. struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
  474. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  475. u8 key_index, bool pairwise, const u8 *mac_addr,
  476. struct key_params *params);
  477. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  478. u8 key_index, bool pairwise, const u8 *mac_addr);
  479. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  480. u8 key_index, bool unicast, bool multicast);
  481. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  482. u8 *mac, struct station_info *sinfo);
  483. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  484. int idx, u8 *mac, struct station_info *sinfo);
  485. static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  486. struct cfg80211_pmksa *pmksa);
  487. static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  488. struct cfg80211_pmksa *pmksa);
  489. static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
  490. static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
  491. bool enabled, int timeout);
  492. static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
  493. struct net_device *dev,
  494. s32 rssi_thold, u32 rssi_hyst);
  495. static const struct cfg80211_ops rndis_config_ops = {
  496. .change_virtual_intf = rndis_change_virtual_intf,
  497. .scan = rndis_scan,
  498. .set_wiphy_params = rndis_set_wiphy_params,
  499. .set_tx_power = rndis_set_tx_power,
  500. .get_tx_power = rndis_get_tx_power,
  501. .connect = rndis_connect,
  502. .disconnect = rndis_disconnect,
  503. .join_ibss = rndis_join_ibss,
  504. .leave_ibss = rndis_leave_ibss,
  505. .set_channel = rndis_set_channel,
  506. .add_key = rndis_add_key,
  507. .del_key = rndis_del_key,
  508. .set_default_key = rndis_set_default_key,
  509. .get_station = rndis_get_station,
  510. .dump_station = rndis_dump_station,
  511. .set_pmksa = rndis_set_pmksa,
  512. .del_pmksa = rndis_del_pmksa,
  513. .flush_pmksa = rndis_flush_pmksa,
  514. .set_power_mgmt = rndis_set_power_mgmt,
  515. .set_cqm_rssi_config = rndis_set_cqm_rssi_config,
  516. };
  517. static void *rndis_wiphy_privid = &rndis_wiphy_privid;
  518. static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
  519. {
  520. return (struct rndis_wlan_private *)dev->driver_priv;
  521. }
  522. static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
  523. {
  524. switch (priv->param_power_output) {
  525. default:
  526. case 3:
  527. return BCM4320_DEFAULT_TXPOWER_DBM_100;
  528. case 2:
  529. return BCM4320_DEFAULT_TXPOWER_DBM_75;
  530. case 1:
  531. return BCM4320_DEFAULT_TXPOWER_DBM_50;
  532. case 0:
  533. return BCM4320_DEFAULT_TXPOWER_DBM_25;
  534. }
  535. }
  536. static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
  537. {
  538. int cipher = priv->encr_keys[idx].cipher;
  539. return (cipher == WLAN_CIPHER_SUITE_CCMP ||
  540. cipher == WLAN_CIPHER_SUITE_TKIP);
  541. }
  542. static int rndis_cipher_to_alg(u32 cipher)
  543. {
  544. switch (cipher) {
  545. default:
  546. return RNDIS_WLAN_ALG_NONE;
  547. case WLAN_CIPHER_SUITE_WEP40:
  548. case WLAN_CIPHER_SUITE_WEP104:
  549. return RNDIS_WLAN_ALG_WEP;
  550. case WLAN_CIPHER_SUITE_TKIP:
  551. return RNDIS_WLAN_ALG_TKIP;
  552. case WLAN_CIPHER_SUITE_CCMP:
  553. return RNDIS_WLAN_ALG_CCMP;
  554. }
  555. }
  556. static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
  557. {
  558. switch (akm_suite) {
  559. default:
  560. return RNDIS_WLAN_KEY_MGMT_NONE;
  561. case WLAN_AKM_SUITE_8021X:
  562. return RNDIS_WLAN_KEY_MGMT_802_1X;
  563. case WLAN_AKM_SUITE_PSK:
  564. return RNDIS_WLAN_KEY_MGMT_PSK;
  565. }
  566. }
  567. #ifdef DEBUG
  568. static const char *oid_to_string(__le32 oid)
  569. {
  570. switch (oid) {
  571. #define OID_STR(oid) case oid: return(#oid)
  572. /* from rndis_host.h */
  573. OID_STR(OID_802_3_PERMANENT_ADDRESS);
  574. OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
  575. OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
  576. OID_STR(OID_GEN_PHYSICAL_MEDIUM);
  577. /* from rndis_wlan.c */
  578. OID_STR(OID_GEN_LINK_SPEED);
  579. OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
  580. OID_STR(OID_GEN_XMIT_OK);
  581. OID_STR(OID_GEN_RCV_OK);
  582. OID_STR(OID_GEN_XMIT_ERROR);
  583. OID_STR(OID_GEN_RCV_ERROR);
  584. OID_STR(OID_GEN_RCV_NO_BUFFER);
  585. OID_STR(OID_802_3_CURRENT_ADDRESS);
  586. OID_STR(OID_802_3_MULTICAST_LIST);
  587. OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
  588. OID_STR(OID_802_11_BSSID);
  589. OID_STR(OID_802_11_SSID);
  590. OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
  591. OID_STR(OID_802_11_ADD_WEP);
  592. OID_STR(OID_802_11_REMOVE_WEP);
  593. OID_STR(OID_802_11_DISASSOCIATE);
  594. OID_STR(OID_802_11_AUTHENTICATION_MODE);
  595. OID_STR(OID_802_11_PRIVACY_FILTER);
  596. OID_STR(OID_802_11_BSSID_LIST_SCAN);
  597. OID_STR(OID_802_11_ENCRYPTION_STATUS);
  598. OID_STR(OID_802_11_ADD_KEY);
  599. OID_STR(OID_802_11_REMOVE_KEY);
  600. OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
  601. OID_STR(OID_802_11_CAPABILITY);
  602. OID_STR(OID_802_11_PMKID);
  603. OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
  604. OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
  605. OID_STR(OID_802_11_TX_POWER_LEVEL);
  606. OID_STR(OID_802_11_RSSI);
  607. OID_STR(OID_802_11_RSSI_TRIGGER);
  608. OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
  609. OID_STR(OID_802_11_RTS_THRESHOLD);
  610. OID_STR(OID_802_11_SUPPORTED_RATES);
  611. OID_STR(OID_802_11_CONFIGURATION);
  612. OID_STR(OID_802_11_POWER_MODE);
  613. OID_STR(OID_802_11_BSSID_LIST);
  614. #undef OID_STR
  615. }
  616. return "?";
  617. }
  618. #else
  619. static const char *oid_to_string(__le32 oid)
  620. {
  621. return "?";
  622. }
  623. #endif
  624. /* translate error code */
  625. static int rndis_error_status(__le32 rndis_status)
  626. {
  627. int ret = -EINVAL;
  628. switch (rndis_status) {
  629. case RNDIS_STATUS_SUCCESS:
  630. ret = 0;
  631. break;
  632. case RNDIS_STATUS_FAILURE:
  633. case RNDIS_STATUS_INVALID_DATA:
  634. ret = -EINVAL;
  635. break;
  636. case RNDIS_STATUS_NOT_SUPPORTED:
  637. ret = -EOPNOTSUPP;
  638. break;
  639. case RNDIS_STATUS_ADAPTER_NOT_READY:
  640. case RNDIS_STATUS_ADAPTER_NOT_OPEN:
  641. ret = -EBUSY;
  642. break;
  643. }
  644. return ret;
  645. }
  646. static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
  647. {
  648. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  649. union {
  650. void *buf;
  651. struct rndis_msg_hdr *header;
  652. struct rndis_query *get;
  653. struct rndis_query_c *get_c;
  654. } u;
  655. int ret, buflen;
  656. int resplen, respoffs, copylen;
  657. buflen = *len + sizeof(*u.get);
  658. if (buflen < CONTROL_BUFFER_SIZE)
  659. buflen = CONTROL_BUFFER_SIZE;
  660. if (buflen > COMMAND_BUFFER_SIZE) {
  661. u.buf = kmalloc(buflen, GFP_KERNEL);
  662. if (!u.buf)
  663. return -ENOMEM;
  664. } else {
  665. u.buf = priv->command_buffer;
  666. }
  667. mutex_lock(&priv->command_lock);
  668. memset(u.get, 0, sizeof *u.get);
  669. u.get->msg_type = RNDIS_MSG_QUERY;
  670. u.get->msg_len = cpu_to_le32(sizeof *u.get);
  671. u.get->oid = oid;
  672. priv->current_command_oid = oid;
  673. ret = rndis_command(dev, u.header, buflen);
  674. priv->current_command_oid = 0;
  675. if (ret < 0)
  676. netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
  677. __func__, oid_to_string(oid), ret,
  678. le32_to_cpu(u.get_c->status));
  679. if (ret == 0) {
  680. resplen = le32_to_cpu(u.get_c->len);
  681. respoffs = le32_to_cpu(u.get_c->offset) + 8;
  682. if (respoffs > buflen) {
  683. /* Device returned data offset outside buffer, error. */
  684. netdev_dbg(dev->net, "%s(%s): received invalid "
  685. "data offset: %d > %d\n", __func__,
  686. oid_to_string(oid), respoffs, buflen);
  687. ret = -EINVAL;
  688. goto exit_unlock;
  689. }
  690. if ((resplen + respoffs) > buflen) {
  691. /* Device would have returned more data if buffer would
  692. * have been big enough. Copy just the bits that we got.
  693. */
  694. copylen = buflen - respoffs;
  695. } else {
  696. copylen = resplen;
  697. }
  698. if (copylen > *len)
  699. copylen = *len;
  700. memcpy(data, u.buf + respoffs, copylen);
  701. *len = resplen;
  702. ret = rndis_error_status(u.get_c->status);
  703. if (ret < 0)
  704. netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
  705. __func__, oid_to_string(oid),
  706. le32_to_cpu(u.get_c->status), ret);
  707. }
  708. exit_unlock:
  709. mutex_unlock(&priv->command_lock);
  710. if (u.buf != priv->command_buffer)
  711. kfree(u.buf);
  712. return ret;
  713. }
  714. static int rndis_set_oid(struct usbnet *dev, __le32 oid, const void *data,
  715. int len)
  716. {
  717. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  718. union {
  719. void *buf;
  720. struct rndis_msg_hdr *header;
  721. struct rndis_set *set;
  722. struct rndis_set_c *set_c;
  723. } u;
  724. int ret, buflen;
  725. buflen = len + sizeof(*u.set);
  726. if (buflen < CONTROL_BUFFER_SIZE)
  727. buflen = CONTROL_BUFFER_SIZE;
  728. if (buflen > COMMAND_BUFFER_SIZE) {
  729. u.buf = kmalloc(buflen, GFP_KERNEL);
  730. if (!u.buf)
  731. return -ENOMEM;
  732. } else {
  733. u.buf = priv->command_buffer;
  734. }
  735. mutex_lock(&priv->command_lock);
  736. memset(u.set, 0, sizeof *u.set);
  737. u.set->msg_type = RNDIS_MSG_SET;
  738. u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
  739. u.set->oid = oid;
  740. u.set->len = cpu_to_le32(len);
  741. u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
  742. u.set->handle = cpu_to_le32(0);
  743. memcpy(u.buf + sizeof(*u.set), data, len);
  744. priv->current_command_oid = oid;
  745. ret = rndis_command(dev, u.header, buflen);
  746. priv->current_command_oid = 0;
  747. if (ret < 0)
  748. netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
  749. __func__, oid_to_string(oid), ret,
  750. le32_to_cpu(u.set_c->status));
  751. if (ret == 0) {
  752. ret = rndis_error_status(u.set_c->status);
  753. if (ret < 0)
  754. netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
  755. __func__, oid_to_string(oid),
  756. le32_to_cpu(u.set_c->status), ret);
  757. }
  758. mutex_unlock(&priv->command_lock);
  759. if (u.buf != priv->command_buffer)
  760. kfree(u.buf);
  761. return ret;
  762. }
  763. static int rndis_reset(struct usbnet *usbdev)
  764. {
  765. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  766. struct rndis_reset *reset;
  767. int ret;
  768. mutex_lock(&priv->command_lock);
  769. reset = (void *)priv->command_buffer;
  770. memset(reset, 0, sizeof(*reset));
  771. reset->msg_type = RNDIS_MSG_RESET;
  772. reset->msg_len = cpu_to_le32(sizeof(*reset));
  773. priv->current_command_oid = 0;
  774. ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
  775. mutex_unlock(&priv->command_lock);
  776. if (ret < 0)
  777. return ret;
  778. return 0;
  779. }
  780. /*
  781. * Specs say that we can only set config parameters only soon after device
  782. * initialization.
  783. * value_type: 0 = u32, 2 = unicode string
  784. */
  785. static int rndis_set_config_parameter(struct usbnet *dev, char *param,
  786. int value_type, void *value)
  787. {
  788. struct ndis_config_param *infobuf;
  789. int value_len, info_len, param_len, ret, i;
  790. __le16 *unibuf;
  791. __le32 *dst_value;
  792. if (value_type == 0)
  793. value_len = sizeof(__le32);
  794. else if (value_type == 2)
  795. value_len = strlen(value) * sizeof(__le16);
  796. else
  797. return -EINVAL;
  798. param_len = strlen(param) * sizeof(__le16);
  799. info_len = sizeof(*infobuf) + param_len + value_len;
  800. #ifdef DEBUG
  801. info_len += 12;
  802. #endif
  803. infobuf = kmalloc(info_len, GFP_KERNEL);
  804. if (!infobuf)
  805. return -ENOMEM;
  806. #ifdef DEBUG
  807. info_len -= 12;
  808. /* extra 12 bytes are for padding (debug output) */
  809. memset(infobuf, 0xCC, info_len + 12);
  810. #endif
  811. if (value_type == 2)
  812. netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
  813. param, (u8 *)value);
  814. else
  815. netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
  816. param, *(u32 *)value);
  817. infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
  818. infobuf->name_length = cpu_to_le32(param_len);
  819. infobuf->type = cpu_to_le32(value_type);
  820. infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
  821. infobuf->value_length = cpu_to_le32(value_len);
  822. /* simple string to unicode string conversion */
  823. unibuf = (void *)infobuf + sizeof(*infobuf);
  824. for (i = 0; i < param_len / sizeof(__le16); i++)
  825. unibuf[i] = cpu_to_le16(param[i]);
  826. if (value_type == 2) {
  827. unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
  828. for (i = 0; i < value_len / sizeof(__le16); i++)
  829. unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
  830. } else {
  831. dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
  832. *dst_value = cpu_to_le32(*(u32 *)value);
  833. }
  834. #ifdef DEBUG
  835. netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
  836. for (i = 0; i < info_len; i += 12) {
  837. u32 *tmp = (u32 *)((u8 *)infobuf + i);
  838. netdev_dbg(dev->net, "%08X:%08X:%08X\n",
  839. cpu_to_be32(tmp[0]),
  840. cpu_to_be32(tmp[1]),
  841. cpu_to_be32(tmp[2]));
  842. }
  843. #endif
  844. ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
  845. infobuf, info_len);
  846. if (ret != 0)
  847. netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
  848. ret);
  849. kfree(infobuf);
  850. return ret;
  851. }
  852. static int rndis_set_config_parameter_str(struct usbnet *dev,
  853. char *param, char *value)
  854. {
  855. return rndis_set_config_parameter(dev, param, 2, value);
  856. }
  857. /*
  858. * data conversion functions
  859. */
  860. static int level_to_qual(int level)
  861. {
  862. int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
  863. return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
  864. }
  865. /*
  866. * common functions
  867. */
  868. static int set_infra_mode(struct usbnet *usbdev, int mode);
  869. static void restore_keys(struct usbnet *usbdev);
  870. static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
  871. bool *matched);
  872. static int rndis_start_bssid_list_scan(struct usbnet *usbdev)
  873. {
  874. __le32 tmp;
  875. /* Note: OID_802_11_BSSID_LIST_SCAN clears internal BSS list. */
  876. tmp = cpu_to_le32(1);
  877. return rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  878. sizeof(tmp));
  879. }
  880. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  881. {
  882. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  883. int ret;
  884. ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
  885. if (ret < 0) {
  886. netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
  887. return ret;
  888. }
  889. if (ret == 0) {
  890. priv->radio_on = true;
  891. netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
  892. }
  893. return ret;
  894. }
  895. static int set_bssid(struct usbnet *usbdev, const u8 *bssid)
  896. {
  897. int ret;
  898. ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
  899. if (ret < 0) {
  900. netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
  901. bssid, ret);
  902. return ret;
  903. }
  904. return ret;
  905. }
  906. static int clear_bssid(struct usbnet *usbdev)
  907. {
  908. static const u8 broadcast_mac[ETH_ALEN] = {
  909. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  910. };
  911. return set_bssid(usbdev, broadcast_mac);
  912. }
  913. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  914. {
  915. int ret, len;
  916. len = ETH_ALEN;
  917. ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
  918. if (ret != 0)
  919. memset(bssid, 0, ETH_ALEN);
  920. return ret;
  921. }
  922. static int get_association_info(struct usbnet *usbdev,
  923. struct ndis_80211_assoc_info *info, int len)
  924. {
  925. return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
  926. info, &len);
  927. }
  928. static bool is_associated(struct usbnet *usbdev)
  929. {
  930. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  931. u8 bssid[ETH_ALEN];
  932. int ret;
  933. if (!priv->radio_on)
  934. return false;
  935. ret = get_bssid(usbdev, bssid);
  936. return (ret == 0 && !is_zero_ether_addr(bssid));
  937. }
  938. static int disassociate(struct usbnet *usbdev, bool reset_ssid)
  939. {
  940. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  941. struct ndis_80211_ssid ssid;
  942. int i, ret = 0;
  943. if (priv->radio_on) {
  944. ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
  945. if (ret == 0) {
  946. priv->radio_on = false;
  947. netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
  948. __func__);
  949. if (reset_ssid)
  950. msleep(100);
  951. }
  952. }
  953. /* disassociate causes radio to be turned off; if reset_ssid
  954. * is given, set random ssid to enable radio */
  955. if (reset_ssid) {
  956. /* Set device to infrastructure mode so we don't get ad-hoc
  957. * 'media connect' indications with the random ssid.
  958. */
  959. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  960. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  961. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  962. ssid.essid[0] = 0x1;
  963. ssid.essid[1] = 0xff;
  964. for (i = 2; i < sizeof(ssid.essid); i++)
  965. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  966. ret = set_essid(usbdev, &ssid);
  967. }
  968. return ret;
  969. }
  970. static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
  971. enum nl80211_auth_type auth_type, int keymgmt)
  972. {
  973. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  974. __le32 tmp;
  975. int auth_mode, ret;
  976. netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
  977. __func__, wpa_version, auth_type, keymgmt);
  978. if (wpa_version & NL80211_WPA_VERSION_2) {
  979. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  980. auth_mode = NDIS_80211_AUTH_WPA2;
  981. else
  982. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  983. } else if (wpa_version & NL80211_WPA_VERSION_1) {
  984. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  985. auth_mode = NDIS_80211_AUTH_WPA;
  986. else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
  987. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  988. else
  989. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  990. } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  991. auth_mode = NDIS_80211_AUTH_SHARED;
  992. else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
  993. auth_mode = NDIS_80211_AUTH_OPEN;
  994. else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
  995. auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
  996. else
  997. return -ENOTSUPP;
  998. tmp = cpu_to_le32(auth_mode);
  999. ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
  1000. sizeof(tmp));
  1001. if (ret != 0) {
  1002. netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
  1003. ret);
  1004. return ret;
  1005. }
  1006. priv->wpa_version = wpa_version;
  1007. return 0;
  1008. }
  1009. static int set_priv_filter(struct usbnet *usbdev)
  1010. {
  1011. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1012. __le32 tmp;
  1013. netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
  1014. __func__, priv->wpa_version);
  1015. if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
  1016. priv->wpa_version & NL80211_WPA_VERSION_1)
  1017. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  1018. else
  1019. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  1020. return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
  1021. sizeof(tmp));
  1022. }
  1023. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  1024. {
  1025. __le32 tmp;
  1026. int encr_mode, ret;
  1027. netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
  1028. __func__, pairwise, groupwise);
  1029. if (pairwise & RNDIS_WLAN_ALG_CCMP)
  1030. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  1031. else if (pairwise & RNDIS_WLAN_ALG_TKIP)
  1032. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  1033. else if (pairwise & RNDIS_WLAN_ALG_WEP)
  1034. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  1035. else if (groupwise & RNDIS_WLAN_ALG_CCMP)
  1036. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  1037. else if (groupwise & RNDIS_WLAN_ALG_TKIP)
  1038. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  1039. else
  1040. encr_mode = NDIS_80211_ENCR_DISABLED;
  1041. tmp = cpu_to_le32(encr_mode);
  1042. ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
  1043. sizeof(tmp));
  1044. if (ret != 0) {
  1045. netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
  1046. ret);
  1047. return ret;
  1048. }
  1049. return 0;
  1050. }
  1051. static int set_infra_mode(struct usbnet *usbdev, int mode)
  1052. {
  1053. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1054. __le32 tmp;
  1055. int ret;
  1056. netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
  1057. __func__, priv->infra_mode);
  1058. tmp = cpu_to_le32(mode);
  1059. ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
  1060. sizeof(tmp));
  1061. if (ret != 0) {
  1062. netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
  1063. ret);
  1064. return ret;
  1065. }
  1066. /* NDIS drivers clear keys when infrastructure mode is
  1067. * changed. But Linux tools assume otherwise. So set the
  1068. * keys */
  1069. restore_keys(usbdev);
  1070. priv->infra_mode = mode;
  1071. return 0;
  1072. }
  1073. static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
  1074. {
  1075. __le32 tmp;
  1076. netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
  1077. if (rts_threshold < 0 || rts_threshold > 2347)
  1078. rts_threshold = 2347;
  1079. tmp = cpu_to_le32(rts_threshold);
  1080. return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
  1081. sizeof(tmp));
  1082. }
  1083. static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
  1084. {
  1085. __le32 tmp;
  1086. netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
  1087. if (frag_threshold < 256 || frag_threshold > 2346)
  1088. frag_threshold = 2346;
  1089. tmp = cpu_to_le32(frag_threshold);
  1090. return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
  1091. sizeof(tmp));
  1092. }
  1093. static void set_default_iw_params(struct usbnet *usbdev)
  1094. {
  1095. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1096. set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
  1097. RNDIS_WLAN_KEY_MGMT_NONE);
  1098. set_priv_filter(usbdev);
  1099. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1100. }
  1101. static int deauthenticate(struct usbnet *usbdev)
  1102. {
  1103. int ret;
  1104. ret = disassociate(usbdev, true);
  1105. set_default_iw_params(usbdev);
  1106. return ret;
  1107. }
  1108. static int set_channel(struct usbnet *usbdev, int channel)
  1109. {
  1110. struct ndis_80211_conf config;
  1111. unsigned int dsconfig;
  1112. int len, ret;
  1113. netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
  1114. /* this OID is valid only when not associated */
  1115. if (is_associated(usbdev))
  1116. return 0;
  1117. dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
  1118. len = sizeof(config);
  1119. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1120. if (ret < 0) {
  1121. netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
  1122. __func__);
  1123. return ret;
  1124. }
  1125. config.ds_config = cpu_to_le32(dsconfig);
  1126. ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
  1127. sizeof(config));
  1128. netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
  1129. return ret;
  1130. }
  1131. static struct ieee80211_channel *get_current_channel(struct usbnet *usbdev,
  1132. u16 *beacon_interval)
  1133. {
  1134. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1135. struct ieee80211_channel *channel;
  1136. struct ndis_80211_conf config;
  1137. int len, ret;
  1138. /* Get channel and beacon interval */
  1139. len = sizeof(config);
  1140. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1141. netdev_dbg(usbdev->net, "%s(): OID_802_11_CONFIGURATION -> %d\n",
  1142. __func__, ret);
  1143. if (ret < 0)
  1144. return NULL;
  1145. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1146. KHZ_TO_MHZ(le32_to_cpu(config.ds_config)));
  1147. if (!channel)
  1148. return NULL;
  1149. if (beacon_interval)
  1150. *beacon_interval = le16_to_cpu(config.beacon_period);
  1151. return channel;
  1152. }
  1153. /* index must be 0 - N, as per NDIS */
  1154. static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1155. int index)
  1156. {
  1157. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1158. struct ndis_80211_wep_key ndis_key;
  1159. u32 cipher;
  1160. int ret;
  1161. netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
  1162. __func__, index, key_len);
  1163. if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
  1164. return -EINVAL;
  1165. if (key_len == 5)
  1166. cipher = WLAN_CIPHER_SUITE_WEP40;
  1167. else
  1168. cipher = WLAN_CIPHER_SUITE_WEP104;
  1169. memset(&ndis_key, 0, sizeof(ndis_key));
  1170. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1171. ndis_key.length = cpu_to_le32(key_len);
  1172. ndis_key.index = cpu_to_le32(index);
  1173. memcpy(&ndis_key.material, key, key_len);
  1174. if (index == priv->encr_tx_key_index) {
  1175. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1176. ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
  1177. RNDIS_WLAN_ALG_NONE);
  1178. if (ret)
  1179. netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
  1180. ret);
  1181. }
  1182. ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
  1183. sizeof(ndis_key));
  1184. if (ret != 0) {
  1185. netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
  1186. index + 1, ret);
  1187. return ret;
  1188. }
  1189. priv->encr_keys[index].len = key_len;
  1190. priv->encr_keys[index].cipher = cipher;
  1191. memcpy(&priv->encr_keys[index].material, key, key_len);
  1192. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1193. return 0;
  1194. }
  1195. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1196. int index, const u8 *addr, const u8 *rx_seq,
  1197. int seq_len, u32 cipher, __le32 flags)
  1198. {
  1199. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1200. struct ndis_80211_key ndis_key;
  1201. bool is_addr_ok;
  1202. int ret;
  1203. if (index < 0 || index >= 4) {
  1204. netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
  1205. __func__, index);
  1206. return -EINVAL;
  1207. }
  1208. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1209. netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
  1210. __func__, key_len);
  1211. return -EINVAL;
  1212. }
  1213. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
  1214. if (!rx_seq || seq_len <= 0) {
  1215. netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
  1216. __func__);
  1217. return -EINVAL;
  1218. }
  1219. if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
  1220. netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
  1221. return -EINVAL;
  1222. }
  1223. }
  1224. is_addr_ok = addr && !is_zero_ether_addr(addr) &&
  1225. !is_broadcast_ether_addr(addr);
  1226. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1227. netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
  1228. __func__, addr);
  1229. return -EINVAL;
  1230. }
  1231. netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
  1232. __func__, index,
  1233. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1234. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1235. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1236. memset(&ndis_key, 0, sizeof(ndis_key));
  1237. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1238. sizeof(ndis_key.material) + key_len);
  1239. ndis_key.length = cpu_to_le32(key_len);
  1240. ndis_key.index = cpu_to_le32(index) | flags;
  1241. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1242. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1243. * different order than NDIS spec, so swap the order here. */
  1244. memcpy(ndis_key.material, key, 16);
  1245. memcpy(ndis_key.material + 16, key + 24, 8);
  1246. memcpy(ndis_key.material + 24, key + 16, 8);
  1247. } else
  1248. memcpy(ndis_key.material, key, key_len);
  1249. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1250. memcpy(ndis_key.rsc, rx_seq, seq_len);
  1251. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1252. /* pairwise key */
  1253. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1254. } else {
  1255. /* group key */
  1256. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1257. memset(ndis_key.bssid, 0xff, ETH_ALEN);
  1258. else
  1259. get_bssid(usbdev, ndis_key.bssid);
  1260. }
  1261. ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
  1262. le32_to_cpu(ndis_key.size));
  1263. netdev_dbg(usbdev->net, "%s(): OID_802_11_ADD_KEY -> %08X\n",
  1264. __func__, ret);
  1265. if (ret != 0)
  1266. return ret;
  1267. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1268. priv->encr_keys[index].len = key_len;
  1269. priv->encr_keys[index].cipher = cipher;
  1270. memcpy(&priv->encr_keys[index].material, key, key_len);
  1271. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1272. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1273. else
  1274. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1275. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1276. priv->encr_tx_key_index = index;
  1277. return 0;
  1278. }
  1279. static int restore_key(struct usbnet *usbdev, int key_idx)
  1280. {
  1281. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1282. struct rndis_wlan_encr_key key;
  1283. if (is_wpa_key(priv, key_idx))
  1284. return 0;
  1285. key = priv->encr_keys[key_idx];
  1286. netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
  1287. if (key.len == 0)
  1288. return 0;
  1289. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1290. }
  1291. static void restore_keys(struct usbnet *usbdev)
  1292. {
  1293. int i;
  1294. for (i = 0; i < 4; i++)
  1295. restore_key(usbdev, i);
  1296. }
  1297. static void clear_key(struct rndis_wlan_private *priv, int idx)
  1298. {
  1299. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1300. }
  1301. /* remove_key is for both wep and wpa */
  1302. static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
  1303. {
  1304. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1305. struct ndis_80211_remove_key remove_key;
  1306. __le32 keyindex;
  1307. bool is_wpa;
  1308. int ret;
  1309. if (index >= RNDIS_WLAN_NUM_KEYS)
  1310. return -ENOENT;
  1311. if (priv->encr_keys[index].len == 0)
  1312. return 0;
  1313. is_wpa = is_wpa_key(priv, index);
  1314. netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
  1315. __func__, index, is_wpa ? "wpa" : "wep",
  1316. priv->encr_keys[index].len);
  1317. clear_key(priv, index);
  1318. if (is_wpa) {
  1319. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1320. remove_key.index = cpu_to_le32(index);
  1321. if (bssid) {
  1322. /* pairwise key */
  1323. if (!is_broadcast_ether_addr(bssid))
  1324. remove_key.index |=
  1325. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1326. memcpy(remove_key.bssid, bssid,
  1327. sizeof(remove_key.bssid));
  1328. } else
  1329. memset(remove_key.bssid, 0xff,
  1330. sizeof(remove_key.bssid));
  1331. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
  1332. sizeof(remove_key));
  1333. if (ret != 0)
  1334. return ret;
  1335. } else {
  1336. keyindex = cpu_to_le32(index);
  1337. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
  1338. sizeof(keyindex));
  1339. if (ret != 0) {
  1340. netdev_warn(usbdev->net,
  1341. "removing encryption key %d failed (%08X)\n",
  1342. index, ret);
  1343. return ret;
  1344. }
  1345. }
  1346. /* if it is transmit key, disable encryption */
  1347. if (index == priv->encr_tx_key_index)
  1348. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1349. return 0;
  1350. }
  1351. static void set_multicast_list(struct usbnet *usbdev)
  1352. {
  1353. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1354. struct netdev_hw_addr *ha;
  1355. __le32 filter, basefilter;
  1356. int ret;
  1357. char *mc_addrs = NULL;
  1358. int mc_count;
  1359. basefilter = filter = RNDIS_PACKET_TYPE_DIRECTED |
  1360. RNDIS_PACKET_TYPE_BROADCAST;
  1361. if (usbdev->net->flags & IFF_PROMISC) {
  1362. filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
  1363. RNDIS_PACKET_TYPE_ALL_LOCAL;
  1364. } else if (usbdev->net->flags & IFF_ALLMULTI) {
  1365. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1366. }
  1367. if (filter != basefilter)
  1368. goto set_filter;
  1369. /*
  1370. * mc_list should be accessed holding the lock, so copy addresses to
  1371. * local buffer first.
  1372. */
  1373. netif_addr_lock_bh(usbdev->net);
  1374. mc_count = netdev_mc_count(usbdev->net);
  1375. if (mc_count > priv->multicast_size) {
  1376. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1377. } else if (mc_count) {
  1378. int i = 0;
  1379. mc_addrs = kmalloc(mc_count * ETH_ALEN, GFP_ATOMIC);
  1380. if (!mc_addrs) {
  1381. netdev_warn(usbdev->net,
  1382. "couldn't alloc %d bytes of memory\n",
  1383. mc_count * ETH_ALEN);
  1384. netif_addr_unlock_bh(usbdev->net);
  1385. return;
  1386. }
  1387. netdev_for_each_mc_addr(ha, usbdev->net)
  1388. memcpy(mc_addrs + i++ * ETH_ALEN,
  1389. ha->addr, ETH_ALEN);
  1390. }
  1391. netif_addr_unlock_bh(usbdev->net);
  1392. if (filter != basefilter)
  1393. goto set_filter;
  1394. if (mc_count) {
  1395. ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, mc_addrs,
  1396. mc_count * ETH_ALEN);
  1397. kfree(mc_addrs);
  1398. if (ret == 0)
  1399. filter |= RNDIS_PACKET_TYPE_MULTICAST;
  1400. else
  1401. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1402. netdev_dbg(usbdev->net, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
  1403. mc_count, priv->multicast_size, ret);
  1404. }
  1405. set_filter:
  1406. ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1407. sizeof(filter));
  1408. if (ret < 0) {
  1409. netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
  1410. le32_to_cpu(filter));
  1411. }
  1412. netdev_dbg(usbdev->net, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
  1413. le32_to_cpu(filter), ret);
  1414. }
  1415. #ifdef DEBUG
  1416. static void debug_print_pmkids(struct usbnet *usbdev,
  1417. struct ndis_80211_pmkid *pmkids,
  1418. const char *func_str)
  1419. {
  1420. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1421. int i, len, count, max_pmkids, entry_len;
  1422. max_pmkids = priv->wdev.wiphy->max_num_pmkids;
  1423. len = le32_to_cpu(pmkids->length);
  1424. count = le32_to_cpu(pmkids->bssid_info_count);
  1425. entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
  1426. netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
  1427. "%d)\n", func_str, count, len, entry_len);
  1428. if (count > max_pmkids)
  1429. count = max_pmkids;
  1430. for (i = 0; i < count; i++) {
  1431. u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
  1432. netdev_dbg(usbdev->net, "%s(): bssid: %pM, "
  1433. "pmkid: %08X:%08X:%08X:%08X\n",
  1434. func_str, pmkids->bssid_info[i].bssid,
  1435. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  1436. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  1437. }
  1438. }
  1439. #else
  1440. static void debug_print_pmkids(struct usbnet *usbdev,
  1441. struct ndis_80211_pmkid *pmkids,
  1442. const char *func_str)
  1443. {
  1444. return;
  1445. }
  1446. #endif
  1447. static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
  1448. {
  1449. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1450. struct ndis_80211_pmkid *pmkids;
  1451. int len, ret, max_pmkids;
  1452. max_pmkids = priv->wdev.wiphy->max_num_pmkids;
  1453. len = sizeof(*pmkids) + max_pmkids * sizeof(pmkids->bssid_info[0]);
  1454. pmkids = kzalloc(len, GFP_KERNEL);
  1455. if (!pmkids)
  1456. return ERR_PTR(-ENOMEM);
  1457. pmkids->length = cpu_to_le32(len);
  1458. pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
  1459. ret = rndis_query_oid(usbdev, OID_802_11_PMKID, pmkids, &len);
  1460. if (ret < 0) {
  1461. netdev_dbg(usbdev->net, "%s(): OID_802_11_PMKID(%d, %d)"
  1462. " -> %d\n", __func__, len, max_pmkids, ret);
  1463. kfree(pmkids);
  1464. return ERR_PTR(ret);
  1465. }
  1466. if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
  1467. pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
  1468. debug_print_pmkids(usbdev, pmkids, __func__);
  1469. return pmkids;
  1470. }
  1471. static int set_device_pmkids(struct usbnet *usbdev,
  1472. struct ndis_80211_pmkid *pmkids)
  1473. {
  1474. int ret, len, num_pmkids;
  1475. num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
  1476. len = sizeof(*pmkids) + num_pmkids * sizeof(pmkids->bssid_info[0]);
  1477. pmkids->length = cpu_to_le32(len);
  1478. debug_print_pmkids(usbdev, pmkids, __func__);
  1479. ret = rndis_set_oid(usbdev, OID_802_11_PMKID, pmkids,
  1480. le32_to_cpu(pmkids->length));
  1481. if (ret < 0) {
  1482. netdev_dbg(usbdev->net, "%s(): OID_802_11_PMKID(%d, %d) -> %d"
  1483. "\n", __func__, len, num_pmkids, ret);
  1484. }
  1485. kfree(pmkids);
  1486. return ret;
  1487. }
  1488. static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
  1489. struct ndis_80211_pmkid *pmkids,
  1490. struct cfg80211_pmksa *pmksa,
  1491. int max_pmkids)
  1492. {
  1493. int i, len, count, newlen, err;
  1494. len = le32_to_cpu(pmkids->length);
  1495. count = le32_to_cpu(pmkids->bssid_info_count);
  1496. if (count > max_pmkids)
  1497. count = max_pmkids;
  1498. for (i = 0; i < count; i++)
  1499. if (!compare_ether_addr(pmkids->bssid_info[i].bssid,
  1500. pmksa->bssid))
  1501. break;
  1502. /* pmkid not found */
  1503. if (i == count) {
  1504. netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
  1505. __func__, pmksa->bssid);
  1506. err = -ENOENT;
  1507. goto error;
  1508. }
  1509. for (; i + 1 < count; i++)
  1510. pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
  1511. count--;
  1512. newlen = sizeof(*pmkids) + count * sizeof(pmkids->bssid_info[0]);
  1513. pmkids->length = cpu_to_le32(newlen);
  1514. pmkids->bssid_info_count = cpu_to_le32(count);
  1515. return pmkids;
  1516. error:
  1517. kfree(pmkids);
  1518. return ERR_PTR(err);
  1519. }
  1520. static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
  1521. struct ndis_80211_pmkid *pmkids,
  1522. struct cfg80211_pmksa *pmksa,
  1523. int max_pmkids)
  1524. {
  1525. int i, err, len, count, newlen;
  1526. len = le32_to_cpu(pmkids->length);
  1527. count = le32_to_cpu(pmkids->bssid_info_count);
  1528. if (count > max_pmkids)
  1529. count = max_pmkids;
  1530. /* update with new pmkid */
  1531. for (i = 0; i < count; i++) {
  1532. if (compare_ether_addr(pmkids->bssid_info[i].bssid,
  1533. pmksa->bssid))
  1534. continue;
  1535. memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
  1536. WLAN_PMKID_LEN);
  1537. return pmkids;
  1538. }
  1539. /* out of space, return error */
  1540. if (i == max_pmkids) {
  1541. netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
  1542. err = -ENOSPC;
  1543. goto error;
  1544. }
  1545. /* add new pmkid */
  1546. newlen = sizeof(*pmkids) + (count + 1) * sizeof(pmkids->bssid_info[0]);
  1547. pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
  1548. if (!pmkids) {
  1549. err = -ENOMEM;
  1550. goto error;
  1551. }
  1552. pmkids->length = cpu_to_le32(newlen);
  1553. pmkids->bssid_info_count = cpu_to_le32(count + 1);
  1554. memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
  1555. memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
  1556. return pmkids;
  1557. error:
  1558. kfree(pmkids);
  1559. return ERR_PTR(err);
  1560. }
  1561. /*
  1562. * cfg80211 ops
  1563. */
  1564. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1565. struct net_device *dev,
  1566. enum nl80211_iftype type, u32 *flags,
  1567. struct vif_params *params)
  1568. {
  1569. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1570. struct usbnet *usbdev = priv->usbdev;
  1571. int mode;
  1572. switch (type) {
  1573. case NL80211_IFTYPE_ADHOC:
  1574. mode = NDIS_80211_INFRA_ADHOC;
  1575. break;
  1576. case NL80211_IFTYPE_STATION:
  1577. mode = NDIS_80211_INFRA_INFRA;
  1578. break;
  1579. default:
  1580. return -EINVAL;
  1581. }
  1582. priv->wdev.iftype = type;
  1583. return set_infra_mode(usbdev, mode);
  1584. }
  1585. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1586. {
  1587. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1588. struct usbnet *usbdev = priv->usbdev;
  1589. int err;
  1590. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1591. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1592. if (err < 0)
  1593. return err;
  1594. }
  1595. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1596. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1597. if (err < 0)
  1598. return err;
  1599. }
  1600. return 0;
  1601. }
  1602. static int rndis_set_tx_power(struct wiphy *wiphy,
  1603. enum nl80211_tx_power_setting type,
  1604. int mbm)
  1605. {
  1606. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1607. struct usbnet *usbdev = priv->usbdev;
  1608. netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
  1609. __func__, type, mbm);
  1610. if (mbm < 0 || (mbm % 100))
  1611. return -ENOTSUPP;
  1612. /* Device doesn't support changing txpower after initialization, only
  1613. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1614. * currently used.
  1615. */
  1616. if (type == NL80211_TX_POWER_AUTOMATIC ||
  1617. MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
  1618. if (!priv->radio_on)
  1619. disassociate(usbdev, true); /* turn on radio */
  1620. return 0;
  1621. }
  1622. return -ENOTSUPP;
  1623. }
  1624. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
  1625. {
  1626. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1627. struct usbnet *usbdev = priv->usbdev;
  1628. *dbm = get_bcm4320_power_dbm(priv);
  1629. netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
  1630. return 0;
  1631. }
  1632. #define SCAN_DELAY_JIFFIES (6 * HZ)
  1633. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  1634. struct cfg80211_scan_request *request)
  1635. {
  1636. struct usbnet *usbdev = netdev_priv(dev);
  1637. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1638. int ret;
  1639. int delay = SCAN_DELAY_JIFFIES;
  1640. netdev_dbg(usbdev->net, "cfg80211.scan\n");
  1641. /* Get current bssid list from device before new scan, as new scan
  1642. * clears internal bssid list.
  1643. */
  1644. rndis_check_bssid_list(usbdev, NULL, NULL);
  1645. if (!request)
  1646. return -EINVAL;
  1647. if (priv->scan_request && priv->scan_request != request)
  1648. return -EBUSY;
  1649. priv->scan_request = request;
  1650. ret = rndis_start_bssid_list_scan(usbdev);
  1651. if (ret == 0) {
  1652. if (priv->device_type == RNDIS_BCM4320A)
  1653. delay = HZ;
  1654. /* Wait before retrieving scan results from device */
  1655. queue_delayed_work(priv->workqueue, &priv->scan_work, delay);
  1656. }
  1657. return ret;
  1658. }
  1659. static bool rndis_bss_info_update(struct usbnet *usbdev,
  1660. struct ndis_80211_bssid_ex *bssid)
  1661. {
  1662. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1663. struct ieee80211_channel *channel;
  1664. struct cfg80211_bss *bss;
  1665. s32 signal;
  1666. u64 timestamp;
  1667. u16 capability;
  1668. u16 beacon_interval;
  1669. struct ndis_80211_fixed_ies *fixed;
  1670. int ie_len, bssid_len;
  1671. u8 *ie;
  1672. netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM], len: %d\n",
  1673. bssid->ssid.essid, bssid->mac, le32_to_cpu(bssid->length));
  1674. /* parse bssid structure */
  1675. bssid_len = le32_to_cpu(bssid->length);
  1676. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1677. sizeof(struct ndis_80211_fixed_ies))
  1678. return NULL;
  1679. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1680. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1681. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1682. (int)le32_to_cpu(bssid->ie_length));
  1683. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1684. if (ie_len < 0)
  1685. return NULL;
  1686. /* extract data for cfg80211_inform_bss */
  1687. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1688. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1689. if (!channel)
  1690. return NULL;
  1691. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1692. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1693. capability = le16_to_cpu(fixed->capabilities);
  1694. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1695. bss = cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1696. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1697. GFP_KERNEL);
  1698. cfg80211_put_bss(bss);
  1699. return (bss != NULL);
  1700. }
  1701. static struct ndis_80211_bssid_ex *next_bssid_list_item(
  1702. struct ndis_80211_bssid_ex *bssid,
  1703. int *bssid_len, void *buf, int len)
  1704. {
  1705. void *buf_end, *bssid_end;
  1706. buf_end = (char *)buf + len;
  1707. bssid_end = (char *)bssid + *bssid_len;
  1708. if ((int)(buf_end - bssid_end) < sizeof(bssid->length)) {
  1709. *bssid_len = 0;
  1710. return NULL;
  1711. } else {
  1712. bssid = (void *)((char *)bssid + *bssid_len);
  1713. *bssid_len = le32_to_cpu(bssid->length);
  1714. return bssid;
  1715. }
  1716. }
  1717. static bool check_bssid_list_item(struct ndis_80211_bssid_ex *bssid,
  1718. int bssid_len, void *buf, int len)
  1719. {
  1720. void *buf_end, *bssid_end;
  1721. if (!bssid || bssid_len <= 0 || bssid_len > len)
  1722. return false;
  1723. buf_end = (char *)buf + len;
  1724. bssid_end = (char *)bssid + bssid_len;
  1725. return (int)(buf_end - bssid_end) >= 0 && (int)(bssid_end - buf) >= 0;
  1726. }
  1727. static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
  1728. bool *matched)
  1729. {
  1730. void *buf = NULL;
  1731. struct ndis_80211_bssid_list_ex *bssid_list;
  1732. struct ndis_80211_bssid_ex *bssid;
  1733. int ret = -EINVAL, len, count, bssid_len, real_count, new_len;
  1734. netdev_dbg(usbdev->net, "%s()\n", __func__);
  1735. len = CONTROL_BUFFER_SIZE;
  1736. resize_buf:
  1737. buf = kzalloc(len, GFP_KERNEL);
  1738. if (!buf) {
  1739. ret = -ENOMEM;
  1740. goto out;
  1741. }
  1742. /* BSSID-list might have got bigger last time we checked, keep
  1743. * resizing until it won't get any bigger.
  1744. */
  1745. new_len = len;
  1746. ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &new_len);
  1747. if (ret != 0 || new_len < sizeof(struct ndis_80211_bssid_list_ex))
  1748. goto out;
  1749. if (new_len > len) {
  1750. len = new_len;
  1751. kfree(buf);
  1752. goto resize_buf;
  1753. }
  1754. len = new_len;
  1755. bssid_list = buf;
  1756. count = le32_to_cpu(bssid_list->num_items);
  1757. real_count = 0;
  1758. netdev_dbg(usbdev->net, "%s(): buflen: %d\n", __func__, len);
  1759. bssid_len = 0;
  1760. bssid = next_bssid_list_item(bssid_list->bssid, &bssid_len, buf, len);
  1761. /* Device returns incorrect 'num_items'. Workaround by ignoring the
  1762. * received 'num_items' and walking through full bssid buffer instead.
  1763. */
  1764. while (check_bssid_list_item(bssid, bssid_len, buf, len)) {
  1765. if (rndis_bss_info_update(usbdev, bssid) && match_bssid &&
  1766. matched) {
  1767. if (compare_ether_addr(bssid->mac, match_bssid))
  1768. *matched = true;
  1769. }
  1770. real_count++;
  1771. bssid = next_bssid_list_item(bssid, &bssid_len, buf, len);
  1772. }
  1773. netdev_dbg(usbdev->net, "%s(): num_items from device: %d, really found:"
  1774. " %d\n", __func__, count, real_count);
  1775. out:
  1776. kfree(buf);
  1777. return ret;
  1778. }
  1779. static void rndis_get_scan_results(struct work_struct *work)
  1780. {
  1781. struct rndis_wlan_private *priv =
  1782. container_of(work, struct rndis_wlan_private, scan_work.work);
  1783. struct usbnet *usbdev = priv->usbdev;
  1784. int ret;
  1785. netdev_dbg(usbdev->net, "get_scan_results\n");
  1786. if (!priv->scan_request)
  1787. return;
  1788. ret = rndis_check_bssid_list(usbdev, NULL, NULL);
  1789. cfg80211_scan_done(priv->scan_request, ret < 0);
  1790. priv->scan_request = NULL;
  1791. }
  1792. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  1793. struct cfg80211_connect_params *sme)
  1794. {
  1795. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1796. struct usbnet *usbdev = priv->usbdev;
  1797. struct ieee80211_channel *channel = sme->channel;
  1798. struct ndis_80211_ssid ssid;
  1799. int pairwise = RNDIS_WLAN_ALG_NONE;
  1800. int groupwise = RNDIS_WLAN_ALG_NONE;
  1801. int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
  1802. int length, i, ret, chan = -1;
  1803. if (channel)
  1804. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1805. groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
  1806. for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
  1807. pairwise |=
  1808. rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
  1809. if (sme->crypto.n_ciphers_pairwise > 0 &&
  1810. pairwise == RNDIS_WLAN_ALG_NONE) {
  1811. netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
  1812. return -ENOTSUPP;
  1813. }
  1814. for (i = 0; i < sme->crypto.n_akm_suites; i++)
  1815. keymgmt |=
  1816. rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
  1817. if (sme->crypto.n_akm_suites > 0 &&
  1818. keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
  1819. netdev_err(usbdev->net, "Invalid keymgmt\n");
  1820. return -ENOTSUPP;
  1821. }
  1822. netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
  1823. sme->ssid, sme->bssid, chan,
  1824. sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
  1825. groupwise, pairwise, keymgmt);
  1826. if (is_associated(usbdev))
  1827. disassociate(usbdev, false);
  1828. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1829. if (ret < 0) {
  1830. netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
  1831. ret);
  1832. goto err_turn_radio_on;
  1833. }
  1834. ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
  1835. keymgmt);
  1836. if (ret < 0) {
  1837. netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
  1838. ret);
  1839. goto err_turn_radio_on;
  1840. }
  1841. set_priv_filter(usbdev);
  1842. ret = set_encr_mode(usbdev, pairwise, groupwise);
  1843. if (ret < 0) {
  1844. netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
  1845. ret);
  1846. goto err_turn_radio_on;
  1847. }
  1848. if (channel) {
  1849. ret = set_channel(usbdev, chan);
  1850. if (ret < 0) {
  1851. netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
  1852. ret);
  1853. goto err_turn_radio_on;
  1854. }
  1855. }
  1856. if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
  1857. priv->encr_tx_key_index = sme->key_idx;
  1858. ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
  1859. if (ret < 0) {
  1860. netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
  1861. ret, sme->key_len, sme->key_idx);
  1862. goto err_turn_radio_on;
  1863. }
  1864. }
  1865. if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
  1866. !is_broadcast_ether_addr(sme->bssid)) {
  1867. ret = set_bssid(usbdev, sme->bssid);
  1868. if (ret < 0) {
  1869. netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
  1870. ret);
  1871. goto err_turn_radio_on;
  1872. }
  1873. } else
  1874. clear_bssid(usbdev);
  1875. length = sme->ssid_len;
  1876. if (length > NDIS_802_11_LENGTH_SSID)
  1877. length = NDIS_802_11_LENGTH_SSID;
  1878. memset(&ssid, 0, sizeof(ssid));
  1879. ssid.length = cpu_to_le32(length);
  1880. memcpy(ssid.essid, sme->ssid, length);
  1881. /* Pause and purge rx queue, so we don't pass packets before
  1882. * 'media connect'-indication.
  1883. */
  1884. usbnet_pause_rx(usbdev);
  1885. usbnet_purge_paused_rxq(usbdev);
  1886. ret = set_essid(usbdev, &ssid);
  1887. if (ret < 0)
  1888. netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
  1889. return ret;
  1890. err_turn_radio_on:
  1891. disassociate(usbdev, true);
  1892. return ret;
  1893. }
  1894. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1895. u16 reason_code)
  1896. {
  1897. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1898. struct usbnet *usbdev = priv->usbdev;
  1899. netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
  1900. priv->connected = false;
  1901. memset(priv->bssid, 0, ETH_ALEN);
  1902. return deauthenticate(usbdev);
  1903. }
  1904. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1905. struct cfg80211_ibss_params *params)
  1906. {
  1907. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1908. struct usbnet *usbdev = priv->usbdev;
  1909. struct ieee80211_channel *channel = params->channel;
  1910. struct ndis_80211_ssid ssid;
  1911. enum nl80211_auth_type auth_type;
  1912. int ret, alg, length, chan = -1;
  1913. if (channel)
  1914. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1915. /* TODO: How to handle ad-hoc encryption?
  1916. * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
  1917. * pre-shared for encryption (open/shared/wpa), is key set before
  1918. * join_ibss? Which auth_type to use (not in params)? What about WPA?
  1919. */
  1920. if (params->privacy) {
  1921. auth_type = NL80211_AUTHTYPE_SHARED_KEY;
  1922. alg = RNDIS_WLAN_ALG_WEP;
  1923. } else {
  1924. auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  1925. alg = RNDIS_WLAN_ALG_NONE;
  1926. }
  1927. netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
  1928. params->ssid, params->bssid, chan, params->privacy);
  1929. if (is_associated(usbdev))
  1930. disassociate(usbdev, false);
  1931. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
  1932. if (ret < 0) {
  1933. netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
  1934. ret);
  1935. goto err_turn_radio_on;
  1936. }
  1937. ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
  1938. if (ret < 0) {
  1939. netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
  1940. ret);
  1941. goto err_turn_radio_on;
  1942. }
  1943. set_priv_filter(usbdev);
  1944. ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
  1945. if (ret < 0) {
  1946. netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
  1947. ret);
  1948. goto err_turn_radio_on;
  1949. }
  1950. if (channel) {
  1951. ret = set_channel(usbdev, chan);
  1952. if (ret < 0) {
  1953. netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
  1954. ret);
  1955. goto err_turn_radio_on;
  1956. }
  1957. }
  1958. if (params->bssid && !is_zero_ether_addr(params->bssid) &&
  1959. !is_broadcast_ether_addr(params->bssid)) {
  1960. ret = set_bssid(usbdev, params->bssid);
  1961. if (ret < 0) {
  1962. netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
  1963. ret);
  1964. goto err_turn_radio_on;
  1965. }
  1966. } else
  1967. clear_bssid(usbdev);
  1968. length = params->ssid_len;
  1969. if (length > NDIS_802_11_LENGTH_SSID)
  1970. length = NDIS_802_11_LENGTH_SSID;
  1971. memset(&ssid, 0, sizeof(ssid));
  1972. ssid.length = cpu_to_le32(length);
  1973. memcpy(ssid.essid, params->ssid, length);
  1974. /* Don't need to pause rx queue for ad-hoc. */
  1975. usbnet_purge_paused_rxq(usbdev);
  1976. usbnet_resume_rx(usbdev);
  1977. ret = set_essid(usbdev, &ssid);
  1978. if (ret < 0)
  1979. netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
  1980. ret);
  1981. return ret;
  1982. err_turn_radio_on:
  1983. disassociate(usbdev, true);
  1984. return ret;
  1985. }
  1986. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1987. {
  1988. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1989. struct usbnet *usbdev = priv->usbdev;
  1990. netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
  1991. priv->connected = false;
  1992. memset(priv->bssid, 0, ETH_ALEN);
  1993. return deauthenticate(usbdev);
  1994. }
  1995. static int rndis_set_channel(struct wiphy *wiphy, struct net_device *netdev,
  1996. struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
  1997. {
  1998. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1999. struct usbnet *usbdev = priv->usbdev;
  2000. return set_channel(usbdev,
  2001. ieee80211_frequency_to_channel(chan->center_freq));
  2002. }
  2003. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  2004. u8 key_index, bool pairwise, const u8 *mac_addr,
  2005. struct key_params *params)
  2006. {
  2007. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2008. struct usbnet *usbdev = priv->usbdev;
  2009. __le32 flags;
  2010. netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
  2011. __func__, key_index, mac_addr, params->cipher);
  2012. switch (params->cipher) {
  2013. case WLAN_CIPHER_SUITE_WEP40:
  2014. case WLAN_CIPHER_SUITE_WEP104:
  2015. return add_wep_key(usbdev, params->key, params->key_len,
  2016. key_index);
  2017. case WLAN_CIPHER_SUITE_TKIP:
  2018. case WLAN_CIPHER_SUITE_CCMP:
  2019. flags = 0;
  2020. if (params->seq && params->seq_len > 0)
  2021. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  2022. if (mac_addr)
  2023. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
  2024. NDIS_80211_ADDKEY_TRANSMIT_KEY;
  2025. return add_wpa_key(usbdev, params->key, params->key_len,
  2026. key_index, mac_addr, params->seq,
  2027. params->seq_len, params->cipher, flags);
  2028. default:
  2029. netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
  2030. __func__, params->cipher);
  2031. return -ENOTSUPP;
  2032. }
  2033. }
  2034. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  2035. u8 key_index, bool pairwise, const u8 *mac_addr)
  2036. {
  2037. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2038. struct usbnet *usbdev = priv->usbdev;
  2039. netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
  2040. return remove_key(usbdev, key_index, mac_addr);
  2041. }
  2042. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  2043. u8 key_index, bool unicast, bool multicast)
  2044. {
  2045. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2046. struct usbnet *usbdev = priv->usbdev;
  2047. struct rndis_wlan_encr_key key;
  2048. netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
  2049. if (key_index >= RNDIS_WLAN_NUM_KEYS)
  2050. return -ENOENT;
  2051. priv->encr_tx_key_index = key_index;
  2052. if (is_wpa_key(priv, key_index))
  2053. return 0;
  2054. key = priv->encr_keys[key_index];
  2055. return add_wep_key(usbdev, key.material, key.len, key_index);
  2056. }
  2057. static void rndis_fill_station_info(struct usbnet *usbdev,
  2058. struct station_info *sinfo)
  2059. {
  2060. __le32 linkspeed, rssi;
  2061. int ret, len;
  2062. memset(sinfo, 0, sizeof(*sinfo));
  2063. len = sizeof(linkspeed);
  2064. ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
  2065. if (ret == 0) {
  2066. sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
  2067. sinfo->filled |= STATION_INFO_TX_BITRATE;
  2068. }
  2069. len = sizeof(rssi);
  2070. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  2071. if (ret == 0) {
  2072. sinfo->signal = level_to_qual(le32_to_cpu(rssi));
  2073. sinfo->filled |= STATION_INFO_SIGNAL;
  2074. }
  2075. }
  2076. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  2077. u8 *mac, struct station_info *sinfo)
  2078. {
  2079. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2080. struct usbnet *usbdev = priv->usbdev;
  2081. if (compare_ether_addr(priv->bssid, mac))
  2082. return -ENOENT;
  2083. rndis_fill_station_info(usbdev, sinfo);
  2084. return 0;
  2085. }
  2086. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  2087. int idx, u8 *mac, struct station_info *sinfo)
  2088. {
  2089. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2090. struct usbnet *usbdev = priv->usbdev;
  2091. if (idx != 0)
  2092. return -ENOENT;
  2093. memcpy(mac, priv->bssid, ETH_ALEN);
  2094. rndis_fill_station_info(usbdev, sinfo);
  2095. return 0;
  2096. }
  2097. static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  2098. struct cfg80211_pmksa *pmksa)
  2099. {
  2100. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2101. struct usbnet *usbdev = priv->usbdev;
  2102. struct ndis_80211_pmkid *pmkids;
  2103. u32 *tmp = (u32 *)pmksa->pmkid;
  2104. netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
  2105. pmksa->bssid,
  2106. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  2107. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  2108. pmkids = get_device_pmkids(usbdev);
  2109. if (IS_ERR(pmkids)) {
  2110. /* couldn't read PMKID cache from device */
  2111. return PTR_ERR(pmkids);
  2112. }
  2113. pmkids = update_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
  2114. if (IS_ERR(pmkids)) {
  2115. /* not found, list full, etc */
  2116. return PTR_ERR(pmkids);
  2117. }
  2118. return set_device_pmkids(usbdev, pmkids);
  2119. }
  2120. static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  2121. struct cfg80211_pmksa *pmksa)
  2122. {
  2123. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2124. struct usbnet *usbdev = priv->usbdev;
  2125. struct ndis_80211_pmkid *pmkids;
  2126. u32 *tmp = (u32 *)pmksa->pmkid;
  2127. netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
  2128. pmksa->bssid,
  2129. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  2130. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  2131. pmkids = get_device_pmkids(usbdev);
  2132. if (IS_ERR(pmkids)) {
  2133. /* Couldn't read PMKID cache from device */
  2134. return PTR_ERR(pmkids);
  2135. }
  2136. pmkids = remove_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
  2137. if (IS_ERR(pmkids)) {
  2138. /* not found, etc */
  2139. return PTR_ERR(pmkids);
  2140. }
  2141. return set_device_pmkids(usbdev, pmkids);
  2142. }
  2143. static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
  2144. {
  2145. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2146. struct usbnet *usbdev = priv->usbdev;
  2147. struct ndis_80211_pmkid pmkid;
  2148. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2149. memset(&pmkid, 0, sizeof(pmkid));
  2150. pmkid.length = cpu_to_le32(sizeof(pmkid));
  2151. pmkid.bssid_info_count = cpu_to_le32(0);
  2152. return rndis_set_oid(usbdev, OID_802_11_PMKID, &pmkid, sizeof(pmkid));
  2153. }
  2154. static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
  2155. bool enabled, int timeout)
  2156. {
  2157. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2158. struct usbnet *usbdev = priv->usbdev;
  2159. int power_mode;
  2160. __le32 mode;
  2161. int ret;
  2162. if (priv->device_type != RNDIS_BCM4320B)
  2163. return -ENOTSUPP;
  2164. netdev_dbg(usbdev->net, "%s(): %s, %d\n", __func__,
  2165. enabled ? "enabled" : "disabled",
  2166. timeout);
  2167. if (enabled)
  2168. power_mode = NDIS_80211_POWER_MODE_FAST_PSP;
  2169. else
  2170. power_mode = NDIS_80211_POWER_MODE_CAM;
  2171. if (power_mode == priv->power_mode)
  2172. return 0;
  2173. priv->power_mode = power_mode;
  2174. mode = cpu_to_le32(power_mode);
  2175. ret = rndis_set_oid(usbdev, OID_802_11_POWER_MODE, &mode, sizeof(mode));
  2176. netdev_dbg(usbdev->net, "%s(): OID_802_11_POWER_MODE -> %d\n",
  2177. __func__, ret);
  2178. return ret;
  2179. }
  2180. static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
  2181. struct net_device *dev,
  2182. s32 rssi_thold, u32 rssi_hyst)
  2183. {
  2184. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2185. priv->cqm_rssi_thold = rssi_thold;
  2186. priv->cqm_rssi_hyst = rssi_hyst;
  2187. priv->last_cqm_event_rssi = 0;
  2188. return 0;
  2189. }
  2190. static void rndis_wlan_craft_connected_bss(struct usbnet *usbdev, u8 *bssid,
  2191. struct ndis_80211_assoc_info *info)
  2192. {
  2193. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2194. struct ieee80211_channel *channel;
  2195. struct ndis_80211_ssid ssid;
  2196. struct cfg80211_bss *bss;
  2197. s32 signal;
  2198. u64 timestamp;
  2199. u16 capability;
  2200. u16 beacon_interval = 0;
  2201. __le32 rssi;
  2202. u8 ie_buf[34];
  2203. int len, ret, ie_len;
  2204. /* Get signal quality, in case of error use rssi=0 and ignore error. */
  2205. len = sizeof(rssi);
  2206. rssi = 0;
  2207. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  2208. signal = level_to_qual(le32_to_cpu(rssi));
  2209. netdev_dbg(usbdev->net, "%s(): OID_802_11_RSSI -> %d, "
  2210. "rssi:%d, qual: %d\n", __func__, ret, le32_to_cpu(rssi),
  2211. level_to_qual(le32_to_cpu(rssi)));
  2212. /* Get AP capabilities */
  2213. if (info) {
  2214. capability = le16_to_cpu(info->resp_ie.capa);
  2215. } else {
  2216. /* Set atleast ESS/IBSS capability */
  2217. capability = (priv->infra_mode == NDIS_80211_INFRA_INFRA) ?
  2218. WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS;
  2219. }
  2220. /* Get channel and beacon interval */
  2221. channel = get_current_channel(usbdev, &beacon_interval);
  2222. if (!channel) {
  2223. netdev_warn(usbdev->net, "%s(): could not get channel.\n",
  2224. __func__);
  2225. return;
  2226. }
  2227. /* Get SSID, in case of error, use zero length SSID and ignore error. */
  2228. len = sizeof(ssid);
  2229. memset(&ssid, 0, sizeof(ssid));
  2230. ret = rndis_query_oid(usbdev, OID_802_11_SSID, &ssid, &len);
  2231. netdev_dbg(usbdev->net, "%s(): OID_802_11_SSID -> %d, len: %d, ssid: "
  2232. "'%.32s'\n", __func__, ret,
  2233. le32_to_cpu(ssid.length), ssid.essid);
  2234. if (le32_to_cpu(ssid.length) > 32)
  2235. ssid.length = cpu_to_le32(32);
  2236. ie_buf[0] = WLAN_EID_SSID;
  2237. ie_buf[1] = le32_to_cpu(ssid.length);
  2238. memcpy(&ie_buf[2], ssid.essid, le32_to_cpu(ssid.length));
  2239. ie_len = le32_to_cpu(ssid.length) + 2;
  2240. /* no tsf */
  2241. timestamp = 0;
  2242. netdev_dbg(usbdev->net, "%s(): channel:%d(freq), bssid:[%pM], tsf:%d, "
  2243. "capa:%x, beacon int:%d, resp_ie(len:%d, essid:'%.32s'), "
  2244. "signal:%d\n", __func__, (channel ? channel->center_freq : -1),
  2245. bssid, (u32)timestamp, capability, beacon_interval, ie_len,
  2246. ssid.essid, signal);
  2247. bss = cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid,
  2248. timestamp, capability, beacon_interval, ie_buf, ie_len,
  2249. signal, GFP_KERNEL);
  2250. cfg80211_put_bss(bss);
  2251. }
  2252. /*
  2253. * workers, indication handlers, device poller
  2254. */
  2255. static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
  2256. {
  2257. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2258. struct ndis_80211_assoc_info *info = NULL;
  2259. u8 bssid[ETH_ALEN];
  2260. int resp_ie_len, req_ie_len;
  2261. u8 *req_ie, *resp_ie;
  2262. int ret, offset;
  2263. bool roamed = false;
  2264. bool match_bss;
  2265. if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
  2266. /* received media connect indication while connected, either
  2267. * device reassociated with same AP or roamed to new. */
  2268. roamed = true;
  2269. }
  2270. req_ie_len = 0;
  2271. resp_ie_len = 0;
  2272. req_ie = NULL;
  2273. resp_ie = NULL;
  2274. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  2275. info = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
  2276. if (!info) {
  2277. /* No memory? Try resume work later */
  2278. set_bit(WORK_LINK_UP, &priv->work_pending);
  2279. queue_work(priv->workqueue, &priv->work);
  2280. return;
  2281. }
  2282. /* Get association info IEs from device. */
  2283. ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
  2284. if (!ret) {
  2285. req_ie_len = le32_to_cpu(info->req_ie_length);
  2286. if (req_ie_len > 0) {
  2287. offset = le32_to_cpu(info->offset_req_ies);
  2288. if (offset > CONTROL_BUFFER_SIZE)
  2289. offset = CONTROL_BUFFER_SIZE;
  2290. req_ie = (u8 *)info + offset;
  2291. if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
  2292. req_ie_len =
  2293. CONTROL_BUFFER_SIZE - offset;
  2294. }
  2295. resp_ie_len = le32_to_cpu(info->resp_ie_length);
  2296. if (resp_ie_len > 0) {
  2297. offset = le32_to_cpu(info->offset_resp_ies);
  2298. if (offset > CONTROL_BUFFER_SIZE)
  2299. offset = CONTROL_BUFFER_SIZE;
  2300. resp_ie = (u8 *)info + offset;
  2301. if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
  2302. resp_ie_len =
  2303. CONTROL_BUFFER_SIZE - offset;
  2304. }
  2305. } else {
  2306. /* Since rndis_wlan_craft_connected_bss() might use info
  2307. * later and expects info to contain valid data if
  2308. * non-null, free info and set NULL here.
  2309. */
  2310. kfree(info);
  2311. info = NULL;
  2312. }
  2313. } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
  2314. return;
  2315. ret = get_bssid(usbdev, bssid);
  2316. if (ret < 0)
  2317. memset(bssid, 0, sizeof(bssid));
  2318. netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
  2319. bssid, roamed ? " roamed" : "");
  2320. /* Internal bss list in device should contain at least the currently
  2321. * connected bss and we can get it to cfg80211 with
  2322. * rndis_check_bssid_list().
  2323. *
  2324. * NDIS spec says: "If the device is associated, but the associated
  2325. * BSSID is not in its BSSID scan list, then the driver must add an
  2326. * entry for the BSSID at the end of the data that it returns in
  2327. * response to query of OID_802_11_BSSID_LIST."
  2328. *
  2329. * NOTE: Seems to be true for BCM4320b variant, but not BCM4320a.
  2330. */
  2331. match_bss = false;
  2332. rndis_check_bssid_list(usbdev, bssid, &match_bss);
  2333. if (!is_zero_ether_addr(bssid) && !match_bss) {
  2334. /* Couldn't get bss from device, we need to manually craft bss
  2335. * for cfg80211.
  2336. */
  2337. rndis_wlan_craft_connected_bss(usbdev, bssid, info);
  2338. }
  2339. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  2340. if (!roamed)
  2341. cfg80211_connect_result(usbdev->net, bssid, req_ie,
  2342. req_ie_len, resp_ie,
  2343. resp_ie_len, 0, GFP_KERNEL);
  2344. else
  2345. cfg80211_roamed(usbdev->net,
  2346. get_current_channel(usbdev, NULL),
  2347. bssid, req_ie, req_ie_len,
  2348. resp_ie, resp_ie_len, GFP_KERNEL);
  2349. } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  2350. cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
  2351. if (info != NULL)
  2352. kfree(info);
  2353. priv->connected = true;
  2354. memcpy(priv->bssid, bssid, ETH_ALEN);
  2355. usbnet_resume_rx(usbdev);
  2356. netif_carrier_on(usbdev->net);
  2357. }
  2358. static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
  2359. {
  2360. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2361. if (priv->connected) {
  2362. priv->connected = false;
  2363. memset(priv->bssid, 0, ETH_ALEN);
  2364. deauthenticate(usbdev);
  2365. cfg80211_disconnected(usbdev->net, 0, NULL, 0, GFP_KERNEL);
  2366. }
  2367. netif_carrier_off(usbdev->net);
  2368. }
  2369. static void rndis_wlan_worker(struct work_struct *work)
  2370. {
  2371. struct rndis_wlan_private *priv =
  2372. container_of(work, struct rndis_wlan_private, work);
  2373. struct usbnet *usbdev = priv->usbdev;
  2374. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
  2375. rndis_wlan_do_link_up_work(usbdev);
  2376. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
  2377. rndis_wlan_do_link_down_work(usbdev);
  2378. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  2379. set_multicast_list(usbdev);
  2380. }
  2381. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  2382. {
  2383. struct usbnet *usbdev = netdev_priv(dev);
  2384. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2385. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  2386. return;
  2387. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  2388. queue_work(priv->workqueue, &priv->work);
  2389. }
  2390. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  2391. struct ndis_80211_status_indication *indication,
  2392. int len)
  2393. {
  2394. u8 *buf;
  2395. const char *type;
  2396. int flags, buflen, key_id;
  2397. bool pairwise_error, group_error;
  2398. struct ndis_80211_auth_request *auth_req;
  2399. enum nl80211_key_type key_type;
  2400. /* must have at least one array entry */
  2401. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2402. sizeof(struct ndis_80211_auth_request)) {
  2403. netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
  2404. len);
  2405. return;
  2406. }
  2407. buf = (void *)&indication->u.auth_request[0];
  2408. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  2409. while (buflen >= sizeof(*auth_req)) {
  2410. auth_req = (void *)buf;
  2411. type = "unknown";
  2412. flags = le32_to_cpu(auth_req->flags);
  2413. pairwise_error = false;
  2414. group_error = false;
  2415. if (flags & 0x1)
  2416. type = "reauth request";
  2417. if (flags & 0x2)
  2418. type = "key update request";
  2419. if (flags & 0x6) {
  2420. pairwise_error = true;
  2421. type = "pairwise_error";
  2422. }
  2423. if (flags & 0xe) {
  2424. group_error = true;
  2425. type = "group_error";
  2426. }
  2427. netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
  2428. type, le32_to_cpu(auth_req->flags));
  2429. if (pairwise_error) {
  2430. key_type = NL80211_KEYTYPE_PAIRWISE;
  2431. key_id = -1;
  2432. cfg80211_michael_mic_failure(usbdev->net,
  2433. auth_req->bssid,
  2434. key_type, key_id, NULL,
  2435. GFP_KERNEL);
  2436. }
  2437. if (group_error) {
  2438. key_type = NL80211_KEYTYPE_GROUP;
  2439. key_id = -1;
  2440. cfg80211_michael_mic_failure(usbdev->net,
  2441. auth_req->bssid,
  2442. key_type, key_id, NULL,
  2443. GFP_KERNEL);
  2444. }
  2445. buflen -= le32_to_cpu(auth_req->length);
  2446. buf += le32_to_cpu(auth_req->length);
  2447. }
  2448. }
  2449. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  2450. struct ndis_80211_status_indication *indication,
  2451. int len)
  2452. {
  2453. struct ndis_80211_pmkid_cand_list *cand_list;
  2454. int list_len, expected_len, i;
  2455. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2456. sizeof(struct ndis_80211_pmkid_cand_list)) {
  2457. netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
  2458. len);
  2459. return;
  2460. }
  2461. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  2462. sizeof(struct ndis_80211_pmkid_candidate);
  2463. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  2464. offsetof(struct ndis_80211_status_indication, u);
  2465. if (len < expected_len) {
  2466. netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
  2467. len, expected_len);
  2468. return;
  2469. }
  2470. cand_list = &indication->u.cand_list;
  2471. netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
  2472. le32_to_cpu(cand_list->version),
  2473. le32_to_cpu(cand_list->num_candidates));
  2474. if (le32_to_cpu(cand_list->version) != 1)
  2475. return;
  2476. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  2477. struct ndis_80211_pmkid_candidate *cand =
  2478. &cand_list->candidate_list[i];
  2479. bool preauth = !!(cand->flags & NDIS_80211_PMKID_CAND_PREAUTH);
  2480. netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, preauth: %d, bssid: %pM\n",
  2481. i, le32_to_cpu(cand->flags), preauth, cand->bssid);
  2482. cfg80211_pmksa_candidate_notify(usbdev->net, i, cand->bssid,
  2483. preauth, GFP_ATOMIC);
  2484. }
  2485. }
  2486. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  2487. struct rndis_indicate *msg, int buflen)
  2488. {
  2489. struct ndis_80211_status_indication *indication;
  2490. int len, offset;
  2491. offset = offsetof(struct rndis_indicate, status) +
  2492. le32_to_cpu(msg->offset);
  2493. len = le32_to_cpu(msg->length);
  2494. if (len < 8) {
  2495. netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
  2496. len);
  2497. return;
  2498. }
  2499. if (offset + len > buflen) {
  2500. netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
  2501. offset + len, buflen);
  2502. return;
  2503. }
  2504. indication = (void *)((u8 *)msg + offset);
  2505. switch (le32_to_cpu(indication->status_type)) {
  2506. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  2507. netdev_info(usbdev->net, "radio state indication: %i\n",
  2508. le32_to_cpu(indication->u.radio_status));
  2509. return;
  2510. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  2511. netdev_info(usbdev->net, "media stream mode indication: %i\n",
  2512. le32_to_cpu(indication->u.media_stream_mode));
  2513. return;
  2514. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  2515. rndis_wlan_auth_indication(usbdev, indication, len);
  2516. return;
  2517. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2518. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2519. return;
  2520. default:
  2521. netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
  2522. le32_to_cpu(indication->status_type));
  2523. }
  2524. }
  2525. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2526. {
  2527. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2528. struct rndis_indicate *msg = ind;
  2529. switch (msg->status) {
  2530. case RNDIS_STATUS_MEDIA_CONNECT:
  2531. if (priv->current_command_oid == OID_802_11_ADD_KEY) {
  2532. /* OID_802_11_ADD_KEY causes sometimes extra
  2533. * "media connect" indications which confuses driver
  2534. * and userspace to think that device is
  2535. * roaming/reassociating when it isn't.
  2536. */
  2537. netdev_dbg(usbdev->net, "ignored OID_802_11_ADD_KEY triggered 'media connect'\n");
  2538. return;
  2539. }
  2540. usbnet_pause_rx(usbdev);
  2541. netdev_info(usbdev->net, "media connect\n");
  2542. /* queue work to avoid recursive calls into rndis_command */
  2543. set_bit(WORK_LINK_UP, &priv->work_pending);
  2544. queue_work(priv->workqueue, &priv->work);
  2545. break;
  2546. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2547. netdev_info(usbdev->net, "media disconnect\n");
  2548. /* queue work to avoid recursive calls into rndis_command */
  2549. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2550. queue_work(priv->workqueue, &priv->work);
  2551. break;
  2552. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2553. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2554. break;
  2555. default:
  2556. netdev_info(usbdev->net, "indication: 0x%08x\n",
  2557. le32_to_cpu(msg->status));
  2558. break;
  2559. }
  2560. }
  2561. static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
  2562. {
  2563. struct {
  2564. __le32 num_items;
  2565. __le32 items[8];
  2566. } networks_supported;
  2567. struct ndis_80211_capability *caps;
  2568. u8 caps_buf[sizeof(*caps) + sizeof(caps->auth_encr_pair) * 16];
  2569. int len, retval, i, n;
  2570. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2571. /* determine supported modes */
  2572. len = sizeof(networks_supported);
  2573. retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
  2574. &networks_supported, &len);
  2575. if (retval >= 0) {
  2576. n = le32_to_cpu(networks_supported.num_items);
  2577. if (n > 8)
  2578. n = 8;
  2579. for (i = 0; i < n; i++) {
  2580. switch (le32_to_cpu(networks_supported.items[i])) {
  2581. case NDIS_80211_TYPE_FREQ_HOP:
  2582. case NDIS_80211_TYPE_DIRECT_SEQ:
  2583. priv->caps |= CAP_MODE_80211B;
  2584. break;
  2585. case NDIS_80211_TYPE_OFDM_A:
  2586. priv->caps |= CAP_MODE_80211A;
  2587. break;
  2588. case NDIS_80211_TYPE_OFDM_G:
  2589. priv->caps |= CAP_MODE_80211G;
  2590. break;
  2591. }
  2592. }
  2593. }
  2594. /* get device 802.11 capabilities, number of PMKIDs */
  2595. caps = (struct ndis_80211_capability *)caps_buf;
  2596. len = sizeof(caps_buf);
  2597. retval = rndis_query_oid(usbdev, OID_802_11_CAPABILITY, caps, &len);
  2598. if (retval >= 0) {
  2599. netdev_dbg(usbdev->net, "OID_802_11_CAPABILITY -> len %d, "
  2600. "ver %d, pmkids %d, auth-encr-pairs %d\n",
  2601. le32_to_cpu(caps->length),
  2602. le32_to_cpu(caps->version),
  2603. le32_to_cpu(caps->num_pmkids),
  2604. le32_to_cpu(caps->num_auth_encr_pair));
  2605. wiphy->max_num_pmkids = le32_to_cpu(caps->num_pmkids);
  2606. } else
  2607. wiphy->max_num_pmkids = 0;
  2608. return retval;
  2609. }
  2610. static void rndis_do_cqm(struct usbnet *usbdev, s32 rssi)
  2611. {
  2612. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2613. enum nl80211_cqm_rssi_threshold_event event;
  2614. int thold, hyst, last_event;
  2615. if (priv->cqm_rssi_thold >= 0 || rssi >= 0)
  2616. return;
  2617. if (priv->infra_mode != NDIS_80211_INFRA_INFRA)
  2618. return;
  2619. last_event = priv->last_cqm_event_rssi;
  2620. thold = priv->cqm_rssi_thold;
  2621. hyst = priv->cqm_rssi_hyst;
  2622. if (rssi < thold && (last_event == 0 || rssi < last_event - hyst))
  2623. event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
  2624. else if (rssi > thold && (last_event == 0 || rssi > last_event + hyst))
  2625. event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
  2626. else
  2627. return;
  2628. priv->last_cqm_event_rssi = rssi;
  2629. cfg80211_cqm_rssi_notify(usbdev->net, event, GFP_KERNEL);
  2630. }
  2631. #define DEVICE_POLLER_JIFFIES (HZ)
  2632. static void rndis_device_poller(struct work_struct *work)
  2633. {
  2634. struct rndis_wlan_private *priv =
  2635. container_of(work, struct rndis_wlan_private,
  2636. dev_poller_work.work);
  2637. struct usbnet *usbdev = priv->usbdev;
  2638. __le32 rssi, tmp;
  2639. int len, ret, j;
  2640. int update_jiffies = DEVICE_POLLER_JIFFIES;
  2641. void *buf;
  2642. /* Only check/do workaround when connected. Calling is_associated()
  2643. * also polls device with rndis_command() and catches for media link
  2644. * indications.
  2645. */
  2646. if (!is_associated(usbdev)) {
  2647. /* Workaround bad scanning in BCM4320a devices with active
  2648. * background scanning when not associated.
  2649. */
  2650. if (priv->device_type == RNDIS_BCM4320A && priv->radio_on &&
  2651. !priv->scan_request) {
  2652. /* Get previous scan results */
  2653. rndis_check_bssid_list(usbdev, NULL, NULL);
  2654. /* Initiate new scan */
  2655. rndis_start_bssid_list_scan(usbdev);
  2656. }
  2657. goto end;
  2658. }
  2659. len = sizeof(rssi);
  2660. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  2661. if (ret == 0) {
  2662. priv->last_qual = level_to_qual(le32_to_cpu(rssi));
  2663. rndis_do_cqm(usbdev, le32_to_cpu(rssi));
  2664. }
  2665. netdev_dbg(usbdev->net, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
  2666. ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
  2667. /* Workaround transfer stalls on poor quality links.
  2668. * TODO: find right way to fix these stalls (as stalls do not happen
  2669. * with ndiswrapper/windows driver). */
  2670. if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
  2671. /* Decrease stats worker interval to catch stalls.
  2672. * faster. Faster than 400-500ms causes packet loss,
  2673. * Slower doesn't catch stalls fast enough.
  2674. */
  2675. j = msecs_to_jiffies(priv->param_workaround_interval);
  2676. if (j > DEVICE_POLLER_JIFFIES)
  2677. j = DEVICE_POLLER_JIFFIES;
  2678. else if (j <= 0)
  2679. j = 1;
  2680. update_jiffies = j;
  2681. /* Send scan OID. Use of both OIDs is required to get device
  2682. * working.
  2683. */
  2684. tmp = cpu_to_le32(1);
  2685. rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  2686. sizeof(tmp));
  2687. len = CONTROL_BUFFER_SIZE;
  2688. buf = kmalloc(len, GFP_KERNEL);
  2689. if (!buf)
  2690. goto end;
  2691. rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  2692. kfree(buf);
  2693. }
  2694. end:
  2695. if (update_jiffies >= HZ)
  2696. update_jiffies = round_jiffies_relative(update_jiffies);
  2697. else {
  2698. j = round_jiffies_relative(update_jiffies);
  2699. if (abs(j - update_jiffies) <= 10)
  2700. update_jiffies = j;
  2701. }
  2702. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2703. update_jiffies);
  2704. }
  2705. /*
  2706. * driver/device initialization
  2707. */
  2708. static void rndis_copy_module_params(struct usbnet *usbdev, int device_type)
  2709. {
  2710. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2711. priv->device_type = device_type;
  2712. priv->param_country[0] = modparam_country[0];
  2713. priv->param_country[1] = modparam_country[1];
  2714. priv->param_country[2] = 0;
  2715. priv->param_frameburst = modparam_frameburst;
  2716. priv->param_afterburner = modparam_afterburner;
  2717. priv->param_power_save = modparam_power_save;
  2718. priv->param_power_output = modparam_power_output;
  2719. priv->param_roamtrigger = modparam_roamtrigger;
  2720. priv->param_roamdelta = modparam_roamdelta;
  2721. priv->param_country[0] = toupper(priv->param_country[0]);
  2722. priv->param_country[1] = toupper(priv->param_country[1]);
  2723. /* doesn't support EU as country code, use FI instead */
  2724. if (!strcmp(priv->param_country, "EU"))
  2725. strcpy(priv->param_country, "FI");
  2726. if (priv->param_power_save < 0)
  2727. priv->param_power_save = 0;
  2728. else if (priv->param_power_save > 2)
  2729. priv->param_power_save = 2;
  2730. if (priv->param_power_output < 0)
  2731. priv->param_power_output = 0;
  2732. else if (priv->param_power_output > 3)
  2733. priv->param_power_output = 3;
  2734. if (priv->param_roamtrigger < -80)
  2735. priv->param_roamtrigger = -80;
  2736. else if (priv->param_roamtrigger > -60)
  2737. priv->param_roamtrigger = -60;
  2738. if (priv->param_roamdelta < 0)
  2739. priv->param_roamdelta = 0;
  2740. else if (priv->param_roamdelta > 2)
  2741. priv->param_roamdelta = 2;
  2742. if (modparam_workaround_interval < 0)
  2743. priv->param_workaround_interval = 500;
  2744. else
  2745. priv->param_workaround_interval = modparam_workaround_interval;
  2746. }
  2747. static int unknown_early_init(struct usbnet *usbdev)
  2748. {
  2749. /* copy module parameters for unknown so that iwconfig reports txpower
  2750. * and workaround parameter is copied to private structure correctly.
  2751. */
  2752. rndis_copy_module_params(usbdev, RNDIS_UNKNOWN);
  2753. /* This is unknown device, so do not try set configuration parameters.
  2754. */
  2755. return 0;
  2756. }
  2757. static int bcm4320a_early_init(struct usbnet *usbdev)
  2758. {
  2759. /* copy module parameters for bcm4320a so that iwconfig reports txpower
  2760. * and workaround parameter is copied to private structure correctly.
  2761. */
  2762. rndis_copy_module_params(usbdev, RNDIS_BCM4320A);
  2763. /* bcm4320a doesn't handle configuration parameters well. Try
  2764. * set any and you get partially zeroed mac and broken device.
  2765. */
  2766. return 0;
  2767. }
  2768. static int bcm4320b_early_init(struct usbnet *usbdev)
  2769. {
  2770. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2771. char buf[8];
  2772. rndis_copy_module_params(usbdev, RNDIS_BCM4320B);
  2773. /* Early initialization settings, setting these won't have effect
  2774. * if called after generic_rndis_bind().
  2775. */
  2776. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2777. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2778. priv->param_frameburst ? "1" : "0");
  2779. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2780. priv->param_afterburner ? "1" : "0");
  2781. sprintf(buf, "%d", priv->param_power_save);
  2782. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2783. sprintf(buf, "%d", priv->param_power_output);
  2784. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2785. sprintf(buf, "%d", priv->param_roamtrigger);
  2786. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2787. sprintf(buf, "%d", priv->param_roamdelta);
  2788. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2789. return 0;
  2790. }
  2791. /* same as rndis_netdev_ops but with local multicast handler */
  2792. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2793. .ndo_open = usbnet_open,
  2794. .ndo_stop = usbnet_stop,
  2795. .ndo_start_xmit = usbnet_start_xmit,
  2796. .ndo_tx_timeout = usbnet_tx_timeout,
  2797. .ndo_set_mac_address = eth_mac_addr,
  2798. .ndo_validate_addr = eth_validate_addr,
  2799. .ndo_set_rx_mode = rndis_wlan_set_multicast_list,
  2800. };
  2801. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2802. {
  2803. struct wiphy *wiphy;
  2804. struct rndis_wlan_private *priv;
  2805. int retval, len;
  2806. __le32 tmp;
  2807. /* allocate wiphy and rndis private data
  2808. * NOTE: We only support a single virtual interface, so wiphy
  2809. * and wireless_dev are somewhat synonymous for this device.
  2810. */
  2811. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2812. if (!wiphy)
  2813. return -ENOMEM;
  2814. priv = wiphy_priv(wiphy);
  2815. usbdev->net->ieee80211_ptr = &priv->wdev;
  2816. priv->wdev.wiphy = wiphy;
  2817. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2818. /* These have to be initialized before calling generic_rndis_bind().
  2819. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2820. */
  2821. usbdev->driver_priv = priv;
  2822. priv->usbdev = usbdev;
  2823. mutex_init(&priv->command_lock);
  2824. /* because rndis_command() sleeps we need to use workqueue */
  2825. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2826. INIT_WORK(&priv->work, rndis_wlan_worker);
  2827. INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
  2828. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2829. /* try bind rndis_host */
  2830. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2831. if (retval < 0)
  2832. goto fail;
  2833. /* generic_rndis_bind set packet filter to multicast_all+
  2834. * promisc mode which doesn't work well for our devices (device
  2835. * picks up rssi to closest station instead of to access point).
  2836. *
  2837. * rndis_host wants to avoid all OID as much as possible
  2838. * so do promisc/multicast handling in rndis_wlan.
  2839. */
  2840. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2841. tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  2842. retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
  2843. sizeof(tmp));
  2844. len = sizeof(tmp);
  2845. retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
  2846. &len);
  2847. priv->multicast_size = le32_to_cpu(tmp);
  2848. if (retval < 0 || priv->multicast_size < 0)
  2849. priv->multicast_size = 0;
  2850. if (priv->multicast_size > 0)
  2851. usbdev->net->flags |= IFF_MULTICAST;
  2852. else
  2853. usbdev->net->flags &= ~IFF_MULTICAST;
  2854. /* fill-out wiphy structure and register w/ cfg80211 */
  2855. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2856. wiphy->privid = rndis_wiphy_privid;
  2857. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2858. | BIT(NL80211_IFTYPE_ADHOC);
  2859. wiphy->max_scan_ssids = 1;
  2860. /* TODO: fill-out band/encr information based on priv->caps */
  2861. rndis_wlan_get_caps(usbdev, wiphy);
  2862. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2863. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2864. priv->band.channels = priv->channels;
  2865. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2866. priv->band.bitrates = priv->rates;
  2867. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2868. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2869. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2870. memcpy(priv->cipher_suites, rndis_cipher_suites,
  2871. sizeof(rndis_cipher_suites));
  2872. wiphy->cipher_suites = priv->cipher_suites;
  2873. wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
  2874. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2875. if (wiphy_register(wiphy)) {
  2876. retval = -ENODEV;
  2877. goto fail;
  2878. }
  2879. set_default_iw_params(usbdev);
  2880. priv->power_mode = -1;
  2881. /* set default rts/frag */
  2882. rndis_set_wiphy_params(wiphy,
  2883. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2884. /* turn radio off on init */
  2885. priv->radio_on = false;
  2886. disassociate(usbdev, false);
  2887. netif_carrier_off(usbdev->net);
  2888. return 0;
  2889. fail:
  2890. cancel_delayed_work_sync(&priv->dev_poller_work);
  2891. cancel_delayed_work_sync(&priv->scan_work);
  2892. cancel_work_sync(&priv->work);
  2893. flush_workqueue(priv->workqueue);
  2894. destroy_workqueue(priv->workqueue);
  2895. wiphy_free(wiphy);
  2896. return retval;
  2897. }
  2898. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2899. {
  2900. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2901. /* turn radio off */
  2902. disassociate(usbdev, false);
  2903. cancel_delayed_work_sync(&priv->dev_poller_work);
  2904. cancel_delayed_work_sync(&priv->scan_work);
  2905. cancel_work_sync(&priv->work);
  2906. flush_workqueue(priv->workqueue);
  2907. destroy_workqueue(priv->workqueue);
  2908. rndis_unbind(usbdev, intf);
  2909. wiphy_unregister(priv->wdev.wiphy);
  2910. wiphy_free(priv->wdev.wiphy);
  2911. }
  2912. static int rndis_wlan_reset(struct usbnet *usbdev)
  2913. {
  2914. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2915. int retval;
  2916. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2917. retval = rndis_reset(usbdev);
  2918. if (retval)
  2919. netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
  2920. /* rndis_reset cleared multicast list, so restore here.
  2921. (set_multicast_list() also turns on current packet filter) */
  2922. set_multicast_list(usbdev);
  2923. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2924. round_jiffies_relative(DEVICE_POLLER_JIFFIES));
  2925. return deauthenticate(usbdev);
  2926. }
  2927. static int rndis_wlan_stop(struct usbnet *usbdev)
  2928. {
  2929. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2930. int retval;
  2931. __le32 filter;
  2932. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2933. retval = disassociate(usbdev, false);
  2934. priv->work_pending = 0;
  2935. cancel_delayed_work_sync(&priv->dev_poller_work);
  2936. cancel_delayed_work_sync(&priv->scan_work);
  2937. cancel_work_sync(&priv->work);
  2938. flush_workqueue(priv->workqueue);
  2939. if (priv->scan_request) {
  2940. cfg80211_scan_done(priv->scan_request, true);
  2941. priv->scan_request = NULL;
  2942. }
  2943. /* Set current packet filter zero to block receiving data packets from
  2944. device. */
  2945. filter = 0;
  2946. rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2947. sizeof(filter));
  2948. return retval;
  2949. }
  2950. static const struct driver_info bcm4320b_info = {
  2951. .description = "Wireless RNDIS device, BCM4320b based",
  2952. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2953. FLAG_AVOID_UNLINK_URBS,
  2954. .bind = rndis_wlan_bind,
  2955. .unbind = rndis_wlan_unbind,
  2956. .status = rndis_status,
  2957. .rx_fixup = rndis_rx_fixup,
  2958. .tx_fixup = rndis_tx_fixup,
  2959. .reset = rndis_wlan_reset,
  2960. .stop = rndis_wlan_stop,
  2961. .early_init = bcm4320b_early_init,
  2962. .indication = rndis_wlan_indication,
  2963. };
  2964. static const struct driver_info bcm4320a_info = {
  2965. .description = "Wireless RNDIS device, BCM4320a based",
  2966. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2967. FLAG_AVOID_UNLINK_URBS,
  2968. .bind = rndis_wlan_bind,
  2969. .unbind = rndis_wlan_unbind,
  2970. .status = rndis_status,
  2971. .rx_fixup = rndis_rx_fixup,
  2972. .tx_fixup = rndis_tx_fixup,
  2973. .reset = rndis_wlan_reset,
  2974. .stop = rndis_wlan_stop,
  2975. .early_init = bcm4320a_early_init,
  2976. .indication = rndis_wlan_indication,
  2977. };
  2978. static const struct driver_info rndis_wlan_info = {
  2979. .description = "Wireless RNDIS device",
  2980. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2981. FLAG_AVOID_UNLINK_URBS,
  2982. .bind = rndis_wlan_bind,
  2983. .unbind = rndis_wlan_unbind,
  2984. .status = rndis_status,
  2985. .rx_fixup = rndis_rx_fixup,
  2986. .tx_fixup = rndis_tx_fixup,
  2987. .reset = rndis_wlan_reset,
  2988. .stop = rndis_wlan_stop,
  2989. .early_init = unknown_early_init,
  2990. .indication = rndis_wlan_indication,
  2991. };
  2992. /*-------------------------------------------------------------------------*/
  2993. static const struct usb_device_id products [] = {
  2994. #define RNDIS_MASTER_INTERFACE \
  2995. .bInterfaceClass = USB_CLASS_COMM, \
  2996. .bInterfaceSubClass = 2 /* ACM */, \
  2997. .bInterfaceProtocol = 0x0ff
  2998. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2999. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  3000. */
  3001. {
  3002. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3003. | USB_DEVICE_ID_MATCH_DEVICE,
  3004. .idVendor = 0x0411,
  3005. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  3006. RNDIS_MASTER_INTERFACE,
  3007. .driver_info = (unsigned long) &bcm4320b_info,
  3008. }, {
  3009. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3010. | USB_DEVICE_ID_MATCH_DEVICE,
  3011. .idVendor = 0x0baf,
  3012. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  3013. RNDIS_MASTER_INTERFACE,
  3014. .driver_info = (unsigned long) &bcm4320b_info,
  3015. }, {
  3016. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3017. | USB_DEVICE_ID_MATCH_DEVICE,
  3018. .idVendor = 0x050d,
  3019. .idProduct = 0x011b, /* Belkin F5D7051 */
  3020. RNDIS_MASTER_INTERFACE,
  3021. .driver_info = (unsigned long) &bcm4320b_info,
  3022. }, {
  3023. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3024. | USB_DEVICE_ID_MATCH_DEVICE,
  3025. .idVendor = 0x1799, /* Belkin has two vendor ids */
  3026. .idProduct = 0x011b, /* Belkin F5D7051 */
  3027. RNDIS_MASTER_INTERFACE,
  3028. .driver_info = (unsigned long) &bcm4320b_info,
  3029. }, {
  3030. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3031. | USB_DEVICE_ID_MATCH_DEVICE,
  3032. .idVendor = 0x13b1,
  3033. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  3034. RNDIS_MASTER_INTERFACE,
  3035. .driver_info = (unsigned long) &bcm4320b_info,
  3036. }, {
  3037. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3038. | USB_DEVICE_ID_MATCH_DEVICE,
  3039. .idVendor = 0x13b1,
  3040. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  3041. RNDIS_MASTER_INTERFACE,
  3042. .driver_info = (unsigned long) &bcm4320b_info,
  3043. }, {
  3044. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3045. | USB_DEVICE_ID_MATCH_DEVICE,
  3046. .idVendor = 0x0b05,
  3047. .idProduct = 0x1717, /* Asus WL169gE */
  3048. RNDIS_MASTER_INTERFACE,
  3049. .driver_info = (unsigned long) &bcm4320b_info,
  3050. }, {
  3051. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3052. | USB_DEVICE_ID_MATCH_DEVICE,
  3053. .idVendor = 0x0a5c,
  3054. .idProduct = 0xd11b, /* Eminent EM4045 */
  3055. RNDIS_MASTER_INTERFACE,
  3056. .driver_info = (unsigned long) &bcm4320b_info,
  3057. }, {
  3058. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3059. | USB_DEVICE_ID_MATCH_DEVICE,
  3060. .idVendor = 0x1690,
  3061. .idProduct = 0x0715, /* BT Voyager 1055 */
  3062. RNDIS_MASTER_INTERFACE,
  3063. .driver_info = (unsigned long) &bcm4320b_info,
  3064. },
  3065. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  3066. * parameters available, hardware probably contain older firmware version with
  3067. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  3068. */
  3069. {
  3070. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3071. | USB_DEVICE_ID_MATCH_DEVICE,
  3072. .idVendor = 0x13b1,
  3073. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  3074. RNDIS_MASTER_INTERFACE,
  3075. .driver_info = (unsigned long) &bcm4320a_info,
  3076. }, {
  3077. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3078. | USB_DEVICE_ID_MATCH_DEVICE,
  3079. .idVendor = 0x0baf,
  3080. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  3081. RNDIS_MASTER_INTERFACE,
  3082. .driver_info = (unsigned long) &bcm4320a_info,
  3083. }, {
  3084. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3085. | USB_DEVICE_ID_MATCH_DEVICE,
  3086. .idVendor = 0x0411,
  3087. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  3088. RNDIS_MASTER_INTERFACE,
  3089. .driver_info = (unsigned long) &bcm4320a_info,
  3090. },
  3091. /* Generic Wireless RNDIS devices that we don't have exact
  3092. * idVendor/idProduct/chip yet.
  3093. */
  3094. {
  3095. /* RNDIS is MSFT's un-official variant of CDC ACM */
  3096. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  3097. .driver_info = (unsigned long) &rndis_wlan_info,
  3098. }, {
  3099. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  3100. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  3101. .driver_info = (unsigned long) &rndis_wlan_info,
  3102. },
  3103. { }, // END
  3104. };
  3105. MODULE_DEVICE_TABLE(usb, products);
  3106. static struct usb_driver rndis_wlan_driver = {
  3107. .name = "rndis_wlan",
  3108. .id_table = products,
  3109. .probe = usbnet_probe,
  3110. .disconnect = usbnet_disconnect,
  3111. .suspend = usbnet_suspend,
  3112. .resume = usbnet_resume,
  3113. };
  3114. module_usb_driver(rndis_wlan_driver);
  3115. MODULE_AUTHOR("Bjorge Dijkstra");
  3116. MODULE_AUTHOR("Jussi Kivilinna");
  3117. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  3118. MODULE_LICENSE("GPL");