rndis_wlan.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112
  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, basefilter;
  1257. int ret;
  1258. char *mc_addrs = NULL;
  1259. int mc_count;
  1260. basefilter = filter = RNDIS_PACKET_TYPE_DIRECTED |
  1261. RNDIS_PACKET_TYPE_BROADCAST;
  1262. if (usbdev->net->flags & IFF_PROMISC) {
  1263. filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
  1264. RNDIS_PACKET_TYPE_ALL_LOCAL;
  1265. } else if (usbdev->net->flags & IFF_ALLMULTI) {
  1266. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1267. }
  1268. if (filter != basefilter)
  1269. goto set_filter;
  1270. /*
  1271. * mc_list should be accessed holding the lock, so copy addresses to
  1272. * local buffer first.
  1273. */
  1274. netif_addr_lock_bh(usbdev->net);
  1275. mc_count = netdev_mc_count(usbdev->net);
  1276. if (mc_count > priv->multicast_size) {
  1277. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1278. } else if (mc_count) {
  1279. int i = 0;
  1280. mc_addrs = kmalloc(mc_count * ETH_ALEN, GFP_ATOMIC);
  1281. if (!mc_addrs) {
  1282. netdev_warn(usbdev->net,
  1283. "couldn't alloc %d bytes of memory\n",
  1284. mc_count * ETH_ALEN);
  1285. netif_addr_unlock_bh(usbdev->net);
  1286. return;
  1287. }
  1288. netdev_for_each_mc_addr(mclist, usbdev->net)
  1289. memcpy(mc_addrs + i++ * ETH_ALEN,
  1290. mclist->dmi_addr, ETH_ALEN);
  1291. }
  1292. netif_addr_unlock_bh(usbdev->net);
  1293. if (filter != basefilter)
  1294. goto set_filter;
  1295. if (mc_count) {
  1296. ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, mc_addrs,
  1297. mc_count * ETH_ALEN);
  1298. kfree(mc_addrs);
  1299. if (ret == 0)
  1300. filter |= RNDIS_PACKET_TYPE_MULTICAST;
  1301. else
  1302. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1303. netdev_dbg(usbdev->net, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
  1304. mc_count, priv->multicast_size, ret);
  1305. }
  1306. set_filter:
  1307. ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1308. sizeof(filter));
  1309. if (ret < 0) {
  1310. netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
  1311. le32_to_cpu(filter));
  1312. }
  1313. netdev_dbg(usbdev->net, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
  1314. le32_to_cpu(filter), ret);
  1315. }
  1316. /*
  1317. * cfg80211 ops
  1318. */
  1319. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1320. struct net_device *dev,
  1321. enum nl80211_iftype type, u32 *flags,
  1322. struct vif_params *params)
  1323. {
  1324. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1325. struct usbnet *usbdev = priv->usbdev;
  1326. int mode;
  1327. switch (type) {
  1328. case NL80211_IFTYPE_ADHOC:
  1329. mode = NDIS_80211_INFRA_ADHOC;
  1330. break;
  1331. case NL80211_IFTYPE_STATION:
  1332. mode = NDIS_80211_INFRA_INFRA;
  1333. break;
  1334. default:
  1335. return -EINVAL;
  1336. }
  1337. priv->wdev.iftype = type;
  1338. return set_infra_mode(usbdev, mode);
  1339. }
  1340. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1341. {
  1342. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1343. struct usbnet *usbdev = priv->usbdev;
  1344. int err;
  1345. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1346. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1347. if (err < 0)
  1348. return err;
  1349. }
  1350. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1351. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1352. if (err < 0)
  1353. return err;
  1354. }
  1355. return 0;
  1356. }
  1357. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  1358. int dbm)
  1359. {
  1360. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1361. struct usbnet *usbdev = priv->usbdev;
  1362. netdev_dbg(usbdev->net, "%s(): type:0x%x dbm:%i\n",
  1363. __func__, type, dbm);
  1364. /* Device doesn't support changing txpower after initialization, only
  1365. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1366. * currently used.
  1367. */
  1368. if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
  1369. if (!priv->radio_on)
  1370. disassociate(usbdev, true); /* turn on radio */
  1371. return 0;
  1372. }
  1373. return -ENOTSUPP;
  1374. }
  1375. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
  1376. {
  1377. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1378. struct usbnet *usbdev = priv->usbdev;
  1379. *dbm = get_bcm4320_power_dbm(priv);
  1380. netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
  1381. return 0;
  1382. }
  1383. #define SCAN_DELAY_JIFFIES (6 * HZ)
  1384. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  1385. struct cfg80211_scan_request *request)
  1386. {
  1387. struct usbnet *usbdev = netdev_priv(dev);
  1388. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1389. int ret;
  1390. __le32 tmp;
  1391. netdev_dbg(usbdev->net, "cfg80211.scan\n");
  1392. /* Get current bssid list from device before new scan, as new scan
  1393. * clears internal bssid list.
  1394. */
  1395. rndis_check_bssid_list(usbdev);
  1396. if (!request)
  1397. return -EINVAL;
  1398. if (priv->scan_request && priv->scan_request != request)
  1399. return -EBUSY;
  1400. priv->scan_request = request;
  1401. tmp = cpu_to_le32(1);
  1402. ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  1403. sizeof(tmp));
  1404. if (ret == 0) {
  1405. /* Wait before retrieving scan results from device */
  1406. queue_delayed_work(priv->workqueue, &priv->scan_work,
  1407. SCAN_DELAY_JIFFIES);
  1408. }
  1409. return ret;
  1410. }
  1411. static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
  1412. struct ndis_80211_bssid_ex *bssid)
  1413. {
  1414. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1415. struct ieee80211_channel *channel;
  1416. s32 signal;
  1417. u64 timestamp;
  1418. u16 capability;
  1419. u16 beacon_interval;
  1420. struct ndis_80211_fixed_ies *fixed;
  1421. int ie_len, bssid_len;
  1422. u8 *ie;
  1423. netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM]\n",
  1424. bssid->ssid.essid, bssid->mac);
  1425. /* parse bssid structure */
  1426. bssid_len = le32_to_cpu(bssid->length);
  1427. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1428. sizeof(struct ndis_80211_fixed_ies))
  1429. return NULL;
  1430. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1431. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1432. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1433. (int)le32_to_cpu(bssid->ie_length));
  1434. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1435. if (ie_len < 0)
  1436. return NULL;
  1437. /* extract data for cfg80211_inform_bss */
  1438. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1439. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1440. if (!channel)
  1441. return NULL;
  1442. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1443. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1444. capability = le16_to_cpu(fixed->capabilities);
  1445. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1446. return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1447. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1448. GFP_KERNEL);
  1449. }
  1450. static int rndis_check_bssid_list(struct usbnet *usbdev)
  1451. {
  1452. void *buf = NULL;
  1453. struct ndis_80211_bssid_list_ex *bssid_list;
  1454. struct ndis_80211_bssid_ex *bssid;
  1455. int ret = -EINVAL, len, count, bssid_len;
  1456. bool resized = false;
  1457. netdev_dbg(usbdev->net, "check_bssid_list\n");
  1458. len = CONTROL_BUFFER_SIZE;
  1459. resize_buf:
  1460. buf = kmalloc(len, GFP_KERNEL);
  1461. if (!buf) {
  1462. ret = -ENOMEM;
  1463. goto out;
  1464. }
  1465. ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  1466. if (ret != 0)
  1467. goto out;
  1468. if (!resized && len > CONTROL_BUFFER_SIZE) {
  1469. resized = true;
  1470. kfree(buf);
  1471. goto resize_buf;
  1472. }
  1473. bssid_list = buf;
  1474. bssid = bssid_list->bssid;
  1475. bssid_len = le32_to_cpu(bssid->length);
  1476. count = le32_to_cpu(bssid_list->num_items);
  1477. netdev_dbg(usbdev->net, "check_bssid_list: %d BSSIDs found (buflen: %d)\n",
  1478. count, len);
  1479. while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
  1480. rndis_bss_info_update(usbdev, bssid);
  1481. bssid = (void *)bssid + bssid_len;
  1482. bssid_len = le32_to_cpu(bssid->length);
  1483. count--;
  1484. }
  1485. out:
  1486. kfree(buf);
  1487. return ret;
  1488. }
  1489. static void rndis_get_scan_results(struct work_struct *work)
  1490. {
  1491. struct rndis_wlan_private *priv =
  1492. container_of(work, struct rndis_wlan_private, scan_work.work);
  1493. struct usbnet *usbdev = priv->usbdev;
  1494. int ret;
  1495. netdev_dbg(usbdev->net, "get_scan_results\n");
  1496. if (!priv->scan_request)
  1497. return;
  1498. ret = rndis_check_bssid_list(usbdev);
  1499. cfg80211_scan_done(priv->scan_request, ret < 0);
  1500. priv->scan_request = NULL;
  1501. }
  1502. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  1503. struct cfg80211_connect_params *sme)
  1504. {
  1505. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1506. struct usbnet *usbdev = priv->usbdev;
  1507. struct ieee80211_channel *channel = sme->channel;
  1508. struct ndis_80211_ssid ssid;
  1509. int pairwise = RNDIS_WLAN_ALG_NONE;
  1510. int groupwise = RNDIS_WLAN_ALG_NONE;
  1511. int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
  1512. int length, i, ret, chan = -1;
  1513. if (channel)
  1514. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1515. groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
  1516. for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
  1517. pairwise |=
  1518. rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
  1519. if (sme->crypto.n_ciphers_pairwise > 0 &&
  1520. pairwise == RNDIS_WLAN_ALG_NONE) {
  1521. netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
  1522. return -ENOTSUPP;
  1523. }
  1524. for (i = 0; i < sme->crypto.n_akm_suites; i++)
  1525. keymgmt |=
  1526. rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
  1527. if (sme->crypto.n_akm_suites > 0 &&
  1528. keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
  1529. netdev_err(usbdev->net, "Invalid keymgmt\n");
  1530. return -ENOTSUPP;
  1531. }
  1532. netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
  1533. sme->ssid, sme->bssid, chan,
  1534. sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
  1535. groupwise, pairwise, keymgmt);
  1536. if (is_associated(usbdev))
  1537. disassociate(usbdev, false);
  1538. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1539. if (ret < 0) {
  1540. netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
  1541. ret);
  1542. goto err_turn_radio_on;
  1543. }
  1544. ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
  1545. keymgmt);
  1546. if (ret < 0) {
  1547. netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
  1548. ret);
  1549. goto err_turn_radio_on;
  1550. }
  1551. set_priv_filter(usbdev);
  1552. ret = set_encr_mode(usbdev, pairwise, groupwise);
  1553. if (ret < 0) {
  1554. netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
  1555. ret);
  1556. goto err_turn_radio_on;
  1557. }
  1558. if (channel) {
  1559. ret = set_channel(usbdev, chan);
  1560. if (ret < 0) {
  1561. netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
  1562. ret);
  1563. goto err_turn_radio_on;
  1564. }
  1565. }
  1566. if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
  1567. priv->encr_tx_key_index = sme->key_idx;
  1568. ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
  1569. if (ret < 0) {
  1570. netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
  1571. ret, sme->key_len, sme->key_idx);
  1572. goto err_turn_radio_on;
  1573. }
  1574. }
  1575. if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
  1576. !is_broadcast_ether_addr(sme->bssid)) {
  1577. ret = set_bssid(usbdev, sme->bssid);
  1578. if (ret < 0) {
  1579. netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
  1580. ret);
  1581. goto err_turn_radio_on;
  1582. }
  1583. } else
  1584. clear_bssid(usbdev);
  1585. length = sme->ssid_len;
  1586. if (length > NDIS_802_11_LENGTH_SSID)
  1587. length = NDIS_802_11_LENGTH_SSID;
  1588. memset(&ssid, 0, sizeof(ssid));
  1589. ssid.length = cpu_to_le32(length);
  1590. memcpy(ssid.essid, sme->ssid, length);
  1591. /* Pause and purge rx queue, so we don't pass packets before
  1592. * 'media connect'-indication.
  1593. */
  1594. usbnet_pause_rx(usbdev);
  1595. usbnet_purge_paused_rxq(usbdev);
  1596. ret = set_essid(usbdev, &ssid);
  1597. if (ret < 0)
  1598. netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
  1599. return ret;
  1600. err_turn_radio_on:
  1601. disassociate(usbdev, true);
  1602. return ret;
  1603. }
  1604. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1605. u16 reason_code)
  1606. {
  1607. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1608. struct usbnet *usbdev = priv->usbdev;
  1609. netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
  1610. priv->connected = false;
  1611. memset(priv->bssid, 0, ETH_ALEN);
  1612. return deauthenticate(usbdev);
  1613. }
  1614. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1615. struct cfg80211_ibss_params *params)
  1616. {
  1617. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1618. struct usbnet *usbdev = priv->usbdev;
  1619. struct ieee80211_channel *channel = params->channel;
  1620. struct ndis_80211_ssid ssid;
  1621. enum nl80211_auth_type auth_type;
  1622. int ret, alg, length, chan = -1;
  1623. if (channel)
  1624. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1625. /* TODO: How to handle ad-hoc encryption?
  1626. * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
  1627. * pre-shared for encryption (open/shared/wpa), is key set before
  1628. * join_ibss? Which auth_type to use (not in params)? What about WPA?
  1629. */
  1630. if (params->privacy) {
  1631. auth_type = NL80211_AUTHTYPE_SHARED_KEY;
  1632. alg = RNDIS_WLAN_ALG_WEP;
  1633. } else {
  1634. auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  1635. alg = RNDIS_WLAN_ALG_NONE;
  1636. }
  1637. netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
  1638. params->ssid, params->bssid, chan, params->privacy);
  1639. if (is_associated(usbdev))
  1640. disassociate(usbdev, false);
  1641. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
  1642. if (ret < 0) {
  1643. netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
  1644. ret);
  1645. goto err_turn_radio_on;
  1646. }
  1647. ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
  1648. if (ret < 0) {
  1649. netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
  1650. ret);
  1651. goto err_turn_radio_on;
  1652. }
  1653. set_priv_filter(usbdev);
  1654. ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
  1655. if (ret < 0) {
  1656. netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
  1657. ret);
  1658. goto err_turn_radio_on;
  1659. }
  1660. if (channel) {
  1661. ret = set_channel(usbdev, chan);
  1662. if (ret < 0) {
  1663. netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
  1664. ret);
  1665. goto err_turn_radio_on;
  1666. }
  1667. }
  1668. if (params->bssid && !is_zero_ether_addr(params->bssid) &&
  1669. !is_broadcast_ether_addr(params->bssid)) {
  1670. ret = set_bssid(usbdev, params->bssid);
  1671. if (ret < 0) {
  1672. netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
  1673. ret);
  1674. goto err_turn_radio_on;
  1675. }
  1676. } else
  1677. clear_bssid(usbdev);
  1678. length = params->ssid_len;
  1679. if (length > NDIS_802_11_LENGTH_SSID)
  1680. length = NDIS_802_11_LENGTH_SSID;
  1681. memset(&ssid, 0, sizeof(ssid));
  1682. ssid.length = cpu_to_le32(length);
  1683. memcpy(ssid.essid, params->ssid, length);
  1684. /* Don't need to pause rx queue for ad-hoc. */
  1685. usbnet_purge_paused_rxq(usbdev);
  1686. usbnet_resume_rx(usbdev);
  1687. ret = set_essid(usbdev, &ssid);
  1688. if (ret < 0)
  1689. netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
  1690. ret);
  1691. return ret;
  1692. err_turn_radio_on:
  1693. disassociate(usbdev, true);
  1694. return ret;
  1695. }
  1696. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1697. {
  1698. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1699. struct usbnet *usbdev = priv->usbdev;
  1700. netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
  1701. priv->connected = false;
  1702. memset(priv->bssid, 0, ETH_ALEN);
  1703. return deauthenticate(usbdev);
  1704. }
  1705. static int rndis_set_channel(struct wiphy *wiphy,
  1706. struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
  1707. {
  1708. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1709. struct usbnet *usbdev = priv->usbdev;
  1710. return set_channel(usbdev,
  1711. ieee80211_frequency_to_channel(chan->center_freq));
  1712. }
  1713. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  1714. u8 key_index, const u8 *mac_addr,
  1715. struct key_params *params)
  1716. {
  1717. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1718. struct usbnet *usbdev = priv->usbdev;
  1719. __le32 flags;
  1720. netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
  1721. __func__, key_index, mac_addr, params->cipher);
  1722. switch (params->cipher) {
  1723. case WLAN_CIPHER_SUITE_WEP40:
  1724. case WLAN_CIPHER_SUITE_WEP104:
  1725. return add_wep_key(usbdev, params->key, params->key_len,
  1726. key_index);
  1727. case WLAN_CIPHER_SUITE_TKIP:
  1728. case WLAN_CIPHER_SUITE_CCMP:
  1729. flags = 0;
  1730. if (params->seq && params->seq_len > 0)
  1731. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  1732. if (mac_addr)
  1733. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
  1734. NDIS_80211_ADDKEY_TRANSMIT_KEY;
  1735. return add_wpa_key(usbdev, params->key, params->key_len,
  1736. key_index, mac_addr, params->seq,
  1737. params->seq_len, params->cipher, flags);
  1738. default:
  1739. netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
  1740. __func__, params->cipher);
  1741. return -ENOTSUPP;
  1742. }
  1743. }
  1744. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  1745. u8 key_index, const u8 *mac_addr)
  1746. {
  1747. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1748. struct usbnet *usbdev = priv->usbdev;
  1749. netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
  1750. return remove_key(usbdev, key_index, mac_addr);
  1751. }
  1752. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  1753. u8 key_index)
  1754. {
  1755. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1756. struct usbnet *usbdev = priv->usbdev;
  1757. struct rndis_wlan_encr_key key;
  1758. netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
  1759. priv->encr_tx_key_index = key_index;
  1760. key = priv->encr_keys[key_index];
  1761. return add_wep_key(usbdev, key.material, key.len, key_index);
  1762. }
  1763. static void rndis_fill_station_info(struct usbnet *usbdev,
  1764. struct station_info *sinfo)
  1765. {
  1766. __le32 linkspeed, rssi;
  1767. int ret, len;
  1768. memset(sinfo, 0, sizeof(*sinfo));
  1769. len = sizeof(linkspeed);
  1770. ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
  1771. if (ret == 0) {
  1772. sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
  1773. sinfo->filled |= STATION_INFO_TX_BITRATE;
  1774. }
  1775. len = sizeof(rssi);
  1776. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  1777. if (ret == 0) {
  1778. sinfo->signal = level_to_qual(le32_to_cpu(rssi));
  1779. sinfo->filled |= STATION_INFO_SIGNAL;
  1780. }
  1781. }
  1782. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  1783. u8 *mac, struct station_info *sinfo)
  1784. {
  1785. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1786. struct usbnet *usbdev = priv->usbdev;
  1787. if (compare_ether_addr(priv->bssid, mac))
  1788. return -ENOENT;
  1789. rndis_fill_station_info(usbdev, sinfo);
  1790. return 0;
  1791. }
  1792. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  1793. int idx, u8 *mac, struct station_info *sinfo)
  1794. {
  1795. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1796. struct usbnet *usbdev = priv->usbdev;
  1797. if (idx != 0)
  1798. return -ENOENT;
  1799. memcpy(mac, priv->bssid, ETH_ALEN);
  1800. rndis_fill_station_info(usbdev, sinfo);
  1801. return 0;
  1802. }
  1803. /*
  1804. * workers, indication handlers, device poller
  1805. */
  1806. static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
  1807. {
  1808. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1809. struct ndis_80211_assoc_info *info;
  1810. u8 assoc_buf[sizeof(*info) + IW_CUSTOM_MAX + 32];
  1811. u8 bssid[ETH_ALEN];
  1812. int resp_ie_len, req_ie_len;
  1813. u8 *req_ie, *resp_ie;
  1814. int ret, offset;
  1815. bool roamed = false;
  1816. if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
  1817. /* received media connect indication while connected, either
  1818. * device reassociated with same AP or roamed to new. */
  1819. roamed = true;
  1820. }
  1821. req_ie_len = 0;
  1822. resp_ie_len = 0;
  1823. req_ie = NULL;
  1824. resp_ie = NULL;
  1825. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  1826. memset(assoc_buf, 0, sizeof(assoc_buf));
  1827. info = (void *)assoc_buf;
  1828. /* Get association info IEs from device and send them back to
  1829. * userspace. */
  1830. ret = get_association_info(usbdev, info, sizeof(assoc_buf));
  1831. if (!ret) {
  1832. req_ie_len = le32_to_cpu(info->req_ie_length);
  1833. if (req_ie_len > 0) {
  1834. offset = le32_to_cpu(info->offset_req_ies);
  1835. req_ie = (u8 *)info + offset;
  1836. }
  1837. resp_ie_len = le32_to_cpu(info->resp_ie_length);
  1838. if (resp_ie_len > 0) {
  1839. offset = le32_to_cpu(info->offset_resp_ies);
  1840. resp_ie = (u8 *)info + offset;
  1841. }
  1842. }
  1843. } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
  1844. return;
  1845. ret = get_bssid(usbdev, bssid);
  1846. if (ret < 0)
  1847. memset(bssid, 0, sizeof(bssid));
  1848. netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
  1849. bssid, roamed ? " roamed" : "");
  1850. /* Internal bss list in device always contains at least the currently
  1851. * connected bss and we can get it to cfg80211 with
  1852. * rndis_check_bssid_list().
  1853. * NOTE: This is true for Broadcom chip, but not mentioned in RNDIS
  1854. * spec.
  1855. */
  1856. rndis_check_bssid_list(usbdev);
  1857. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  1858. if (!roamed)
  1859. cfg80211_connect_result(usbdev->net, bssid, req_ie,
  1860. req_ie_len, resp_ie,
  1861. resp_ie_len, 0, GFP_KERNEL);
  1862. else
  1863. cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
  1864. resp_ie, resp_ie_len, GFP_KERNEL);
  1865. } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1866. cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
  1867. priv->connected = true;
  1868. memcpy(priv->bssid, bssid, ETH_ALEN);
  1869. usbnet_resume_rx(usbdev);
  1870. netif_carrier_on(usbdev->net);
  1871. }
  1872. static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
  1873. {
  1874. union iwreq_data evt;
  1875. netif_carrier_off(usbdev->net);
  1876. evt.data.flags = 0;
  1877. evt.data.length = 0;
  1878. memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
  1879. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1880. }
  1881. static void rndis_wlan_worker(struct work_struct *work)
  1882. {
  1883. struct rndis_wlan_private *priv =
  1884. container_of(work, struct rndis_wlan_private, work);
  1885. struct usbnet *usbdev = priv->usbdev;
  1886. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
  1887. rndis_wlan_do_link_up_work(usbdev);
  1888. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
  1889. rndis_wlan_do_link_down_work(usbdev);
  1890. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1891. set_multicast_list(usbdev);
  1892. }
  1893. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  1894. {
  1895. struct usbnet *usbdev = netdev_priv(dev);
  1896. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1897. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1898. return;
  1899. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  1900. queue_work(priv->workqueue, &priv->work);
  1901. }
  1902. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  1903. struct ndis_80211_status_indication *indication,
  1904. int len)
  1905. {
  1906. u8 *buf;
  1907. const char *type;
  1908. int flags, buflen, key_id;
  1909. bool pairwise_error, group_error;
  1910. struct ndis_80211_auth_request *auth_req;
  1911. enum nl80211_key_type key_type;
  1912. /* must have at least one array entry */
  1913. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1914. sizeof(struct ndis_80211_auth_request)) {
  1915. netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
  1916. len);
  1917. return;
  1918. }
  1919. buf = (void *)&indication->u.auth_request[0];
  1920. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  1921. while (buflen >= sizeof(*auth_req)) {
  1922. auth_req = (void *)buf;
  1923. type = "unknown";
  1924. flags = le32_to_cpu(auth_req->flags);
  1925. pairwise_error = false;
  1926. group_error = false;
  1927. if (flags & 0x1)
  1928. type = "reauth request";
  1929. if (flags & 0x2)
  1930. type = "key update request";
  1931. if (flags & 0x6) {
  1932. pairwise_error = true;
  1933. type = "pairwise_error";
  1934. }
  1935. if (flags & 0xe) {
  1936. group_error = true;
  1937. type = "group_error";
  1938. }
  1939. netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
  1940. type, le32_to_cpu(auth_req->flags));
  1941. if (pairwise_error) {
  1942. key_type = NL80211_KEYTYPE_PAIRWISE;
  1943. key_id = -1;
  1944. cfg80211_michael_mic_failure(usbdev->net,
  1945. auth_req->bssid,
  1946. key_type, key_id, NULL,
  1947. GFP_KERNEL);
  1948. }
  1949. if (group_error) {
  1950. key_type = NL80211_KEYTYPE_GROUP;
  1951. key_id = -1;
  1952. cfg80211_michael_mic_failure(usbdev->net,
  1953. auth_req->bssid,
  1954. key_type, key_id, NULL,
  1955. GFP_KERNEL);
  1956. }
  1957. buflen -= le32_to_cpu(auth_req->length);
  1958. buf += le32_to_cpu(auth_req->length);
  1959. }
  1960. }
  1961. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  1962. struct ndis_80211_status_indication *indication,
  1963. int len)
  1964. {
  1965. struct ndis_80211_pmkid_cand_list *cand_list;
  1966. int list_len, expected_len, i;
  1967. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1968. sizeof(struct ndis_80211_pmkid_cand_list)) {
  1969. netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
  1970. len);
  1971. return;
  1972. }
  1973. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  1974. sizeof(struct ndis_80211_pmkid_candidate);
  1975. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  1976. offsetof(struct ndis_80211_status_indication, u);
  1977. if (len < expected_len) {
  1978. netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
  1979. len, expected_len);
  1980. return;
  1981. }
  1982. cand_list = &indication->u.cand_list;
  1983. netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
  1984. le32_to_cpu(cand_list->version),
  1985. le32_to_cpu(cand_list->num_candidates));
  1986. if (le32_to_cpu(cand_list->version) != 1)
  1987. return;
  1988. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  1989. struct ndis_80211_pmkid_candidate *cand =
  1990. &cand_list->candidate_list[i];
  1991. netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, bssid: %pM\n",
  1992. i, le32_to_cpu(cand->flags), cand->bssid);
  1993. #if 0
  1994. struct iw_pmkid_cand pcand;
  1995. union iwreq_data wrqu;
  1996. memset(&pcand, 0, sizeof(pcand));
  1997. if (le32_to_cpu(cand->flags) & 0x01)
  1998. pcand.flags |= IW_PMKID_CAND_PREAUTH;
  1999. pcand.index = i;
  2000. memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
  2001. memset(&wrqu, 0, sizeof(wrqu));
  2002. wrqu.data.length = sizeof(pcand);
  2003. wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
  2004. (u8 *)&pcand);
  2005. #endif
  2006. }
  2007. }
  2008. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  2009. struct rndis_indicate *msg, int buflen)
  2010. {
  2011. struct ndis_80211_status_indication *indication;
  2012. int len, offset;
  2013. offset = offsetof(struct rndis_indicate, status) +
  2014. le32_to_cpu(msg->offset);
  2015. len = le32_to_cpu(msg->length);
  2016. if (len < 8) {
  2017. netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
  2018. len);
  2019. return;
  2020. }
  2021. if (offset + len > buflen) {
  2022. netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
  2023. offset + len, buflen);
  2024. return;
  2025. }
  2026. indication = (void *)((u8 *)msg + offset);
  2027. switch (le32_to_cpu(indication->status_type)) {
  2028. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  2029. netdev_info(usbdev->net, "radio state indication: %i\n",
  2030. le32_to_cpu(indication->u.radio_status));
  2031. return;
  2032. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  2033. netdev_info(usbdev->net, "media stream mode indication: %i\n",
  2034. le32_to_cpu(indication->u.media_stream_mode));
  2035. return;
  2036. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  2037. rndis_wlan_auth_indication(usbdev, indication, len);
  2038. return;
  2039. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2040. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2041. return;
  2042. default:
  2043. netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
  2044. le32_to_cpu(indication->status_type));
  2045. }
  2046. }
  2047. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2048. {
  2049. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2050. struct rndis_indicate *msg = ind;
  2051. switch (msg->status) {
  2052. case RNDIS_STATUS_MEDIA_CONNECT:
  2053. if (priv->current_command_oid == OID_802_11_ADD_KEY) {
  2054. /* OID_802_11_ADD_KEY causes sometimes extra
  2055. * "media connect" indications which confuses driver
  2056. * and userspace to think that device is
  2057. * roaming/reassociating when it isn't.
  2058. */
  2059. netdev_dbg(usbdev->net, "ignored OID_802_11_ADD_KEY triggered 'media connect'\n");
  2060. return;
  2061. }
  2062. usbnet_pause_rx(usbdev);
  2063. netdev_info(usbdev->net, "media connect\n");
  2064. /* queue work to avoid recursive calls into rndis_command */
  2065. set_bit(WORK_LINK_UP, &priv->work_pending);
  2066. queue_work(priv->workqueue, &priv->work);
  2067. break;
  2068. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2069. netdev_info(usbdev->net, "media disconnect\n");
  2070. /* queue work to avoid recursive calls into rndis_command */
  2071. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2072. queue_work(priv->workqueue, &priv->work);
  2073. break;
  2074. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2075. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2076. break;
  2077. default:
  2078. netdev_info(usbdev->net, "indication: 0x%08x\n",
  2079. le32_to_cpu(msg->status));
  2080. break;
  2081. }
  2082. }
  2083. static int rndis_wlan_get_caps(struct usbnet *usbdev)
  2084. {
  2085. struct {
  2086. __le32 num_items;
  2087. __le32 items[8];
  2088. } networks_supported;
  2089. int len, retval, i, n;
  2090. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2091. /* determine supported modes */
  2092. len = sizeof(networks_supported);
  2093. retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
  2094. &networks_supported, &len);
  2095. if (retval >= 0) {
  2096. n = le32_to_cpu(networks_supported.num_items);
  2097. if (n > 8)
  2098. n = 8;
  2099. for (i = 0; i < n; i++) {
  2100. switch (le32_to_cpu(networks_supported.items[i])) {
  2101. case NDIS_80211_TYPE_FREQ_HOP:
  2102. case NDIS_80211_TYPE_DIRECT_SEQ:
  2103. priv->caps |= CAP_MODE_80211B;
  2104. break;
  2105. case NDIS_80211_TYPE_OFDM_A:
  2106. priv->caps |= CAP_MODE_80211A;
  2107. break;
  2108. case NDIS_80211_TYPE_OFDM_G:
  2109. priv->caps |= CAP_MODE_80211G;
  2110. break;
  2111. }
  2112. }
  2113. }
  2114. return retval;
  2115. }
  2116. #define DEVICE_POLLER_JIFFIES (HZ)
  2117. static void rndis_device_poller(struct work_struct *work)
  2118. {
  2119. struct rndis_wlan_private *priv =
  2120. container_of(work, struct rndis_wlan_private,
  2121. dev_poller_work.work);
  2122. struct usbnet *usbdev = priv->usbdev;
  2123. __le32 rssi, tmp;
  2124. int len, ret, j;
  2125. int update_jiffies = DEVICE_POLLER_JIFFIES;
  2126. void *buf;
  2127. /* Only check/do workaround when connected. Calling is_associated()
  2128. * also polls device with rndis_command() and catches for media link
  2129. * indications.
  2130. */
  2131. if (!is_associated(usbdev))
  2132. goto end;
  2133. len = sizeof(rssi);
  2134. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  2135. if (ret == 0)
  2136. priv->last_qual = level_to_qual(le32_to_cpu(rssi));
  2137. netdev_dbg(usbdev->net, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
  2138. ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
  2139. /* Workaround transfer stalls on poor quality links.
  2140. * TODO: find right way to fix these stalls (as stalls do not happen
  2141. * with ndiswrapper/windows driver). */
  2142. if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
  2143. /* Decrease stats worker interval to catch stalls.
  2144. * faster. Faster than 400-500ms causes packet loss,
  2145. * Slower doesn't catch stalls fast enough.
  2146. */
  2147. j = msecs_to_jiffies(priv->param_workaround_interval);
  2148. if (j > DEVICE_POLLER_JIFFIES)
  2149. j = DEVICE_POLLER_JIFFIES;
  2150. else if (j <= 0)
  2151. j = 1;
  2152. update_jiffies = j;
  2153. /* Send scan OID. Use of both OIDs is required to get device
  2154. * working.
  2155. */
  2156. tmp = cpu_to_le32(1);
  2157. rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  2158. sizeof(tmp));
  2159. len = CONTROL_BUFFER_SIZE;
  2160. buf = kmalloc(len, GFP_KERNEL);
  2161. if (!buf)
  2162. goto end;
  2163. rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  2164. kfree(buf);
  2165. }
  2166. end:
  2167. if (update_jiffies >= HZ)
  2168. update_jiffies = round_jiffies_relative(update_jiffies);
  2169. else {
  2170. j = round_jiffies_relative(update_jiffies);
  2171. if (abs(j - update_jiffies) <= 10)
  2172. update_jiffies = j;
  2173. }
  2174. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2175. update_jiffies);
  2176. }
  2177. /*
  2178. * driver/device initialization
  2179. */
  2180. static void rndis_copy_module_params(struct usbnet *usbdev)
  2181. {
  2182. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2183. priv->param_country[0] = modparam_country[0];
  2184. priv->param_country[1] = modparam_country[1];
  2185. priv->param_country[2] = 0;
  2186. priv->param_frameburst = modparam_frameburst;
  2187. priv->param_afterburner = modparam_afterburner;
  2188. priv->param_power_save = modparam_power_save;
  2189. priv->param_power_output = modparam_power_output;
  2190. priv->param_roamtrigger = modparam_roamtrigger;
  2191. priv->param_roamdelta = modparam_roamdelta;
  2192. priv->param_country[0] = toupper(priv->param_country[0]);
  2193. priv->param_country[1] = toupper(priv->param_country[1]);
  2194. /* doesn't support EU as country code, use FI instead */
  2195. if (!strcmp(priv->param_country, "EU"))
  2196. strcpy(priv->param_country, "FI");
  2197. if (priv->param_power_save < 0)
  2198. priv->param_power_save = 0;
  2199. else if (priv->param_power_save > 2)
  2200. priv->param_power_save = 2;
  2201. if (priv->param_power_output < 0)
  2202. priv->param_power_output = 0;
  2203. else if (priv->param_power_output > 3)
  2204. priv->param_power_output = 3;
  2205. if (priv->param_roamtrigger < -80)
  2206. priv->param_roamtrigger = -80;
  2207. else if (priv->param_roamtrigger > -60)
  2208. priv->param_roamtrigger = -60;
  2209. if (priv->param_roamdelta < 0)
  2210. priv->param_roamdelta = 0;
  2211. else if (priv->param_roamdelta > 2)
  2212. priv->param_roamdelta = 2;
  2213. if (modparam_workaround_interval < 0)
  2214. priv->param_workaround_interval = 500;
  2215. else
  2216. priv->param_workaround_interval = modparam_workaround_interval;
  2217. }
  2218. static int bcm4320a_early_init(struct usbnet *usbdev)
  2219. {
  2220. /* copy module parameters for bcm4320a so that iwconfig reports txpower
  2221. * and workaround parameter is copied to private structure correctly.
  2222. */
  2223. rndis_copy_module_params(usbdev);
  2224. /* bcm4320a doesn't handle configuration parameters well. Try
  2225. * set any and you get partially zeroed mac and broken device.
  2226. */
  2227. return 0;
  2228. }
  2229. static int bcm4320b_early_init(struct usbnet *usbdev)
  2230. {
  2231. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2232. char buf[8];
  2233. rndis_copy_module_params(usbdev);
  2234. /* Early initialization settings, setting these won't have effect
  2235. * if called after generic_rndis_bind().
  2236. */
  2237. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2238. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2239. priv->param_frameburst ? "1" : "0");
  2240. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2241. priv->param_afterburner ? "1" : "0");
  2242. sprintf(buf, "%d", priv->param_power_save);
  2243. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2244. sprintf(buf, "%d", priv->param_power_output);
  2245. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2246. sprintf(buf, "%d", priv->param_roamtrigger);
  2247. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2248. sprintf(buf, "%d", priv->param_roamdelta);
  2249. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2250. return 0;
  2251. }
  2252. /* same as rndis_netdev_ops but with local multicast handler */
  2253. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2254. .ndo_open = usbnet_open,
  2255. .ndo_stop = usbnet_stop,
  2256. .ndo_start_xmit = usbnet_start_xmit,
  2257. .ndo_tx_timeout = usbnet_tx_timeout,
  2258. .ndo_set_mac_address = eth_mac_addr,
  2259. .ndo_validate_addr = eth_validate_addr,
  2260. .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
  2261. };
  2262. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2263. {
  2264. struct wiphy *wiphy;
  2265. struct rndis_wlan_private *priv;
  2266. int retval, len;
  2267. __le32 tmp;
  2268. /* allocate wiphy and rndis private data
  2269. * NOTE: We only support a single virtual interface, so wiphy
  2270. * and wireless_dev are somewhat synonymous for this device.
  2271. */
  2272. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2273. if (!wiphy)
  2274. return -ENOMEM;
  2275. priv = wiphy_priv(wiphy);
  2276. usbdev->net->ieee80211_ptr = &priv->wdev;
  2277. priv->wdev.wiphy = wiphy;
  2278. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2279. /* These have to be initialized before calling generic_rndis_bind().
  2280. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2281. */
  2282. usbdev->driver_priv = priv;
  2283. priv->usbdev = usbdev;
  2284. mutex_init(&priv->command_lock);
  2285. /* because rndis_command() sleeps we need to use workqueue */
  2286. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2287. INIT_WORK(&priv->work, rndis_wlan_worker);
  2288. INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
  2289. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2290. /* try bind rndis_host */
  2291. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2292. if (retval < 0)
  2293. goto fail;
  2294. /* generic_rndis_bind set packet filter to multicast_all+
  2295. * promisc mode which doesn't work well for our devices (device
  2296. * picks up rssi to closest station instead of to access point).
  2297. *
  2298. * rndis_host wants to avoid all OID as much as possible
  2299. * so do promisc/multicast handling in rndis_wlan.
  2300. */
  2301. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2302. tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  2303. retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
  2304. sizeof(tmp));
  2305. len = sizeof(tmp);
  2306. retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
  2307. &len);
  2308. priv->multicast_size = le32_to_cpu(tmp);
  2309. if (retval < 0 || priv->multicast_size < 0)
  2310. priv->multicast_size = 0;
  2311. if (priv->multicast_size > 0)
  2312. usbdev->net->flags |= IFF_MULTICAST;
  2313. else
  2314. usbdev->net->flags &= ~IFF_MULTICAST;
  2315. /* fill-out wiphy structure and register w/ cfg80211 */
  2316. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2317. wiphy->privid = rndis_wiphy_privid;
  2318. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2319. | BIT(NL80211_IFTYPE_ADHOC);
  2320. wiphy->max_scan_ssids = 1;
  2321. /* TODO: fill-out band/encr information based on priv->caps */
  2322. rndis_wlan_get_caps(usbdev);
  2323. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2324. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2325. priv->band.channels = priv->channels;
  2326. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2327. priv->band.bitrates = priv->rates;
  2328. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2329. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2330. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2331. memcpy(priv->cipher_suites, rndis_cipher_suites,
  2332. sizeof(rndis_cipher_suites));
  2333. wiphy->cipher_suites = priv->cipher_suites;
  2334. wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
  2335. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2336. if (wiphy_register(wiphy)) {
  2337. retval = -ENODEV;
  2338. goto fail;
  2339. }
  2340. set_default_iw_params(usbdev);
  2341. /* set default rts/frag */
  2342. rndis_set_wiphy_params(wiphy,
  2343. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2344. /* turn radio on */
  2345. priv->radio_on = true;
  2346. disassociate(usbdev, true);
  2347. netif_carrier_off(usbdev->net);
  2348. return 0;
  2349. fail:
  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. wiphy_free(wiphy);
  2356. return retval;
  2357. }
  2358. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2359. {
  2360. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2361. /* turn radio off */
  2362. disassociate(usbdev, false);
  2363. cancel_delayed_work_sync(&priv->dev_poller_work);
  2364. cancel_delayed_work_sync(&priv->scan_work);
  2365. cancel_work_sync(&priv->work);
  2366. flush_workqueue(priv->workqueue);
  2367. destroy_workqueue(priv->workqueue);
  2368. if (priv && priv->wpa_ie_len)
  2369. kfree(priv->wpa_ie);
  2370. rndis_unbind(usbdev, intf);
  2371. wiphy_unregister(priv->wdev.wiphy);
  2372. wiphy_free(priv->wdev.wiphy);
  2373. }
  2374. static int rndis_wlan_reset(struct usbnet *usbdev)
  2375. {
  2376. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2377. int retval;
  2378. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2379. retval = rndis_reset(usbdev);
  2380. if (retval)
  2381. netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
  2382. /* rndis_reset cleared multicast list, so restore here.
  2383. (set_multicast_list() also turns on current packet filter) */
  2384. set_multicast_list(usbdev);
  2385. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2386. round_jiffies_relative(DEVICE_POLLER_JIFFIES));
  2387. return deauthenticate(usbdev);
  2388. }
  2389. static int rndis_wlan_stop(struct usbnet *usbdev)
  2390. {
  2391. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2392. int retval;
  2393. __le32 filter;
  2394. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2395. retval = disassociate(usbdev, false);
  2396. priv->work_pending = 0;
  2397. cancel_delayed_work_sync(&priv->dev_poller_work);
  2398. cancel_delayed_work_sync(&priv->scan_work);
  2399. cancel_work_sync(&priv->work);
  2400. flush_workqueue(priv->workqueue);
  2401. if (priv->scan_request) {
  2402. cfg80211_scan_done(priv->scan_request, true);
  2403. priv->scan_request = NULL;
  2404. }
  2405. /* Set current packet filter zero to block receiving data packets from
  2406. device. */
  2407. filter = 0;
  2408. rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2409. sizeof(filter));
  2410. return retval;
  2411. }
  2412. static const struct driver_info bcm4320b_info = {
  2413. .description = "Wireless RNDIS device, BCM4320b based",
  2414. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2415. FLAG_AVOID_UNLINK_URBS,
  2416. .bind = rndis_wlan_bind,
  2417. .unbind = rndis_wlan_unbind,
  2418. .status = rndis_status,
  2419. .rx_fixup = rndis_rx_fixup,
  2420. .tx_fixup = rndis_tx_fixup,
  2421. .reset = rndis_wlan_reset,
  2422. .stop = rndis_wlan_stop,
  2423. .early_init = bcm4320b_early_init,
  2424. .indication = rndis_wlan_indication,
  2425. };
  2426. static const struct driver_info bcm4320a_info = {
  2427. .description = "Wireless RNDIS device, BCM4320a based",
  2428. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2429. FLAG_AVOID_UNLINK_URBS,
  2430. .bind = rndis_wlan_bind,
  2431. .unbind = rndis_wlan_unbind,
  2432. .status = rndis_status,
  2433. .rx_fixup = rndis_rx_fixup,
  2434. .tx_fixup = rndis_tx_fixup,
  2435. .reset = rndis_wlan_reset,
  2436. .stop = rndis_wlan_stop,
  2437. .early_init = bcm4320a_early_init,
  2438. .indication = rndis_wlan_indication,
  2439. };
  2440. static const struct driver_info rndis_wlan_info = {
  2441. .description = "Wireless RNDIS device",
  2442. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2443. FLAG_AVOID_UNLINK_URBS,
  2444. .bind = rndis_wlan_bind,
  2445. .unbind = rndis_wlan_unbind,
  2446. .status = rndis_status,
  2447. .rx_fixup = rndis_rx_fixup,
  2448. .tx_fixup = rndis_tx_fixup,
  2449. .reset = rndis_wlan_reset,
  2450. .stop = rndis_wlan_stop,
  2451. .early_init = bcm4320a_early_init,
  2452. .indication = rndis_wlan_indication,
  2453. };
  2454. /*-------------------------------------------------------------------------*/
  2455. static const struct usb_device_id products [] = {
  2456. #define RNDIS_MASTER_INTERFACE \
  2457. .bInterfaceClass = USB_CLASS_COMM, \
  2458. .bInterfaceSubClass = 2 /* ACM */, \
  2459. .bInterfaceProtocol = 0x0ff
  2460. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2461. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  2462. */
  2463. {
  2464. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2465. | USB_DEVICE_ID_MATCH_DEVICE,
  2466. .idVendor = 0x0411,
  2467. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  2468. RNDIS_MASTER_INTERFACE,
  2469. .driver_info = (unsigned long) &bcm4320b_info,
  2470. }, {
  2471. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2472. | USB_DEVICE_ID_MATCH_DEVICE,
  2473. .idVendor = 0x0baf,
  2474. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  2475. RNDIS_MASTER_INTERFACE,
  2476. .driver_info = (unsigned long) &bcm4320b_info,
  2477. }, {
  2478. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2479. | USB_DEVICE_ID_MATCH_DEVICE,
  2480. .idVendor = 0x050d,
  2481. .idProduct = 0x011b, /* Belkin F5D7051 */
  2482. RNDIS_MASTER_INTERFACE,
  2483. .driver_info = (unsigned long) &bcm4320b_info,
  2484. }, {
  2485. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2486. | USB_DEVICE_ID_MATCH_DEVICE,
  2487. .idVendor = 0x1799, /* Belkin has two vendor ids */
  2488. .idProduct = 0x011b, /* Belkin F5D7051 */
  2489. RNDIS_MASTER_INTERFACE,
  2490. .driver_info = (unsigned long) &bcm4320b_info,
  2491. }, {
  2492. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2493. | USB_DEVICE_ID_MATCH_DEVICE,
  2494. .idVendor = 0x13b1,
  2495. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  2496. RNDIS_MASTER_INTERFACE,
  2497. .driver_info = (unsigned long) &bcm4320b_info,
  2498. }, {
  2499. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2500. | USB_DEVICE_ID_MATCH_DEVICE,
  2501. .idVendor = 0x13b1,
  2502. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  2503. RNDIS_MASTER_INTERFACE,
  2504. .driver_info = (unsigned long) &bcm4320b_info,
  2505. }, {
  2506. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2507. | USB_DEVICE_ID_MATCH_DEVICE,
  2508. .idVendor = 0x0b05,
  2509. .idProduct = 0x1717, /* Asus WL169gE */
  2510. RNDIS_MASTER_INTERFACE,
  2511. .driver_info = (unsigned long) &bcm4320b_info,
  2512. }, {
  2513. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2514. | USB_DEVICE_ID_MATCH_DEVICE,
  2515. .idVendor = 0x0a5c,
  2516. .idProduct = 0xd11b, /* Eminent EM4045 */
  2517. RNDIS_MASTER_INTERFACE,
  2518. .driver_info = (unsigned long) &bcm4320b_info,
  2519. }, {
  2520. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2521. | USB_DEVICE_ID_MATCH_DEVICE,
  2522. .idVendor = 0x1690,
  2523. .idProduct = 0x0715, /* BT Voyager 1055 */
  2524. RNDIS_MASTER_INTERFACE,
  2525. .driver_info = (unsigned long) &bcm4320b_info,
  2526. },
  2527. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  2528. * parameters available, hardware probably contain older firmware version with
  2529. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  2530. */
  2531. {
  2532. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2533. | USB_DEVICE_ID_MATCH_DEVICE,
  2534. .idVendor = 0x13b1,
  2535. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  2536. RNDIS_MASTER_INTERFACE,
  2537. .driver_info = (unsigned long) &bcm4320a_info,
  2538. }, {
  2539. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2540. | USB_DEVICE_ID_MATCH_DEVICE,
  2541. .idVendor = 0x0baf,
  2542. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  2543. RNDIS_MASTER_INTERFACE,
  2544. .driver_info = (unsigned long) &bcm4320a_info,
  2545. }, {
  2546. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2547. | USB_DEVICE_ID_MATCH_DEVICE,
  2548. .idVendor = 0x0411,
  2549. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  2550. RNDIS_MASTER_INTERFACE,
  2551. .driver_info = (unsigned long) &bcm4320a_info,
  2552. },
  2553. /* Generic Wireless RNDIS devices that we don't have exact
  2554. * idVendor/idProduct/chip yet.
  2555. */
  2556. {
  2557. /* RNDIS is MSFT's un-official variant of CDC ACM */
  2558. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  2559. .driver_info = (unsigned long) &rndis_wlan_info,
  2560. }, {
  2561. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  2562. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  2563. .driver_info = (unsigned long) &rndis_wlan_info,
  2564. },
  2565. { }, // END
  2566. };
  2567. MODULE_DEVICE_TABLE(usb, products);
  2568. static struct usb_driver rndis_wlan_driver = {
  2569. .name = "rndis_wlan",
  2570. .id_table = products,
  2571. .probe = usbnet_probe,
  2572. .disconnect = usbnet_disconnect,
  2573. .suspend = usbnet_suspend,
  2574. .resume = usbnet_resume,
  2575. };
  2576. static int __init rndis_wlan_init(void)
  2577. {
  2578. return usb_register(&rndis_wlan_driver);
  2579. }
  2580. module_init(rndis_wlan_init);
  2581. static void __exit rndis_wlan_exit(void)
  2582. {
  2583. usb_deregister(&rndis_wlan_driver);
  2584. }
  2585. module_exit(rndis_wlan_exit);
  2586. MODULE_AUTHOR("Bjorge Dijkstra");
  2587. MODULE_AUTHOR("Jussi Kivilinna");
  2588. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  2589. MODULE_LICENSE("GPL");