rndis_wlan.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258
  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/wireless.h>
  38. #include <linux/ieee80211.h>
  39. #include <linux/if_arp.h>
  40. #include <linux/ctype.h>
  41. #include <linux/spinlock.h>
  42. #include <net/iw_handler.h>
  43. #include <net/cfg80211.h>
  44. #include <linux/usb/usbnet.h>
  45. #include <linux/usb/rndis_host.h>
  46. /* NOTE: All these are settings for Broadcom chipset */
  47. static char modparam_country[4] = "EU";
  48. module_param_string(country, modparam_country, 4, 0444);
  49. MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
  50. static int modparam_frameburst = 1;
  51. module_param_named(frameburst, modparam_frameburst, int, 0444);
  52. MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
  53. static int modparam_afterburner = 0;
  54. module_param_named(afterburner, modparam_afterburner, int, 0444);
  55. MODULE_PARM_DESC(afterburner,
  56. "enable afterburner aka '125 High Speed Mode' (default: off)");
  57. static int modparam_power_save = 0;
  58. module_param_named(power_save, modparam_power_save, int, 0444);
  59. MODULE_PARM_DESC(power_save,
  60. "set power save mode: 0=off, 1=on, 2=fast (default: off)");
  61. static int modparam_power_output = 3;
  62. module_param_named(power_output, modparam_power_output, int, 0444);
  63. MODULE_PARM_DESC(power_output,
  64. "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
  65. static int modparam_roamtrigger = -70;
  66. module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
  67. MODULE_PARM_DESC(roamtrigger,
  68. "set roaming dBm trigger: -80=optimize for distance, "
  69. "-60=bandwidth (default: -70)");
  70. static int modparam_roamdelta = 1;
  71. module_param_named(roamdelta, modparam_roamdelta, int, 0444);
  72. MODULE_PARM_DESC(roamdelta,
  73. "set roaming tendency: 0=aggressive, 1=moderate, "
  74. "2=conservative (default: moderate)");
  75. static int modparam_workaround_interval = 500;
  76. module_param_named(workaround_interval, modparam_workaround_interval,
  77. int, 0444);
  78. MODULE_PARM_DESC(workaround_interval,
  79. "set stall workaround interval in msecs (default: 500)");
  80. /* various RNDIS OID defs */
  81. #define OID_GEN_LINK_SPEED cpu_to_le32(0x00010107)
  82. #define OID_GEN_RNDIS_CONFIG_PARAMETER cpu_to_le32(0x0001021b)
  83. #define OID_GEN_XMIT_OK cpu_to_le32(0x00020101)
  84. #define OID_GEN_RCV_OK cpu_to_le32(0x00020102)
  85. #define OID_GEN_XMIT_ERROR cpu_to_le32(0x00020103)
  86. #define OID_GEN_RCV_ERROR cpu_to_le32(0x00020104)
  87. #define OID_GEN_RCV_NO_BUFFER cpu_to_le32(0x00020105)
  88. #define OID_802_3_CURRENT_ADDRESS cpu_to_le32(0x01010102)
  89. #define OID_802_3_MULTICAST_LIST cpu_to_le32(0x01010103)
  90. #define OID_802_3_MAXIMUM_LIST_SIZE cpu_to_le32(0x01010104)
  91. #define OID_802_11_BSSID cpu_to_le32(0x0d010101)
  92. #define OID_802_11_SSID cpu_to_le32(0x0d010102)
  93. #define OID_802_11_INFRASTRUCTURE_MODE cpu_to_le32(0x0d010108)
  94. #define OID_802_11_ADD_WEP cpu_to_le32(0x0d010113)
  95. #define OID_802_11_REMOVE_WEP cpu_to_le32(0x0d010114)
  96. #define OID_802_11_DISASSOCIATE cpu_to_le32(0x0d010115)
  97. #define OID_802_11_AUTHENTICATION_MODE cpu_to_le32(0x0d010118)
  98. #define OID_802_11_PRIVACY_FILTER cpu_to_le32(0x0d010119)
  99. #define OID_802_11_BSSID_LIST_SCAN cpu_to_le32(0x0d01011a)
  100. #define OID_802_11_ENCRYPTION_STATUS cpu_to_le32(0x0d01011b)
  101. #define OID_802_11_ADD_KEY cpu_to_le32(0x0d01011d)
  102. #define OID_802_11_REMOVE_KEY cpu_to_le32(0x0d01011e)
  103. #define OID_802_11_ASSOCIATION_INFORMATION cpu_to_le32(0x0d01011f)
  104. #define OID_802_11_PMKID cpu_to_le32(0x0d010123)
  105. #define OID_802_11_NETWORK_TYPES_SUPPORTED cpu_to_le32(0x0d010203)
  106. #define OID_802_11_NETWORK_TYPE_IN_USE cpu_to_le32(0x0d010204)
  107. #define OID_802_11_TX_POWER_LEVEL cpu_to_le32(0x0d010205)
  108. #define OID_802_11_RSSI cpu_to_le32(0x0d010206)
  109. #define OID_802_11_RSSI_TRIGGER cpu_to_le32(0x0d010207)
  110. #define OID_802_11_FRAGMENTATION_THRESHOLD cpu_to_le32(0x0d010209)
  111. #define OID_802_11_RTS_THRESHOLD cpu_to_le32(0x0d01020a)
  112. #define OID_802_11_SUPPORTED_RATES cpu_to_le32(0x0d01020e)
  113. #define OID_802_11_CONFIGURATION cpu_to_le32(0x0d010211)
  114. #define OID_802_11_BSSID_LIST cpu_to_le32(0x0d010217)
  115. /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
  116. #define WL_NOISE -96 /* typical noise level in dBm */
  117. #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
  118. /* Assume that Broadcom 4320 (only chipset at time of writing known to be
  119. * based on wireless rndis) has default txpower of 13dBm.
  120. * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
  121. * 100% : 20 mW ~ 13dBm
  122. * 75% : 15 mW ~ 12dBm
  123. * 50% : 10 mW ~ 10dBm
  124. * 25% : 5 mW ~ 7dBm
  125. */
  126. #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
  127. #define BCM4320_DEFAULT_TXPOWER_DBM_75 12
  128. #define BCM4320_DEFAULT_TXPOWER_DBM_50 10
  129. #define BCM4320_DEFAULT_TXPOWER_DBM_25 7
  130. /* codes for "status" field of completion messages */
  131. #define RNDIS_STATUS_ADAPTER_NOT_READY cpu_to_le32(0xc0010011)
  132. #define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(0xc0010012)
  133. /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
  134. * slightly modified for datatype endianess, etc
  135. */
  136. #define NDIS_802_11_LENGTH_SSID 32
  137. #define NDIS_802_11_LENGTH_RATES 8
  138. #define NDIS_802_11_LENGTH_RATES_EX 16
  139. enum ndis_80211_net_type {
  140. NDIS_80211_TYPE_FREQ_HOP,
  141. NDIS_80211_TYPE_DIRECT_SEQ,
  142. NDIS_80211_TYPE_OFDM_A,
  143. NDIS_80211_TYPE_OFDM_G
  144. };
  145. enum ndis_80211_net_infra {
  146. NDIS_80211_INFRA_ADHOC,
  147. NDIS_80211_INFRA_INFRA,
  148. NDIS_80211_INFRA_AUTO_UNKNOWN
  149. };
  150. enum ndis_80211_auth_mode {
  151. NDIS_80211_AUTH_OPEN,
  152. NDIS_80211_AUTH_SHARED,
  153. NDIS_80211_AUTH_AUTO_SWITCH,
  154. NDIS_80211_AUTH_WPA,
  155. NDIS_80211_AUTH_WPA_PSK,
  156. NDIS_80211_AUTH_WPA_NONE,
  157. NDIS_80211_AUTH_WPA2,
  158. NDIS_80211_AUTH_WPA2_PSK
  159. };
  160. enum ndis_80211_encr_status {
  161. NDIS_80211_ENCR_WEP_ENABLED,
  162. NDIS_80211_ENCR_DISABLED,
  163. NDIS_80211_ENCR_WEP_KEY_ABSENT,
  164. NDIS_80211_ENCR_NOT_SUPPORTED,
  165. NDIS_80211_ENCR_TKIP_ENABLED,
  166. NDIS_80211_ENCR_TKIP_KEY_ABSENT,
  167. NDIS_80211_ENCR_CCMP_ENABLED,
  168. NDIS_80211_ENCR_CCMP_KEY_ABSENT
  169. };
  170. enum ndis_80211_priv_filter {
  171. NDIS_80211_PRIV_ACCEPT_ALL,
  172. NDIS_80211_PRIV_8021X_WEP
  173. };
  174. enum ndis_80211_status_type {
  175. NDIS_80211_STATUSTYPE_AUTHENTICATION,
  176. NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
  177. NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
  178. NDIS_80211_STATUSTYPE_RADIOSTATE,
  179. };
  180. enum ndis_80211_media_stream_mode {
  181. NDIS_80211_MEDIA_STREAM_OFF,
  182. NDIS_80211_MEDIA_STREAM_ON
  183. };
  184. enum ndis_80211_radio_status {
  185. NDIS_80211_RADIO_STATUS_ON,
  186. NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
  187. NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
  188. };
  189. enum ndis_80211_addkey_bits {
  190. NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
  191. NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
  192. NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
  193. NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  194. };
  195. enum ndis_80211_addwep_bits {
  196. NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
  197. NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  198. };
  199. struct ndis_80211_auth_request {
  200. __le32 length;
  201. u8 bssid[6];
  202. u8 padding[2];
  203. __le32 flags;
  204. } __attribute__((packed));
  205. struct ndis_80211_pmkid_candidate {
  206. u8 bssid[6];
  207. u8 padding[2];
  208. __le32 flags;
  209. } __attribute__((packed));
  210. struct ndis_80211_pmkid_cand_list {
  211. __le32 version;
  212. __le32 num_candidates;
  213. struct ndis_80211_pmkid_candidate candidate_list[0];
  214. } __attribute__((packed));
  215. struct ndis_80211_status_indication {
  216. __le32 status_type;
  217. union {
  218. enum ndis_80211_media_stream_mode media_stream_mode;
  219. enum ndis_80211_radio_status radio_status;
  220. struct ndis_80211_auth_request auth_request[0];
  221. struct ndis_80211_pmkid_cand_list cand_list;
  222. } u;
  223. } __attribute__((packed));
  224. struct ndis_80211_ssid {
  225. __le32 length;
  226. u8 essid[NDIS_802_11_LENGTH_SSID];
  227. } __attribute__((packed));
  228. struct ndis_80211_conf_freq_hop {
  229. __le32 length;
  230. __le32 hop_pattern;
  231. __le32 hop_set;
  232. __le32 dwell_time;
  233. } __attribute__((packed));
  234. struct ndis_80211_conf {
  235. __le32 length;
  236. __le32 beacon_period;
  237. __le32 atim_window;
  238. __le32 ds_config;
  239. struct ndis_80211_conf_freq_hop fh_config;
  240. } __attribute__((packed));
  241. struct ndis_80211_bssid_ex {
  242. __le32 length;
  243. u8 mac[6];
  244. u8 padding[2];
  245. struct ndis_80211_ssid ssid;
  246. __le32 privacy;
  247. __le32 rssi;
  248. __le32 net_type;
  249. struct ndis_80211_conf config;
  250. __le32 net_infra;
  251. u8 rates[NDIS_802_11_LENGTH_RATES_EX];
  252. __le32 ie_length;
  253. u8 ies[0];
  254. } __attribute__((packed));
  255. struct ndis_80211_bssid_list_ex {
  256. __le32 num_items;
  257. struct ndis_80211_bssid_ex bssid[0];
  258. } __attribute__((packed));
  259. struct ndis_80211_fixed_ies {
  260. u8 timestamp[8];
  261. __le16 beacon_interval;
  262. __le16 capabilities;
  263. } __attribute__((packed));
  264. struct ndis_80211_wep_key {
  265. __le32 size;
  266. __le32 index;
  267. __le32 length;
  268. u8 material[32];
  269. } __attribute__((packed));
  270. struct ndis_80211_key {
  271. __le32 size;
  272. __le32 index;
  273. __le32 length;
  274. u8 bssid[6];
  275. u8 padding[6];
  276. u8 rsc[8];
  277. u8 material[32];
  278. } __attribute__((packed));
  279. struct ndis_80211_remove_key {
  280. __le32 size;
  281. __le32 index;
  282. u8 bssid[6];
  283. u8 padding[2];
  284. } __attribute__((packed));
  285. struct ndis_config_param {
  286. __le32 name_offs;
  287. __le32 name_length;
  288. __le32 type;
  289. __le32 value_offs;
  290. __le32 value_length;
  291. } __attribute__((packed));
  292. struct ndis_80211_assoc_info {
  293. __le32 length;
  294. __le16 req_ies;
  295. struct req_ie {
  296. __le16 capa;
  297. __le16 listen_interval;
  298. u8 cur_ap_address[6];
  299. } req_ie;
  300. __le32 req_ie_length;
  301. __le32 offset_req_ies;
  302. __le16 resp_ies;
  303. struct resp_ie {
  304. __le16 capa;
  305. __le16 status_code;
  306. __le16 assoc_id;
  307. } resp_ie;
  308. __le32 resp_ie_length;
  309. __le32 offset_resp_ies;
  310. } __attribute__((packed));
  311. /* these have to match what is in wpa_supplicant */
  312. enum wpa_alg { WPA_ALG_NONE, WPA_ALG_WEP, WPA_ALG_TKIP, WPA_ALG_CCMP };
  313. enum wpa_cipher { CIPHER_NONE, CIPHER_WEP40, CIPHER_TKIP, CIPHER_CCMP,
  314. CIPHER_WEP104 };
  315. enum wpa_key_mgmt { KEY_MGMT_802_1X, KEY_MGMT_PSK, KEY_MGMT_NONE,
  316. KEY_MGMT_802_1X_NO_WPA, KEY_MGMT_WPA_NONE };
  317. /*
  318. * private data
  319. */
  320. #define NET_TYPE_11FB 0
  321. #define CAP_MODE_80211A 1
  322. #define CAP_MODE_80211B 2
  323. #define CAP_MODE_80211G 4
  324. #define CAP_MODE_MASK 7
  325. #define WORK_LINK_UP (1<<0)
  326. #define WORK_LINK_DOWN (1<<1)
  327. #define WORK_SET_MULTICAST_LIST (1<<2)
  328. #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
  329. static const struct ieee80211_channel rndis_channels[] = {
  330. { .center_freq = 2412 },
  331. { .center_freq = 2417 },
  332. { .center_freq = 2422 },
  333. { .center_freq = 2427 },
  334. { .center_freq = 2432 },
  335. { .center_freq = 2437 },
  336. { .center_freq = 2442 },
  337. { .center_freq = 2447 },
  338. { .center_freq = 2452 },
  339. { .center_freq = 2457 },
  340. { .center_freq = 2462 },
  341. { .center_freq = 2467 },
  342. { .center_freq = 2472 },
  343. { .center_freq = 2484 },
  344. };
  345. static const struct ieee80211_rate rndis_rates[] = {
  346. { .bitrate = 10 },
  347. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  348. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  349. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  350. { .bitrate = 60 },
  351. { .bitrate = 90 },
  352. { .bitrate = 120 },
  353. { .bitrate = 180 },
  354. { .bitrate = 240 },
  355. { .bitrate = 360 },
  356. { .bitrate = 480 },
  357. { .bitrate = 540 }
  358. };
  359. static const u32 rndis_cipher_suites[] = {
  360. WLAN_CIPHER_SUITE_WEP40,
  361. WLAN_CIPHER_SUITE_WEP104,
  362. WLAN_CIPHER_SUITE_TKIP,
  363. WLAN_CIPHER_SUITE_CCMP,
  364. };
  365. struct rndis_wlan_encr_key {
  366. int len;
  367. int cipher;
  368. u8 material[32];
  369. u8 bssid[ETH_ALEN];
  370. bool pairwise;
  371. bool tx_key;
  372. };
  373. /* RNDIS device private data */
  374. struct rndis_wlan_private {
  375. struct usbnet *usbdev;
  376. struct wireless_dev wdev;
  377. struct cfg80211_scan_request *scan_request;
  378. struct workqueue_struct *workqueue;
  379. struct delayed_work stats_work;
  380. struct delayed_work scan_work;
  381. struct work_struct work;
  382. struct mutex command_lock;
  383. spinlock_t stats_lock;
  384. unsigned long work_pending;
  385. struct ieee80211_supported_band band;
  386. struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
  387. struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
  388. u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
  389. struct iw_statistics iwstats;
  390. struct iw_statistics privstats;
  391. int caps;
  392. int multicast_size;
  393. /* module parameters */
  394. char param_country[4];
  395. int param_frameburst;
  396. int param_afterburner;
  397. int param_power_save;
  398. int param_power_output;
  399. int param_roamtrigger;
  400. int param_roamdelta;
  401. u32 param_workaround_interval;
  402. /* hardware state */
  403. int radio_on;
  404. int infra_mode;
  405. struct ndis_80211_ssid essid;
  406. __le32 current_command_oid;
  407. /* encryption stuff */
  408. int encr_tx_key_index;
  409. struct rndis_wlan_encr_key encr_keys[4];
  410. int wpa_version;
  411. int wpa_keymgmt;
  412. int wpa_authalg;
  413. int wpa_ie_len;
  414. u8 *wpa_ie;
  415. int wpa_cipher_pair;
  416. int wpa_cipher_group;
  417. u8 command_buffer[COMMAND_BUFFER_SIZE];
  418. };
  419. /*
  420. * cfg80211 ops
  421. */
  422. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  423. struct net_device *dev,
  424. enum nl80211_iftype type, u32 *flags,
  425. struct vif_params *params);
  426. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  427. struct cfg80211_scan_request *request);
  428. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
  429. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  430. int dbm);
  431. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
  432. static struct cfg80211_ops rndis_config_ops = {
  433. .change_virtual_intf = rndis_change_virtual_intf,
  434. .scan = rndis_scan,
  435. .set_wiphy_params = rndis_set_wiphy_params,
  436. .set_tx_power = rndis_set_tx_power,
  437. .get_tx_power = rndis_get_tx_power,
  438. };
  439. static void *rndis_wiphy_privid = &rndis_wiphy_privid;
  440. static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
  441. {
  442. return (struct rndis_wlan_private *)dev->driver_priv;
  443. }
  444. static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
  445. {
  446. switch (priv->param_power_output) {
  447. default:
  448. case 3:
  449. return BCM4320_DEFAULT_TXPOWER_DBM_100;
  450. case 2:
  451. return BCM4320_DEFAULT_TXPOWER_DBM_75;
  452. case 1:
  453. return BCM4320_DEFAULT_TXPOWER_DBM_50;
  454. case 0:
  455. return BCM4320_DEFAULT_TXPOWER_DBM_25;
  456. }
  457. }
  458. static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
  459. {
  460. int cipher = priv->encr_keys[idx].cipher;
  461. return (cipher == WLAN_CIPHER_SUITE_CCMP ||
  462. cipher == WLAN_CIPHER_SUITE_TKIP);
  463. }
  464. #ifdef DEBUG
  465. static const char *oid_to_string(__le32 oid)
  466. {
  467. switch (oid) {
  468. #define OID_STR(oid) case oid: return(#oid)
  469. /* from rndis_host.h */
  470. OID_STR(OID_802_3_PERMANENT_ADDRESS);
  471. OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
  472. OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
  473. OID_STR(OID_GEN_PHYSICAL_MEDIUM);
  474. /* from rndis_wlan.c */
  475. OID_STR(OID_GEN_LINK_SPEED);
  476. OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
  477. OID_STR(OID_GEN_XMIT_OK);
  478. OID_STR(OID_GEN_RCV_OK);
  479. OID_STR(OID_GEN_XMIT_ERROR);
  480. OID_STR(OID_GEN_RCV_ERROR);
  481. OID_STR(OID_GEN_RCV_NO_BUFFER);
  482. OID_STR(OID_802_3_CURRENT_ADDRESS);
  483. OID_STR(OID_802_3_MULTICAST_LIST);
  484. OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
  485. OID_STR(OID_802_11_BSSID);
  486. OID_STR(OID_802_11_SSID);
  487. OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
  488. OID_STR(OID_802_11_ADD_WEP);
  489. OID_STR(OID_802_11_REMOVE_WEP);
  490. OID_STR(OID_802_11_DISASSOCIATE);
  491. OID_STR(OID_802_11_AUTHENTICATION_MODE);
  492. OID_STR(OID_802_11_PRIVACY_FILTER);
  493. OID_STR(OID_802_11_BSSID_LIST_SCAN);
  494. OID_STR(OID_802_11_ENCRYPTION_STATUS);
  495. OID_STR(OID_802_11_ADD_KEY);
  496. OID_STR(OID_802_11_REMOVE_KEY);
  497. OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
  498. OID_STR(OID_802_11_PMKID);
  499. OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
  500. OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
  501. OID_STR(OID_802_11_TX_POWER_LEVEL);
  502. OID_STR(OID_802_11_RSSI);
  503. OID_STR(OID_802_11_RSSI_TRIGGER);
  504. OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
  505. OID_STR(OID_802_11_RTS_THRESHOLD);
  506. OID_STR(OID_802_11_SUPPORTED_RATES);
  507. OID_STR(OID_802_11_CONFIGURATION);
  508. OID_STR(OID_802_11_BSSID_LIST);
  509. #undef OID_STR
  510. }
  511. return "?";
  512. }
  513. #else
  514. static const char *oid_to_string(__le32 oid)
  515. {
  516. return "?";
  517. }
  518. #endif
  519. /* translate error code */
  520. static int rndis_error_status(__le32 rndis_status)
  521. {
  522. int ret = -EINVAL;
  523. switch (rndis_status) {
  524. case RNDIS_STATUS_SUCCESS:
  525. ret = 0;
  526. break;
  527. case RNDIS_STATUS_FAILURE:
  528. case RNDIS_STATUS_INVALID_DATA:
  529. ret = -EINVAL;
  530. break;
  531. case RNDIS_STATUS_NOT_SUPPORTED:
  532. ret = -EOPNOTSUPP;
  533. break;
  534. case RNDIS_STATUS_ADAPTER_NOT_READY:
  535. case RNDIS_STATUS_ADAPTER_NOT_OPEN:
  536. ret = -EBUSY;
  537. break;
  538. }
  539. return ret;
  540. }
  541. static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
  542. {
  543. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  544. union {
  545. void *buf;
  546. struct rndis_msg_hdr *header;
  547. struct rndis_query *get;
  548. struct rndis_query_c *get_c;
  549. } u;
  550. int ret, buflen;
  551. buflen = *len + sizeof(*u.get);
  552. if (buflen < CONTROL_BUFFER_SIZE)
  553. buflen = CONTROL_BUFFER_SIZE;
  554. if (buflen > COMMAND_BUFFER_SIZE) {
  555. u.buf = kmalloc(buflen, GFP_KERNEL);
  556. if (!u.buf)
  557. return -ENOMEM;
  558. } else {
  559. u.buf = priv->command_buffer;
  560. }
  561. mutex_lock(&priv->command_lock);
  562. memset(u.get, 0, sizeof *u.get);
  563. u.get->msg_type = RNDIS_MSG_QUERY;
  564. u.get->msg_len = cpu_to_le32(sizeof *u.get);
  565. u.get->oid = oid;
  566. priv->current_command_oid = oid;
  567. ret = rndis_command(dev, u.header, buflen);
  568. priv->current_command_oid = 0;
  569. if (ret < 0)
  570. devdbg(dev, "rndis_query_oid(%s): rndis_command() failed, %d "
  571. "(%08x)", oid_to_string(oid), ret,
  572. le32_to_cpu(u.get_c->status));
  573. if (ret == 0) {
  574. ret = le32_to_cpu(u.get_c->len);
  575. if (ret > *len)
  576. *len = ret;
  577. memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
  578. ret = rndis_error_status(u.get_c->status);
  579. if (ret < 0)
  580. devdbg(dev, "rndis_query_oid(%s): device returned "
  581. "error, 0x%08x (%d)", oid_to_string(oid),
  582. le32_to_cpu(u.get_c->status), ret);
  583. }
  584. mutex_unlock(&priv->command_lock);
  585. if (u.buf != priv->command_buffer)
  586. kfree(u.buf);
  587. return ret;
  588. }
  589. static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
  590. {
  591. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  592. union {
  593. void *buf;
  594. struct rndis_msg_hdr *header;
  595. struct rndis_set *set;
  596. struct rndis_set_c *set_c;
  597. } u;
  598. int ret, buflen;
  599. buflen = len + sizeof(*u.set);
  600. if (buflen < CONTROL_BUFFER_SIZE)
  601. buflen = CONTROL_BUFFER_SIZE;
  602. if (buflen > COMMAND_BUFFER_SIZE) {
  603. u.buf = kmalloc(buflen, GFP_KERNEL);
  604. if (!u.buf)
  605. return -ENOMEM;
  606. } else {
  607. u.buf = priv->command_buffer;
  608. }
  609. mutex_lock(&priv->command_lock);
  610. memset(u.set, 0, sizeof *u.set);
  611. u.set->msg_type = RNDIS_MSG_SET;
  612. u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
  613. u.set->oid = oid;
  614. u.set->len = cpu_to_le32(len);
  615. u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
  616. u.set->handle = cpu_to_le32(0);
  617. memcpy(u.buf + sizeof(*u.set), data, len);
  618. priv->current_command_oid = oid;
  619. ret = rndis_command(dev, u.header, buflen);
  620. priv->current_command_oid = 0;
  621. if (ret < 0)
  622. devdbg(dev, "rndis_set_oid(%s): rndis_command() failed, %d "
  623. "(%08x)", oid_to_string(oid), ret,
  624. le32_to_cpu(u.set_c->status));
  625. if (ret == 0) {
  626. ret = rndis_error_status(u.set_c->status);
  627. if (ret < 0)
  628. devdbg(dev, "rndis_set_oid(%s): device returned error, "
  629. "0x%08x (%d)", oid_to_string(oid),
  630. le32_to_cpu(u.set_c->status), ret);
  631. }
  632. mutex_unlock(&priv->command_lock);
  633. if (u.buf != priv->command_buffer)
  634. kfree(u.buf);
  635. return ret;
  636. }
  637. static int rndis_reset(struct usbnet *usbdev)
  638. {
  639. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  640. struct rndis_reset *reset;
  641. int ret;
  642. mutex_lock(&priv->command_lock);
  643. reset = (void *)priv->command_buffer;
  644. memset(reset, 0, sizeof(*reset));
  645. reset->msg_type = RNDIS_MSG_RESET;
  646. reset->msg_len = cpu_to_le32(sizeof(*reset));
  647. priv->current_command_oid = 0;
  648. ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
  649. mutex_unlock(&priv->command_lock);
  650. if (ret < 0)
  651. return ret;
  652. return 0;
  653. }
  654. /*
  655. * Specs say that we can only set config parameters only soon after device
  656. * initialization.
  657. * value_type: 0 = u32, 2 = unicode string
  658. */
  659. static int rndis_set_config_parameter(struct usbnet *dev, char *param,
  660. int value_type, void *value)
  661. {
  662. struct ndis_config_param *infobuf;
  663. int value_len, info_len, param_len, ret, i;
  664. __le16 *unibuf;
  665. __le32 *dst_value;
  666. if (value_type == 0)
  667. value_len = sizeof(__le32);
  668. else if (value_type == 2)
  669. value_len = strlen(value) * sizeof(__le16);
  670. else
  671. return -EINVAL;
  672. param_len = strlen(param) * sizeof(__le16);
  673. info_len = sizeof(*infobuf) + param_len + value_len;
  674. #ifdef DEBUG
  675. info_len += 12;
  676. #endif
  677. infobuf = kmalloc(info_len, GFP_KERNEL);
  678. if (!infobuf)
  679. return -ENOMEM;
  680. #ifdef DEBUG
  681. info_len -= 12;
  682. /* extra 12 bytes are for padding (debug output) */
  683. memset(infobuf, 0xCC, info_len + 12);
  684. #endif
  685. if (value_type == 2)
  686. devdbg(dev, "setting config parameter: %s, value: %s",
  687. param, (u8 *)value);
  688. else
  689. devdbg(dev, "setting config parameter: %s, value: %d",
  690. param, *(u32 *)value);
  691. infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
  692. infobuf->name_length = cpu_to_le32(param_len);
  693. infobuf->type = cpu_to_le32(value_type);
  694. infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
  695. infobuf->value_length = cpu_to_le32(value_len);
  696. /* simple string to unicode string conversion */
  697. unibuf = (void *)infobuf + sizeof(*infobuf);
  698. for (i = 0; i < param_len / sizeof(__le16); i++)
  699. unibuf[i] = cpu_to_le16(param[i]);
  700. if (value_type == 2) {
  701. unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
  702. for (i = 0; i < value_len / sizeof(__le16); i++)
  703. unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
  704. } else {
  705. dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
  706. *dst_value = cpu_to_le32(*(u32 *)value);
  707. }
  708. #ifdef DEBUG
  709. devdbg(dev, "info buffer (len: %d):", info_len);
  710. for (i = 0; i < info_len; i += 12) {
  711. u32 *tmp = (u32 *)((u8 *)infobuf + i);
  712. devdbg(dev, "%08X:%08X:%08X",
  713. cpu_to_be32(tmp[0]),
  714. cpu_to_be32(tmp[1]),
  715. cpu_to_be32(tmp[2]));
  716. }
  717. #endif
  718. ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
  719. infobuf, info_len);
  720. if (ret != 0)
  721. devdbg(dev, "setting rndis config parameter failed, %d.", ret);
  722. kfree(infobuf);
  723. return ret;
  724. }
  725. static int rndis_set_config_parameter_str(struct usbnet *dev,
  726. char *param, char *value)
  727. {
  728. return(rndis_set_config_parameter(dev, param, 2, value));
  729. }
  730. /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
  731. char *param, u32 value)
  732. {
  733. return(rndis_set_config_parameter(dev, param, 0, &value));
  734. }*/
  735. /*
  736. * data conversion functions
  737. */
  738. static int level_to_qual(int level)
  739. {
  740. int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
  741. return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
  742. }
  743. static void dsconfig_to_freq(unsigned int dsconfig, struct iw_freq *freq)
  744. {
  745. freq->e = 0;
  746. freq->i = 0;
  747. freq->flags = 0;
  748. /* see comment in wireless.h above the "struct iw_freq"
  749. * definition for an explanation of this if
  750. * NOTE: 1000000 is due to the kHz
  751. */
  752. if (dsconfig > 1000000) {
  753. freq->m = dsconfig / 10;
  754. freq->e = 1;
  755. } else
  756. freq->m = dsconfig;
  757. /* convert from kHz to Hz */
  758. freq->e += 3;
  759. }
  760. static int freq_to_dsconfig(struct iw_freq *freq, unsigned int *dsconfig)
  761. {
  762. if (freq->m < 1000 && freq->e == 0) {
  763. if (freq->m >= 1 && freq->m <= 14)
  764. *dsconfig = ieee80211_dsss_chan_to_freq(freq->m) * 1000;
  765. else
  766. return -1;
  767. } else {
  768. int i;
  769. *dsconfig = freq->m;
  770. for (i = freq->e; i > 0; i--)
  771. *dsconfig *= 10;
  772. *dsconfig /= 1000;
  773. }
  774. return 0;
  775. }
  776. /*
  777. * common functions
  778. */
  779. static void restore_keys(struct usbnet *usbdev);
  780. static int rndis_check_bssid_list(struct usbnet *usbdev);
  781. static int get_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  782. {
  783. int ret, len;
  784. len = sizeof(*ssid);
  785. ret = rndis_query_oid(usbdev, OID_802_11_SSID, ssid, &len);
  786. if (ret != 0)
  787. ssid->length = 0;
  788. #ifdef DEBUG
  789. {
  790. unsigned char tmp[NDIS_802_11_LENGTH_SSID + 1];
  791. memcpy(tmp, ssid->essid, le32_to_cpu(ssid->length));
  792. tmp[le32_to_cpu(ssid->length)] = 0;
  793. devdbg(usbdev, "get_essid: '%s', ret: %d", tmp, ret);
  794. }
  795. #endif
  796. return ret;
  797. }
  798. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  799. {
  800. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  801. int ret;
  802. ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
  803. if (ret == 0) {
  804. memcpy(&priv->essid, ssid, sizeof(priv->essid));
  805. priv->radio_on = 1;
  806. devdbg(usbdev, "set_essid: radio_on = 1");
  807. }
  808. return ret;
  809. }
  810. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  811. {
  812. int ret, len;
  813. len = ETH_ALEN;
  814. ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
  815. if (ret != 0)
  816. memset(bssid, 0, ETH_ALEN);
  817. return ret;
  818. }
  819. static int get_association_info(struct usbnet *usbdev,
  820. struct ndis_80211_assoc_info *info, int len)
  821. {
  822. return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
  823. info, &len);
  824. }
  825. static int is_associated(struct usbnet *usbdev)
  826. {
  827. u8 bssid[ETH_ALEN];
  828. int ret;
  829. ret = get_bssid(usbdev, bssid);
  830. return (ret == 0 && !is_zero_ether_addr(bssid));
  831. }
  832. static int disassociate(struct usbnet *usbdev, int reset_ssid)
  833. {
  834. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  835. struct ndis_80211_ssid ssid;
  836. int i, ret = 0;
  837. if (priv->radio_on) {
  838. ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
  839. if (ret == 0) {
  840. priv->radio_on = 0;
  841. devdbg(usbdev, "disassociate: radio_on = 0");
  842. if (reset_ssid)
  843. msleep(100);
  844. }
  845. }
  846. /* disassociate causes radio to be turned off; if reset_ssid
  847. * is given, set random ssid to enable radio */
  848. if (reset_ssid) {
  849. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  850. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  851. ssid.essid[0] = 0x1;
  852. ssid.essid[1] = 0xff;
  853. for (i = 2; i < sizeof(ssid.essid); i++)
  854. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  855. ret = set_essid(usbdev, &ssid);
  856. }
  857. return ret;
  858. }
  859. static int set_auth_mode(struct usbnet *usbdev, int wpa_version, int authalg)
  860. {
  861. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  862. __le32 tmp;
  863. int auth_mode, ret;
  864. devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
  865. "keymgmt=0x%x", wpa_version, authalg, priv->wpa_keymgmt);
  866. if (wpa_version & IW_AUTH_WPA_VERSION_WPA2) {
  867. if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
  868. auth_mode = NDIS_80211_AUTH_WPA2;
  869. else
  870. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  871. } else if (wpa_version & IW_AUTH_WPA_VERSION_WPA) {
  872. if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
  873. auth_mode = NDIS_80211_AUTH_WPA;
  874. else if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_PSK)
  875. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  876. else
  877. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  878. } else if (authalg & IW_AUTH_ALG_SHARED_KEY) {
  879. if (authalg & IW_AUTH_ALG_OPEN_SYSTEM)
  880. auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
  881. else
  882. auth_mode = NDIS_80211_AUTH_SHARED;
  883. } else
  884. auth_mode = NDIS_80211_AUTH_OPEN;
  885. tmp = cpu_to_le32(auth_mode);
  886. ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
  887. sizeof(tmp));
  888. if (ret != 0) {
  889. devwarn(usbdev, "setting auth mode failed (%08X)", ret);
  890. return ret;
  891. }
  892. priv->wpa_version = wpa_version;
  893. priv->wpa_authalg = authalg;
  894. return 0;
  895. }
  896. static int set_priv_filter(struct usbnet *usbdev)
  897. {
  898. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  899. __le32 tmp;
  900. devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
  901. if (priv->wpa_version & IW_AUTH_WPA_VERSION_WPA2 ||
  902. priv->wpa_version & IW_AUTH_WPA_VERSION_WPA)
  903. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  904. else
  905. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  906. return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
  907. sizeof(tmp));
  908. }
  909. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  910. {
  911. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  912. __le32 tmp;
  913. int encr_mode, ret;
  914. devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
  915. pairwise,
  916. groupwise);
  917. if (pairwise & IW_AUTH_CIPHER_CCMP)
  918. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  919. else if (pairwise & IW_AUTH_CIPHER_TKIP)
  920. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  921. else if (pairwise &
  922. (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
  923. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  924. else if (groupwise & IW_AUTH_CIPHER_CCMP)
  925. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  926. else if (groupwise & IW_AUTH_CIPHER_TKIP)
  927. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  928. else
  929. encr_mode = NDIS_80211_ENCR_DISABLED;
  930. tmp = cpu_to_le32(encr_mode);
  931. ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
  932. sizeof(tmp));
  933. if (ret != 0) {
  934. devwarn(usbdev, "setting encr mode failed (%08X)", ret);
  935. return ret;
  936. }
  937. priv->wpa_cipher_pair = pairwise;
  938. priv->wpa_cipher_group = groupwise;
  939. return 0;
  940. }
  941. static int set_assoc_params(struct usbnet *usbdev)
  942. {
  943. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  944. set_auth_mode(usbdev, priv->wpa_version, priv->wpa_authalg);
  945. set_priv_filter(usbdev);
  946. set_encr_mode(usbdev, priv->wpa_cipher_pair, priv->wpa_cipher_group);
  947. return 0;
  948. }
  949. static int set_infra_mode(struct usbnet *usbdev, int mode)
  950. {
  951. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  952. __le32 tmp;
  953. int ret;
  954. devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
  955. tmp = cpu_to_le32(mode);
  956. ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
  957. sizeof(tmp));
  958. if (ret != 0) {
  959. devwarn(usbdev, "setting infra mode failed (%08X)", ret);
  960. return ret;
  961. }
  962. /* NDIS drivers clear keys when infrastructure mode is
  963. * changed. But Linux tools assume otherwise. So set the
  964. * keys */
  965. restore_keys(usbdev);
  966. priv->infra_mode = mode;
  967. return 0;
  968. }
  969. static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
  970. {
  971. __le32 tmp;
  972. devdbg(usbdev, "set_rts_threshold %i", rts_threshold);
  973. if (rts_threshold < 0 || rts_threshold > 2347)
  974. rts_threshold = 2347;
  975. tmp = cpu_to_le32(rts_threshold);
  976. return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
  977. sizeof(tmp));
  978. }
  979. static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
  980. {
  981. __le32 tmp;
  982. devdbg(usbdev, "set_frag_threshold %i", frag_threshold);
  983. if (frag_threshold < 256 || frag_threshold > 2346)
  984. frag_threshold = 2346;
  985. tmp = cpu_to_le32(frag_threshold);
  986. return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
  987. sizeof(tmp));
  988. }
  989. static void set_default_iw_params(struct usbnet *usbdev)
  990. {
  991. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  992. priv->wpa_keymgmt = 0;
  993. priv->wpa_version = 0;
  994. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  995. set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  996. IW_AUTH_ALG_OPEN_SYSTEM);
  997. set_priv_filter(usbdev);
  998. set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
  999. }
  1000. static int deauthenticate(struct usbnet *usbdev)
  1001. {
  1002. int ret;
  1003. ret = disassociate(usbdev, 1);
  1004. set_default_iw_params(usbdev);
  1005. return ret;
  1006. }
  1007. /* index must be 0 - N, as per NDIS */
  1008. static int add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index)
  1009. {
  1010. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1011. struct ndis_80211_wep_key ndis_key;
  1012. int cipher, ret;
  1013. if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
  1014. return -EINVAL;
  1015. if (key_len == 5)
  1016. cipher = WLAN_CIPHER_SUITE_WEP40;
  1017. else
  1018. cipher = WLAN_CIPHER_SUITE_WEP104;
  1019. memset(&ndis_key, 0, sizeof(ndis_key));
  1020. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1021. ndis_key.length = cpu_to_le32(key_len);
  1022. ndis_key.index = cpu_to_le32(index);
  1023. memcpy(&ndis_key.material, key, key_len);
  1024. if (index == priv->encr_tx_key_index) {
  1025. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1026. ret = set_encr_mode(usbdev, IW_AUTH_CIPHER_WEP104,
  1027. IW_AUTH_CIPHER_NONE);
  1028. if (ret)
  1029. devwarn(usbdev, "encryption couldn't be enabled (%08X)",
  1030. ret);
  1031. }
  1032. ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
  1033. sizeof(ndis_key));
  1034. if (ret != 0) {
  1035. devwarn(usbdev, "adding encryption key %d failed (%08X)",
  1036. index+1, ret);
  1037. return ret;
  1038. }
  1039. priv->encr_keys[index].len = key_len;
  1040. priv->encr_keys[index].cipher = cipher;
  1041. memcpy(&priv->encr_keys[index].material, key, key_len);
  1042. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1043. return 0;
  1044. }
  1045. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1046. int index, const u8 *addr, const u8 *rx_seq, int cipher,
  1047. int flags)
  1048. {
  1049. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1050. struct ndis_80211_key ndis_key;
  1051. bool is_addr_ok;
  1052. int ret;
  1053. if (index < 0 || index >= 4) {
  1054. devdbg(usbdev, "add_wpa_key: index out of range (%i)", index);
  1055. return -EINVAL;
  1056. }
  1057. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1058. devdbg(usbdev, "add_wpa_key: key length out of range (%i)",
  1059. key_len);
  1060. return -EINVAL;
  1061. }
  1062. if ((flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) && !rx_seq) {
  1063. devdbg(usbdev, "add_wpa_key: recv seq flag without buffer");
  1064. return -EINVAL;
  1065. }
  1066. is_addr_ok = addr && !is_zero_ether_addr(addr) &&
  1067. !is_broadcast_ether_addr(addr);
  1068. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1069. devdbg(usbdev, "add_wpa_key: pairwise but bssid invalid (%pM)",
  1070. addr);
  1071. return -EINVAL;
  1072. }
  1073. devdbg(usbdev, "add_wpa_key(%i): flags:%i%i%i", index,
  1074. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1075. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1076. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1077. memset(&ndis_key, 0, sizeof(ndis_key));
  1078. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1079. sizeof(ndis_key.material) + key_len);
  1080. ndis_key.length = cpu_to_le32(key_len);
  1081. ndis_key.index = cpu_to_le32(index) | flags;
  1082. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1083. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1084. * different order than NDIS spec, so swap the order here. */
  1085. memcpy(ndis_key.material, key, 16);
  1086. memcpy(ndis_key.material + 16, key + 24, 8);
  1087. memcpy(ndis_key.material + 24, key + 16, 8);
  1088. } else
  1089. memcpy(ndis_key.material, key, key_len);
  1090. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1091. memcpy(ndis_key.rsc, rx_seq, 6);
  1092. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1093. /* pairwise key */
  1094. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1095. } else {
  1096. /* group key */
  1097. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1098. memset(ndis_key.bssid, 0xff, ETH_ALEN);
  1099. else
  1100. get_bssid(usbdev, ndis_key.bssid);
  1101. }
  1102. ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
  1103. le32_to_cpu(ndis_key.size));
  1104. devdbg(usbdev, "add_wpa_key: OID_802_11_ADD_KEY -> %08X", ret);
  1105. if (ret != 0)
  1106. return ret;
  1107. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1108. priv->encr_keys[index].len = key_len;
  1109. priv->encr_keys[index].cipher = cipher;
  1110. memcpy(&priv->encr_keys[index].material, key, key_len);
  1111. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1112. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1113. else
  1114. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1115. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1116. priv->encr_tx_key_index = index;
  1117. return 0;
  1118. }
  1119. static int restore_key(struct usbnet *usbdev, int key_idx)
  1120. {
  1121. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1122. struct rndis_wlan_encr_key key;
  1123. int flags;
  1124. key = priv->encr_keys[key_idx];
  1125. devdbg(usbdev, "restore_key: %i:%s:%i", key_idx,
  1126. is_wpa_key(priv, key_idx) ? "wpa" : "wep",
  1127. key.len);
  1128. if (key.len == 0)
  1129. return 0;
  1130. if (is_wpa_key(priv, key_idx)) {
  1131. flags = 0;
  1132. /*if (priv->encr_tx_key_index == key_idx)
  1133. flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;*/
  1134. if (!is_zero_ether_addr(key.bssid) &&
  1135. !is_broadcast_ether_addr(key.bssid))
  1136. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1137. return add_wpa_key(usbdev, key.material, key.len, key_idx,
  1138. key.bssid, NULL, key.cipher, flags);
  1139. }
  1140. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1141. }
  1142. static void restore_keys(struct usbnet *usbdev)
  1143. {
  1144. int i;
  1145. for (i = 0; i < 4; i++)
  1146. restore_key(usbdev, i);
  1147. }
  1148. static void clear_key(struct rndis_wlan_private *priv, int idx)
  1149. {
  1150. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1151. }
  1152. /* remove_key is for both wep and wpa */
  1153. static int remove_key(struct usbnet *usbdev, int index, u8 bssid[ETH_ALEN])
  1154. {
  1155. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1156. struct ndis_80211_remove_key remove_key;
  1157. __le32 keyindex;
  1158. bool is_wpa;
  1159. int ret;
  1160. if (priv->encr_keys[index].len == 0)
  1161. return 0;
  1162. is_wpa = is_wpa_key(priv, index);
  1163. devdbg(usbdev, "remove_key: %i:%s:%i", index, is_wpa ? "wpa" : "wep",
  1164. priv->encr_keys[index].len);
  1165. clear_key(priv, index);
  1166. if (is_wpa) {
  1167. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1168. remove_key.index = cpu_to_le32(index);
  1169. if (bssid) {
  1170. /* pairwise key */
  1171. if (!is_broadcast_ether_addr(bssid))
  1172. remove_key.index |=
  1173. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1174. memcpy(remove_key.bssid, bssid,
  1175. sizeof(remove_key.bssid));
  1176. } else
  1177. memset(remove_key.bssid, 0xff,
  1178. sizeof(remove_key.bssid));
  1179. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
  1180. sizeof(remove_key));
  1181. if (ret != 0)
  1182. return ret;
  1183. } else {
  1184. keyindex = cpu_to_le32(index);
  1185. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
  1186. sizeof(keyindex));
  1187. if (ret != 0) {
  1188. devwarn(usbdev,
  1189. "removing encryption key %d failed (%08X)",
  1190. index, ret);
  1191. return ret;
  1192. }
  1193. }
  1194. /* if it is transmit key, disable encryption */
  1195. if (index == priv->encr_tx_key_index)
  1196. set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
  1197. return 0;
  1198. }
  1199. static void set_multicast_list(struct usbnet *usbdev)
  1200. {
  1201. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1202. struct dev_mc_list *mclist;
  1203. __le32 filter;
  1204. int ret, i, size;
  1205. char *buf;
  1206. filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  1207. if (usbdev->net->flags & IFF_PROMISC) {
  1208. filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
  1209. RNDIS_PACKET_TYPE_ALL_LOCAL;
  1210. } else if (usbdev->net->flags & IFF_ALLMULTI ||
  1211. usbdev->net->mc_count > priv->multicast_size) {
  1212. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1213. } else if (usbdev->net->mc_count > 0) {
  1214. size = min(priv->multicast_size, usbdev->net->mc_count);
  1215. buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
  1216. if (!buf) {
  1217. devwarn(usbdev,
  1218. "couldn't alloc %d bytes of memory",
  1219. size * ETH_ALEN);
  1220. return;
  1221. }
  1222. mclist = usbdev->net->mc_list;
  1223. for (i = 0; i < size && mclist; mclist = mclist->next) {
  1224. if (mclist->dmi_addrlen != ETH_ALEN)
  1225. continue;
  1226. memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
  1227. i++;
  1228. }
  1229. ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
  1230. i * ETH_ALEN);
  1231. if (ret == 0 && i > 0)
  1232. filter |= RNDIS_PACKET_TYPE_MULTICAST;
  1233. else
  1234. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1235. devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
  1236. i, priv->multicast_size, ret);
  1237. kfree(buf);
  1238. }
  1239. ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1240. sizeof(filter));
  1241. if (ret < 0) {
  1242. devwarn(usbdev, "couldn't set packet filter: %08x",
  1243. le32_to_cpu(filter));
  1244. }
  1245. devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
  1246. le32_to_cpu(filter), ret);
  1247. }
  1248. /*
  1249. * cfg80211 ops
  1250. */
  1251. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1252. struct net_device *dev,
  1253. enum nl80211_iftype type, u32 *flags,
  1254. struct vif_params *params)
  1255. {
  1256. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1257. struct usbnet *usbdev = priv->usbdev;
  1258. int mode;
  1259. switch (type) {
  1260. case NL80211_IFTYPE_ADHOC:
  1261. mode = NDIS_80211_INFRA_ADHOC;
  1262. break;
  1263. case NL80211_IFTYPE_STATION:
  1264. mode = NDIS_80211_INFRA_INFRA;
  1265. break;
  1266. default:
  1267. return -EINVAL;
  1268. }
  1269. priv->wdev.iftype = type;
  1270. return set_infra_mode(usbdev, mode);
  1271. }
  1272. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1273. {
  1274. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1275. struct usbnet *usbdev = priv->usbdev;
  1276. int err;
  1277. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1278. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1279. if (err < 0)
  1280. return err;
  1281. }
  1282. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1283. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1284. if (err < 0)
  1285. return err;
  1286. }
  1287. return 0;
  1288. }
  1289. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  1290. int dbm)
  1291. {
  1292. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1293. struct usbnet *usbdev = priv->usbdev;
  1294. devdbg(usbdev, "rndis_set_tx_power type:0x%x dbm:%i", type, dbm);
  1295. /* Device doesn't support changing txpower after initialization, only
  1296. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1297. * currently used.
  1298. */
  1299. if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
  1300. if (!priv->radio_on)
  1301. disassociate(usbdev, 1); /* turn on radio */
  1302. return 0;
  1303. }
  1304. return -ENOTSUPP;
  1305. }
  1306. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
  1307. {
  1308. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1309. struct usbnet *usbdev = priv->usbdev;
  1310. *dbm = get_bcm4320_power_dbm(priv);
  1311. devdbg(usbdev, "rndis_get_tx_power dbm:%i", *dbm);
  1312. return 0;
  1313. }
  1314. #define SCAN_DELAY_JIFFIES (6 * HZ)
  1315. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  1316. struct cfg80211_scan_request *request)
  1317. {
  1318. struct usbnet *usbdev = netdev_priv(dev);
  1319. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1320. int ret;
  1321. __le32 tmp;
  1322. devdbg(usbdev, "cfg80211.scan");
  1323. /* Get current bssid list from device before new scan, as new scan
  1324. * clears internal bssid list.
  1325. */
  1326. rndis_check_bssid_list(usbdev);
  1327. if (!request)
  1328. return -EINVAL;
  1329. if (priv->scan_request && priv->scan_request != request)
  1330. return -EBUSY;
  1331. priv->scan_request = request;
  1332. tmp = cpu_to_le32(1);
  1333. ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  1334. sizeof(tmp));
  1335. if (ret == 0) {
  1336. /* Wait before retrieving scan results from device */
  1337. queue_delayed_work(priv->workqueue, &priv->scan_work,
  1338. SCAN_DELAY_JIFFIES);
  1339. }
  1340. return ret;
  1341. }
  1342. static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
  1343. struct ndis_80211_bssid_ex *bssid)
  1344. {
  1345. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1346. struct ieee80211_channel *channel;
  1347. s32 signal;
  1348. u64 timestamp;
  1349. u16 capability;
  1350. u16 beacon_interval;
  1351. struct ndis_80211_fixed_ies *fixed;
  1352. int ie_len, bssid_len;
  1353. u8 *ie;
  1354. devdbg(usbdev, " found bssid: '%.32s' [%pM]", bssid->ssid.essid,
  1355. bssid->mac);
  1356. /* parse bssid structure */
  1357. bssid_len = le32_to_cpu(bssid->length);
  1358. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1359. sizeof(struct ndis_80211_fixed_ies))
  1360. return NULL;
  1361. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1362. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1363. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1364. (int)le32_to_cpu(bssid->ie_length));
  1365. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1366. if (ie_len < 0)
  1367. return NULL;
  1368. /* extract data for cfg80211_inform_bss */
  1369. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1370. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1371. if (!channel)
  1372. return NULL;
  1373. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1374. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1375. capability = le16_to_cpu(fixed->capabilities);
  1376. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1377. return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1378. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1379. GFP_KERNEL);
  1380. }
  1381. static int rndis_check_bssid_list(struct usbnet *usbdev)
  1382. {
  1383. void *buf = NULL;
  1384. struct ndis_80211_bssid_list_ex *bssid_list;
  1385. struct ndis_80211_bssid_ex *bssid;
  1386. int ret = -EINVAL, len, count, bssid_len;
  1387. bool resized = false;
  1388. devdbg(usbdev, "check_bssid_list");
  1389. len = CONTROL_BUFFER_SIZE;
  1390. resize_buf:
  1391. buf = kmalloc(len, GFP_KERNEL);
  1392. if (!buf) {
  1393. ret = -ENOMEM;
  1394. goto out;
  1395. }
  1396. ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  1397. if (ret != 0)
  1398. goto out;
  1399. if (!resized && len > CONTROL_BUFFER_SIZE) {
  1400. resized = true;
  1401. kfree(buf);
  1402. goto resize_buf;
  1403. }
  1404. bssid_list = buf;
  1405. bssid = bssid_list->bssid;
  1406. bssid_len = le32_to_cpu(bssid->length);
  1407. count = le32_to_cpu(bssid_list->num_items);
  1408. devdbg(usbdev, "check_bssid_list: %d BSSIDs found (buflen: %d)", count,
  1409. len);
  1410. while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
  1411. rndis_bss_info_update(usbdev, bssid);
  1412. bssid = (void *)bssid + bssid_len;
  1413. bssid_len = le32_to_cpu(bssid->length);
  1414. count--;
  1415. }
  1416. out:
  1417. kfree(buf);
  1418. return ret;
  1419. }
  1420. static void rndis_get_scan_results(struct work_struct *work)
  1421. {
  1422. struct rndis_wlan_private *priv =
  1423. container_of(work, struct rndis_wlan_private, scan_work.work);
  1424. struct usbnet *usbdev = priv->usbdev;
  1425. int ret;
  1426. devdbg(usbdev, "get_scan_results");
  1427. if (!priv->scan_request)
  1428. return;
  1429. ret = rndis_check_bssid_list(usbdev);
  1430. cfg80211_scan_done(priv->scan_request, ret < 0);
  1431. priv->scan_request = NULL;
  1432. }
  1433. /*
  1434. * wireless extension handlers
  1435. */
  1436. static int rndis_iw_commit(struct net_device *dev,
  1437. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1438. {
  1439. /* dummy op */
  1440. return 0;
  1441. }
  1442. static int rndis_iw_set_essid(struct net_device *dev,
  1443. struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
  1444. {
  1445. struct ndis_80211_ssid ssid;
  1446. int length = wrqu->essid.length;
  1447. struct usbnet *usbdev = netdev_priv(dev);
  1448. devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
  1449. wrqu->essid.flags, wrqu->essid.length, essid);
  1450. if (length > NDIS_802_11_LENGTH_SSID)
  1451. length = NDIS_802_11_LENGTH_SSID;
  1452. ssid.length = cpu_to_le32(length);
  1453. if (length > 0)
  1454. memcpy(ssid.essid, essid, length);
  1455. else
  1456. memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
  1457. set_assoc_params(usbdev);
  1458. if (!wrqu->essid.flags || length == 0)
  1459. return disassociate(usbdev, 1);
  1460. else {
  1461. /* Pause and purge rx queue, so we don't pass packets before
  1462. * 'media connect'-indication.
  1463. */
  1464. usbnet_pause_rx(usbdev);
  1465. usbnet_purge_paused_rxq(usbdev);
  1466. return set_essid(usbdev, &ssid);
  1467. }
  1468. }
  1469. static int rndis_iw_get_essid(struct net_device *dev,
  1470. struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
  1471. {
  1472. struct ndis_80211_ssid ssid;
  1473. struct usbnet *usbdev = netdev_priv(dev);
  1474. int ret;
  1475. ret = get_essid(usbdev, &ssid);
  1476. if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
  1477. wrqu->essid.flags = 1;
  1478. wrqu->essid.length = le32_to_cpu(ssid.length);
  1479. memcpy(essid, ssid.essid, wrqu->essid.length);
  1480. essid[wrqu->essid.length] = 0;
  1481. } else {
  1482. memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
  1483. wrqu->essid.flags = 0;
  1484. wrqu->essid.length = 0;
  1485. }
  1486. devdbg(usbdev, "SIOCGIWESSID: %s", essid);
  1487. return ret;
  1488. }
  1489. static int rndis_iw_get_bssid(struct net_device *dev,
  1490. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1491. {
  1492. struct usbnet *usbdev = netdev_priv(dev);
  1493. unsigned char bssid[ETH_ALEN];
  1494. int ret;
  1495. ret = get_bssid(usbdev, bssid);
  1496. if (ret == 0)
  1497. devdbg(usbdev, "SIOCGIWAP: %pM", bssid);
  1498. else
  1499. devdbg(usbdev, "SIOCGIWAP: <not associated>");
  1500. wrqu->ap_addr.sa_family = ARPHRD_ETHER;
  1501. memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
  1502. return ret;
  1503. }
  1504. static int rndis_iw_set_bssid(struct net_device *dev,
  1505. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1506. {
  1507. struct usbnet *usbdev = netdev_priv(dev);
  1508. u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
  1509. int ret;
  1510. devdbg(usbdev, "SIOCSIWAP: %pM", bssid);
  1511. ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
  1512. /* user apps may set ap's mac address, which is not required;
  1513. * they may fail to work if this function fails, so return
  1514. * success */
  1515. if (ret)
  1516. devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
  1517. return 0;
  1518. }
  1519. static int rndis_iw_set_auth(struct net_device *dev,
  1520. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1521. {
  1522. struct iw_param *p = &wrqu->param;
  1523. struct usbnet *usbdev = netdev_priv(dev);
  1524. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1525. int ret = -ENOTSUPP;
  1526. switch (p->flags & IW_AUTH_INDEX) {
  1527. case IW_AUTH_WPA_VERSION:
  1528. devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
  1529. priv->wpa_version = p->value;
  1530. ret = 0;
  1531. break;
  1532. case IW_AUTH_CIPHER_PAIRWISE:
  1533. devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
  1534. priv->wpa_cipher_pair = p->value;
  1535. ret = 0;
  1536. break;
  1537. case IW_AUTH_CIPHER_GROUP:
  1538. devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
  1539. priv->wpa_cipher_group = p->value;
  1540. ret = 0;
  1541. break;
  1542. case IW_AUTH_KEY_MGMT:
  1543. devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
  1544. priv->wpa_keymgmt = p->value;
  1545. ret = 0;
  1546. break;
  1547. case IW_AUTH_TKIP_COUNTERMEASURES:
  1548. devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
  1549. p->value);
  1550. ret = 0;
  1551. break;
  1552. case IW_AUTH_DROP_UNENCRYPTED:
  1553. devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
  1554. ret = 0;
  1555. break;
  1556. case IW_AUTH_80211_AUTH_ALG:
  1557. devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
  1558. priv->wpa_authalg = p->value;
  1559. ret = 0;
  1560. break;
  1561. case IW_AUTH_WPA_ENABLED:
  1562. devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
  1563. if (wrqu->param.value)
  1564. deauthenticate(usbdev);
  1565. ret = 0;
  1566. break;
  1567. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  1568. devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
  1569. p->value);
  1570. ret = 0;
  1571. break;
  1572. case IW_AUTH_ROAMING_CONTROL:
  1573. devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
  1574. ret = 0;
  1575. break;
  1576. case IW_AUTH_PRIVACY_INVOKED:
  1577. devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
  1578. wrqu->param.flags & IW_AUTH_INDEX);
  1579. return -EOPNOTSUPP;
  1580. default:
  1581. devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
  1582. p->flags & IW_AUTH_INDEX, p->value);
  1583. }
  1584. return ret;
  1585. }
  1586. static int rndis_iw_get_auth(struct net_device *dev,
  1587. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1588. {
  1589. struct iw_param *p = &wrqu->param;
  1590. struct usbnet *usbdev = netdev_priv(dev);
  1591. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1592. switch (p->flags & IW_AUTH_INDEX) {
  1593. case IW_AUTH_WPA_VERSION:
  1594. p->value = priv->wpa_version;
  1595. break;
  1596. case IW_AUTH_CIPHER_PAIRWISE:
  1597. p->value = priv->wpa_cipher_pair;
  1598. break;
  1599. case IW_AUTH_CIPHER_GROUP:
  1600. p->value = priv->wpa_cipher_group;
  1601. break;
  1602. case IW_AUTH_KEY_MGMT:
  1603. p->value = priv->wpa_keymgmt;
  1604. break;
  1605. case IW_AUTH_80211_AUTH_ALG:
  1606. p->value = priv->wpa_authalg;
  1607. break;
  1608. default:
  1609. devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
  1610. wrqu->param.flags & IW_AUTH_INDEX);
  1611. return -EOPNOTSUPP;
  1612. }
  1613. return 0;
  1614. }
  1615. static int rndis_iw_set_encode(struct net_device *dev,
  1616. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1617. {
  1618. struct usbnet *usbdev = netdev_priv(dev);
  1619. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1620. struct rndis_wlan_encr_key key;
  1621. int ret, index, key_len;
  1622. u8 *keybuf;
  1623. index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
  1624. /* iwconfig gives index as 1 - N */
  1625. if (index > 0)
  1626. index--;
  1627. else
  1628. index = priv->encr_tx_key_index;
  1629. if (index < 0 || index >= 4) {
  1630. devwarn(usbdev, "encryption index out of range (%u)", index);
  1631. return -EINVAL;
  1632. }
  1633. /* remove key if disabled */
  1634. if (wrqu->data.flags & IW_ENCODE_DISABLED) {
  1635. if (remove_key(usbdev, index, NULL))
  1636. return -EINVAL;
  1637. else
  1638. return 0;
  1639. }
  1640. /* global encryption state (for all keys) */
  1641. if (wrqu->data.flags & IW_ENCODE_OPEN)
  1642. ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1643. IW_AUTH_ALG_OPEN_SYSTEM);
  1644. else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
  1645. ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1646. IW_AUTH_ALG_SHARED_KEY);
  1647. if (ret != 0)
  1648. return ret;
  1649. if (wrqu->data.length > 0) {
  1650. key_len = wrqu->data.length;
  1651. keybuf = extra;
  1652. } else {
  1653. /* must be set as tx key */
  1654. if (priv->encr_keys[index].len == 0)
  1655. return -EINVAL;
  1656. key = priv->encr_keys[index];
  1657. key_len = key.len;
  1658. keybuf = key.material;
  1659. priv->encr_tx_key_index = index;
  1660. }
  1661. if (add_wep_key(usbdev, keybuf, key_len, index) != 0)
  1662. return -EINVAL;
  1663. if (index == priv->encr_tx_key_index)
  1664. /* ndis drivers want essid to be set after setting encr */
  1665. set_essid(usbdev, &priv->essid);
  1666. return 0;
  1667. }
  1668. static int rndis_iw_set_encode_ext(struct net_device *dev,
  1669. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1670. {
  1671. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1672. struct usbnet *usbdev = netdev_priv(dev);
  1673. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1674. int keyidx, flags, cipher;
  1675. keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
  1676. /* iwconfig gives index as 1 - N */
  1677. if (keyidx)
  1678. keyidx--;
  1679. else
  1680. keyidx = priv->encr_tx_key_index;
  1681. if (keyidx < 0 || keyidx >= 4) {
  1682. devwarn(usbdev, "encryption index out of range (%u)", keyidx);
  1683. return -EINVAL;
  1684. }
  1685. if (ext->alg == WPA_ALG_WEP) {
  1686. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
  1687. priv->encr_tx_key_index = keyidx;
  1688. return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
  1689. }
  1690. cipher = -1;
  1691. if (ext->alg == IW_ENCODE_ALG_TKIP)
  1692. cipher = WLAN_CIPHER_SUITE_TKIP;
  1693. else if (ext->alg == IW_ENCODE_ALG_CCMP)
  1694. cipher = WLAN_CIPHER_SUITE_CCMP;
  1695. if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
  1696. ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
  1697. return remove_key(usbdev, keyidx, NULL);
  1698. if (cipher == -1)
  1699. return -EOPNOTSUPP;
  1700. flags = 0;
  1701. if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
  1702. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  1703. if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY))
  1704. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1705. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
  1706. flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;
  1707. return add_wpa_key(usbdev, ext->key, ext->key_len, keyidx,
  1708. (u8 *)&ext->addr.sa_data, ext->rx_seq, cipher,
  1709. flags);
  1710. }
  1711. static int rndis_iw_set_genie(struct net_device *dev,
  1712. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1713. {
  1714. struct usbnet *usbdev = netdev_priv(dev);
  1715. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1716. int ret = 0;
  1717. #ifdef DEBUG
  1718. int j;
  1719. u8 *gie = extra;
  1720. for (j = 0; j < wrqu->data.length; j += 8)
  1721. devdbg(usbdev,
  1722. "SIOCSIWGENIE %04x - "
  1723. "%02x %02x %02x %02x %02x %02x %02x %02x", j,
  1724. gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
  1725. gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
  1726. #endif
  1727. /* clear existing IEs */
  1728. if (priv->wpa_ie_len) {
  1729. kfree(priv->wpa_ie);
  1730. priv->wpa_ie_len = 0;
  1731. }
  1732. /* set new IEs */
  1733. priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
  1734. if (priv->wpa_ie) {
  1735. priv->wpa_ie_len = wrqu->data.length;
  1736. memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
  1737. } else
  1738. ret = -ENOMEM;
  1739. return ret;
  1740. }
  1741. static int rndis_iw_get_genie(struct net_device *dev,
  1742. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1743. {
  1744. struct usbnet *usbdev = netdev_priv(dev);
  1745. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1746. devdbg(usbdev, "SIOCGIWGENIE");
  1747. if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
  1748. wrqu->data.length = 0;
  1749. return 0;
  1750. }
  1751. if (wrqu->data.length < priv->wpa_ie_len)
  1752. return -E2BIG;
  1753. wrqu->data.length = priv->wpa_ie_len;
  1754. memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
  1755. return 0;
  1756. }
  1757. static int rndis_iw_set_freq(struct net_device *dev,
  1758. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1759. {
  1760. struct usbnet *usbdev = netdev_priv(dev);
  1761. struct ndis_80211_conf config;
  1762. unsigned int dsconfig;
  1763. int len, ret;
  1764. /* this OID is valid only when not associated */
  1765. if (is_associated(usbdev))
  1766. return 0;
  1767. dsconfig = 0;
  1768. if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
  1769. return -EINVAL;
  1770. len = sizeof(config);
  1771. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1772. if (ret != 0) {
  1773. devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
  1774. return 0;
  1775. }
  1776. config.ds_config = cpu_to_le32(dsconfig);
  1777. devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
  1778. return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
  1779. sizeof(config));
  1780. }
  1781. static int rndis_iw_get_freq(struct net_device *dev,
  1782. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1783. {
  1784. struct usbnet *usbdev = netdev_priv(dev);
  1785. struct ndis_80211_conf config;
  1786. int len, ret;
  1787. len = sizeof(config);
  1788. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1789. if (ret == 0)
  1790. dsconfig_to_freq(le32_to_cpu(config.ds_config), &wrqu->freq);
  1791. devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
  1792. return ret;
  1793. }
  1794. static int rndis_iw_get_rate(struct net_device *dev,
  1795. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1796. {
  1797. struct usbnet *usbdev = netdev_priv(dev);
  1798. __le32 tmp;
  1799. int ret, len;
  1800. len = sizeof(tmp);
  1801. ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
  1802. if (ret == 0) {
  1803. wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
  1804. wrqu->bitrate.disabled = 0;
  1805. wrqu->bitrate.flags = 1;
  1806. }
  1807. return ret;
  1808. }
  1809. static int rndis_iw_set_mlme(struct net_device *dev,
  1810. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1811. {
  1812. struct usbnet *usbdev = netdev_priv(dev);
  1813. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1814. struct iw_mlme *mlme = (struct iw_mlme *)extra;
  1815. unsigned char bssid[ETH_ALEN];
  1816. get_bssid(usbdev, bssid);
  1817. if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
  1818. return -EINVAL;
  1819. switch (mlme->cmd) {
  1820. case IW_MLME_DEAUTH:
  1821. return deauthenticate(usbdev);
  1822. case IW_MLME_DISASSOC:
  1823. return disassociate(usbdev, priv->radio_on);
  1824. default:
  1825. return -EOPNOTSUPP;
  1826. }
  1827. return 0;
  1828. }
  1829. static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
  1830. {
  1831. struct usbnet *usbdev = netdev_priv(dev);
  1832. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1833. unsigned long flags;
  1834. spin_lock_irqsave(&priv->stats_lock, flags);
  1835. memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
  1836. spin_unlock_irqrestore(&priv->stats_lock, flags);
  1837. return &priv->iwstats;
  1838. }
  1839. #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
  1840. static const iw_handler rndis_iw_handler[] =
  1841. {
  1842. IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
  1843. IW_IOCTL(SIOCGIWNAME) = (iw_handler) cfg80211_wext_giwname,
  1844. IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
  1845. IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
  1846. IW_IOCTL(SIOCSIWMODE) = (iw_handler) cfg80211_wext_siwmode,
  1847. IW_IOCTL(SIOCGIWMODE) = (iw_handler) cfg80211_wext_giwmode,
  1848. IW_IOCTL(SIOCGIWRANGE) = (iw_handler) cfg80211_wext_giwrange,
  1849. IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
  1850. IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
  1851. IW_IOCTL(SIOCSIWSCAN) = (iw_handler) cfg80211_wext_siwscan,
  1852. IW_IOCTL(SIOCGIWSCAN) = (iw_handler) cfg80211_wext_giwscan,
  1853. IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
  1854. IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
  1855. IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
  1856. IW_IOCTL(SIOCSIWRTS) = (iw_handler) cfg80211_wext_siwrts,
  1857. IW_IOCTL(SIOCGIWRTS) = (iw_handler) cfg80211_wext_giwrts,
  1858. IW_IOCTL(SIOCSIWFRAG) = (iw_handler) cfg80211_wext_siwfrag,
  1859. IW_IOCTL(SIOCGIWFRAG) = (iw_handler) cfg80211_wext_giwfrag,
  1860. IW_IOCTL(SIOCSIWTXPOW) = (iw_handler) cfg80211_wext_siwtxpower,
  1861. IW_IOCTL(SIOCGIWTXPOW) = (iw_handler) cfg80211_wext_giwtxpower,
  1862. IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
  1863. IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
  1864. IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
  1865. IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
  1866. IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
  1867. IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
  1868. IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
  1869. };
  1870. static const iw_handler rndis_wlan_private_handler[] = {
  1871. };
  1872. static const struct iw_priv_args rndis_wlan_private_args[] = {
  1873. };
  1874. static const struct iw_handler_def rndis_iw_handlers = {
  1875. .num_standard = ARRAY_SIZE(rndis_iw_handler),
  1876. .num_private = ARRAY_SIZE(rndis_wlan_private_handler),
  1877. .num_private_args = ARRAY_SIZE(rndis_wlan_private_args),
  1878. .standard = (iw_handler *)rndis_iw_handler,
  1879. .private = (iw_handler *)rndis_wlan_private_handler,
  1880. .private_args = (struct iw_priv_args *)rndis_wlan_private_args,
  1881. .get_wireless_stats = rndis_get_wireless_stats,
  1882. };
  1883. static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
  1884. {
  1885. struct ndis_80211_assoc_info *info;
  1886. union iwreq_data evt;
  1887. u8 assoc_buf[sizeof(*info) + IW_CUSTOM_MAX + 32];
  1888. u8 bssid[ETH_ALEN];
  1889. int ret, offset;
  1890. memset(assoc_buf, 0, sizeof(assoc_buf));
  1891. info = (void *)assoc_buf;
  1892. netif_carrier_on(usbdev->net);
  1893. /* Get association info IEs from device and send them back to
  1894. * userspace. */
  1895. ret = get_association_info(usbdev, info, sizeof(assoc_buf));
  1896. if (!ret) {
  1897. evt.data.length = le32_to_cpu(info->req_ie_length);
  1898. if (evt.data.length > 0) {
  1899. offset = le32_to_cpu(info->offset_req_ies);
  1900. wireless_send_event(usbdev->net,
  1901. IWEVASSOCREQIE, &evt,
  1902. (char *)info + offset);
  1903. }
  1904. evt.data.length = le32_to_cpu(info->resp_ie_length);
  1905. if (evt.data.length > 0) {
  1906. offset = le32_to_cpu(info->offset_resp_ies);
  1907. wireless_send_event(usbdev->net,
  1908. IWEVASSOCRESPIE, &evt,
  1909. (char *)info + offset);
  1910. }
  1911. usbnet_resume_rx(usbdev);
  1912. }
  1913. ret = get_bssid(usbdev, bssid);
  1914. if (!ret) {
  1915. evt.data.flags = 0;
  1916. evt.data.length = 0;
  1917. memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
  1918. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1919. }
  1920. usbnet_resume_rx(usbdev);
  1921. }
  1922. static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
  1923. {
  1924. union iwreq_data evt;
  1925. netif_carrier_off(usbdev->net);
  1926. evt.data.flags = 0;
  1927. evt.data.length = 0;
  1928. memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
  1929. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1930. }
  1931. static void rndis_wlan_worker(struct work_struct *work)
  1932. {
  1933. struct rndis_wlan_private *priv =
  1934. container_of(work, struct rndis_wlan_private, work);
  1935. struct usbnet *usbdev = priv->usbdev;
  1936. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
  1937. rndis_wlan_do_link_up_work(usbdev);
  1938. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
  1939. rndis_wlan_do_link_down_work(usbdev);
  1940. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1941. set_multicast_list(usbdev);
  1942. }
  1943. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  1944. {
  1945. struct usbnet *usbdev = netdev_priv(dev);
  1946. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1947. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1948. return;
  1949. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  1950. queue_work(priv->workqueue, &priv->work);
  1951. }
  1952. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  1953. struct ndis_80211_status_indication *indication,
  1954. int len)
  1955. {
  1956. u8 *buf;
  1957. const char *type;
  1958. int flags, buflen;
  1959. bool pairwise_error, group_error;
  1960. struct ndis_80211_auth_request *auth_req;
  1961. /* must have at least one array entry */
  1962. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1963. sizeof(struct ndis_80211_auth_request)) {
  1964. devinfo(usbdev, "authentication indication: "
  1965. "too short message (%i)", len);
  1966. return;
  1967. }
  1968. buf = (void *)&indication->u.auth_request[0];
  1969. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  1970. while (buflen >= sizeof(*auth_req)) {
  1971. auth_req = (void *)buf;
  1972. type = "unknown";
  1973. flags = le32_to_cpu(auth_req->flags);
  1974. pairwise_error = false;
  1975. group_error = false;
  1976. if (flags & 0x1)
  1977. type = "reauth request";
  1978. if (flags & 0x2)
  1979. type = "key update request";
  1980. if (flags & 0x6) {
  1981. pairwise_error = true;
  1982. type = "pairwise_error";
  1983. }
  1984. if (flags & 0xe) {
  1985. group_error = true;
  1986. type = "group_error";
  1987. }
  1988. devinfo(usbdev, "authentication indication: %s (0x%08x)", type,
  1989. le32_to_cpu(auth_req->flags));
  1990. if (pairwise_error || group_error) {
  1991. union iwreq_data wrqu;
  1992. struct iw_michaelmicfailure micfailure;
  1993. memset(&micfailure, 0, sizeof(micfailure));
  1994. if (pairwise_error)
  1995. micfailure.flags |= IW_MICFAILURE_PAIRWISE;
  1996. if (group_error)
  1997. micfailure.flags |= IW_MICFAILURE_GROUP;
  1998. memcpy(micfailure.src_addr.sa_data, auth_req->bssid,
  1999. ETH_ALEN);
  2000. memset(&wrqu, 0, sizeof(wrqu));
  2001. wrqu.data.length = sizeof(micfailure);
  2002. wireless_send_event(usbdev->net, IWEVMICHAELMICFAILURE,
  2003. &wrqu, (u8 *)&micfailure);
  2004. }
  2005. buflen -= le32_to_cpu(auth_req->length);
  2006. buf += le32_to_cpu(auth_req->length);
  2007. }
  2008. }
  2009. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  2010. struct ndis_80211_status_indication *indication,
  2011. int len)
  2012. {
  2013. struct ndis_80211_pmkid_cand_list *cand_list;
  2014. int list_len, expected_len, i;
  2015. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2016. sizeof(struct ndis_80211_pmkid_cand_list)) {
  2017. devinfo(usbdev, "pmkid candidate list indication: "
  2018. "too short message (%i)", len);
  2019. return;
  2020. }
  2021. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  2022. sizeof(struct ndis_80211_pmkid_candidate);
  2023. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  2024. offsetof(struct ndis_80211_status_indication, u);
  2025. if (len < expected_len) {
  2026. devinfo(usbdev, "pmkid candidate list indication: "
  2027. "list larger than buffer (%i < %i)",
  2028. len, expected_len);
  2029. return;
  2030. }
  2031. cand_list = &indication->u.cand_list;
  2032. devinfo(usbdev, "pmkid candidate list indication: "
  2033. "version %i, candidates %i",
  2034. le32_to_cpu(cand_list->version),
  2035. le32_to_cpu(cand_list->num_candidates));
  2036. if (le32_to_cpu(cand_list->version) != 1)
  2037. return;
  2038. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  2039. struct iw_pmkid_cand pcand;
  2040. union iwreq_data wrqu;
  2041. struct ndis_80211_pmkid_candidate *cand =
  2042. &cand_list->candidate_list[i];
  2043. devdbg(usbdev, "cand[%i]: flags: 0x%08x, bssid: %pM",
  2044. i, le32_to_cpu(cand->flags), cand->bssid);
  2045. memset(&pcand, 0, sizeof(pcand));
  2046. if (le32_to_cpu(cand->flags) & 0x01)
  2047. pcand.flags |= IW_PMKID_CAND_PREAUTH;
  2048. pcand.index = i;
  2049. memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
  2050. memset(&wrqu, 0, sizeof(wrqu));
  2051. wrqu.data.length = sizeof(pcand);
  2052. wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
  2053. (u8 *)&pcand);
  2054. }
  2055. }
  2056. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  2057. struct rndis_indicate *msg, int buflen)
  2058. {
  2059. struct ndis_80211_status_indication *indication;
  2060. int len, offset;
  2061. offset = offsetof(struct rndis_indicate, status) +
  2062. le32_to_cpu(msg->offset);
  2063. len = le32_to_cpu(msg->length);
  2064. if (len < 8) {
  2065. devinfo(usbdev, "media specific indication, "
  2066. "ignore too short message (%i < 8)", len);
  2067. return;
  2068. }
  2069. if (offset + len > buflen) {
  2070. devinfo(usbdev, "media specific indication, "
  2071. "too large to fit to buffer (%i > %i)",
  2072. offset + len, buflen);
  2073. return;
  2074. }
  2075. indication = (void *)((u8 *)msg + offset);
  2076. switch (le32_to_cpu(indication->status_type)) {
  2077. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  2078. devinfo(usbdev, "radio state indication: %i",
  2079. le32_to_cpu(indication->u.radio_status));
  2080. return;
  2081. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  2082. devinfo(usbdev, "media stream mode indication: %i",
  2083. le32_to_cpu(indication->u.media_stream_mode));
  2084. return;
  2085. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  2086. rndis_wlan_auth_indication(usbdev, indication, len);
  2087. return;
  2088. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2089. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2090. return;
  2091. default:
  2092. devinfo(usbdev, "media specific indication: "
  2093. "unknown status type 0x%08x",
  2094. le32_to_cpu(indication->status_type));
  2095. }
  2096. }
  2097. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2098. {
  2099. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2100. struct rndis_indicate *msg = ind;
  2101. switch (msg->status) {
  2102. case RNDIS_STATUS_MEDIA_CONNECT:
  2103. if (priv->current_command_oid == OID_802_11_ADD_KEY) {
  2104. /* OID_802_11_ADD_KEY causes sometimes extra
  2105. * "media connect" indications which confuses driver
  2106. * and userspace to think that device is
  2107. * roaming/reassociating when it isn't.
  2108. */
  2109. devdbg(usbdev, "ignored OID_802_11_ADD_KEY triggered "
  2110. "'media connect'");
  2111. return;
  2112. }
  2113. usbnet_pause_rx(usbdev);
  2114. devinfo(usbdev, "media connect");
  2115. /* queue work to avoid recursive calls into rndis_command */
  2116. set_bit(WORK_LINK_UP, &priv->work_pending);
  2117. queue_work(priv->workqueue, &priv->work);
  2118. break;
  2119. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2120. devinfo(usbdev, "media disconnect");
  2121. /* queue work to avoid recursive calls into rndis_command */
  2122. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2123. queue_work(priv->workqueue, &priv->work);
  2124. break;
  2125. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2126. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2127. break;
  2128. default:
  2129. devinfo(usbdev, "indication: 0x%08x",
  2130. le32_to_cpu(msg->status));
  2131. break;
  2132. }
  2133. }
  2134. static int rndis_wlan_get_caps(struct usbnet *usbdev)
  2135. {
  2136. struct {
  2137. __le32 num_items;
  2138. __le32 items[8];
  2139. } networks_supported;
  2140. int len, retval, i, n;
  2141. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2142. /* determine supported modes */
  2143. len = sizeof(networks_supported);
  2144. retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
  2145. &networks_supported, &len);
  2146. if (retval >= 0) {
  2147. n = le32_to_cpu(networks_supported.num_items);
  2148. if (n > 8)
  2149. n = 8;
  2150. for (i = 0; i < n; i++) {
  2151. switch (le32_to_cpu(networks_supported.items[i])) {
  2152. case NDIS_80211_TYPE_FREQ_HOP:
  2153. case NDIS_80211_TYPE_DIRECT_SEQ:
  2154. priv->caps |= CAP_MODE_80211B;
  2155. break;
  2156. case NDIS_80211_TYPE_OFDM_A:
  2157. priv->caps |= CAP_MODE_80211A;
  2158. break;
  2159. case NDIS_80211_TYPE_OFDM_G:
  2160. priv->caps |= CAP_MODE_80211G;
  2161. break;
  2162. }
  2163. }
  2164. }
  2165. return retval;
  2166. }
  2167. #define STATS_UPDATE_JIFFIES (HZ)
  2168. static void rndis_update_wireless_stats(struct work_struct *work)
  2169. {
  2170. struct rndis_wlan_private *priv =
  2171. container_of(work, struct rndis_wlan_private, stats_work.work);
  2172. struct usbnet *usbdev = priv->usbdev;
  2173. struct iw_statistics iwstats;
  2174. __le32 rssi, tmp;
  2175. int len, ret, j;
  2176. unsigned long flags;
  2177. int update_jiffies = STATS_UPDATE_JIFFIES;
  2178. void *buf;
  2179. spin_lock_irqsave(&priv->stats_lock, flags);
  2180. memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
  2181. spin_unlock_irqrestore(&priv->stats_lock, flags);
  2182. /* only update stats when connected */
  2183. if (!is_associated(usbdev)) {
  2184. iwstats.qual.qual = 0;
  2185. iwstats.qual.level = 0;
  2186. iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2187. | IW_QUAL_LEVEL_UPDATED
  2188. | IW_QUAL_NOISE_INVALID
  2189. | IW_QUAL_QUAL_INVALID
  2190. | IW_QUAL_LEVEL_INVALID;
  2191. goto end;
  2192. }
  2193. len = sizeof(rssi);
  2194. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  2195. devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
  2196. le32_to_cpu(rssi));
  2197. if (ret == 0) {
  2198. memset(&iwstats.qual, 0, sizeof(iwstats.qual));
  2199. iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
  2200. iwstats.qual.level = level_to_qual(le32_to_cpu(rssi));
  2201. iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2202. | IW_QUAL_LEVEL_UPDATED
  2203. | IW_QUAL_NOISE_INVALID;
  2204. }
  2205. memset(&iwstats.discard, 0, sizeof(iwstats.discard));
  2206. len = sizeof(tmp);
  2207. ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
  2208. if (ret == 0)
  2209. iwstats.discard.misc += le32_to_cpu(tmp);
  2210. len = sizeof(tmp);
  2211. ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
  2212. if (ret == 0)
  2213. iwstats.discard.misc += le32_to_cpu(tmp);
  2214. len = sizeof(tmp);
  2215. ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
  2216. if (ret == 0)
  2217. iwstats.discard.misc += le32_to_cpu(tmp);
  2218. /* Workaround transfer stalls on poor quality links.
  2219. * TODO: find right way to fix these stalls (as stalls do not happen
  2220. * with ndiswrapper/windows driver). */
  2221. if (iwstats.qual.qual <= 25) {
  2222. /* Decrease stats worker interval to catch stalls.
  2223. * faster. Faster than 400-500ms causes packet loss,
  2224. * Slower doesn't catch stalls fast enough.
  2225. */
  2226. j = msecs_to_jiffies(priv->param_workaround_interval);
  2227. if (j > STATS_UPDATE_JIFFIES)
  2228. j = STATS_UPDATE_JIFFIES;
  2229. else if (j <= 0)
  2230. j = 1;
  2231. update_jiffies = j;
  2232. /* Send scan OID. Use of both OIDs is required to get device
  2233. * working.
  2234. */
  2235. tmp = cpu_to_le32(1);
  2236. rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  2237. sizeof(tmp));
  2238. len = CONTROL_BUFFER_SIZE;
  2239. buf = kmalloc(len, GFP_KERNEL);
  2240. if (!buf)
  2241. goto end;
  2242. rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  2243. kfree(buf);
  2244. }
  2245. end:
  2246. spin_lock_irqsave(&priv->stats_lock, flags);
  2247. memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
  2248. spin_unlock_irqrestore(&priv->stats_lock, flags);
  2249. if (update_jiffies >= HZ)
  2250. update_jiffies = round_jiffies_relative(update_jiffies);
  2251. else {
  2252. j = round_jiffies_relative(update_jiffies);
  2253. if (abs(j - update_jiffies) <= 10)
  2254. update_jiffies = j;
  2255. }
  2256. queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
  2257. }
  2258. static int bcm4320a_early_init(struct usbnet *usbdev)
  2259. {
  2260. /* bcm4320a doesn't handle configuration parameters well. Try
  2261. * set any and you get partially zeroed mac and broken device.
  2262. */
  2263. return 0;
  2264. }
  2265. static int bcm4320b_early_init(struct usbnet *usbdev)
  2266. {
  2267. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2268. char buf[8];
  2269. /* Early initialization settings, setting these won't have effect
  2270. * if called after generic_rndis_bind().
  2271. */
  2272. priv->param_country[0] = modparam_country[0];
  2273. priv->param_country[1] = modparam_country[1];
  2274. priv->param_country[2] = 0;
  2275. priv->param_frameburst = modparam_frameburst;
  2276. priv->param_afterburner = modparam_afterburner;
  2277. priv->param_power_save = modparam_power_save;
  2278. priv->param_power_output = modparam_power_output;
  2279. priv->param_roamtrigger = modparam_roamtrigger;
  2280. priv->param_roamdelta = modparam_roamdelta;
  2281. priv->param_country[0] = toupper(priv->param_country[0]);
  2282. priv->param_country[1] = toupper(priv->param_country[1]);
  2283. /* doesn't support EU as country code, use FI instead */
  2284. if (!strcmp(priv->param_country, "EU"))
  2285. strcpy(priv->param_country, "FI");
  2286. if (priv->param_power_save < 0)
  2287. priv->param_power_save = 0;
  2288. else if (priv->param_power_save > 2)
  2289. priv->param_power_save = 2;
  2290. if (priv->param_power_output < 0)
  2291. priv->param_power_output = 0;
  2292. else if (priv->param_power_output > 3)
  2293. priv->param_power_output = 3;
  2294. if (priv->param_roamtrigger < -80)
  2295. priv->param_roamtrigger = -80;
  2296. else if (priv->param_roamtrigger > -60)
  2297. priv->param_roamtrigger = -60;
  2298. if (priv->param_roamdelta < 0)
  2299. priv->param_roamdelta = 0;
  2300. else if (priv->param_roamdelta > 2)
  2301. priv->param_roamdelta = 2;
  2302. if (modparam_workaround_interval < 0)
  2303. priv->param_workaround_interval = 500;
  2304. else
  2305. priv->param_workaround_interval = modparam_workaround_interval;
  2306. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2307. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2308. priv->param_frameburst ? "1" : "0");
  2309. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2310. priv->param_afterburner ? "1" : "0");
  2311. sprintf(buf, "%d", priv->param_power_save);
  2312. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2313. sprintf(buf, "%d", priv->param_power_output);
  2314. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2315. sprintf(buf, "%d", priv->param_roamtrigger);
  2316. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2317. sprintf(buf, "%d", priv->param_roamdelta);
  2318. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2319. return 0;
  2320. }
  2321. /* same as rndis_netdev_ops but with local multicast handler */
  2322. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2323. .ndo_open = usbnet_open,
  2324. .ndo_stop = usbnet_stop,
  2325. .ndo_start_xmit = usbnet_start_xmit,
  2326. .ndo_tx_timeout = usbnet_tx_timeout,
  2327. .ndo_set_mac_address = eth_mac_addr,
  2328. .ndo_validate_addr = eth_validate_addr,
  2329. .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
  2330. };
  2331. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2332. {
  2333. struct wiphy *wiphy;
  2334. struct rndis_wlan_private *priv;
  2335. int retval, len;
  2336. __le32 tmp;
  2337. /* allocate wiphy and rndis private data
  2338. * NOTE: We only support a single virtual interface, so wiphy
  2339. * and wireless_dev are somewhat synonymous for this device.
  2340. */
  2341. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2342. if (!wiphy)
  2343. return -ENOMEM;
  2344. priv = wiphy_priv(wiphy);
  2345. usbdev->net->ieee80211_ptr = &priv->wdev;
  2346. priv->wdev.wiphy = wiphy;
  2347. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2348. /* These have to be initialized before calling generic_rndis_bind().
  2349. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2350. */
  2351. usbdev->driver_priv = priv;
  2352. usbdev->net->wireless_handlers = &rndis_iw_handlers;
  2353. priv->usbdev = usbdev;
  2354. mutex_init(&priv->command_lock);
  2355. spin_lock_init(&priv->stats_lock);
  2356. /* because rndis_command() sleeps we need to use workqueue */
  2357. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2358. INIT_WORK(&priv->work, rndis_wlan_worker);
  2359. INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
  2360. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2361. /* try bind rndis_host */
  2362. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2363. if (retval < 0)
  2364. goto fail;
  2365. /* generic_rndis_bind set packet filter to multicast_all+
  2366. * promisc mode which doesn't work well for our devices (device
  2367. * picks up rssi to closest station instead of to access point).
  2368. *
  2369. * rndis_host wants to avoid all OID as much as possible
  2370. * so do promisc/multicast handling in rndis_wlan.
  2371. */
  2372. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2373. tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  2374. retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
  2375. sizeof(tmp));
  2376. len = sizeof(tmp);
  2377. retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
  2378. &len);
  2379. priv->multicast_size = le32_to_cpu(tmp);
  2380. if (retval < 0 || priv->multicast_size < 0)
  2381. priv->multicast_size = 0;
  2382. if (priv->multicast_size > 0)
  2383. usbdev->net->flags |= IFF_MULTICAST;
  2384. else
  2385. usbdev->net->flags &= ~IFF_MULTICAST;
  2386. priv->iwstats.qual.qual = 0;
  2387. priv->iwstats.qual.level = 0;
  2388. priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2389. | IW_QUAL_LEVEL_UPDATED
  2390. | IW_QUAL_NOISE_INVALID
  2391. | IW_QUAL_QUAL_INVALID
  2392. | IW_QUAL_LEVEL_INVALID;
  2393. /* fill-out wiphy structure and register w/ cfg80211 */
  2394. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2395. wiphy->privid = rndis_wiphy_privid;
  2396. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2397. | BIT(NL80211_IFTYPE_ADHOC);
  2398. wiphy->max_scan_ssids = 1;
  2399. /* TODO: fill-out band/encr information based on priv->caps */
  2400. rndis_wlan_get_caps(usbdev);
  2401. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2402. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2403. priv->band.channels = priv->channels;
  2404. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2405. priv->band.bitrates = priv->rates;
  2406. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2407. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2408. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2409. memcpy(priv->cipher_suites, rndis_cipher_suites,
  2410. sizeof(rndis_cipher_suites));
  2411. wiphy->cipher_suites = priv->cipher_suites;
  2412. wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
  2413. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2414. if (wiphy_register(wiphy)) {
  2415. retval = -ENODEV;
  2416. goto fail;
  2417. }
  2418. set_default_iw_params(usbdev);
  2419. /* set default rts/frag */
  2420. rndis_set_wiphy_params(wiphy,
  2421. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2422. /* turn radio on */
  2423. priv->radio_on = 1;
  2424. disassociate(usbdev, 1);
  2425. netif_carrier_off(usbdev->net);
  2426. return 0;
  2427. fail:
  2428. cancel_delayed_work_sync(&priv->stats_work);
  2429. cancel_delayed_work_sync(&priv->scan_work);
  2430. cancel_work_sync(&priv->work);
  2431. flush_workqueue(priv->workqueue);
  2432. destroy_workqueue(priv->workqueue);
  2433. wiphy_free(wiphy);
  2434. return retval;
  2435. }
  2436. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2437. {
  2438. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2439. /* turn radio off */
  2440. disassociate(usbdev, 0);
  2441. cancel_delayed_work_sync(&priv->stats_work);
  2442. cancel_delayed_work_sync(&priv->scan_work);
  2443. cancel_work_sync(&priv->work);
  2444. flush_workqueue(priv->workqueue);
  2445. destroy_workqueue(priv->workqueue);
  2446. if (priv && priv->wpa_ie_len)
  2447. kfree(priv->wpa_ie);
  2448. rndis_unbind(usbdev, intf);
  2449. wiphy_unregister(priv->wdev.wiphy);
  2450. wiphy_free(priv->wdev.wiphy);
  2451. }
  2452. static int rndis_wlan_reset(struct usbnet *usbdev)
  2453. {
  2454. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2455. int retval;
  2456. devdbg(usbdev, "rndis_wlan_reset");
  2457. retval = rndis_reset(usbdev);
  2458. if (retval)
  2459. devwarn(usbdev, "rndis_reset() failed: %d", retval);
  2460. /* rndis_reset cleared multicast list, so restore here.
  2461. (set_multicast_list() also turns on current packet filter) */
  2462. set_multicast_list(usbdev);
  2463. queue_delayed_work(priv->workqueue, &priv->stats_work,
  2464. round_jiffies_relative(STATS_UPDATE_JIFFIES));
  2465. return deauthenticate(usbdev);
  2466. }
  2467. static int rndis_wlan_stop(struct usbnet *usbdev)
  2468. {
  2469. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2470. int retval;
  2471. __le32 filter;
  2472. devdbg(usbdev, "rndis_wlan_stop");
  2473. retval = disassociate(usbdev, 0);
  2474. priv->work_pending = 0;
  2475. cancel_delayed_work_sync(&priv->stats_work);
  2476. cancel_delayed_work_sync(&priv->scan_work);
  2477. cancel_work_sync(&priv->work);
  2478. flush_workqueue(priv->workqueue);
  2479. if (priv->scan_request) {
  2480. cfg80211_scan_done(priv->scan_request, true);
  2481. priv->scan_request = NULL;
  2482. }
  2483. /* Set current packet filter zero to block receiving data packets from
  2484. device. */
  2485. filter = 0;
  2486. rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2487. sizeof(filter));
  2488. return retval;
  2489. }
  2490. static const struct driver_info bcm4320b_info = {
  2491. .description = "Wireless RNDIS device, BCM4320b based",
  2492. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2493. FLAG_AVOID_UNLINK_URBS,
  2494. .bind = rndis_wlan_bind,
  2495. .unbind = rndis_wlan_unbind,
  2496. .status = rndis_status,
  2497. .rx_fixup = rndis_rx_fixup,
  2498. .tx_fixup = rndis_tx_fixup,
  2499. .reset = rndis_wlan_reset,
  2500. .stop = rndis_wlan_stop,
  2501. .early_init = bcm4320b_early_init,
  2502. .indication = rndis_wlan_indication,
  2503. };
  2504. static const struct driver_info bcm4320a_info = {
  2505. .description = "Wireless RNDIS device, BCM4320a based",
  2506. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2507. FLAG_AVOID_UNLINK_URBS,
  2508. .bind = rndis_wlan_bind,
  2509. .unbind = rndis_wlan_unbind,
  2510. .status = rndis_status,
  2511. .rx_fixup = rndis_rx_fixup,
  2512. .tx_fixup = rndis_tx_fixup,
  2513. .reset = rndis_wlan_reset,
  2514. .stop = rndis_wlan_stop,
  2515. .early_init = bcm4320a_early_init,
  2516. .indication = rndis_wlan_indication,
  2517. };
  2518. static const struct driver_info rndis_wlan_info = {
  2519. .description = "Wireless RNDIS device",
  2520. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2521. FLAG_AVOID_UNLINK_URBS,
  2522. .bind = rndis_wlan_bind,
  2523. .unbind = rndis_wlan_unbind,
  2524. .status = rndis_status,
  2525. .rx_fixup = rndis_rx_fixup,
  2526. .tx_fixup = rndis_tx_fixup,
  2527. .reset = rndis_wlan_reset,
  2528. .stop = rndis_wlan_stop,
  2529. .early_init = bcm4320a_early_init,
  2530. .indication = rndis_wlan_indication,
  2531. };
  2532. /*-------------------------------------------------------------------------*/
  2533. static const struct usb_device_id products [] = {
  2534. #define RNDIS_MASTER_INTERFACE \
  2535. .bInterfaceClass = USB_CLASS_COMM, \
  2536. .bInterfaceSubClass = 2 /* ACM */, \
  2537. .bInterfaceProtocol = 0x0ff
  2538. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2539. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  2540. */
  2541. {
  2542. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2543. | USB_DEVICE_ID_MATCH_DEVICE,
  2544. .idVendor = 0x0411,
  2545. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  2546. RNDIS_MASTER_INTERFACE,
  2547. .driver_info = (unsigned long) &bcm4320b_info,
  2548. }, {
  2549. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2550. | USB_DEVICE_ID_MATCH_DEVICE,
  2551. .idVendor = 0x0baf,
  2552. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  2553. RNDIS_MASTER_INTERFACE,
  2554. .driver_info = (unsigned long) &bcm4320b_info,
  2555. }, {
  2556. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2557. | USB_DEVICE_ID_MATCH_DEVICE,
  2558. .idVendor = 0x050d,
  2559. .idProduct = 0x011b, /* Belkin F5D7051 */
  2560. RNDIS_MASTER_INTERFACE,
  2561. .driver_info = (unsigned long) &bcm4320b_info,
  2562. }, {
  2563. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2564. | USB_DEVICE_ID_MATCH_DEVICE,
  2565. .idVendor = 0x1799, /* Belkin has two vendor ids */
  2566. .idProduct = 0x011b, /* Belkin F5D7051 */
  2567. RNDIS_MASTER_INTERFACE,
  2568. .driver_info = (unsigned long) &bcm4320b_info,
  2569. }, {
  2570. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2571. | USB_DEVICE_ID_MATCH_DEVICE,
  2572. .idVendor = 0x13b1,
  2573. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  2574. RNDIS_MASTER_INTERFACE,
  2575. .driver_info = (unsigned long) &bcm4320b_info,
  2576. }, {
  2577. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2578. | USB_DEVICE_ID_MATCH_DEVICE,
  2579. .idVendor = 0x13b1,
  2580. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  2581. RNDIS_MASTER_INTERFACE,
  2582. .driver_info = (unsigned long) &bcm4320b_info,
  2583. }, {
  2584. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2585. | USB_DEVICE_ID_MATCH_DEVICE,
  2586. .idVendor = 0x0b05,
  2587. .idProduct = 0x1717, /* Asus WL169gE */
  2588. RNDIS_MASTER_INTERFACE,
  2589. .driver_info = (unsigned long) &bcm4320b_info,
  2590. }, {
  2591. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2592. | USB_DEVICE_ID_MATCH_DEVICE,
  2593. .idVendor = 0x0a5c,
  2594. .idProduct = 0xd11b, /* Eminent EM4045 */
  2595. RNDIS_MASTER_INTERFACE,
  2596. .driver_info = (unsigned long) &bcm4320b_info,
  2597. }, {
  2598. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2599. | USB_DEVICE_ID_MATCH_DEVICE,
  2600. .idVendor = 0x1690,
  2601. .idProduct = 0x0715, /* BT Voyager 1055 */
  2602. RNDIS_MASTER_INTERFACE,
  2603. .driver_info = (unsigned long) &bcm4320b_info,
  2604. },
  2605. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  2606. * parameters available, hardware probably contain older firmware version with
  2607. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  2608. */
  2609. {
  2610. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2611. | USB_DEVICE_ID_MATCH_DEVICE,
  2612. .idVendor = 0x13b1,
  2613. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  2614. RNDIS_MASTER_INTERFACE,
  2615. .driver_info = (unsigned long) &bcm4320a_info,
  2616. }, {
  2617. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2618. | USB_DEVICE_ID_MATCH_DEVICE,
  2619. .idVendor = 0x0baf,
  2620. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  2621. RNDIS_MASTER_INTERFACE,
  2622. .driver_info = (unsigned long) &bcm4320a_info,
  2623. }, {
  2624. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2625. | USB_DEVICE_ID_MATCH_DEVICE,
  2626. .idVendor = 0x0411,
  2627. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  2628. RNDIS_MASTER_INTERFACE,
  2629. .driver_info = (unsigned long) &bcm4320a_info,
  2630. },
  2631. /* Generic Wireless RNDIS devices that we don't have exact
  2632. * idVendor/idProduct/chip yet.
  2633. */
  2634. {
  2635. /* RNDIS is MSFT's un-official variant of CDC ACM */
  2636. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  2637. .driver_info = (unsigned long) &rndis_wlan_info,
  2638. }, {
  2639. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  2640. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  2641. .driver_info = (unsigned long) &rndis_wlan_info,
  2642. },
  2643. { }, // END
  2644. };
  2645. MODULE_DEVICE_TABLE(usb, products);
  2646. static struct usb_driver rndis_wlan_driver = {
  2647. .name = "rndis_wlan",
  2648. .id_table = products,
  2649. .probe = usbnet_probe,
  2650. .disconnect = usbnet_disconnect,
  2651. .suspend = usbnet_suspend,
  2652. .resume = usbnet_resume,
  2653. };
  2654. static int __init rndis_wlan_init(void)
  2655. {
  2656. return usb_register(&rndis_wlan_driver);
  2657. }
  2658. module_init(rndis_wlan_init);
  2659. static void __exit rndis_wlan_exit(void)
  2660. {
  2661. usb_deregister(&rndis_wlan_driver);
  2662. }
  2663. module_exit(rndis_wlan_exit);
  2664. MODULE_AUTHOR("Bjorge Dijkstra");
  2665. MODULE_AUTHOR("Jussi Kivilinna");
  2666. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  2667. MODULE_LICENSE("GPL");