rndis_wlan.c 84 KB

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