rndis_wlan.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264
  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 int set_infra_mode(struct usbnet *usbdev, int mode);
  780. static void restore_keys(struct usbnet *usbdev);
  781. static int rndis_check_bssid_list(struct usbnet *usbdev);
  782. static int get_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  783. {
  784. int ret, len;
  785. len = sizeof(*ssid);
  786. ret = rndis_query_oid(usbdev, OID_802_11_SSID, ssid, &len);
  787. if (ret != 0)
  788. ssid->length = 0;
  789. #ifdef DEBUG
  790. {
  791. unsigned char tmp[NDIS_802_11_LENGTH_SSID + 1];
  792. memcpy(tmp, ssid->essid, le32_to_cpu(ssid->length));
  793. tmp[le32_to_cpu(ssid->length)] = 0;
  794. devdbg(usbdev, "get_essid: '%s', ret: %d", tmp, ret);
  795. }
  796. #endif
  797. return ret;
  798. }
  799. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  800. {
  801. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  802. int ret;
  803. ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
  804. if (ret == 0) {
  805. memcpy(&priv->essid, ssid, sizeof(priv->essid));
  806. priv->radio_on = 1;
  807. devdbg(usbdev, "set_essid: radio_on = 1");
  808. }
  809. return ret;
  810. }
  811. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  812. {
  813. int ret, len;
  814. len = ETH_ALEN;
  815. ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
  816. if (ret != 0)
  817. memset(bssid, 0, ETH_ALEN);
  818. return ret;
  819. }
  820. static int get_association_info(struct usbnet *usbdev,
  821. struct ndis_80211_assoc_info *info, int len)
  822. {
  823. return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
  824. info, &len);
  825. }
  826. static int is_associated(struct usbnet *usbdev)
  827. {
  828. u8 bssid[ETH_ALEN];
  829. int ret;
  830. ret = get_bssid(usbdev, bssid);
  831. return (ret == 0 && !is_zero_ether_addr(bssid));
  832. }
  833. static int disassociate(struct usbnet *usbdev, int reset_ssid)
  834. {
  835. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  836. struct ndis_80211_ssid ssid;
  837. int i, ret = 0;
  838. if (priv->radio_on) {
  839. ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
  840. if (ret == 0) {
  841. priv->radio_on = 0;
  842. devdbg(usbdev, "disassociate: radio_on = 0");
  843. if (reset_ssid)
  844. msleep(100);
  845. }
  846. }
  847. /* disassociate causes radio to be turned off; if reset_ssid
  848. * is given, set random ssid to enable radio */
  849. if (reset_ssid) {
  850. /* Set device to infrastructure mode so we don't get ad-hoc
  851. * 'media connect' indications with the random ssid.
  852. */
  853. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  854. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  855. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  856. ssid.essid[0] = 0x1;
  857. ssid.essid[1] = 0xff;
  858. for (i = 2; i < sizeof(ssid.essid); i++)
  859. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  860. ret = set_essid(usbdev, &ssid);
  861. }
  862. return ret;
  863. }
  864. static int set_auth_mode(struct usbnet *usbdev, int wpa_version, int authalg)
  865. {
  866. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  867. __le32 tmp;
  868. int auth_mode, ret;
  869. devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
  870. "keymgmt=0x%x", wpa_version, authalg, priv->wpa_keymgmt);
  871. if (wpa_version & IW_AUTH_WPA_VERSION_WPA2) {
  872. if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
  873. auth_mode = NDIS_80211_AUTH_WPA2;
  874. else
  875. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  876. } else if (wpa_version & IW_AUTH_WPA_VERSION_WPA) {
  877. if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
  878. auth_mode = NDIS_80211_AUTH_WPA;
  879. else if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_PSK)
  880. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  881. else
  882. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  883. } else if (authalg & IW_AUTH_ALG_SHARED_KEY) {
  884. if (authalg & IW_AUTH_ALG_OPEN_SYSTEM)
  885. auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
  886. else
  887. auth_mode = NDIS_80211_AUTH_SHARED;
  888. } else
  889. auth_mode = NDIS_80211_AUTH_OPEN;
  890. tmp = cpu_to_le32(auth_mode);
  891. ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
  892. sizeof(tmp));
  893. if (ret != 0) {
  894. devwarn(usbdev, "setting auth mode failed (%08X)", ret);
  895. return ret;
  896. }
  897. priv->wpa_version = wpa_version;
  898. priv->wpa_authalg = authalg;
  899. return 0;
  900. }
  901. static int set_priv_filter(struct usbnet *usbdev)
  902. {
  903. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  904. __le32 tmp;
  905. devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
  906. if (priv->wpa_version & IW_AUTH_WPA_VERSION_WPA2 ||
  907. priv->wpa_version & IW_AUTH_WPA_VERSION_WPA)
  908. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  909. else
  910. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  911. return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
  912. sizeof(tmp));
  913. }
  914. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  915. {
  916. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  917. __le32 tmp;
  918. int encr_mode, ret;
  919. devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
  920. pairwise,
  921. groupwise);
  922. if (pairwise & IW_AUTH_CIPHER_CCMP)
  923. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  924. else if (pairwise & IW_AUTH_CIPHER_TKIP)
  925. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  926. else if (pairwise &
  927. (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
  928. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  929. else if (groupwise & IW_AUTH_CIPHER_CCMP)
  930. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  931. else if (groupwise & IW_AUTH_CIPHER_TKIP)
  932. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  933. else
  934. encr_mode = NDIS_80211_ENCR_DISABLED;
  935. tmp = cpu_to_le32(encr_mode);
  936. ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
  937. sizeof(tmp));
  938. if (ret != 0) {
  939. devwarn(usbdev, "setting encr mode failed (%08X)", ret);
  940. return ret;
  941. }
  942. priv->wpa_cipher_pair = pairwise;
  943. priv->wpa_cipher_group = groupwise;
  944. return 0;
  945. }
  946. static int set_assoc_params(struct usbnet *usbdev)
  947. {
  948. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  949. set_auth_mode(usbdev, priv->wpa_version, priv->wpa_authalg);
  950. set_priv_filter(usbdev);
  951. set_encr_mode(usbdev, priv->wpa_cipher_pair, priv->wpa_cipher_group);
  952. return 0;
  953. }
  954. static int set_infra_mode(struct usbnet *usbdev, int mode)
  955. {
  956. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  957. __le32 tmp;
  958. int ret;
  959. devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
  960. tmp = cpu_to_le32(mode);
  961. ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
  962. sizeof(tmp));
  963. if (ret != 0) {
  964. devwarn(usbdev, "setting infra mode failed (%08X)", ret);
  965. return ret;
  966. }
  967. /* NDIS drivers clear keys when infrastructure mode is
  968. * changed. But Linux tools assume otherwise. So set the
  969. * keys */
  970. restore_keys(usbdev);
  971. priv->infra_mode = mode;
  972. return 0;
  973. }
  974. static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
  975. {
  976. __le32 tmp;
  977. devdbg(usbdev, "set_rts_threshold %i", rts_threshold);
  978. if (rts_threshold < 0 || rts_threshold > 2347)
  979. rts_threshold = 2347;
  980. tmp = cpu_to_le32(rts_threshold);
  981. return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
  982. sizeof(tmp));
  983. }
  984. static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
  985. {
  986. __le32 tmp;
  987. devdbg(usbdev, "set_frag_threshold %i", frag_threshold);
  988. if (frag_threshold < 256 || frag_threshold > 2346)
  989. frag_threshold = 2346;
  990. tmp = cpu_to_le32(frag_threshold);
  991. return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
  992. sizeof(tmp));
  993. }
  994. static void set_default_iw_params(struct usbnet *usbdev)
  995. {
  996. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  997. priv->wpa_keymgmt = 0;
  998. priv->wpa_version = 0;
  999. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1000. set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1001. IW_AUTH_ALG_OPEN_SYSTEM);
  1002. set_priv_filter(usbdev);
  1003. set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
  1004. }
  1005. static int deauthenticate(struct usbnet *usbdev)
  1006. {
  1007. int ret;
  1008. ret = disassociate(usbdev, 1);
  1009. set_default_iw_params(usbdev);
  1010. return ret;
  1011. }
  1012. /* index must be 0 - N, as per NDIS */
  1013. static int add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index)
  1014. {
  1015. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1016. struct ndis_80211_wep_key ndis_key;
  1017. int cipher, ret;
  1018. if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
  1019. return -EINVAL;
  1020. if (key_len == 5)
  1021. cipher = WLAN_CIPHER_SUITE_WEP40;
  1022. else
  1023. cipher = WLAN_CIPHER_SUITE_WEP104;
  1024. memset(&ndis_key, 0, sizeof(ndis_key));
  1025. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1026. ndis_key.length = cpu_to_le32(key_len);
  1027. ndis_key.index = cpu_to_le32(index);
  1028. memcpy(&ndis_key.material, key, key_len);
  1029. if (index == priv->encr_tx_key_index) {
  1030. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1031. ret = set_encr_mode(usbdev, IW_AUTH_CIPHER_WEP104,
  1032. IW_AUTH_CIPHER_NONE);
  1033. if (ret)
  1034. devwarn(usbdev, "encryption couldn't be enabled (%08X)",
  1035. ret);
  1036. }
  1037. ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
  1038. sizeof(ndis_key));
  1039. if (ret != 0) {
  1040. devwarn(usbdev, "adding encryption key %d failed (%08X)",
  1041. index+1, ret);
  1042. return ret;
  1043. }
  1044. priv->encr_keys[index].len = key_len;
  1045. priv->encr_keys[index].cipher = cipher;
  1046. memcpy(&priv->encr_keys[index].material, key, key_len);
  1047. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1048. return 0;
  1049. }
  1050. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1051. int index, const u8 *addr, const u8 *rx_seq, int cipher,
  1052. int flags)
  1053. {
  1054. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1055. struct ndis_80211_key ndis_key;
  1056. bool is_addr_ok;
  1057. int ret;
  1058. if (index < 0 || index >= 4) {
  1059. devdbg(usbdev, "add_wpa_key: index out of range (%i)", index);
  1060. return -EINVAL;
  1061. }
  1062. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1063. devdbg(usbdev, "add_wpa_key: key length out of range (%i)",
  1064. key_len);
  1065. return -EINVAL;
  1066. }
  1067. if ((flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) && !rx_seq) {
  1068. devdbg(usbdev, "add_wpa_key: recv seq flag without buffer");
  1069. return -EINVAL;
  1070. }
  1071. is_addr_ok = addr && !is_zero_ether_addr(addr) &&
  1072. !is_broadcast_ether_addr(addr);
  1073. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1074. devdbg(usbdev, "add_wpa_key: pairwise but bssid invalid (%pM)",
  1075. addr);
  1076. return -EINVAL;
  1077. }
  1078. devdbg(usbdev, "add_wpa_key(%i): flags:%i%i%i", index,
  1079. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1080. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1081. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1082. memset(&ndis_key, 0, sizeof(ndis_key));
  1083. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1084. sizeof(ndis_key.material) + key_len);
  1085. ndis_key.length = cpu_to_le32(key_len);
  1086. ndis_key.index = cpu_to_le32(index) | flags;
  1087. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1088. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1089. * different order than NDIS spec, so swap the order here. */
  1090. memcpy(ndis_key.material, key, 16);
  1091. memcpy(ndis_key.material + 16, key + 24, 8);
  1092. memcpy(ndis_key.material + 24, key + 16, 8);
  1093. } else
  1094. memcpy(ndis_key.material, key, key_len);
  1095. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1096. memcpy(ndis_key.rsc, rx_seq, 6);
  1097. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1098. /* pairwise key */
  1099. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1100. } else {
  1101. /* group key */
  1102. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1103. memset(ndis_key.bssid, 0xff, ETH_ALEN);
  1104. else
  1105. get_bssid(usbdev, ndis_key.bssid);
  1106. }
  1107. ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
  1108. le32_to_cpu(ndis_key.size));
  1109. devdbg(usbdev, "add_wpa_key: OID_802_11_ADD_KEY -> %08X", ret);
  1110. if (ret != 0)
  1111. return ret;
  1112. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1113. priv->encr_keys[index].len = key_len;
  1114. priv->encr_keys[index].cipher = cipher;
  1115. memcpy(&priv->encr_keys[index].material, key, key_len);
  1116. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1117. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1118. else
  1119. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1120. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1121. priv->encr_tx_key_index = index;
  1122. return 0;
  1123. }
  1124. static int restore_key(struct usbnet *usbdev, int key_idx)
  1125. {
  1126. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1127. struct rndis_wlan_encr_key key;
  1128. int flags;
  1129. key = priv->encr_keys[key_idx];
  1130. devdbg(usbdev, "restore_key: %i:%s:%i", key_idx,
  1131. is_wpa_key(priv, key_idx) ? "wpa" : "wep",
  1132. key.len);
  1133. if (key.len == 0)
  1134. return 0;
  1135. if (is_wpa_key(priv, key_idx)) {
  1136. flags = 0;
  1137. /*if (priv->encr_tx_key_index == key_idx)
  1138. flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;*/
  1139. if (!is_zero_ether_addr(key.bssid) &&
  1140. !is_broadcast_ether_addr(key.bssid))
  1141. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1142. return add_wpa_key(usbdev, key.material, key.len, key_idx,
  1143. key.bssid, NULL, key.cipher, flags);
  1144. }
  1145. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1146. }
  1147. static void restore_keys(struct usbnet *usbdev)
  1148. {
  1149. int i;
  1150. for (i = 0; i < 4; i++)
  1151. restore_key(usbdev, i);
  1152. }
  1153. static void clear_key(struct rndis_wlan_private *priv, int idx)
  1154. {
  1155. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1156. }
  1157. /* remove_key is for both wep and wpa */
  1158. static int remove_key(struct usbnet *usbdev, int index, u8 bssid[ETH_ALEN])
  1159. {
  1160. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1161. struct ndis_80211_remove_key remove_key;
  1162. __le32 keyindex;
  1163. bool is_wpa;
  1164. int ret;
  1165. if (priv->encr_keys[index].len == 0)
  1166. return 0;
  1167. is_wpa = is_wpa_key(priv, index);
  1168. devdbg(usbdev, "remove_key: %i:%s:%i", index, is_wpa ? "wpa" : "wep",
  1169. priv->encr_keys[index].len);
  1170. clear_key(priv, index);
  1171. if (is_wpa) {
  1172. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1173. remove_key.index = cpu_to_le32(index);
  1174. if (bssid) {
  1175. /* pairwise key */
  1176. if (!is_broadcast_ether_addr(bssid))
  1177. remove_key.index |=
  1178. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1179. memcpy(remove_key.bssid, bssid,
  1180. sizeof(remove_key.bssid));
  1181. } else
  1182. memset(remove_key.bssid, 0xff,
  1183. sizeof(remove_key.bssid));
  1184. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
  1185. sizeof(remove_key));
  1186. if (ret != 0)
  1187. return ret;
  1188. } else {
  1189. keyindex = cpu_to_le32(index);
  1190. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
  1191. sizeof(keyindex));
  1192. if (ret != 0) {
  1193. devwarn(usbdev,
  1194. "removing encryption key %d failed (%08X)",
  1195. index, ret);
  1196. return ret;
  1197. }
  1198. }
  1199. /* if it is transmit key, disable encryption */
  1200. if (index == priv->encr_tx_key_index)
  1201. set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
  1202. return 0;
  1203. }
  1204. static void set_multicast_list(struct usbnet *usbdev)
  1205. {
  1206. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1207. struct dev_mc_list *mclist;
  1208. __le32 filter;
  1209. int ret, i, size;
  1210. char *buf;
  1211. filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  1212. if (usbdev->net->flags & IFF_PROMISC) {
  1213. filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
  1214. RNDIS_PACKET_TYPE_ALL_LOCAL;
  1215. } else if (usbdev->net->flags & IFF_ALLMULTI ||
  1216. usbdev->net->mc_count > priv->multicast_size) {
  1217. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1218. } else if (usbdev->net->mc_count > 0) {
  1219. size = min(priv->multicast_size, usbdev->net->mc_count);
  1220. buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
  1221. if (!buf) {
  1222. devwarn(usbdev,
  1223. "couldn't alloc %d bytes of memory",
  1224. size * ETH_ALEN);
  1225. return;
  1226. }
  1227. mclist = usbdev->net->mc_list;
  1228. for (i = 0; i < size && mclist; mclist = mclist->next) {
  1229. if (mclist->dmi_addrlen != ETH_ALEN)
  1230. continue;
  1231. memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
  1232. i++;
  1233. }
  1234. ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
  1235. i * ETH_ALEN);
  1236. if (ret == 0 && i > 0)
  1237. filter |= RNDIS_PACKET_TYPE_MULTICAST;
  1238. else
  1239. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1240. devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
  1241. i, priv->multicast_size, ret);
  1242. kfree(buf);
  1243. }
  1244. ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1245. sizeof(filter));
  1246. if (ret < 0) {
  1247. devwarn(usbdev, "couldn't set packet filter: %08x",
  1248. le32_to_cpu(filter));
  1249. }
  1250. devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
  1251. le32_to_cpu(filter), ret);
  1252. }
  1253. /*
  1254. * cfg80211 ops
  1255. */
  1256. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1257. struct net_device *dev,
  1258. enum nl80211_iftype type, u32 *flags,
  1259. struct vif_params *params)
  1260. {
  1261. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1262. struct usbnet *usbdev = priv->usbdev;
  1263. int mode;
  1264. switch (type) {
  1265. case NL80211_IFTYPE_ADHOC:
  1266. mode = NDIS_80211_INFRA_ADHOC;
  1267. break;
  1268. case NL80211_IFTYPE_STATION:
  1269. mode = NDIS_80211_INFRA_INFRA;
  1270. break;
  1271. default:
  1272. return -EINVAL;
  1273. }
  1274. priv->wdev.iftype = type;
  1275. return set_infra_mode(usbdev, mode);
  1276. }
  1277. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1278. {
  1279. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1280. struct usbnet *usbdev = priv->usbdev;
  1281. int err;
  1282. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1283. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1284. if (err < 0)
  1285. return err;
  1286. }
  1287. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1288. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1289. if (err < 0)
  1290. return err;
  1291. }
  1292. return 0;
  1293. }
  1294. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  1295. int dbm)
  1296. {
  1297. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1298. struct usbnet *usbdev = priv->usbdev;
  1299. devdbg(usbdev, "rndis_set_tx_power type:0x%x dbm:%i", type, dbm);
  1300. /* Device doesn't support changing txpower after initialization, only
  1301. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1302. * currently used.
  1303. */
  1304. if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
  1305. if (!priv->radio_on)
  1306. disassociate(usbdev, 1); /* turn on radio */
  1307. return 0;
  1308. }
  1309. return -ENOTSUPP;
  1310. }
  1311. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
  1312. {
  1313. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1314. struct usbnet *usbdev = priv->usbdev;
  1315. *dbm = get_bcm4320_power_dbm(priv);
  1316. devdbg(usbdev, "rndis_get_tx_power dbm:%i", *dbm);
  1317. return 0;
  1318. }
  1319. #define SCAN_DELAY_JIFFIES (6 * HZ)
  1320. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  1321. struct cfg80211_scan_request *request)
  1322. {
  1323. struct usbnet *usbdev = netdev_priv(dev);
  1324. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1325. int ret;
  1326. __le32 tmp;
  1327. devdbg(usbdev, "cfg80211.scan");
  1328. /* Get current bssid list from device before new scan, as new scan
  1329. * clears internal bssid list.
  1330. */
  1331. rndis_check_bssid_list(usbdev);
  1332. if (!request)
  1333. return -EINVAL;
  1334. if (priv->scan_request && priv->scan_request != request)
  1335. return -EBUSY;
  1336. priv->scan_request = request;
  1337. tmp = cpu_to_le32(1);
  1338. ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  1339. sizeof(tmp));
  1340. if (ret == 0) {
  1341. /* Wait before retrieving scan results from device */
  1342. queue_delayed_work(priv->workqueue, &priv->scan_work,
  1343. SCAN_DELAY_JIFFIES);
  1344. }
  1345. return ret;
  1346. }
  1347. static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
  1348. struct ndis_80211_bssid_ex *bssid)
  1349. {
  1350. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1351. struct ieee80211_channel *channel;
  1352. s32 signal;
  1353. u64 timestamp;
  1354. u16 capability;
  1355. u16 beacon_interval;
  1356. struct ndis_80211_fixed_ies *fixed;
  1357. int ie_len, bssid_len;
  1358. u8 *ie;
  1359. devdbg(usbdev, " found bssid: '%.32s' [%pM]", bssid->ssid.essid,
  1360. bssid->mac);
  1361. /* parse bssid structure */
  1362. bssid_len = le32_to_cpu(bssid->length);
  1363. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1364. sizeof(struct ndis_80211_fixed_ies))
  1365. return NULL;
  1366. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1367. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1368. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1369. (int)le32_to_cpu(bssid->ie_length));
  1370. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1371. if (ie_len < 0)
  1372. return NULL;
  1373. /* extract data for cfg80211_inform_bss */
  1374. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1375. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1376. if (!channel)
  1377. return NULL;
  1378. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1379. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1380. capability = le16_to_cpu(fixed->capabilities);
  1381. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1382. return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1383. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1384. GFP_KERNEL);
  1385. }
  1386. static int rndis_check_bssid_list(struct usbnet *usbdev)
  1387. {
  1388. void *buf = NULL;
  1389. struct ndis_80211_bssid_list_ex *bssid_list;
  1390. struct ndis_80211_bssid_ex *bssid;
  1391. int ret = -EINVAL, len, count, bssid_len;
  1392. bool resized = false;
  1393. devdbg(usbdev, "check_bssid_list");
  1394. len = CONTROL_BUFFER_SIZE;
  1395. resize_buf:
  1396. buf = kmalloc(len, GFP_KERNEL);
  1397. if (!buf) {
  1398. ret = -ENOMEM;
  1399. goto out;
  1400. }
  1401. ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  1402. if (ret != 0)
  1403. goto out;
  1404. if (!resized && len > CONTROL_BUFFER_SIZE) {
  1405. resized = true;
  1406. kfree(buf);
  1407. goto resize_buf;
  1408. }
  1409. bssid_list = buf;
  1410. bssid = bssid_list->bssid;
  1411. bssid_len = le32_to_cpu(bssid->length);
  1412. count = le32_to_cpu(bssid_list->num_items);
  1413. devdbg(usbdev, "check_bssid_list: %d BSSIDs found (buflen: %d)", count,
  1414. len);
  1415. while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
  1416. rndis_bss_info_update(usbdev, bssid);
  1417. bssid = (void *)bssid + bssid_len;
  1418. bssid_len = le32_to_cpu(bssid->length);
  1419. count--;
  1420. }
  1421. out:
  1422. kfree(buf);
  1423. return ret;
  1424. }
  1425. static void rndis_get_scan_results(struct work_struct *work)
  1426. {
  1427. struct rndis_wlan_private *priv =
  1428. container_of(work, struct rndis_wlan_private, scan_work.work);
  1429. struct usbnet *usbdev = priv->usbdev;
  1430. int ret;
  1431. devdbg(usbdev, "get_scan_results");
  1432. if (!priv->scan_request)
  1433. return;
  1434. ret = rndis_check_bssid_list(usbdev);
  1435. cfg80211_scan_done(priv->scan_request, ret < 0);
  1436. priv->scan_request = NULL;
  1437. }
  1438. /*
  1439. * wireless extension handlers
  1440. */
  1441. static int rndis_iw_commit(struct net_device *dev,
  1442. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1443. {
  1444. /* dummy op */
  1445. return 0;
  1446. }
  1447. static int rndis_iw_set_essid(struct net_device *dev,
  1448. struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
  1449. {
  1450. struct ndis_80211_ssid ssid;
  1451. int length = wrqu->essid.length;
  1452. struct usbnet *usbdev = netdev_priv(dev);
  1453. devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
  1454. wrqu->essid.flags, wrqu->essid.length, essid);
  1455. if (length > NDIS_802_11_LENGTH_SSID)
  1456. length = NDIS_802_11_LENGTH_SSID;
  1457. ssid.length = cpu_to_le32(length);
  1458. if (length > 0)
  1459. memcpy(ssid.essid, essid, length);
  1460. else
  1461. memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
  1462. set_assoc_params(usbdev);
  1463. if (!wrqu->essid.flags || length == 0)
  1464. return disassociate(usbdev, 1);
  1465. else {
  1466. /* Pause and purge rx queue, so we don't pass packets before
  1467. * 'media connect'-indication.
  1468. */
  1469. usbnet_pause_rx(usbdev);
  1470. usbnet_purge_paused_rxq(usbdev);
  1471. return set_essid(usbdev, &ssid);
  1472. }
  1473. }
  1474. static int rndis_iw_get_essid(struct net_device *dev,
  1475. struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
  1476. {
  1477. struct ndis_80211_ssid ssid;
  1478. struct usbnet *usbdev = netdev_priv(dev);
  1479. int ret;
  1480. ret = get_essid(usbdev, &ssid);
  1481. if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
  1482. wrqu->essid.flags = 1;
  1483. wrqu->essid.length = le32_to_cpu(ssid.length);
  1484. memcpy(essid, ssid.essid, wrqu->essid.length);
  1485. essid[wrqu->essid.length] = 0;
  1486. } else {
  1487. memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
  1488. wrqu->essid.flags = 0;
  1489. wrqu->essid.length = 0;
  1490. }
  1491. devdbg(usbdev, "SIOCGIWESSID: %s", essid);
  1492. return ret;
  1493. }
  1494. static int rndis_iw_get_bssid(struct net_device *dev,
  1495. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1496. {
  1497. struct usbnet *usbdev = netdev_priv(dev);
  1498. unsigned char bssid[ETH_ALEN];
  1499. int ret;
  1500. ret = get_bssid(usbdev, bssid);
  1501. if (ret == 0)
  1502. devdbg(usbdev, "SIOCGIWAP: %pM", bssid);
  1503. else
  1504. devdbg(usbdev, "SIOCGIWAP: <not associated>");
  1505. wrqu->ap_addr.sa_family = ARPHRD_ETHER;
  1506. memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
  1507. return ret;
  1508. }
  1509. static int rndis_iw_set_bssid(struct net_device *dev,
  1510. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1511. {
  1512. struct usbnet *usbdev = netdev_priv(dev);
  1513. u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
  1514. int ret;
  1515. devdbg(usbdev, "SIOCSIWAP: %pM", bssid);
  1516. ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
  1517. /* user apps may set ap's mac address, which is not required;
  1518. * they may fail to work if this function fails, so return
  1519. * success */
  1520. if (ret)
  1521. devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
  1522. return 0;
  1523. }
  1524. static int rndis_iw_set_auth(struct net_device *dev,
  1525. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1526. {
  1527. struct iw_param *p = &wrqu->param;
  1528. struct usbnet *usbdev = netdev_priv(dev);
  1529. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1530. int ret = -ENOTSUPP;
  1531. switch (p->flags & IW_AUTH_INDEX) {
  1532. case IW_AUTH_WPA_VERSION:
  1533. devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
  1534. priv->wpa_version = p->value;
  1535. ret = 0;
  1536. break;
  1537. case IW_AUTH_CIPHER_PAIRWISE:
  1538. devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
  1539. priv->wpa_cipher_pair = p->value;
  1540. ret = 0;
  1541. break;
  1542. case IW_AUTH_CIPHER_GROUP:
  1543. devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
  1544. priv->wpa_cipher_group = p->value;
  1545. ret = 0;
  1546. break;
  1547. case IW_AUTH_KEY_MGMT:
  1548. devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
  1549. priv->wpa_keymgmt = p->value;
  1550. ret = 0;
  1551. break;
  1552. case IW_AUTH_TKIP_COUNTERMEASURES:
  1553. devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
  1554. p->value);
  1555. ret = 0;
  1556. break;
  1557. case IW_AUTH_DROP_UNENCRYPTED:
  1558. devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
  1559. ret = 0;
  1560. break;
  1561. case IW_AUTH_80211_AUTH_ALG:
  1562. devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
  1563. priv->wpa_authalg = p->value;
  1564. ret = 0;
  1565. break;
  1566. case IW_AUTH_WPA_ENABLED:
  1567. devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
  1568. if (wrqu->param.value)
  1569. deauthenticate(usbdev);
  1570. ret = 0;
  1571. break;
  1572. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  1573. devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
  1574. p->value);
  1575. ret = 0;
  1576. break;
  1577. case IW_AUTH_ROAMING_CONTROL:
  1578. devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
  1579. ret = 0;
  1580. break;
  1581. case IW_AUTH_PRIVACY_INVOKED:
  1582. devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
  1583. wrqu->param.flags & IW_AUTH_INDEX);
  1584. return -EOPNOTSUPP;
  1585. default:
  1586. devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
  1587. p->flags & IW_AUTH_INDEX, p->value);
  1588. }
  1589. return ret;
  1590. }
  1591. static int rndis_iw_get_auth(struct net_device *dev,
  1592. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1593. {
  1594. struct iw_param *p = &wrqu->param;
  1595. struct usbnet *usbdev = netdev_priv(dev);
  1596. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1597. switch (p->flags & IW_AUTH_INDEX) {
  1598. case IW_AUTH_WPA_VERSION:
  1599. p->value = priv->wpa_version;
  1600. break;
  1601. case IW_AUTH_CIPHER_PAIRWISE:
  1602. p->value = priv->wpa_cipher_pair;
  1603. break;
  1604. case IW_AUTH_CIPHER_GROUP:
  1605. p->value = priv->wpa_cipher_group;
  1606. break;
  1607. case IW_AUTH_KEY_MGMT:
  1608. p->value = priv->wpa_keymgmt;
  1609. break;
  1610. case IW_AUTH_80211_AUTH_ALG:
  1611. p->value = priv->wpa_authalg;
  1612. break;
  1613. default:
  1614. devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
  1615. wrqu->param.flags & IW_AUTH_INDEX);
  1616. return -EOPNOTSUPP;
  1617. }
  1618. return 0;
  1619. }
  1620. static int rndis_iw_set_encode(struct net_device *dev,
  1621. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1622. {
  1623. struct usbnet *usbdev = netdev_priv(dev);
  1624. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1625. struct rndis_wlan_encr_key key;
  1626. int ret, index, key_len;
  1627. u8 *keybuf;
  1628. index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
  1629. /* iwconfig gives index as 1 - N */
  1630. if (index > 0)
  1631. index--;
  1632. else
  1633. index = priv->encr_tx_key_index;
  1634. if (index < 0 || index >= 4) {
  1635. devwarn(usbdev, "encryption index out of range (%u)", index);
  1636. return -EINVAL;
  1637. }
  1638. /* remove key if disabled */
  1639. if (wrqu->data.flags & IW_ENCODE_DISABLED) {
  1640. if (remove_key(usbdev, index, NULL))
  1641. return -EINVAL;
  1642. else
  1643. return 0;
  1644. }
  1645. /* global encryption state (for all keys) */
  1646. if (wrqu->data.flags & IW_ENCODE_OPEN)
  1647. ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1648. IW_AUTH_ALG_OPEN_SYSTEM);
  1649. else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
  1650. ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1651. IW_AUTH_ALG_SHARED_KEY);
  1652. if (ret != 0)
  1653. return ret;
  1654. if (wrqu->data.length > 0) {
  1655. key_len = wrqu->data.length;
  1656. keybuf = extra;
  1657. } else {
  1658. /* must be set as tx key */
  1659. if (priv->encr_keys[index].len == 0)
  1660. return -EINVAL;
  1661. key = priv->encr_keys[index];
  1662. key_len = key.len;
  1663. keybuf = key.material;
  1664. priv->encr_tx_key_index = index;
  1665. }
  1666. if (add_wep_key(usbdev, keybuf, key_len, index) != 0)
  1667. return -EINVAL;
  1668. if (index == priv->encr_tx_key_index)
  1669. /* ndis drivers want essid to be set after setting encr */
  1670. set_essid(usbdev, &priv->essid);
  1671. return 0;
  1672. }
  1673. static int rndis_iw_set_encode_ext(struct net_device *dev,
  1674. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1675. {
  1676. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1677. struct usbnet *usbdev = netdev_priv(dev);
  1678. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1679. int keyidx, flags, cipher;
  1680. keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
  1681. /* iwconfig gives index as 1 - N */
  1682. if (keyidx)
  1683. keyidx--;
  1684. else
  1685. keyidx = priv->encr_tx_key_index;
  1686. if (keyidx < 0 || keyidx >= 4) {
  1687. devwarn(usbdev, "encryption index out of range (%u)", keyidx);
  1688. return -EINVAL;
  1689. }
  1690. if (ext->alg == WPA_ALG_WEP) {
  1691. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
  1692. priv->encr_tx_key_index = keyidx;
  1693. return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
  1694. }
  1695. cipher = -1;
  1696. if (ext->alg == IW_ENCODE_ALG_TKIP)
  1697. cipher = WLAN_CIPHER_SUITE_TKIP;
  1698. else if (ext->alg == IW_ENCODE_ALG_CCMP)
  1699. cipher = WLAN_CIPHER_SUITE_CCMP;
  1700. if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
  1701. ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
  1702. return remove_key(usbdev, keyidx, NULL);
  1703. if (cipher == -1)
  1704. return -EOPNOTSUPP;
  1705. flags = 0;
  1706. if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
  1707. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  1708. if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY))
  1709. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1710. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
  1711. flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;
  1712. return add_wpa_key(usbdev, ext->key, ext->key_len, keyidx,
  1713. (u8 *)&ext->addr.sa_data, ext->rx_seq, cipher,
  1714. flags);
  1715. }
  1716. static int rndis_iw_set_genie(struct net_device *dev,
  1717. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1718. {
  1719. struct usbnet *usbdev = netdev_priv(dev);
  1720. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1721. int ret = 0;
  1722. #ifdef DEBUG
  1723. int j;
  1724. u8 *gie = extra;
  1725. for (j = 0; j < wrqu->data.length; j += 8)
  1726. devdbg(usbdev,
  1727. "SIOCSIWGENIE %04x - "
  1728. "%02x %02x %02x %02x %02x %02x %02x %02x", j,
  1729. gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
  1730. gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
  1731. #endif
  1732. /* clear existing IEs */
  1733. if (priv->wpa_ie_len) {
  1734. kfree(priv->wpa_ie);
  1735. priv->wpa_ie_len = 0;
  1736. }
  1737. /* set new IEs */
  1738. priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
  1739. if (priv->wpa_ie) {
  1740. priv->wpa_ie_len = wrqu->data.length;
  1741. memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
  1742. } else
  1743. ret = -ENOMEM;
  1744. return ret;
  1745. }
  1746. static int rndis_iw_get_genie(struct net_device *dev,
  1747. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1748. {
  1749. struct usbnet *usbdev = netdev_priv(dev);
  1750. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1751. devdbg(usbdev, "SIOCGIWGENIE");
  1752. if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
  1753. wrqu->data.length = 0;
  1754. return 0;
  1755. }
  1756. if (wrqu->data.length < priv->wpa_ie_len)
  1757. return -E2BIG;
  1758. wrqu->data.length = priv->wpa_ie_len;
  1759. memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
  1760. return 0;
  1761. }
  1762. static int rndis_iw_set_freq(struct net_device *dev,
  1763. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1764. {
  1765. struct usbnet *usbdev = netdev_priv(dev);
  1766. struct ndis_80211_conf config;
  1767. unsigned int dsconfig;
  1768. int len, ret;
  1769. /* this OID is valid only when not associated */
  1770. if (is_associated(usbdev))
  1771. return 0;
  1772. dsconfig = 0;
  1773. if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
  1774. return -EINVAL;
  1775. len = sizeof(config);
  1776. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1777. if (ret != 0) {
  1778. devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
  1779. return 0;
  1780. }
  1781. config.ds_config = cpu_to_le32(dsconfig);
  1782. devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
  1783. return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
  1784. sizeof(config));
  1785. }
  1786. static int rndis_iw_get_freq(struct net_device *dev,
  1787. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1788. {
  1789. struct usbnet *usbdev = netdev_priv(dev);
  1790. struct ndis_80211_conf config;
  1791. int len, ret;
  1792. len = sizeof(config);
  1793. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1794. if (ret == 0)
  1795. dsconfig_to_freq(le32_to_cpu(config.ds_config), &wrqu->freq);
  1796. devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
  1797. return ret;
  1798. }
  1799. static int rndis_iw_get_rate(struct net_device *dev,
  1800. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1801. {
  1802. struct usbnet *usbdev = netdev_priv(dev);
  1803. __le32 tmp;
  1804. int ret, len;
  1805. len = sizeof(tmp);
  1806. ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
  1807. if (ret == 0) {
  1808. wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
  1809. wrqu->bitrate.disabled = 0;
  1810. wrqu->bitrate.flags = 1;
  1811. }
  1812. return ret;
  1813. }
  1814. static int rndis_iw_set_mlme(struct net_device *dev,
  1815. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1816. {
  1817. struct usbnet *usbdev = netdev_priv(dev);
  1818. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1819. struct iw_mlme *mlme = (struct iw_mlme *)extra;
  1820. unsigned char bssid[ETH_ALEN];
  1821. get_bssid(usbdev, bssid);
  1822. if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
  1823. return -EINVAL;
  1824. switch (mlme->cmd) {
  1825. case IW_MLME_DEAUTH:
  1826. return deauthenticate(usbdev);
  1827. case IW_MLME_DISASSOC:
  1828. return disassociate(usbdev, priv->radio_on);
  1829. default:
  1830. return -EOPNOTSUPP;
  1831. }
  1832. return 0;
  1833. }
  1834. static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
  1835. {
  1836. struct usbnet *usbdev = netdev_priv(dev);
  1837. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1838. unsigned long flags;
  1839. spin_lock_irqsave(&priv->stats_lock, flags);
  1840. memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
  1841. spin_unlock_irqrestore(&priv->stats_lock, flags);
  1842. return &priv->iwstats;
  1843. }
  1844. #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
  1845. static const iw_handler rndis_iw_handler[] =
  1846. {
  1847. IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
  1848. IW_IOCTL(SIOCGIWNAME) = (iw_handler) cfg80211_wext_giwname,
  1849. IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
  1850. IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
  1851. IW_IOCTL(SIOCSIWMODE) = (iw_handler) cfg80211_wext_siwmode,
  1852. IW_IOCTL(SIOCGIWMODE) = (iw_handler) cfg80211_wext_giwmode,
  1853. IW_IOCTL(SIOCGIWRANGE) = (iw_handler) cfg80211_wext_giwrange,
  1854. IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
  1855. IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
  1856. IW_IOCTL(SIOCSIWSCAN) = (iw_handler) cfg80211_wext_siwscan,
  1857. IW_IOCTL(SIOCGIWSCAN) = (iw_handler) cfg80211_wext_giwscan,
  1858. IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
  1859. IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
  1860. IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
  1861. IW_IOCTL(SIOCSIWRTS) = (iw_handler) cfg80211_wext_siwrts,
  1862. IW_IOCTL(SIOCGIWRTS) = (iw_handler) cfg80211_wext_giwrts,
  1863. IW_IOCTL(SIOCSIWFRAG) = (iw_handler) cfg80211_wext_siwfrag,
  1864. IW_IOCTL(SIOCGIWFRAG) = (iw_handler) cfg80211_wext_giwfrag,
  1865. IW_IOCTL(SIOCSIWTXPOW) = (iw_handler) cfg80211_wext_siwtxpower,
  1866. IW_IOCTL(SIOCGIWTXPOW) = (iw_handler) cfg80211_wext_giwtxpower,
  1867. IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
  1868. IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
  1869. IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
  1870. IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
  1871. IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
  1872. IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
  1873. IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
  1874. };
  1875. static const iw_handler rndis_wlan_private_handler[] = {
  1876. };
  1877. static const struct iw_priv_args rndis_wlan_private_args[] = {
  1878. };
  1879. static const struct iw_handler_def rndis_iw_handlers = {
  1880. .num_standard = ARRAY_SIZE(rndis_iw_handler),
  1881. .num_private = ARRAY_SIZE(rndis_wlan_private_handler),
  1882. .num_private_args = ARRAY_SIZE(rndis_wlan_private_args),
  1883. .standard = (iw_handler *)rndis_iw_handler,
  1884. .private = (iw_handler *)rndis_wlan_private_handler,
  1885. .private_args = (struct iw_priv_args *)rndis_wlan_private_args,
  1886. .get_wireless_stats = rndis_get_wireless_stats,
  1887. };
  1888. static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
  1889. {
  1890. struct ndis_80211_assoc_info *info;
  1891. union iwreq_data evt;
  1892. u8 assoc_buf[sizeof(*info) + IW_CUSTOM_MAX + 32];
  1893. u8 bssid[ETH_ALEN];
  1894. int ret, offset;
  1895. memset(assoc_buf, 0, sizeof(assoc_buf));
  1896. info = (void *)assoc_buf;
  1897. netif_carrier_on(usbdev->net);
  1898. /* Get association info IEs from device and send them back to
  1899. * userspace. */
  1900. ret = get_association_info(usbdev, info, sizeof(assoc_buf));
  1901. if (!ret) {
  1902. evt.data.length = le32_to_cpu(info->req_ie_length);
  1903. if (evt.data.length > 0) {
  1904. offset = le32_to_cpu(info->offset_req_ies);
  1905. wireless_send_event(usbdev->net,
  1906. IWEVASSOCREQIE, &evt,
  1907. (char *)info + offset);
  1908. }
  1909. evt.data.length = le32_to_cpu(info->resp_ie_length);
  1910. if (evt.data.length > 0) {
  1911. offset = le32_to_cpu(info->offset_resp_ies);
  1912. wireless_send_event(usbdev->net,
  1913. IWEVASSOCRESPIE, &evt,
  1914. (char *)info + offset);
  1915. }
  1916. usbnet_resume_rx(usbdev);
  1917. }
  1918. ret = get_bssid(usbdev, bssid);
  1919. if (!ret) {
  1920. evt.data.flags = 0;
  1921. evt.data.length = 0;
  1922. memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
  1923. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1924. }
  1925. usbnet_resume_rx(usbdev);
  1926. }
  1927. static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
  1928. {
  1929. union iwreq_data evt;
  1930. netif_carrier_off(usbdev->net);
  1931. evt.data.flags = 0;
  1932. evt.data.length = 0;
  1933. memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
  1934. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1935. }
  1936. static void rndis_wlan_worker(struct work_struct *work)
  1937. {
  1938. struct rndis_wlan_private *priv =
  1939. container_of(work, struct rndis_wlan_private, work);
  1940. struct usbnet *usbdev = priv->usbdev;
  1941. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
  1942. rndis_wlan_do_link_up_work(usbdev);
  1943. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
  1944. rndis_wlan_do_link_down_work(usbdev);
  1945. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1946. set_multicast_list(usbdev);
  1947. }
  1948. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  1949. {
  1950. struct usbnet *usbdev = netdev_priv(dev);
  1951. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1952. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1953. return;
  1954. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  1955. queue_work(priv->workqueue, &priv->work);
  1956. }
  1957. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  1958. struct ndis_80211_status_indication *indication,
  1959. int len)
  1960. {
  1961. u8 *buf;
  1962. const char *type;
  1963. int flags, buflen;
  1964. bool pairwise_error, group_error;
  1965. struct ndis_80211_auth_request *auth_req;
  1966. /* must have at least one array entry */
  1967. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1968. sizeof(struct ndis_80211_auth_request)) {
  1969. devinfo(usbdev, "authentication indication: "
  1970. "too short message (%i)", len);
  1971. return;
  1972. }
  1973. buf = (void *)&indication->u.auth_request[0];
  1974. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  1975. while (buflen >= sizeof(*auth_req)) {
  1976. auth_req = (void *)buf;
  1977. type = "unknown";
  1978. flags = le32_to_cpu(auth_req->flags);
  1979. pairwise_error = false;
  1980. group_error = false;
  1981. if (flags & 0x1)
  1982. type = "reauth request";
  1983. if (flags & 0x2)
  1984. type = "key update request";
  1985. if (flags & 0x6) {
  1986. pairwise_error = true;
  1987. type = "pairwise_error";
  1988. }
  1989. if (flags & 0xe) {
  1990. group_error = true;
  1991. type = "group_error";
  1992. }
  1993. devinfo(usbdev, "authentication indication: %s (0x%08x)", type,
  1994. le32_to_cpu(auth_req->flags));
  1995. if (pairwise_error || group_error) {
  1996. union iwreq_data wrqu;
  1997. struct iw_michaelmicfailure micfailure;
  1998. memset(&micfailure, 0, sizeof(micfailure));
  1999. if (pairwise_error)
  2000. micfailure.flags |= IW_MICFAILURE_PAIRWISE;
  2001. if (group_error)
  2002. micfailure.flags |= IW_MICFAILURE_GROUP;
  2003. memcpy(micfailure.src_addr.sa_data, auth_req->bssid,
  2004. ETH_ALEN);
  2005. memset(&wrqu, 0, sizeof(wrqu));
  2006. wrqu.data.length = sizeof(micfailure);
  2007. wireless_send_event(usbdev->net, IWEVMICHAELMICFAILURE,
  2008. &wrqu, (u8 *)&micfailure);
  2009. }
  2010. buflen -= le32_to_cpu(auth_req->length);
  2011. buf += le32_to_cpu(auth_req->length);
  2012. }
  2013. }
  2014. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  2015. struct ndis_80211_status_indication *indication,
  2016. int len)
  2017. {
  2018. struct ndis_80211_pmkid_cand_list *cand_list;
  2019. int list_len, expected_len, i;
  2020. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2021. sizeof(struct ndis_80211_pmkid_cand_list)) {
  2022. devinfo(usbdev, "pmkid candidate list indication: "
  2023. "too short message (%i)", len);
  2024. return;
  2025. }
  2026. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  2027. sizeof(struct ndis_80211_pmkid_candidate);
  2028. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  2029. offsetof(struct ndis_80211_status_indication, u);
  2030. if (len < expected_len) {
  2031. devinfo(usbdev, "pmkid candidate list indication: "
  2032. "list larger than buffer (%i < %i)",
  2033. len, expected_len);
  2034. return;
  2035. }
  2036. cand_list = &indication->u.cand_list;
  2037. devinfo(usbdev, "pmkid candidate list indication: "
  2038. "version %i, candidates %i",
  2039. le32_to_cpu(cand_list->version),
  2040. le32_to_cpu(cand_list->num_candidates));
  2041. if (le32_to_cpu(cand_list->version) != 1)
  2042. return;
  2043. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  2044. struct iw_pmkid_cand pcand;
  2045. union iwreq_data wrqu;
  2046. struct ndis_80211_pmkid_candidate *cand =
  2047. &cand_list->candidate_list[i];
  2048. devdbg(usbdev, "cand[%i]: flags: 0x%08x, bssid: %pM",
  2049. i, le32_to_cpu(cand->flags), cand->bssid);
  2050. memset(&pcand, 0, sizeof(pcand));
  2051. if (le32_to_cpu(cand->flags) & 0x01)
  2052. pcand.flags |= IW_PMKID_CAND_PREAUTH;
  2053. pcand.index = i;
  2054. memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
  2055. memset(&wrqu, 0, sizeof(wrqu));
  2056. wrqu.data.length = sizeof(pcand);
  2057. wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
  2058. (u8 *)&pcand);
  2059. }
  2060. }
  2061. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  2062. struct rndis_indicate *msg, int buflen)
  2063. {
  2064. struct ndis_80211_status_indication *indication;
  2065. int len, offset;
  2066. offset = offsetof(struct rndis_indicate, status) +
  2067. le32_to_cpu(msg->offset);
  2068. len = le32_to_cpu(msg->length);
  2069. if (len < 8) {
  2070. devinfo(usbdev, "media specific indication, "
  2071. "ignore too short message (%i < 8)", len);
  2072. return;
  2073. }
  2074. if (offset + len > buflen) {
  2075. devinfo(usbdev, "media specific indication, "
  2076. "too large to fit to buffer (%i > %i)",
  2077. offset + len, buflen);
  2078. return;
  2079. }
  2080. indication = (void *)((u8 *)msg + offset);
  2081. switch (le32_to_cpu(indication->status_type)) {
  2082. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  2083. devinfo(usbdev, "radio state indication: %i",
  2084. le32_to_cpu(indication->u.radio_status));
  2085. return;
  2086. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  2087. devinfo(usbdev, "media stream mode indication: %i",
  2088. le32_to_cpu(indication->u.media_stream_mode));
  2089. return;
  2090. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  2091. rndis_wlan_auth_indication(usbdev, indication, len);
  2092. return;
  2093. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2094. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2095. return;
  2096. default:
  2097. devinfo(usbdev, "media specific indication: "
  2098. "unknown status type 0x%08x",
  2099. le32_to_cpu(indication->status_type));
  2100. }
  2101. }
  2102. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2103. {
  2104. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2105. struct rndis_indicate *msg = ind;
  2106. switch (msg->status) {
  2107. case RNDIS_STATUS_MEDIA_CONNECT:
  2108. if (priv->current_command_oid == OID_802_11_ADD_KEY) {
  2109. /* OID_802_11_ADD_KEY causes sometimes extra
  2110. * "media connect" indications which confuses driver
  2111. * and userspace to think that device is
  2112. * roaming/reassociating when it isn't.
  2113. */
  2114. devdbg(usbdev, "ignored OID_802_11_ADD_KEY triggered "
  2115. "'media connect'");
  2116. return;
  2117. }
  2118. usbnet_pause_rx(usbdev);
  2119. devinfo(usbdev, "media connect");
  2120. /* queue work to avoid recursive calls into rndis_command */
  2121. set_bit(WORK_LINK_UP, &priv->work_pending);
  2122. queue_work(priv->workqueue, &priv->work);
  2123. break;
  2124. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2125. devinfo(usbdev, "media disconnect");
  2126. /* queue work to avoid recursive calls into rndis_command */
  2127. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2128. queue_work(priv->workqueue, &priv->work);
  2129. break;
  2130. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2131. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2132. break;
  2133. default:
  2134. devinfo(usbdev, "indication: 0x%08x",
  2135. le32_to_cpu(msg->status));
  2136. break;
  2137. }
  2138. }
  2139. static int rndis_wlan_get_caps(struct usbnet *usbdev)
  2140. {
  2141. struct {
  2142. __le32 num_items;
  2143. __le32 items[8];
  2144. } networks_supported;
  2145. int len, retval, i, n;
  2146. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2147. /* determine supported modes */
  2148. len = sizeof(networks_supported);
  2149. retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
  2150. &networks_supported, &len);
  2151. if (retval >= 0) {
  2152. n = le32_to_cpu(networks_supported.num_items);
  2153. if (n > 8)
  2154. n = 8;
  2155. for (i = 0; i < n; i++) {
  2156. switch (le32_to_cpu(networks_supported.items[i])) {
  2157. case NDIS_80211_TYPE_FREQ_HOP:
  2158. case NDIS_80211_TYPE_DIRECT_SEQ:
  2159. priv->caps |= CAP_MODE_80211B;
  2160. break;
  2161. case NDIS_80211_TYPE_OFDM_A:
  2162. priv->caps |= CAP_MODE_80211A;
  2163. break;
  2164. case NDIS_80211_TYPE_OFDM_G:
  2165. priv->caps |= CAP_MODE_80211G;
  2166. break;
  2167. }
  2168. }
  2169. }
  2170. return retval;
  2171. }
  2172. #define STATS_UPDATE_JIFFIES (HZ)
  2173. static void rndis_update_wireless_stats(struct work_struct *work)
  2174. {
  2175. struct rndis_wlan_private *priv =
  2176. container_of(work, struct rndis_wlan_private, stats_work.work);
  2177. struct usbnet *usbdev = priv->usbdev;
  2178. struct iw_statistics iwstats;
  2179. __le32 rssi, tmp;
  2180. int len, ret, j;
  2181. unsigned long flags;
  2182. int update_jiffies = STATS_UPDATE_JIFFIES;
  2183. void *buf;
  2184. spin_lock_irqsave(&priv->stats_lock, flags);
  2185. memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
  2186. spin_unlock_irqrestore(&priv->stats_lock, flags);
  2187. /* only update stats when connected */
  2188. if (!is_associated(usbdev)) {
  2189. iwstats.qual.qual = 0;
  2190. iwstats.qual.level = 0;
  2191. iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2192. | IW_QUAL_LEVEL_UPDATED
  2193. | IW_QUAL_NOISE_INVALID
  2194. | IW_QUAL_QUAL_INVALID
  2195. | IW_QUAL_LEVEL_INVALID;
  2196. goto end;
  2197. }
  2198. len = sizeof(rssi);
  2199. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  2200. devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
  2201. le32_to_cpu(rssi));
  2202. if (ret == 0) {
  2203. memset(&iwstats.qual, 0, sizeof(iwstats.qual));
  2204. iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
  2205. iwstats.qual.level = level_to_qual(le32_to_cpu(rssi));
  2206. iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2207. | IW_QUAL_LEVEL_UPDATED
  2208. | IW_QUAL_NOISE_INVALID;
  2209. }
  2210. memset(&iwstats.discard, 0, sizeof(iwstats.discard));
  2211. len = sizeof(tmp);
  2212. ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
  2213. if (ret == 0)
  2214. iwstats.discard.misc += le32_to_cpu(tmp);
  2215. len = sizeof(tmp);
  2216. ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
  2217. if (ret == 0)
  2218. iwstats.discard.misc += le32_to_cpu(tmp);
  2219. len = sizeof(tmp);
  2220. ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
  2221. if (ret == 0)
  2222. iwstats.discard.misc += le32_to_cpu(tmp);
  2223. /* Workaround transfer stalls on poor quality links.
  2224. * TODO: find right way to fix these stalls (as stalls do not happen
  2225. * with ndiswrapper/windows driver). */
  2226. if (iwstats.qual.qual <= 25) {
  2227. /* Decrease stats worker interval to catch stalls.
  2228. * faster. Faster than 400-500ms causes packet loss,
  2229. * Slower doesn't catch stalls fast enough.
  2230. */
  2231. j = msecs_to_jiffies(priv->param_workaround_interval);
  2232. if (j > STATS_UPDATE_JIFFIES)
  2233. j = STATS_UPDATE_JIFFIES;
  2234. else if (j <= 0)
  2235. j = 1;
  2236. update_jiffies = j;
  2237. /* Send scan OID. Use of both OIDs is required to get device
  2238. * working.
  2239. */
  2240. tmp = cpu_to_le32(1);
  2241. rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  2242. sizeof(tmp));
  2243. len = CONTROL_BUFFER_SIZE;
  2244. buf = kmalloc(len, GFP_KERNEL);
  2245. if (!buf)
  2246. goto end;
  2247. rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  2248. kfree(buf);
  2249. }
  2250. end:
  2251. spin_lock_irqsave(&priv->stats_lock, flags);
  2252. memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
  2253. spin_unlock_irqrestore(&priv->stats_lock, flags);
  2254. if (update_jiffies >= HZ)
  2255. update_jiffies = round_jiffies_relative(update_jiffies);
  2256. else {
  2257. j = round_jiffies_relative(update_jiffies);
  2258. if (abs(j - update_jiffies) <= 10)
  2259. update_jiffies = j;
  2260. }
  2261. queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
  2262. }
  2263. static int bcm4320a_early_init(struct usbnet *usbdev)
  2264. {
  2265. /* bcm4320a doesn't handle configuration parameters well. Try
  2266. * set any and you get partially zeroed mac and broken device.
  2267. */
  2268. return 0;
  2269. }
  2270. static int bcm4320b_early_init(struct usbnet *usbdev)
  2271. {
  2272. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2273. char buf[8];
  2274. /* Early initialization settings, setting these won't have effect
  2275. * if called after generic_rndis_bind().
  2276. */
  2277. priv->param_country[0] = modparam_country[0];
  2278. priv->param_country[1] = modparam_country[1];
  2279. priv->param_country[2] = 0;
  2280. priv->param_frameburst = modparam_frameburst;
  2281. priv->param_afterburner = modparam_afterburner;
  2282. priv->param_power_save = modparam_power_save;
  2283. priv->param_power_output = modparam_power_output;
  2284. priv->param_roamtrigger = modparam_roamtrigger;
  2285. priv->param_roamdelta = modparam_roamdelta;
  2286. priv->param_country[0] = toupper(priv->param_country[0]);
  2287. priv->param_country[1] = toupper(priv->param_country[1]);
  2288. /* doesn't support EU as country code, use FI instead */
  2289. if (!strcmp(priv->param_country, "EU"))
  2290. strcpy(priv->param_country, "FI");
  2291. if (priv->param_power_save < 0)
  2292. priv->param_power_save = 0;
  2293. else if (priv->param_power_save > 2)
  2294. priv->param_power_save = 2;
  2295. if (priv->param_power_output < 0)
  2296. priv->param_power_output = 0;
  2297. else if (priv->param_power_output > 3)
  2298. priv->param_power_output = 3;
  2299. if (priv->param_roamtrigger < -80)
  2300. priv->param_roamtrigger = -80;
  2301. else if (priv->param_roamtrigger > -60)
  2302. priv->param_roamtrigger = -60;
  2303. if (priv->param_roamdelta < 0)
  2304. priv->param_roamdelta = 0;
  2305. else if (priv->param_roamdelta > 2)
  2306. priv->param_roamdelta = 2;
  2307. if (modparam_workaround_interval < 0)
  2308. priv->param_workaround_interval = 500;
  2309. else
  2310. priv->param_workaround_interval = modparam_workaround_interval;
  2311. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2312. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2313. priv->param_frameburst ? "1" : "0");
  2314. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2315. priv->param_afterburner ? "1" : "0");
  2316. sprintf(buf, "%d", priv->param_power_save);
  2317. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2318. sprintf(buf, "%d", priv->param_power_output);
  2319. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2320. sprintf(buf, "%d", priv->param_roamtrigger);
  2321. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2322. sprintf(buf, "%d", priv->param_roamdelta);
  2323. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2324. return 0;
  2325. }
  2326. /* same as rndis_netdev_ops but with local multicast handler */
  2327. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2328. .ndo_open = usbnet_open,
  2329. .ndo_stop = usbnet_stop,
  2330. .ndo_start_xmit = usbnet_start_xmit,
  2331. .ndo_tx_timeout = usbnet_tx_timeout,
  2332. .ndo_set_mac_address = eth_mac_addr,
  2333. .ndo_validate_addr = eth_validate_addr,
  2334. .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
  2335. };
  2336. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2337. {
  2338. struct wiphy *wiphy;
  2339. struct rndis_wlan_private *priv;
  2340. int retval, len;
  2341. __le32 tmp;
  2342. /* allocate wiphy and rndis private data
  2343. * NOTE: We only support a single virtual interface, so wiphy
  2344. * and wireless_dev are somewhat synonymous for this device.
  2345. */
  2346. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2347. if (!wiphy)
  2348. return -ENOMEM;
  2349. priv = wiphy_priv(wiphy);
  2350. usbdev->net->ieee80211_ptr = &priv->wdev;
  2351. priv->wdev.wiphy = wiphy;
  2352. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2353. /* These have to be initialized before calling generic_rndis_bind().
  2354. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2355. */
  2356. usbdev->driver_priv = priv;
  2357. usbdev->net->wireless_handlers = &rndis_iw_handlers;
  2358. priv->usbdev = usbdev;
  2359. mutex_init(&priv->command_lock);
  2360. spin_lock_init(&priv->stats_lock);
  2361. /* because rndis_command() sleeps we need to use workqueue */
  2362. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2363. INIT_WORK(&priv->work, rndis_wlan_worker);
  2364. INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
  2365. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2366. /* try bind rndis_host */
  2367. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2368. if (retval < 0)
  2369. goto fail;
  2370. /* generic_rndis_bind set packet filter to multicast_all+
  2371. * promisc mode which doesn't work well for our devices (device
  2372. * picks up rssi to closest station instead of to access point).
  2373. *
  2374. * rndis_host wants to avoid all OID as much as possible
  2375. * so do promisc/multicast handling in rndis_wlan.
  2376. */
  2377. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2378. tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  2379. retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
  2380. sizeof(tmp));
  2381. len = sizeof(tmp);
  2382. retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
  2383. &len);
  2384. priv->multicast_size = le32_to_cpu(tmp);
  2385. if (retval < 0 || priv->multicast_size < 0)
  2386. priv->multicast_size = 0;
  2387. if (priv->multicast_size > 0)
  2388. usbdev->net->flags |= IFF_MULTICAST;
  2389. else
  2390. usbdev->net->flags &= ~IFF_MULTICAST;
  2391. priv->iwstats.qual.qual = 0;
  2392. priv->iwstats.qual.level = 0;
  2393. priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2394. | IW_QUAL_LEVEL_UPDATED
  2395. | IW_QUAL_NOISE_INVALID
  2396. | IW_QUAL_QUAL_INVALID
  2397. | IW_QUAL_LEVEL_INVALID;
  2398. /* fill-out wiphy structure and register w/ cfg80211 */
  2399. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2400. wiphy->privid = rndis_wiphy_privid;
  2401. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2402. | BIT(NL80211_IFTYPE_ADHOC);
  2403. wiphy->max_scan_ssids = 1;
  2404. /* TODO: fill-out band/encr information based on priv->caps */
  2405. rndis_wlan_get_caps(usbdev);
  2406. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2407. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2408. priv->band.channels = priv->channels;
  2409. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2410. priv->band.bitrates = priv->rates;
  2411. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2412. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2413. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2414. memcpy(priv->cipher_suites, rndis_cipher_suites,
  2415. sizeof(rndis_cipher_suites));
  2416. wiphy->cipher_suites = priv->cipher_suites;
  2417. wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
  2418. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2419. if (wiphy_register(wiphy)) {
  2420. retval = -ENODEV;
  2421. goto fail;
  2422. }
  2423. set_default_iw_params(usbdev);
  2424. /* set default rts/frag */
  2425. rndis_set_wiphy_params(wiphy,
  2426. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2427. /* turn radio on */
  2428. priv->radio_on = 1;
  2429. disassociate(usbdev, 1);
  2430. netif_carrier_off(usbdev->net);
  2431. return 0;
  2432. fail:
  2433. cancel_delayed_work_sync(&priv->stats_work);
  2434. cancel_delayed_work_sync(&priv->scan_work);
  2435. cancel_work_sync(&priv->work);
  2436. flush_workqueue(priv->workqueue);
  2437. destroy_workqueue(priv->workqueue);
  2438. wiphy_free(wiphy);
  2439. return retval;
  2440. }
  2441. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2442. {
  2443. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2444. /* turn radio off */
  2445. disassociate(usbdev, 0);
  2446. cancel_delayed_work_sync(&priv->stats_work);
  2447. cancel_delayed_work_sync(&priv->scan_work);
  2448. cancel_work_sync(&priv->work);
  2449. flush_workqueue(priv->workqueue);
  2450. destroy_workqueue(priv->workqueue);
  2451. if (priv && priv->wpa_ie_len)
  2452. kfree(priv->wpa_ie);
  2453. rndis_unbind(usbdev, intf);
  2454. wiphy_unregister(priv->wdev.wiphy);
  2455. wiphy_free(priv->wdev.wiphy);
  2456. }
  2457. static int rndis_wlan_reset(struct usbnet *usbdev)
  2458. {
  2459. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2460. int retval;
  2461. devdbg(usbdev, "rndis_wlan_reset");
  2462. retval = rndis_reset(usbdev);
  2463. if (retval)
  2464. devwarn(usbdev, "rndis_reset() failed: %d", retval);
  2465. /* rndis_reset cleared multicast list, so restore here.
  2466. (set_multicast_list() also turns on current packet filter) */
  2467. set_multicast_list(usbdev);
  2468. queue_delayed_work(priv->workqueue, &priv->stats_work,
  2469. round_jiffies_relative(STATS_UPDATE_JIFFIES));
  2470. return deauthenticate(usbdev);
  2471. }
  2472. static int rndis_wlan_stop(struct usbnet *usbdev)
  2473. {
  2474. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2475. int retval;
  2476. __le32 filter;
  2477. devdbg(usbdev, "rndis_wlan_stop");
  2478. retval = disassociate(usbdev, 0);
  2479. priv->work_pending = 0;
  2480. cancel_delayed_work_sync(&priv->stats_work);
  2481. cancel_delayed_work_sync(&priv->scan_work);
  2482. cancel_work_sync(&priv->work);
  2483. flush_workqueue(priv->workqueue);
  2484. if (priv->scan_request) {
  2485. cfg80211_scan_done(priv->scan_request, true);
  2486. priv->scan_request = NULL;
  2487. }
  2488. /* Set current packet filter zero to block receiving data packets from
  2489. device. */
  2490. filter = 0;
  2491. rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2492. sizeof(filter));
  2493. return retval;
  2494. }
  2495. static const struct driver_info bcm4320b_info = {
  2496. .description = "Wireless RNDIS device, BCM4320b based",
  2497. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2498. FLAG_AVOID_UNLINK_URBS,
  2499. .bind = rndis_wlan_bind,
  2500. .unbind = rndis_wlan_unbind,
  2501. .status = rndis_status,
  2502. .rx_fixup = rndis_rx_fixup,
  2503. .tx_fixup = rndis_tx_fixup,
  2504. .reset = rndis_wlan_reset,
  2505. .stop = rndis_wlan_stop,
  2506. .early_init = bcm4320b_early_init,
  2507. .indication = rndis_wlan_indication,
  2508. };
  2509. static const struct driver_info bcm4320a_info = {
  2510. .description = "Wireless RNDIS device, BCM4320a based",
  2511. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2512. FLAG_AVOID_UNLINK_URBS,
  2513. .bind = rndis_wlan_bind,
  2514. .unbind = rndis_wlan_unbind,
  2515. .status = rndis_status,
  2516. .rx_fixup = rndis_rx_fixup,
  2517. .tx_fixup = rndis_tx_fixup,
  2518. .reset = rndis_wlan_reset,
  2519. .stop = rndis_wlan_stop,
  2520. .early_init = bcm4320a_early_init,
  2521. .indication = rndis_wlan_indication,
  2522. };
  2523. static const struct driver_info rndis_wlan_info = {
  2524. .description = "Wireless RNDIS device",
  2525. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2526. FLAG_AVOID_UNLINK_URBS,
  2527. .bind = rndis_wlan_bind,
  2528. .unbind = rndis_wlan_unbind,
  2529. .status = rndis_status,
  2530. .rx_fixup = rndis_rx_fixup,
  2531. .tx_fixup = rndis_tx_fixup,
  2532. .reset = rndis_wlan_reset,
  2533. .stop = rndis_wlan_stop,
  2534. .early_init = bcm4320a_early_init,
  2535. .indication = rndis_wlan_indication,
  2536. };
  2537. /*-------------------------------------------------------------------------*/
  2538. static const struct usb_device_id products [] = {
  2539. #define RNDIS_MASTER_INTERFACE \
  2540. .bInterfaceClass = USB_CLASS_COMM, \
  2541. .bInterfaceSubClass = 2 /* ACM */, \
  2542. .bInterfaceProtocol = 0x0ff
  2543. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2544. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  2545. */
  2546. {
  2547. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2548. | USB_DEVICE_ID_MATCH_DEVICE,
  2549. .idVendor = 0x0411,
  2550. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  2551. RNDIS_MASTER_INTERFACE,
  2552. .driver_info = (unsigned long) &bcm4320b_info,
  2553. }, {
  2554. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2555. | USB_DEVICE_ID_MATCH_DEVICE,
  2556. .idVendor = 0x0baf,
  2557. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  2558. RNDIS_MASTER_INTERFACE,
  2559. .driver_info = (unsigned long) &bcm4320b_info,
  2560. }, {
  2561. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2562. | USB_DEVICE_ID_MATCH_DEVICE,
  2563. .idVendor = 0x050d,
  2564. .idProduct = 0x011b, /* Belkin F5D7051 */
  2565. RNDIS_MASTER_INTERFACE,
  2566. .driver_info = (unsigned long) &bcm4320b_info,
  2567. }, {
  2568. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2569. | USB_DEVICE_ID_MATCH_DEVICE,
  2570. .idVendor = 0x1799, /* Belkin has two vendor ids */
  2571. .idProduct = 0x011b, /* Belkin F5D7051 */
  2572. RNDIS_MASTER_INTERFACE,
  2573. .driver_info = (unsigned long) &bcm4320b_info,
  2574. }, {
  2575. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2576. | USB_DEVICE_ID_MATCH_DEVICE,
  2577. .idVendor = 0x13b1,
  2578. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  2579. RNDIS_MASTER_INTERFACE,
  2580. .driver_info = (unsigned long) &bcm4320b_info,
  2581. }, {
  2582. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2583. | USB_DEVICE_ID_MATCH_DEVICE,
  2584. .idVendor = 0x13b1,
  2585. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  2586. RNDIS_MASTER_INTERFACE,
  2587. .driver_info = (unsigned long) &bcm4320b_info,
  2588. }, {
  2589. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2590. | USB_DEVICE_ID_MATCH_DEVICE,
  2591. .idVendor = 0x0b05,
  2592. .idProduct = 0x1717, /* Asus WL169gE */
  2593. RNDIS_MASTER_INTERFACE,
  2594. .driver_info = (unsigned long) &bcm4320b_info,
  2595. }, {
  2596. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2597. | USB_DEVICE_ID_MATCH_DEVICE,
  2598. .idVendor = 0x0a5c,
  2599. .idProduct = 0xd11b, /* Eminent EM4045 */
  2600. RNDIS_MASTER_INTERFACE,
  2601. .driver_info = (unsigned long) &bcm4320b_info,
  2602. }, {
  2603. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2604. | USB_DEVICE_ID_MATCH_DEVICE,
  2605. .idVendor = 0x1690,
  2606. .idProduct = 0x0715, /* BT Voyager 1055 */
  2607. RNDIS_MASTER_INTERFACE,
  2608. .driver_info = (unsigned long) &bcm4320b_info,
  2609. },
  2610. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  2611. * parameters available, hardware probably contain older firmware version with
  2612. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  2613. */
  2614. {
  2615. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2616. | USB_DEVICE_ID_MATCH_DEVICE,
  2617. .idVendor = 0x13b1,
  2618. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  2619. RNDIS_MASTER_INTERFACE,
  2620. .driver_info = (unsigned long) &bcm4320a_info,
  2621. }, {
  2622. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2623. | USB_DEVICE_ID_MATCH_DEVICE,
  2624. .idVendor = 0x0baf,
  2625. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  2626. RNDIS_MASTER_INTERFACE,
  2627. .driver_info = (unsigned long) &bcm4320a_info,
  2628. }, {
  2629. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2630. | USB_DEVICE_ID_MATCH_DEVICE,
  2631. .idVendor = 0x0411,
  2632. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  2633. RNDIS_MASTER_INTERFACE,
  2634. .driver_info = (unsigned long) &bcm4320a_info,
  2635. },
  2636. /* Generic Wireless RNDIS devices that we don't have exact
  2637. * idVendor/idProduct/chip yet.
  2638. */
  2639. {
  2640. /* RNDIS is MSFT's un-official variant of CDC ACM */
  2641. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  2642. .driver_info = (unsigned long) &rndis_wlan_info,
  2643. }, {
  2644. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  2645. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  2646. .driver_info = (unsigned long) &rndis_wlan_info,
  2647. },
  2648. { }, // END
  2649. };
  2650. MODULE_DEVICE_TABLE(usb, products);
  2651. static struct usb_driver rndis_wlan_driver = {
  2652. .name = "rndis_wlan",
  2653. .id_table = products,
  2654. .probe = usbnet_probe,
  2655. .disconnect = usbnet_disconnect,
  2656. .suspend = usbnet_suspend,
  2657. .resume = usbnet_resume,
  2658. };
  2659. static int __init rndis_wlan_init(void)
  2660. {
  2661. return usb_register(&rndis_wlan_driver);
  2662. }
  2663. module_init(rndis_wlan_init);
  2664. static void __exit rndis_wlan_exit(void)
  2665. {
  2666. usb_deregister(&rndis_wlan_driver);
  2667. }
  2668. module_exit(rndis_wlan_exit);
  2669. MODULE_AUTHOR("Bjorge Dijkstra");
  2670. MODULE_AUTHOR("Jussi Kivilinna");
  2671. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  2672. MODULE_LICENSE("GPL");