rndis_wlan.c 85 KB

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