rndis_wlan.c 72 KB

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