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