rndis_wlan.c 100 KB

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