rndis_wlan.c 102 KB

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