rndis_wlan.c 100 KB

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