rndis_wlan.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763
  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@iki.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, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Portions of this file are based on NDISwrapper project,
  21. * Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
  22. * http://ndiswrapper.sourceforge.net/
  23. */
  24. // #define DEBUG // error path messages, extra info
  25. // #define VERBOSE // more; success messages
  26. #include <linux/module.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/mutex.h>
  32. #include <linux/mii.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/cdc.h>
  35. #include <linux/ieee80211.h>
  36. #include <linux/if_arp.h>
  37. #include <linux/ctype.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/slab.h>
  40. #include <net/cfg80211.h>
  41. #include <linux/usb/usbnet.h>
  42. #include <linux/usb/rndis_host.h>
  43. /* NOTE: All these are settings for Broadcom chipset */
  44. static char modparam_country[4] = "EU";
  45. module_param_string(country, modparam_country, 4, 0444);
  46. MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
  47. static int modparam_frameburst = 1;
  48. module_param_named(frameburst, modparam_frameburst, int, 0444);
  49. MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
  50. static int modparam_afterburner = 0;
  51. module_param_named(afterburner, modparam_afterburner, int, 0444);
  52. MODULE_PARM_DESC(afterburner,
  53. "enable afterburner aka '125 High Speed Mode' (default: off)");
  54. static int modparam_power_save = 0;
  55. module_param_named(power_save, modparam_power_save, int, 0444);
  56. MODULE_PARM_DESC(power_save,
  57. "set power save mode: 0=off, 1=on, 2=fast (default: off)");
  58. static int modparam_power_output = 3;
  59. module_param_named(power_output, modparam_power_output, int, 0444);
  60. MODULE_PARM_DESC(power_output,
  61. "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
  62. static int modparam_roamtrigger = -70;
  63. module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
  64. MODULE_PARM_DESC(roamtrigger,
  65. "set roaming dBm trigger: -80=optimize for distance, "
  66. "-60=bandwidth (default: -70)");
  67. static int modparam_roamdelta = 1;
  68. module_param_named(roamdelta, modparam_roamdelta, int, 0444);
  69. MODULE_PARM_DESC(roamdelta,
  70. "set roaming tendency: 0=aggressive, 1=moderate, "
  71. "2=conservative (default: moderate)");
  72. static int modparam_workaround_interval;
  73. module_param_named(workaround_interval, modparam_workaround_interval,
  74. int, 0444);
  75. MODULE_PARM_DESC(workaround_interval,
  76. "set stall workaround interval in msecs (0=disabled) (default: 0)");
  77. /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
  78. #define WL_NOISE -96 /* typical noise level in dBm */
  79. #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
  80. /* Assume that Broadcom 4320 (only chipset at time of writing known to be
  81. * based on wireless rndis) has default txpower of 13dBm.
  82. * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
  83. * 100% : 20 mW ~ 13dBm
  84. * 75% : 15 mW ~ 12dBm
  85. * 50% : 10 mW ~ 10dBm
  86. * 25% : 5 mW ~ 7dBm
  87. */
  88. #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
  89. #define BCM4320_DEFAULT_TXPOWER_DBM_75 12
  90. #define BCM4320_DEFAULT_TXPOWER_DBM_50 10
  91. #define BCM4320_DEFAULT_TXPOWER_DBM_25 7
  92. /* Known device types */
  93. #define RNDIS_UNKNOWN 0
  94. #define RNDIS_BCM4320A 1
  95. #define RNDIS_BCM4320B 2
  96. /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
  97. * slightly modified for datatype endianess, etc
  98. */
  99. #define NDIS_802_11_LENGTH_SSID 32
  100. #define NDIS_802_11_LENGTH_RATES 8
  101. #define NDIS_802_11_LENGTH_RATES_EX 16
  102. enum ndis_80211_net_type {
  103. NDIS_80211_TYPE_FREQ_HOP,
  104. NDIS_80211_TYPE_DIRECT_SEQ,
  105. NDIS_80211_TYPE_OFDM_A,
  106. NDIS_80211_TYPE_OFDM_G
  107. };
  108. enum ndis_80211_net_infra {
  109. NDIS_80211_INFRA_ADHOC,
  110. NDIS_80211_INFRA_INFRA,
  111. NDIS_80211_INFRA_AUTO_UNKNOWN
  112. };
  113. enum ndis_80211_auth_mode {
  114. NDIS_80211_AUTH_OPEN,
  115. NDIS_80211_AUTH_SHARED,
  116. NDIS_80211_AUTH_AUTO_SWITCH,
  117. NDIS_80211_AUTH_WPA,
  118. NDIS_80211_AUTH_WPA_PSK,
  119. NDIS_80211_AUTH_WPA_NONE,
  120. NDIS_80211_AUTH_WPA2,
  121. NDIS_80211_AUTH_WPA2_PSK
  122. };
  123. enum ndis_80211_encr_status {
  124. NDIS_80211_ENCR_WEP_ENABLED,
  125. NDIS_80211_ENCR_DISABLED,
  126. NDIS_80211_ENCR_WEP_KEY_ABSENT,
  127. NDIS_80211_ENCR_NOT_SUPPORTED,
  128. NDIS_80211_ENCR_TKIP_ENABLED,
  129. NDIS_80211_ENCR_TKIP_KEY_ABSENT,
  130. NDIS_80211_ENCR_CCMP_ENABLED,
  131. NDIS_80211_ENCR_CCMP_KEY_ABSENT
  132. };
  133. enum ndis_80211_priv_filter {
  134. NDIS_80211_PRIV_ACCEPT_ALL,
  135. NDIS_80211_PRIV_8021X_WEP
  136. };
  137. enum ndis_80211_status_type {
  138. NDIS_80211_STATUSTYPE_AUTHENTICATION,
  139. NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
  140. NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
  141. NDIS_80211_STATUSTYPE_RADIOSTATE,
  142. };
  143. enum ndis_80211_media_stream_mode {
  144. NDIS_80211_MEDIA_STREAM_OFF,
  145. NDIS_80211_MEDIA_STREAM_ON
  146. };
  147. enum ndis_80211_radio_status {
  148. NDIS_80211_RADIO_STATUS_ON,
  149. NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
  150. NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
  151. };
  152. enum ndis_80211_addkey_bits {
  153. NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
  154. NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
  155. NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
  156. NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  157. };
  158. enum ndis_80211_addwep_bits {
  159. NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
  160. NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  161. };
  162. enum ndis_80211_power_mode {
  163. NDIS_80211_POWER_MODE_CAM,
  164. NDIS_80211_POWER_MODE_MAX_PSP,
  165. NDIS_80211_POWER_MODE_FAST_PSP,
  166. };
  167. enum ndis_80211_pmkid_cand_list_flag_bits {
  168. NDIS_80211_PMKID_CAND_PREAUTH = cpu_to_le32(1 << 0)
  169. };
  170. struct ndis_80211_auth_request {
  171. __le32 length;
  172. u8 bssid[6];
  173. u8 padding[2];
  174. __le32 flags;
  175. } __packed;
  176. struct ndis_80211_pmkid_candidate {
  177. u8 bssid[6];
  178. u8 padding[2];
  179. __le32 flags;
  180. } __packed;
  181. struct ndis_80211_pmkid_cand_list {
  182. __le32 version;
  183. __le32 num_candidates;
  184. struct ndis_80211_pmkid_candidate candidate_list[0];
  185. } __packed;
  186. struct ndis_80211_status_indication {
  187. __le32 status_type;
  188. union {
  189. __le32 media_stream_mode;
  190. __le32 radio_status;
  191. struct ndis_80211_auth_request auth_request[0];
  192. struct ndis_80211_pmkid_cand_list cand_list;
  193. } u;
  194. } __packed;
  195. struct ndis_80211_ssid {
  196. __le32 length;
  197. u8 essid[NDIS_802_11_LENGTH_SSID];
  198. } __packed;
  199. struct ndis_80211_conf_freq_hop {
  200. __le32 length;
  201. __le32 hop_pattern;
  202. __le32 hop_set;
  203. __le32 dwell_time;
  204. } __packed;
  205. struct ndis_80211_conf {
  206. __le32 length;
  207. __le32 beacon_period;
  208. __le32 atim_window;
  209. __le32 ds_config;
  210. struct ndis_80211_conf_freq_hop fh_config;
  211. } __packed;
  212. struct ndis_80211_bssid_ex {
  213. __le32 length;
  214. u8 mac[6];
  215. u8 padding[2];
  216. struct ndis_80211_ssid ssid;
  217. __le32 privacy;
  218. __le32 rssi;
  219. __le32 net_type;
  220. struct ndis_80211_conf config;
  221. __le32 net_infra;
  222. u8 rates[NDIS_802_11_LENGTH_RATES_EX];
  223. __le32 ie_length;
  224. u8 ies[0];
  225. } __packed;
  226. struct ndis_80211_bssid_list_ex {
  227. __le32 num_items;
  228. struct ndis_80211_bssid_ex bssid[0];
  229. } __packed;
  230. struct ndis_80211_fixed_ies {
  231. u8 timestamp[8];
  232. __le16 beacon_interval;
  233. __le16 capabilities;
  234. } __packed;
  235. struct ndis_80211_wep_key {
  236. __le32 size;
  237. __le32 index;
  238. __le32 length;
  239. u8 material[32];
  240. } __packed;
  241. struct ndis_80211_key {
  242. __le32 size;
  243. __le32 index;
  244. __le32 length;
  245. u8 bssid[6];
  246. u8 padding[6];
  247. u8 rsc[8];
  248. u8 material[32];
  249. } __packed;
  250. struct ndis_80211_remove_key {
  251. __le32 size;
  252. __le32 index;
  253. u8 bssid[6];
  254. u8 padding[2];
  255. } __packed;
  256. struct ndis_config_param {
  257. __le32 name_offs;
  258. __le32 name_length;
  259. __le32 type;
  260. __le32 value_offs;
  261. __le32 value_length;
  262. } __packed;
  263. struct ndis_80211_assoc_info {
  264. __le32 length;
  265. __le16 req_ies;
  266. struct req_ie {
  267. __le16 capa;
  268. __le16 listen_interval;
  269. u8 cur_ap_address[6];
  270. } req_ie;
  271. __le32 req_ie_length;
  272. __le32 offset_req_ies;
  273. __le16 resp_ies;
  274. struct resp_ie {
  275. __le16 capa;
  276. __le16 status_code;
  277. __le16 assoc_id;
  278. } resp_ie;
  279. __le32 resp_ie_length;
  280. __le32 offset_resp_ies;
  281. } __packed;
  282. struct ndis_80211_auth_encr_pair {
  283. __le32 auth_mode;
  284. __le32 encr_mode;
  285. } __packed;
  286. struct ndis_80211_capability {
  287. __le32 length;
  288. __le32 version;
  289. __le32 num_pmkids;
  290. __le32 num_auth_encr_pair;
  291. struct ndis_80211_auth_encr_pair auth_encr_pair[0];
  292. } __packed;
  293. struct ndis_80211_bssid_info {
  294. u8 bssid[6];
  295. u8 pmkid[16];
  296. } __packed;
  297. struct ndis_80211_pmkid {
  298. __le32 length;
  299. __le32 bssid_info_count;
  300. struct ndis_80211_bssid_info bssid_info[0];
  301. } __packed;
  302. /*
  303. * private data
  304. */
  305. #define CAP_MODE_80211A 1
  306. #define CAP_MODE_80211B 2
  307. #define CAP_MODE_80211G 4
  308. #define CAP_MODE_MASK 7
  309. #define WORK_LINK_UP (1<<0)
  310. #define WORK_LINK_DOWN (1<<1)
  311. #define WORK_SET_MULTICAST_LIST (1<<2)
  312. #define RNDIS_WLAN_ALG_NONE 0
  313. #define RNDIS_WLAN_ALG_WEP (1<<0)
  314. #define RNDIS_WLAN_ALG_TKIP (1<<1)
  315. #define RNDIS_WLAN_ALG_CCMP (1<<2)
  316. #define RNDIS_WLAN_NUM_KEYS 4
  317. #define RNDIS_WLAN_KEY_MGMT_NONE 0
  318. #define RNDIS_WLAN_KEY_MGMT_802_1X (1<<0)
  319. #define RNDIS_WLAN_KEY_MGMT_PSK (1<<1)
  320. #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
  321. static const struct ieee80211_channel rndis_channels[] = {
  322. { .center_freq = 2412 },
  323. { .center_freq = 2417 },
  324. { .center_freq = 2422 },
  325. { .center_freq = 2427 },
  326. { .center_freq = 2432 },
  327. { .center_freq = 2437 },
  328. { .center_freq = 2442 },
  329. { .center_freq = 2447 },
  330. { .center_freq = 2452 },
  331. { .center_freq = 2457 },
  332. { .center_freq = 2462 },
  333. { .center_freq = 2467 },
  334. { .center_freq = 2472 },
  335. { .center_freq = 2484 },
  336. };
  337. static const struct ieee80211_rate rndis_rates[] = {
  338. { .bitrate = 10 },
  339. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  340. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  341. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  342. { .bitrate = 60 },
  343. { .bitrate = 90 },
  344. { .bitrate = 120 },
  345. { .bitrate = 180 },
  346. { .bitrate = 240 },
  347. { .bitrate = 360 },
  348. { .bitrate = 480 },
  349. { .bitrate = 540 }
  350. };
  351. static const u32 rndis_cipher_suites[] = {
  352. WLAN_CIPHER_SUITE_WEP40,
  353. WLAN_CIPHER_SUITE_WEP104,
  354. WLAN_CIPHER_SUITE_TKIP,
  355. WLAN_CIPHER_SUITE_CCMP,
  356. };
  357. struct rndis_wlan_encr_key {
  358. int len;
  359. u32 cipher;
  360. u8 material[32];
  361. u8 bssid[ETH_ALEN];
  362. bool pairwise;
  363. bool tx_key;
  364. };
  365. /* RNDIS device private data */
  366. struct rndis_wlan_private {
  367. struct usbnet *usbdev;
  368. struct wireless_dev wdev;
  369. struct cfg80211_scan_request *scan_request;
  370. struct workqueue_struct *workqueue;
  371. struct delayed_work dev_poller_work;
  372. struct delayed_work scan_work;
  373. struct work_struct work;
  374. struct mutex command_lock;
  375. unsigned long work_pending;
  376. int last_qual;
  377. s32 cqm_rssi_thold;
  378. u32 cqm_rssi_hyst;
  379. int last_cqm_event_rssi;
  380. struct ieee80211_supported_band band;
  381. struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
  382. struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
  383. u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
  384. int device_type;
  385. int caps;
  386. int multicast_size;
  387. /* module parameters */
  388. char param_country[4];
  389. int param_frameburst;
  390. int param_afterburner;
  391. int param_power_save;
  392. int param_power_output;
  393. int param_roamtrigger;
  394. int param_roamdelta;
  395. u32 param_workaround_interval;
  396. /* hardware state */
  397. bool radio_on;
  398. int power_mode;
  399. int infra_mode;
  400. bool connected;
  401. u8 bssid[ETH_ALEN];
  402. u32 current_command_oid;
  403. /* encryption stuff */
  404. u8 encr_tx_key_index;
  405. struct rndis_wlan_encr_key encr_keys[RNDIS_WLAN_NUM_KEYS];
  406. int wpa_version;
  407. u8 command_buffer[COMMAND_BUFFER_SIZE];
  408. };
  409. /*
  410. * cfg80211 ops
  411. */
  412. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  413. struct net_device *dev,
  414. enum nl80211_iftype type, u32 *flags,
  415. struct vif_params *params);
  416. static int rndis_scan(struct wiphy *wiphy,
  417. struct cfg80211_scan_request *request);
  418. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
  419. static int rndis_set_tx_power(struct wiphy *wiphy,
  420. struct wireless_dev *wdev,
  421. enum nl80211_tx_power_setting type,
  422. int mbm);
  423. static int rndis_get_tx_power(struct wiphy *wiphy,
  424. struct wireless_dev *wdev,
  425. int *dbm);
  426. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  427. struct cfg80211_connect_params *sme);
  428. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  429. u16 reason_code);
  430. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  431. struct cfg80211_ibss_params *params);
  432. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
  433. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  434. u8 key_index, bool pairwise, const u8 *mac_addr,
  435. struct key_params *params);
  436. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  437. u8 key_index, bool pairwise, const u8 *mac_addr);
  438. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  439. u8 key_index, bool unicast, bool multicast);
  440. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  441. u8 *mac, struct station_info *sinfo);
  442. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  443. int idx, u8 *mac, struct station_info *sinfo);
  444. static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  445. struct cfg80211_pmksa *pmksa);
  446. static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  447. struct cfg80211_pmksa *pmksa);
  448. static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
  449. static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
  450. bool enabled, int timeout);
  451. static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
  452. struct net_device *dev,
  453. s32 rssi_thold, u32 rssi_hyst);
  454. static const struct cfg80211_ops rndis_config_ops = {
  455. .change_virtual_intf = rndis_change_virtual_intf,
  456. .scan = rndis_scan,
  457. .set_wiphy_params = rndis_set_wiphy_params,
  458. .set_tx_power = rndis_set_tx_power,
  459. .get_tx_power = rndis_get_tx_power,
  460. .connect = rndis_connect,
  461. .disconnect = rndis_disconnect,
  462. .join_ibss = rndis_join_ibss,
  463. .leave_ibss = rndis_leave_ibss,
  464. .add_key = rndis_add_key,
  465. .del_key = rndis_del_key,
  466. .set_default_key = rndis_set_default_key,
  467. .get_station = rndis_get_station,
  468. .dump_station = rndis_dump_station,
  469. .set_pmksa = rndis_set_pmksa,
  470. .del_pmksa = rndis_del_pmksa,
  471. .flush_pmksa = rndis_flush_pmksa,
  472. .set_power_mgmt = rndis_set_power_mgmt,
  473. .set_cqm_rssi_config = rndis_set_cqm_rssi_config,
  474. };
  475. static void *rndis_wiphy_privid = &rndis_wiphy_privid;
  476. static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
  477. {
  478. return (struct rndis_wlan_private *)dev->driver_priv;
  479. }
  480. static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
  481. {
  482. switch (priv->param_power_output) {
  483. default:
  484. case 3:
  485. return BCM4320_DEFAULT_TXPOWER_DBM_100;
  486. case 2:
  487. return BCM4320_DEFAULT_TXPOWER_DBM_75;
  488. case 1:
  489. return BCM4320_DEFAULT_TXPOWER_DBM_50;
  490. case 0:
  491. return BCM4320_DEFAULT_TXPOWER_DBM_25;
  492. }
  493. }
  494. static bool is_wpa_key(struct rndis_wlan_private *priv, u8 idx)
  495. {
  496. int cipher = priv->encr_keys[idx].cipher;
  497. return (cipher == WLAN_CIPHER_SUITE_CCMP ||
  498. cipher == WLAN_CIPHER_SUITE_TKIP);
  499. }
  500. static int rndis_cipher_to_alg(u32 cipher)
  501. {
  502. switch (cipher) {
  503. default:
  504. return RNDIS_WLAN_ALG_NONE;
  505. case WLAN_CIPHER_SUITE_WEP40:
  506. case WLAN_CIPHER_SUITE_WEP104:
  507. return RNDIS_WLAN_ALG_WEP;
  508. case WLAN_CIPHER_SUITE_TKIP:
  509. return RNDIS_WLAN_ALG_TKIP;
  510. case WLAN_CIPHER_SUITE_CCMP:
  511. return RNDIS_WLAN_ALG_CCMP;
  512. }
  513. }
  514. static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
  515. {
  516. switch (akm_suite) {
  517. default:
  518. return RNDIS_WLAN_KEY_MGMT_NONE;
  519. case WLAN_AKM_SUITE_8021X:
  520. return RNDIS_WLAN_KEY_MGMT_802_1X;
  521. case WLAN_AKM_SUITE_PSK:
  522. return RNDIS_WLAN_KEY_MGMT_PSK;
  523. }
  524. }
  525. #ifdef DEBUG
  526. static const char *oid_to_string(u32 oid)
  527. {
  528. switch (oid) {
  529. #define OID_STR(oid) case oid: return(#oid)
  530. /* from rndis_host.h */
  531. OID_STR(RNDIS_OID_802_3_PERMANENT_ADDRESS);
  532. OID_STR(RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE);
  533. OID_STR(RNDIS_OID_GEN_CURRENT_PACKET_FILTER);
  534. OID_STR(RNDIS_OID_GEN_PHYSICAL_MEDIUM);
  535. /* from rndis_wlan.c */
  536. OID_STR(RNDIS_OID_GEN_LINK_SPEED);
  537. OID_STR(RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER);
  538. OID_STR(RNDIS_OID_GEN_XMIT_OK);
  539. OID_STR(RNDIS_OID_GEN_RCV_OK);
  540. OID_STR(RNDIS_OID_GEN_XMIT_ERROR);
  541. OID_STR(RNDIS_OID_GEN_RCV_ERROR);
  542. OID_STR(RNDIS_OID_GEN_RCV_NO_BUFFER);
  543. OID_STR(RNDIS_OID_802_3_CURRENT_ADDRESS);
  544. OID_STR(RNDIS_OID_802_3_MULTICAST_LIST);
  545. OID_STR(RNDIS_OID_802_3_MAXIMUM_LIST_SIZE);
  546. OID_STR(RNDIS_OID_802_11_BSSID);
  547. OID_STR(RNDIS_OID_802_11_SSID);
  548. OID_STR(RNDIS_OID_802_11_INFRASTRUCTURE_MODE);
  549. OID_STR(RNDIS_OID_802_11_ADD_WEP);
  550. OID_STR(RNDIS_OID_802_11_REMOVE_WEP);
  551. OID_STR(RNDIS_OID_802_11_DISASSOCIATE);
  552. OID_STR(RNDIS_OID_802_11_AUTHENTICATION_MODE);
  553. OID_STR(RNDIS_OID_802_11_PRIVACY_FILTER);
  554. OID_STR(RNDIS_OID_802_11_BSSID_LIST_SCAN);
  555. OID_STR(RNDIS_OID_802_11_ENCRYPTION_STATUS);
  556. OID_STR(RNDIS_OID_802_11_ADD_KEY);
  557. OID_STR(RNDIS_OID_802_11_REMOVE_KEY);
  558. OID_STR(RNDIS_OID_802_11_ASSOCIATION_INFORMATION);
  559. OID_STR(RNDIS_OID_802_11_CAPABILITY);
  560. OID_STR(RNDIS_OID_802_11_PMKID);
  561. OID_STR(RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED);
  562. OID_STR(RNDIS_OID_802_11_NETWORK_TYPE_IN_USE);
  563. OID_STR(RNDIS_OID_802_11_TX_POWER_LEVEL);
  564. OID_STR(RNDIS_OID_802_11_RSSI);
  565. OID_STR(RNDIS_OID_802_11_RSSI_TRIGGER);
  566. OID_STR(RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD);
  567. OID_STR(RNDIS_OID_802_11_RTS_THRESHOLD);
  568. OID_STR(RNDIS_OID_802_11_SUPPORTED_RATES);
  569. OID_STR(RNDIS_OID_802_11_CONFIGURATION);
  570. OID_STR(RNDIS_OID_802_11_POWER_MODE);
  571. OID_STR(RNDIS_OID_802_11_BSSID_LIST);
  572. #undef OID_STR
  573. }
  574. return "?";
  575. }
  576. #else
  577. static const char *oid_to_string(u32 oid)
  578. {
  579. return "?";
  580. }
  581. #endif
  582. /* translate error code */
  583. static int rndis_error_status(__le32 rndis_status)
  584. {
  585. int ret = -EINVAL;
  586. switch (le32_to_cpu(rndis_status)) {
  587. case RNDIS_STATUS_SUCCESS:
  588. ret = 0;
  589. break;
  590. case RNDIS_STATUS_FAILURE:
  591. case RNDIS_STATUS_INVALID_DATA:
  592. ret = -EINVAL;
  593. break;
  594. case RNDIS_STATUS_NOT_SUPPORTED:
  595. ret = -EOPNOTSUPP;
  596. break;
  597. case RNDIS_STATUS_ADAPTER_NOT_READY:
  598. case RNDIS_STATUS_ADAPTER_NOT_OPEN:
  599. ret = -EBUSY;
  600. break;
  601. }
  602. return ret;
  603. }
  604. static int rndis_query_oid(struct usbnet *dev, u32 oid, void *data, int *len)
  605. {
  606. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  607. union {
  608. void *buf;
  609. struct rndis_msg_hdr *header;
  610. struct rndis_query *get;
  611. struct rndis_query_c *get_c;
  612. } u;
  613. int ret, buflen;
  614. int resplen, respoffs, copylen;
  615. buflen = *len + sizeof(*u.get);
  616. if (buflen < CONTROL_BUFFER_SIZE)
  617. buflen = CONTROL_BUFFER_SIZE;
  618. if (buflen > COMMAND_BUFFER_SIZE) {
  619. u.buf = kmalloc(buflen, GFP_KERNEL);
  620. if (!u.buf)
  621. return -ENOMEM;
  622. } else {
  623. u.buf = priv->command_buffer;
  624. }
  625. mutex_lock(&priv->command_lock);
  626. memset(u.get, 0, sizeof *u.get);
  627. u.get->msg_type = cpu_to_le32(RNDIS_MSG_QUERY);
  628. u.get->msg_len = cpu_to_le32(sizeof *u.get);
  629. u.get->oid = cpu_to_le32(oid);
  630. priv->current_command_oid = oid;
  631. ret = rndis_command(dev, u.header, buflen);
  632. priv->current_command_oid = 0;
  633. if (ret < 0)
  634. netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
  635. __func__, oid_to_string(oid), ret,
  636. le32_to_cpu(u.get_c->status));
  637. if (ret == 0) {
  638. resplen = le32_to_cpu(u.get_c->len);
  639. respoffs = le32_to_cpu(u.get_c->offset) + 8;
  640. if (respoffs > buflen) {
  641. /* Device returned data offset outside buffer, error. */
  642. netdev_dbg(dev->net, "%s(%s): received invalid "
  643. "data offset: %d > %d\n", __func__,
  644. oid_to_string(oid), respoffs, buflen);
  645. ret = -EINVAL;
  646. goto exit_unlock;
  647. }
  648. if ((resplen + respoffs) > buflen) {
  649. /* Device would have returned more data if buffer would
  650. * have been big enough. Copy just the bits that we got.
  651. */
  652. copylen = buflen - respoffs;
  653. } else {
  654. copylen = resplen;
  655. }
  656. if (copylen > *len)
  657. copylen = *len;
  658. memcpy(data, u.buf + respoffs, copylen);
  659. *len = resplen;
  660. ret = rndis_error_status(u.get_c->status);
  661. if (ret < 0)
  662. netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
  663. __func__, oid_to_string(oid),
  664. le32_to_cpu(u.get_c->status), ret);
  665. }
  666. exit_unlock:
  667. mutex_unlock(&priv->command_lock);
  668. if (u.buf != priv->command_buffer)
  669. kfree(u.buf);
  670. return ret;
  671. }
  672. static int rndis_set_oid(struct usbnet *dev, u32 oid, const void *data,
  673. int len)
  674. {
  675. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  676. union {
  677. void *buf;
  678. struct rndis_msg_hdr *header;
  679. struct rndis_set *set;
  680. struct rndis_set_c *set_c;
  681. } u;
  682. int ret, buflen;
  683. buflen = len + sizeof(*u.set);
  684. if (buflen < CONTROL_BUFFER_SIZE)
  685. buflen = CONTROL_BUFFER_SIZE;
  686. if (buflen > COMMAND_BUFFER_SIZE) {
  687. u.buf = kmalloc(buflen, GFP_KERNEL);
  688. if (!u.buf)
  689. return -ENOMEM;
  690. } else {
  691. u.buf = priv->command_buffer;
  692. }
  693. mutex_lock(&priv->command_lock);
  694. memset(u.set, 0, sizeof *u.set);
  695. u.set->msg_type = cpu_to_le32(RNDIS_MSG_SET);
  696. u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
  697. u.set->oid = cpu_to_le32(oid);
  698. u.set->len = cpu_to_le32(len);
  699. u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
  700. u.set->handle = cpu_to_le32(0);
  701. memcpy(u.buf + sizeof(*u.set), data, len);
  702. priv->current_command_oid = oid;
  703. ret = rndis_command(dev, u.header, buflen);
  704. priv->current_command_oid = 0;
  705. if (ret < 0)
  706. netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
  707. __func__, oid_to_string(oid), ret,
  708. le32_to_cpu(u.set_c->status));
  709. if (ret == 0) {
  710. ret = rndis_error_status(u.set_c->status);
  711. if (ret < 0)
  712. netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
  713. __func__, oid_to_string(oid),
  714. le32_to_cpu(u.set_c->status), ret);
  715. }
  716. mutex_unlock(&priv->command_lock);
  717. if (u.buf != priv->command_buffer)
  718. kfree(u.buf);
  719. return ret;
  720. }
  721. static int rndis_reset(struct usbnet *usbdev)
  722. {
  723. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  724. struct rndis_reset *reset;
  725. int ret;
  726. mutex_lock(&priv->command_lock);
  727. reset = (void *)priv->command_buffer;
  728. memset(reset, 0, sizeof(*reset));
  729. reset->msg_type = cpu_to_le32(RNDIS_MSG_RESET);
  730. reset->msg_len = cpu_to_le32(sizeof(*reset));
  731. priv->current_command_oid = 0;
  732. ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
  733. mutex_unlock(&priv->command_lock);
  734. if (ret < 0)
  735. return ret;
  736. return 0;
  737. }
  738. /*
  739. * Specs say that we can only set config parameters only soon after device
  740. * initialization.
  741. * value_type: 0 = u32, 2 = unicode string
  742. */
  743. static int rndis_set_config_parameter(struct usbnet *dev, char *param,
  744. int value_type, void *value)
  745. {
  746. struct ndis_config_param *infobuf;
  747. int value_len, info_len, param_len, ret, i;
  748. __le16 *unibuf;
  749. __le32 *dst_value;
  750. if (value_type == 0)
  751. value_len = sizeof(__le32);
  752. else if (value_type == 2)
  753. value_len = strlen(value) * sizeof(__le16);
  754. else
  755. return -EINVAL;
  756. param_len = strlen(param) * sizeof(__le16);
  757. info_len = sizeof(*infobuf) + param_len + value_len;
  758. #ifdef DEBUG
  759. info_len += 12;
  760. #endif
  761. infobuf = kmalloc(info_len, GFP_KERNEL);
  762. if (!infobuf)
  763. return -ENOMEM;
  764. #ifdef DEBUG
  765. info_len -= 12;
  766. /* extra 12 bytes are for padding (debug output) */
  767. memset(infobuf, 0xCC, info_len + 12);
  768. #endif
  769. if (value_type == 2)
  770. netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
  771. param, (u8 *)value);
  772. else
  773. netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
  774. param, *(u32 *)value);
  775. infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
  776. infobuf->name_length = cpu_to_le32(param_len);
  777. infobuf->type = cpu_to_le32(value_type);
  778. infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
  779. infobuf->value_length = cpu_to_le32(value_len);
  780. /* simple string to unicode string conversion */
  781. unibuf = (void *)infobuf + sizeof(*infobuf);
  782. for (i = 0; i < param_len / sizeof(__le16); i++)
  783. unibuf[i] = cpu_to_le16(param[i]);
  784. if (value_type == 2) {
  785. unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
  786. for (i = 0; i < value_len / sizeof(__le16); i++)
  787. unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
  788. } else {
  789. dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
  790. *dst_value = cpu_to_le32(*(u32 *)value);
  791. }
  792. #ifdef DEBUG
  793. netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
  794. for (i = 0; i < info_len; i += 12) {
  795. u32 *tmp = (u32 *)((u8 *)infobuf + i);
  796. netdev_dbg(dev->net, "%08X:%08X:%08X\n",
  797. cpu_to_be32(tmp[0]),
  798. cpu_to_be32(tmp[1]),
  799. cpu_to_be32(tmp[2]));
  800. }
  801. #endif
  802. ret = rndis_set_oid(dev, RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER,
  803. infobuf, info_len);
  804. if (ret != 0)
  805. netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
  806. ret);
  807. kfree(infobuf);
  808. return ret;
  809. }
  810. static int rndis_set_config_parameter_str(struct usbnet *dev,
  811. char *param, char *value)
  812. {
  813. return rndis_set_config_parameter(dev, param, 2, value);
  814. }
  815. /*
  816. * data conversion functions
  817. */
  818. static int level_to_qual(int level)
  819. {
  820. int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
  821. return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
  822. }
  823. /*
  824. * common functions
  825. */
  826. static int set_infra_mode(struct usbnet *usbdev, int mode);
  827. static void restore_keys(struct usbnet *usbdev);
  828. static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
  829. bool *matched);
  830. static int rndis_start_bssid_list_scan(struct usbnet *usbdev)
  831. {
  832. __le32 tmp;
  833. /* Note: RNDIS_OID_802_11_BSSID_LIST_SCAN clears internal BSS list. */
  834. tmp = cpu_to_le32(1);
  835. return rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST_SCAN, &tmp,
  836. sizeof(tmp));
  837. }
  838. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  839. {
  840. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  841. int ret;
  842. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_SSID,
  843. ssid, sizeof(*ssid));
  844. if (ret < 0) {
  845. netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
  846. return ret;
  847. }
  848. if (ret == 0) {
  849. priv->radio_on = true;
  850. netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
  851. }
  852. return ret;
  853. }
  854. static int set_bssid(struct usbnet *usbdev, const u8 *bssid)
  855. {
  856. int ret;
  857. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID,
  858. bssid, ETH_ALEN);
  859. if (ret < 0) {
  860. netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
  861. bssid, ret);
  862. return ret;
  863. }
  864. return ret;
  865. }
  866. static int clear_bssid(struct usbnet *usbdev)
  867. {
  868. static const u8 broadcast_mac[ETH_ALEN] = {
  869. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  870. };
  871. return set_bssid(usbdev, broadcast_mac);
  872. }
  873. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  874. {
  875. int ret, len;
  876. len = ETH_ALEN;
  877. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID,
  878. bssid, &len);
  879. if (ret != 0)
  880. memset(bssid, 0, ETH_ALEN);
  881. return ret;
  882. }
  883. static int get_association_info(struct usbnet *usbdev,
  884. struct ndis_80211_assoc_info *info, int len)
  885. {
  886. return rndis_query_oid(usbdev,
  887. RNDIS_OID_802_11_ASSOCIATION_INFORMATION,
  888. info, &len);
  889. }
  890. static bool is_associated(struct usbnet *usbdev)
  891. {
  892. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  893. u8 bssid[ETH_ALEN];
  894. int ret;
  895. if (!priv->radio_on)
  896. return false;
  897. ret = get_bssid(usbdev, bssid);
  898. return (ret == 0 && !is_zero_ether_addr(bssid));
  899. }
  900. static int disassociate(struct usbnet *usbdev, bool reset_ssid)
  901. {
  902. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  903. struct ndis_80211_ssid ssid;
  904. int i, ret = 0;
  905. if (priv->radio_on) {
  906. ret = rndis_set_oid(usbdev,
  907. RNDIS_OID_802_11_DISASSOCIATE,
  908. NULL, 0);
  909. if (ret == 0) {
  910. priv->radio_on = false;
  911. netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
  912. __func__);
  913. if (reset_ssid)
  914. msleep(100);
  915. }
  916. }
  917. /* disassociate causes radio to be turned off; if reset_ssid
  918. * is given, set random ssid to enable radio */
  919. if (reset_ssid) {
  920. /* Set device to infrastructure mode so we don't get ad-hoc
  921. * 'media connect' indications with the random ssid.
  922. */
  923. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  924. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  925. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  926. ssid.essid[0] = 0x1;
  927. ssid.essid[1] = 0xff;
  928. for (i = 2; i < sizeof(ssid.essid); i++)
  929. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  930. ret = set_essid(usbdev, &ssid);
  931. }
  932. return ret;
  933. }
  934. static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
  935. enum nl80211_auth_type auth_type, int keymgmt)
  936. {
  937. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  938. __le32 tmp;
  939. int auth_mode, ret;
  940. netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
  941. __func__, wpa_version, auth_type, keymgmt);
  942. if (wpa_version & NL80211_WPA_VERSION_2) {
  943. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  944. auth_mode = NDIS_80211_AUTH_WPA2;
  945. else
  946. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  947. } else if (wpa_version & NL80211_WPA_VERSION_1) {
  948. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  949. auth_mode = NDIS_80211_AUTH_WPA;
  950. else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
  951. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  952. else
  953. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  954. } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  955. auth_mode = NDIS_80211_AUTH_SHARED;
  956. else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
  957. auth_mode = NDIS_80211_AUTH_OPEN;
  958. else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
  959. auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
  960. else
  961. return -ENOTSUPP;
  962. tmp = cpu_to_le32(auth_mode);
  963. ret = rndis_set_oid(usbdev,
  964. RNDIS_OID_802_11_AUTHENTICATION_MODE,
  965. &tmp, sizeof(tmp));
  966. if (ret != 0) {
  967. netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
  968. ret);
  969. return ret;
  970. }
  971. priv->wpa_version = wpa_version;
  972. return 0;
  973. }
  974. static int set_priv_filter(struct usbnet *usbdev)
  975. {
  976. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  977. __le32 tmp;
  978. netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
  979. __func__, priv->wpa_version);
  980. if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
  981. priv->wpa_version & NL80211_WPA_VERSION_1)
  982. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  983. else
  984. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  985. return rndis_set_oid(usbdev,
  986. RNDIS_OID_802_11_PRIVACY_FILTER, &tmp,
  987. sizeof(tmp));
  988. }
  989. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  990. {
  991. __le32 tmp;
  992. int encr_mode, ret;
  993. netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
  994. __func__, pairwise, groupwise);
  995. if (pairwise & RNDIS_WLAN_ALG_CCMP)
  996. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  997. else if (pairwise & RNDIS_WLAN_ALG_TKIP)
  998. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  999. else if (pairwise & RNDIS_WLAN_ALG_WEP)
  1000. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  1001. else if (groupwise & RNDIS_WLAN_ALG_CCMP)
  1002. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  1003. else if (groupwise & RNDIS_WLAN_ALG_TKIP)
  1004. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  1005. else
  1006. encr_mode = NDIS_80211_ENCR_DISABLED;
  1007. tmp = cpu_to_le32(encr_mode);
  1008. ret = rndis_set_oid(usbdev,
  1009. RNDIS_OID_802_11_ENCRYPTION_STATUS, &tmp,
  1010. sizeof(tmp));
  1011. if (ret != 0) {
  1012. netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
  1013. ret);
  1014. return ret;
  1015. }
  1016. return 0;
  1017. }
  1018. static int set_infra_mode(struct usbnet *usbdev, int mode)
  1019. {
  1020. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1021. __le32 tmp;
  1022. int ret;
  1023. netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
  1024. __func__, priv->infra_mode);
  1025. tmp = cpu_to_le32(mode);
  1026. ret = rndis_set_oid(usbdev,
  1027. RNDIS_OID_802_11_INFRASTRUCTURE_MODE,
  1028. &tmp, sizeof(tmp));
  1029. if (ret != 0) {
  1030. netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
  1031. ret);
  1032. return ret;
  1033. }
  1034. /* NDIS drivers clear keys when infrastructure mode is
  1035. * changed. But Linux tools assume otherwise. So set the
  1036. * keys */
  1037. restore_keys(usbdev);
  1038. priv->infra_mode = mode;
  1039. return 0;
  1040. }
  1041. static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
  1042. {
  1043. __le32 tmp;
  1044. netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
  1045. if (rts_threshold < 0 || rts_threshold > 2347)
  1046. rts_threshold = 2347;
  1047. tmp = cpu_to_le32(rts_threshold);
  1048. return rndis_set_oid(usbdev,
  1049. RNDIS_OID_802_11_RTS_THRESHOLD,
  1050. &tmp, sizeof(tmp));
  1051. }
  1052. static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
  1053. {
  1054. __le32 tmp;
  1055. netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
  1056. if (frag_threshold < 256 || frag_threshold > 2346)
  1057. frag_threshold = 2346;
  1058. tmp = cpu_to_le32(frag_threshold);
  1059. return rndis_set_oid(usbdev,
  1060. RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD,
  1061. &tmp, sizeof(tmp));
  1062. }
  1063. static void set_default_iw_params(struct usbnet *usbdev)
  1064. {
  1065. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1066. set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
  1067. RNDIS_WLAN_KEY_MGMT_NONE);
  1068. set_priv_filter(usbdev);
  1069. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1070. }
  1071. static int deauthenticate(struct usbnet *usbdev)
  1072. {
  1073. int ret;
  1074. ret = disassociate(usbdev, true);
  1075. set_default_iw_params(usbdev);
  1076. return ret;
  1077. }
  1078. static int set_channel(struct usbnet *usbdev, int channel)
  1079. {
  1080. struct ndis_80211_conf config;
  1081. unsigned int dsconfig;
  1082. int len, ret;
  1083. netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
  1084. /* this OID is valid only when not associated */
  1085. if (is_associated(usbdev))
  1086. return 0;
  1087. dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
  1088. len = sizeof(config);
  1089. ret = rndis_query_oid(usbdev,
  1090. RNDIS_OID_802_11_CONFIGURATION,
  1091. &config, &len);
  1092. if (ret < 0) {
  1093. netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
  1094. __func__);
  1095. return ret;
  1096. }
  1097. config.ds_config = cpu_to_le32(dsconfig);
  1098. ret = rndis_set_oid(usbdev,
  1099. RNDIS_OID_802_11_CONFIGURATION,
  1100. &config, sizeof(config));
  1101. netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
  1102. return ret;
  1103. }
  1104. static struct ieee80211_channel *get_current_channel(struct usbnet *usbdev,
  1105. u32 *beacon_period)
  1106. {
  1107. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1108. struct ieee80211_channel *channel;
  1109. struct ndis_80211_conf config;
  1110. int len, ret;
  1111. /* Get channel and beacon interval */
  1112. len = sizeof(config);
  1113. ret = rndis_query_oid(usbdev,
  1114. RNDIS_OID_802_11_CONFIGURATION,
  1115. &config, &len);
  1116. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_CONFIGURATION -> %d\n",
  1117. __func__, ret);
  1118. if (ret < 0)
  1119. return NULL;
  1120. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1121. KHZ_TO_MHZ(le32_to_cpu(config.ds_config)));
  1122. if (!channel)
  1123. return NULL;
  1124. if (beacon_period)
  1125. *beacon_period = le32_to_cpu(config.beacon_period);
  1126. return channel;
  1127. }
  1128. /* index must be 0 - N, as per NDIS */
  1129. static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1130. u8 index)
  1131. {
  1132. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1133. struct ndis_80211_wep_key ndis_key;
  1134. u32 cipher;
  1135. int ret;
  1136. netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
  1137. __func__, index, key_len);
  1138. if (index >= RNDIS_WLAN_NUM_KEYS)
  1139. return -EINVAL;
  1140. if (key_len == 5)
  1141. cipher = WLAN_CIPHER_SUITE_WEP40;
  1142. else if (key_len == 13)
  1143. cipher = WLAN_CIPHER_SUITE_WEP104;
  1144. else
  1145. return -EINVAL;
  1146. memset(&ndis_key, 0, sizeof(ndis_key));
  1147. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1148. ndis_key.length = cpu_to_le32(key_len);
  1149. ndis_key.index = cpu_to_le32(index);
  1150. memcpy(&ndis_key.material, key, key_len);
  1151. if (index == priv->encr_tx_key_index) {
  1152. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1153. ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
  1154. RNDIS_WLAN_ALG_NONE);
  1155. if (ret)
  1156. netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
  1157. ret);
  1158. }
  1159. ret = rndis_set_oid(usbdev,
  1160. RNDIS_OID_802_11_ADD_WEP, &ndis_key,
  1161. sizeof(ndis_key));
  1162. if (ret != 0) {
  1163. netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
  1164. index + 1, ret);
  1165. return ret;
  1166. }
  1167. priv->encr_keys[index].len = key_len;
  1168. priv->encr_keys[index].cipher = cipher;
  1169. memcpy(&priv->encr_keys[index].material, key, key_len);
  1170. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1171. return 0;
  1172. }
  1173. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1174. u8 index, const u8 *addr, const u8 *rx_seq,
  1175. int seq_len, u32 cipher, __le32 flags)
  1176. {
  1177. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1178. struct ndis_80211_key ndis_key;
  1179. bool is_addr_ok;
  1180. int ret;
  1181. if (index >= RNDIS_WLAN_NUM_KEYS) {
  1182. netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
  1183. __func__, index);
  1184. return -EINVAL;
  1185. }
  1186. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1187. netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
  1188. __func__, key_len);
  1189. return -EINVAL;
  1190. }
  1191. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
  1192. if (!rx_seq || seq_len <= 0) {
  1193. netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
  1194. __func__);
  1195. return -EINVAL;
  1196. }
  1197. if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
  1198. netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
  1199. return -EINVAL;
  1200. }
  1201. }
  1202. is_addr_ok = addr && !is_zero_ether_addr(addr) &&
  1203. !is_broadcast_ether_addr(addr);
  1204. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1205. netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
  1206. __func__, addr);
  1207. return -EINVAL;
  1208. }
  1209. netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
  1210. __func__, index,
  1211. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1212. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1213. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1214. memset(&ndis_key, 0, sizeof(ndis_key));
  1215. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1216. sizeof(ndis_key.material) + key_len);
  1217. ndis_key.length = cpu_to_le32(key_len);
  1218. ndis_key.index = cpu_to_le32(index) | flags;
  1219. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1220. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1221. * different order than NDIS spec, so swap the order here. */
  1222. memcpy(ndis_key.material, key, 16);
  1223. memcpy(ndis_key.material + 16, key + 24, 8);
  1224. memcpy(ndis_key.material + 24, key + 16, 8);
  1225. } else
  1226. memcpy(ndis_key.material, key, key_len);
  1227. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1228. memcpy(ndis_key.rsc, rx_seq, seq_len);
  1229. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1230. /* pairwise key */
  1231. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1232. } else {
  1233. /* group key */
  1234. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1235. memset(ndis_key.bssid, 0xff, ETH_ALEN);
  1236. else
  1237. get_bssid(usbdev, ndis_key.bssid);
  1238. }
  1239. ret = rndis_set_oid(usbdev,
  1240. RNDIS_OID_802_11_ADD_KEY, &ndis_key,
  1241. le32_to_cpu(ndis_key.size));
  1242. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_ADD_KEY -> %08X\n",
  1243. __func__, ret);
  1244. if (ret != 0)
  1245. return ret;
  1246. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1247. priv->encr_keys[index].len = key_len;
  1248. priv->encr_keys[index].cipher = cipher;
  1249. memcpy(&priv->encr_keys[index].material, key, key_len);
  1250. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1251. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1252. else
  1253. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1254. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1255. priv->encr_tx_key_index = index;
  1256. return 0;
  1257. }
  1258. static int restore_key(struct usbnet *usbdev, u8 key_idx)
  1259. {
  1260. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1261. struct rndis_wlan_encr_key key;
  1262. if (is_wpa_key(priv, key_idx))
  1263. return 0;
  1264. key = priv->encr_keys[key_idx];
  1265. netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
  1266. if (key.len == 0)
  1267. return 0;
  1268. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1269. }
  1270. static void restore_keys(struct usbnet *usbdev)
  1271. {
  1272. int i;
  1273. for (i = 0; i < 4; i++)
  1274. restore_key(usbdev, i);
  1275. }
  1276. static void clear_key(struct rndis_wlan_private *priv, u8 idx)
  1277. {
  1278. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1279. }
  1280. /* remove_key is for both wep and wpa */
  1281. static int remove_key(struct usbnet *usbdev, u8 index, const u8 *bssid)
  1282. {
  1283. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1284. struct ndis_80211_remove_key remove_key;
  1285. __le32 keyindex;
  1286. bool is_wpa;
  1287. int ret;
  1288. if (index >= RNDIS_WLAN_NUM_KEYS)
  1289. return -ENOENT;
  1290. if (priv->encr_keys[index].len == 0)
  1291. return 0;
  1292. is_wpa = is_wpa_key(priv, index);
  1293. netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
  1294. __func__, index, is_wpa ? "wpa" : "wep",
  1295. priv->encr_keys[index].len);
  1296. clear_key(priv, index);
  1297. if (is_wpa) {
  1298. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1299. remove_key.index = cpu_to_le32(index);
  1300. if (bssid) {
  1301. /* pairwise key */
  1302. if (!is_broadcast_ether_addr(bssid))
  1303. remove_key.index |=
  1304. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1305. memcpy(remove_key.bssid, bssid,
  1306. sizeof(remove_key.bssid));
  1307. } else
  1308. memset(remove_key.bssid, 0xff,
  1309. sizeof(remove_key.bssid));
  1310. ret = rndis_set_oid(usbdev,
  1311. RNDIS_OID_802_11_REMOVE_KEY,
  1312. &remove_key, sizeof(remove_key));
  1313. if (ret != 0)
  1314. return ret;
  1315. } else {
  1316. keyindex = cpu_to_le32(index);
  1317. ret = rndis_set_oid(usbdev,
  1318. RNDIS_OID_802_11_REMOVE_WEP,
  1319. &keyindex, sizeof(keyindex));
  1320. if (ret != 0) {
  1321. netdev_warn(usbdev->net,
  1322. "removing encryption key %d failed (%08X)\n",
  1323. index, ret);
  1324. return ret;
  1325. }
  1326. }
  1327. /* if it is transmit key, disable encryption */
  1328. if (index == priv->encr_tx_key_index)
  1329. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1330. return 0;
  1331. }
  1332. static void set_multicast_list(struct usbnet *usbdev)
  1333. {
  1334. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1335. struct netdev_hw_addr *ha;
  1336. __le32 filter, basefilter;
  1337. int ret;
  1338. char *mc_addrs = NULL;
  1339. int mc_count;
  1340. basefilter = filter = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED |
  1341. RNDIS_PACKET_TYPE_BROADCAST);
  1342. if (usbdev->net->flags & IFF_PROMISC) {
  1343. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_PROMISCUOUS |
  1344. RNDIS_PACKET_TYPE_ALL_LOCAL);
  1345. } else if (usbdev->net->flags & IFF_ALLMULTI) {
  1346. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1347. }
  1348. if (filter != basefilter)
  1349. goto set_filter;
  1350. /*
  1351. * mc_list should be accessed holding the lock, so copy addresses to
  1352. * local buffer first.
  1353. */
  1354. netif_addr_lock_bh(usbdev->net);
  1355. mc_count = netdev_mc_count(usbdev->net);
  1356. if (mc_count > priv->multicast_size) {
  1357. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1358. } else if (mc_count) {
  1359. int i = 0;
  1360. mc_addrs = kmalloc_array(mc_count, ETH_ALEN, GFP_ATOMIC);
  1361. if (!mc_addrs) {
  1362. netif_addr_unlock_bh(usbdev->net);
  1363. return;
  1364. }
  1365. netdev_for_each_mc_addr(ha, usbdev->net)
  1366. memcpy(mc_addrs + i++ * ETH_ALEN,
  1367. ha->addr, ETH_ALEN);
  1368. }
  1369. netif_addr_unlock_bh(usbdev->net);
  1370. if (filter != basefilter)
  1371. goto set_filter;
  1372. if (mc_count) {
  1373. ret = rndis_set_oid(usbdev,
  1374. RNDIS_OID_802_3_MULTICAST_LIST,
  1375. mc_addrs, mc_count * ETH_ALEN);
  1376. kfree(mc_addrs);
  1377. if (ret == 0)
  1378. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_MULTICAST);
  1379. else
  1380. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1381. netdev_dbg(usbdev->net, "RNDIS_OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
  1382. mc_count, priv->multicast_size, ret);
  1383. }
  1384. set_filter:
  1385. ret = rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1386. sizeof(filter));
  1387. if (ret < 0) {
  1388. netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
  1389. le32_to_cpu(filter));
  1390. }
  1391. netdev_dbg(usbdev->net, "RNDIS_OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
  1392. le32_to_cpu(filter), ret);
  1393. }
  1394. #ifdef DEBUG
  1395. static void debug_print_pmkids(struct usbnet *usbdev,
  1396. struct ndis_80211_pmkid *pmkids,
  1397. const char *func_str)
  1398. {
  1399. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1400. int i, len, count, max_pmkids, entry_len;
  1401. max_pmkids = priv->wdev.wiphy->max_num_pmkids;
  1402. len = le32_to_cpu(pmkids->length);
  1403. count = le32_to_cpu(pmkids->bssid_info_count);
  1404. entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
  1405. netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
  1406. "%d)\n", func_str, count, len, entry_len);
  1407. if (count > max_pmkids)
  1408. count = max_pmkids;
  1409. for (i = 0; i < count; i++) {
  1410. u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
  1411. netdev_dbg(usbdev->net, "%s(): bssid: %pM, "
  1412. "pmkid: %08X:%08X:%08X:%08X\n",
  1413. func_str, pmkids->bssid_info[i].bssid,
  1414. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  1415. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  1416. }
  1417. }
  1418. #else
  1419. static void debug_print_pmkids(struct usbnet *usbdev,
  1420. struct ndis_80211_pmkid *pmkids,
  1421. const char *func_str)
  1422. {
  1423. return;
  1424. }
  1425. #endif
  1426. static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
  1427. {
  1428. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1429. struct ndis_80211_pmkid *pmkids;
  1430. int len, ret, max_pmkids;
  1431. max_pmkids = priv->wdev.wiphy->max_num_pmkids;
  1432. len = sizeof(*pmkids) + max_pmkids * sizeof(pmkids->bssid_info[0]);
  1433. pmkids = kzalloc(len, GFP_KERNEL);
  1434. if (!pmkids)
  1435. return ERR_PTR(-ENOMEM);
  1436. pmkids->length = cpu_to_le32(len);
  1437. pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
  1438. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_PMKID,
  1439. pmkids, &len);
  1440. if (ret < 0) {
  1441. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d)"
  1442. " -> %d\n", __func__, len, max_pmkids, ret);
  1443. kfree(pmkids);
  1444. return ERR_PTR(ret);
  1445. }
  1446. if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
  1447. pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
  1448. debug_print_pmkids(usbdev, pmkids, __func__);
  1449. return pmkids;
  1450. }
  1451. static int set_device_pmkids(struct usbnet *usbdev,
  1452. struct ndis_80211_pmkid *pmkids)
  1453. {
  1454. int ret, len, num_pmkids;
  1455. num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
  1456. len = sizeof(*pmkids) + num_pmkids * sizeof(pmkids->bssid_info[0]);
  1457. pmkids->length = cpu_to_le32(len);
  1458. debug_print_pmkids(usbdev, pmkids, __func__);
  1459. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID, pmkids,
  1460. le32_to_cpu(pmkids->length));
  1461. if (ret < 0) {
  1462. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d) -> %d"
  1463. "\n", __func__, len, num_pmkids, ret);
  1464. }
  1465. kfree(pmkids);
  1466. return ret;
  1467. }
  1468. static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
  1469. struct ndis_80211_pmkid *pmkids,
  1470. struct cfg80211_pmksa *pmksa,
  1471. int max_pmkids)
  1472. {
  1473. int i, newlen, err;
  1474. unsigned int count;
  1475. count = le32_to_cpu(pmkids->bssid_info_count);
  1476. if (count > max_pmkids)
  1477. count = max_pmkids;
  1478. for (i = 0; i < count; i++)
  1479. if (ether_addr_equal(pmkids->bssid_info[i].bssid,
  1480. pmksa->bssid))
  1481. break;
  1482. /* pmkid not found */
  1483. if (i == count) {
  1484. netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
  1485. __func__, pmksa->bssid);
  1486. err = -ENOENT;
  1487. goto error;
  1488. }
  1489. for (; i + 1 < count; i++)
  1490. pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
  1491. count--;
  1492. newlen = sizeof(*pmkids) + count * sizeof(pmkids->bssid_info[0]);
  1493. pmkids->length = cpu_to_le32(newlen);
  1494. pmkids->bssid_info_count = cpu_to_le32(count);
  1495. return pmkids;
  1496. error:
  1497. kfree(pmkids);
  1498. return ERR_PTR(err);
  1499. }
  1500. static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
  1501. struct ndis_80211_pmkid *pmkids,
  1502. struct cfg80211_pmksa *pmksa,
  1503. int max_pmkids)
  1504. {
  1505. struct ndis_80211_pmkid *new_pmkids;
  1506. int i, err, newlen;
  1507. unsigned int count;
  1508. count = le32_to_cpu(pmkids->bssid_info_count);
  1509. if (count > max_pmkids)
  1510. count = max_pmkids;
  1511. /* update with new pmkid */
  1512. for (i = 0; i < count; i++) {
  1513. if (!ether_addr_equal(pmkids->bssid_info[i].bssid,
  1514. pmksa->bssid))
  1515. continue;
  1516. memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
  1517. WLAN_PMKID_LEN);
  1518. return pmkids;
  1519. }
  1520. /* out of space, return error */
  1521. if (i == max_pmkids) {
  1522. netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
  1523. err = -ENOSPC;
  1524. goto error;
  1525. }
  1526. /* add new pmkid */
  1527. newlen = sizeof(*pmkids) + (count + 1) * sizeof(pmkids->bssid_info[0]);
  1528. new_pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
  1529. if (!new_pmkids) {
  1530. err = -ENOMEM;
  1531. goto error;
  1532. }
  1533. pmkids = new_pmkids;
  1534. pmkids->length = cpu_to_le32(newlen);
  1535. pmkids->bssid_info_count = cpu_to_le32(count + 1);
  1536. memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
  1537. memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
  1538. return pmkids;
  1539. error:
  1540. kfree(pmkids);
  1541. return ERR_PTR(err);
  1542. }
  1543. /*
  1544. * cfg80211 ops
  1545. */
  1546. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1547. struct net_device *dev,
  1548. enum nl80211_iftype type, u32 *flags,
  1549. struct vif_params *params)
  1550. {
  1551. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1552. struct usbnet *usbdev = priv->usbdev;
  1553. int mode;
  1554. switch (type) {
  1555. case NL80211_IFTYPE_ADHOC:
  1556. mode = NDIS_80211_INFRA_ADHOC;
  1557. break;
  1558. case NL80211_IFTYPE_STATION:
  1559. mode = NDIS_80211_INFRA_INFRA;
  1560. break;
  1561. default:
  1562. return -EINVAL;
  1563. }
  1564. priv->wdev.iftype = type;
  1565. return set_infra_mode(usbdev, mode);
  1566. }
  1567. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1568. {
  1569. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1570. struct usbnet *usbdev = priv->usbdev;
  1571. int err;
  1572. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1573. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1574. if (err < 0)
  1575. return err;
  1576. }
  1577. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1578. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1579. if (err < 0)
  1580. return err;
  1581. }
  1582. return 0;
  1583. }
  1584. static int rndis_set_tx_power(struct wiphy *wiphy,
  1585. struct wireless_dev *wdev,
  1586. enum nl80211_tx_power_setting type,
  1587. int mbm)
  1588. {
  1589. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1590. struct usbnet *usbdev = priv->usbdev;
  1591. netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
  1592. __func__, type, mbm);
  1593. if (mbm < 0 || (mbm % 100))
  1594. return -ENOTSUPP;
  1595. /* Device doesn't support changing txpower after initialization, only
  1596. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1597. * currently used.
  1598. */
  1599. if (type == NL80211_TX_POWER_AUTOMATIC ||
  1600. MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
  1601. if (!priv->radio_on)
  1602. disassociate(usbdev, true); /* turn on radio */
  1603. return 0;
  1604. }
  1605. return -ENOTSUPP;
  1606. }
  1607. static int rndis_get_tx_power(struct wiphy *wiphy,
  1608. struct wireless_dev *wdev,
  1609. int *dbm)
  1610. {
  1611. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1612. struct usbnet *usbdev = priv->usbdev;
  1613. *dbm = get_bcm4320_power_dbm(priv);
  1614. netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
  1615. return 0;
  1616. }
  1617. #define SCAN_DELAY_JIFFIES (6 * HZ)
  1618. static int rndis_scan(struct wiphy *wiphy,
  1619. struct cfg80211_scan_request *request)
  1620. {
  1621. struct net_device *dev = request->wdev->netdev;
  1622. struct usbnet *usbdev = netdev_priv(dev);
  1623. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1624. int ret;
  1625. int delay = SCAN_DELAY_JIFFIES;
  1626. netdev_dbg(usbdev->net, "cfg80211.scan\n");
  1627. /* Get current bssid list from device before new scan, as new scan
  1628. * clears internal bssid list.
  1629. */
  1630. rndis_check_bssid_list(usbdev, NULL, NULL);
  1631. if (priv->scan_request && priv->scan_request != request)
  1632. return -EBUSY;
  1633. priv->scan_request = request;
  1634. ret = rndis_start_bssid_list_scan(usbdev);
  1635. if (ret == 0) {
  1636. if (priv->device_type == RNDIS_BCM4320A)
  1637. delay = HZ;
  1638. /* Wait before retrieving scan results from device */
  1639. queue_delayed_work(priv->workqueue, &priv->scan_work, delay);
  1640. }
  1641. return ret;
  1642. }
  1643. static bool rndis_bss_info_update(struct usbnet *usbdev,
  1644. struct ndis_80211_bssid_ex *bssid)
  1645. {
  1646. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1647. struct ieee80211_channel *channel;
  1648. struct cfg80211_bss *bss;
  1649. s32 signal;
  1650. u64 timestamp;
  1651. u16 capability;
  1652. u16 beacon_interval;
  1653. struct ndis_80211_fixed_ies *fixed;
  1654. int ie_len, bssid_len;
  1655. u8 *ie;
  1656. netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM], len: %d\n",
  1657. bssid->ssid.essid, bssid->mac, le32_to_cpu(bssid->length));
  1658. /* parse bssid structure */
  1659. bssid_len = le32_to_cpu(bssid->length);
  1660. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1661. sizeof(struct ndis_80211_fixed_ies))
  1662. return NULL;
  1663. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1664. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1665. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1666. (int)le32_to_cpu(bssid->ie_length));
  1667. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1668. if (ie_len < 0)
  1669. return NULL;
  1670. /* extract data for cfg80211_inform_bss */
  1671. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1672. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1673. if (!channel)
  1674. return NULL;
  1675. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1676. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1677. capability = le16_to_cpu(fixed->capabilities);
  1678. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1679. bss = cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1680. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1681. GFP_KERNEL);
  1682. cfg80211_put_bss(priv->wdev.wiphy, bss);
  1683. return (bss != NULL);
  1684. }
  1685. static struct ndis_80211_bssid_ex *next_bssid_list_item(
  1686. struct ndis_80211_bssid_ex *bssid,
  1687. int *bssid_len, void *buf, int len)
  1688. {
  1689. void *buf_end, *bssid_end;
  1690. buf_end = (char *)buf + len;
  1691. bssid_end = (char *)bssid + *bssid_len;
  1692. if ((int)(buf_end - bssid_end) < sizeof(bssid->length)) {
  1693. *bssid_len = 0;
  1694. return NULL;
  1695. } else {
  1696. bssid = (void *)((char *)bssid + *bssid_len);
  1697. *bssid_len = le32_to_cpu(bssid->length);
  1698. return bssid;
  1699. }
  1700. }
  1701. static bool check_bssid_list_item(struct ndis_80211_bssid_ex *bssid,
  1702. int bssid_len, void *buf, int len)
  1703. {
  1704. void *buf_end, *bssid_end;
  1705. if (!bssid || bssid_len <= 0 || bssid_len > len)
  1706. return false;
  1707. buf_end = (char *)buf + len;
  1708. bssid_end = (char *)bssid + bssid_len;
  1709. return (int)(buf_end - bssid_end) >= 0 && (int)(bssid_end - buf) >= 0;
  1710. }
  1711. static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
  1712. bool *matched)
  1713. {
  1714. void *buf = NULL;
  1715. struct ndis_80211_bssid_list_ex *bssid_list;
  1716. struct ndis_80211_bssid_ex *bssid;
  1717. int ret = -EINVAL, len, count, bssid_len, real_count, new_len;
  1718. netdev_dbg(usbdev->net, "%s()\n", __func__);
  1719. len = CONTROL_BUFFER_SIZE;
  1720. resize_buf:
  1721. buf = kzalloc(len, GFP_KERNEL);
  1722. if (!buf) {
  1723. ret = -ENOMEM;
  1724. goto out;
  1725. }
  1726. /* BSSID-list might have got bigger last time we checked, keep
  1727. * resizing until it won't get any bigger.
  1728. */
  1729. new_len = len;
  1730. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST,
  1731. buf, &new_len);
  1732. if (ret != 0 || new_len < sizeof(struct ndis_80211_bssid_list_ex))
  1733. goto out;
  1734. if (new_len > len) {
  1735. len = new_len;
  1736. kfree(buf);
  1737. goto resize_buf;
  1738. }
  1739. len = new_len;
  1740. bssid_list = buf;
  1741. count = le32_to_cpu(bssid_list->num_items);
  1742. real_count = 0;
  1743. netdev_dbg(usbdev->net, "%s(): buflen: %d\n", __func__, len);
  1744. bssid_len = 0;
  1745. bssid = next_bssid_list_item(bssid_list->bssid, &bssid_len, buf, len);
  1746. /* Device returns incorrect 'num_items'. Workaround by ignoring the
  1747. * received 'num_items' and walking through full bssid buffer instead.
  1748. */
  1749. while (check_bssid_list_item(bssid, bssid_len, buf, len)) {
  1750. if (rndis_bss_info_update(usbdev, bssid) && match_bssid &&
  1751. matched) {
  1752. if (ether_addr_equal(bssid->mac, match_bssid))
  1753. *matched = true;
  1754. }
  1755. real_count++;
  1756. bssid = next_bssid_list_item(bssid, &bssid_len, buf, len);
  1757. }
  1758. netdev_dbg(usbdev->net, "%s(): num_items from device: %d, really found:"
  1759. " %d\n", __func__, count, real_count);
  1760. out:
  1761. kfree(buf);
  1762. return ret;
  1763. }
  1764. static void rndis_get_scan_results(struct work_struct *work)
  1765. {
  1766. struct rndis_wlan_private *priv =
  1767. container_of(work, struct rndis_wlan_private, scan_work.work);
  1768. struct usbnet *usbdev = priv->usbdev;
  1769. int ret;
  1770. netdev_dbg(usbdev->net, "get_scan_results\n");
  1771. if (!priv->scan_request)
  1772. return;
  1773. ret = rndis_check_bssid_list(usbdev, NULL, NULL);
  1774. cfg80211_scan_done(priv->scan_request, ret < 0);
  1775. priv->scan_request = NULL;
  1776. }
  1777. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  1778. struct cfg80211_connect_params *sme)
  1779. {
  1780. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1781. struct usbnet *usbdev = priv->usbdev;
  1782. struct ieee80211_channel *channel = sme->channel;
  1783. struct ndis_80211_ssid ssid;
  1784. int pairwise = RNDIS_WLAN_ALG_NONE;
  1785. int groupwise = RNDIS_WLAN_ALG_NONE;
  1786. int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
  1787. int length, i, ret, chan = -1;
  1788. if (channel)
  1789. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1790. groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
  1791. for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
  1792. pairwise |=
  1793. rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
  1794. if (sme->crypto.n_ciphers_pairwise > 0 &&
  1795. pairwise == RNDIS_WLAN_ALG_NONE) {
  1796. netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
  1797. return -ENOTSUPP;
  1798. }
  1799. for (i = 0; i < sme->crypto.n_akm_suites; i++)
  1800. keymgmt |=
  1801. rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
  1802. if (sme->crypto.n_akm_suites > 0 &&
  1803. keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
  1804. netdev_err(usbdev->net, "Invalid keymgmt\n");
  1805. return -ENOTSUPP;
  1806. }
  1807. netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
  1808. sme->ssid, sme->bssid, chan,
  1809. sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
  1810. groupwise, pairwise, keymgmt);
  1811. if (is_associated(usbdev))
  1812. disassociate(usbdev, false);
  1813. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1814. if (ret < 0) {
  1815. netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
  1816. ret);
  1817. goto err_turn_radio_on;
  1818. }
  1819. ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
  1820. keymgmt);
  1821. if (ret < 0) {
  1822. netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
  1823. ret);
  1824. goto err_turn_radio_on;
  1825. }
  1826. set_priv_filter(usbdev);
  1827. ret = set_encr_mode(usbdev, pairwise, groupwise);
  1828. if (ret < 0) {
  1829. netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
  1830. ret);
  1831. goto err_turn_radio_on;
  1832. }
  1833. if (channel) {
  1834. ret = set_channel(usbdev, chan);
  1835. if (ret < 0) {
  1836. netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
  1837. ret);
  1838. goto err_turn_radio_on;
  1839. }
  1840. }
  1841. if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
  1842. priv->encr_tx_key_index = sme->key_idx;
  1843. ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
  1844. if (ret < 0) {
  1845. netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
  1846. ret, sme->key_len, sme->key_idx);
  1847. goto err_turn_radio_on;
  1848. }
  1849. }
  1850. if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
  1851. !is_broadcast_ether_addr(sme->bssid)) {
  1852. ret = set_bssid(usbdev, sme->bssid);
  1853. if (ret < 0) {
  1854. netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
  1855. ret);
  1856. goto err_turn_radio_on;
  1857. }
  1858. } else
  1859. clear_bssid(usbdev);
  1860. length = sme->ssid_len;
  1861. if (length > NDIS_802_11_LENGTH_SSID)
  1862. length = NDIS_802_11_LENGTH_SSID;
  1863. memset(&ssid, 0, sizeof(ssid));
  1864. ssid.length = cpu_to_le32(length);
  1865. memcpy(ssid.essid, sme->ssid, length);
  1866. /* Pause and purge rx queue, so we don't pass packets before
  1867. * 'media connect'-indication.
  1868. */
  1869. usbnet_pause_rx(usbdev);
  1870. usbnet_purge_paused_rxq(usbdev);
  1871. ret = set_essid(usbdev, &ssid);
  1872. if (ret < 0)
  1873. netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
  1874. return ret;
  1875. err_turn_radio_on:
  1876. disassociate(usbdev, true);
  1877. return ret;
  1878. }
  1879. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1880. u16 reason_code)
  1881. {
  1882. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1883. struct usbnet *usbdev = priv->usbdev;
  1884. netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
  1885. priv->connected = false;
  1886. memset(priv->bssid, 0, ETH_ALEN);
  1887. return deauthenticate(usbdev);
  1888. }
  1889. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1890. struct cfg80211_ibss_params *params)
  1891. {
  1892. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1893. struct usbnet *usbdev = priv->usbdev;
  1894. struct ieee80211_channel *channel = params->chandef.chan;
  1895. struct ndis_80211_ssid ssid;
  1896. enum nl80211_auth_type auth_type;
  1897. int ret, alg, length, chan = -1;
  1898. if (channel)
  1899. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1900. /* TODO: How to handle ad-hoc encryption?
  1901. * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
  1902. * pre-shared for encryption (open/shared/wpa), is key set before
  1903. * join_ibss? Which auth_type to use (not in params)? What about WPA?
  1904. */
  1905. if (params->privacy) {
  1906. auth_type = NL80211_AUTHTYPE_SHARED_KEY;
  1907. alg = RNDIS_WLAN_ALG_WEP;
  1908. } else {
  1909. auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  1910. alg = RNDIS_WLAN_ALG_NONE;
  1911. }
  1912. netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
  1913. params->ssid, params->bssid, chan, params->privacy);
  1914. if (is_associated(usbdev))
  1915. disassociate(usbdev, false);
  1916. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
  1917. if (ret < 0) {
  1918. netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
  1919. ret);
  1920. goto err_turn_radio_on;
  1921. }
  1922. ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
  1923. if (ret < 0) {
  1924. netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
  1925. ret);
  1926. goto err_turn_radio_on;
  1927. }
  1928. set_priv_filter(usbdev);
  1929. ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
  1930. if (ret < 0) {
  1931. netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
  1932. ret);
  1933. goto err_turn_radio_on;
  1934. }
  1935. if (channel) {
  1936. ret = set_channel(usbdev, chan);
  1937. if (ret < 0) {
  1938. netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
  1939. ret);
  1940. goto err_turn_radio_on;
  1941. }
  1942. }
  1943. if (params->bssid && !is_zero_ether_addr(params->bssid) &&
  1944. !is_broadcast_ether_addr(params->bssid)) {
  1945. ret = set_bssid(usbdev, params->bssid);
  1946. if (ret < 0) {
  1947. netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
  1948. ret);
  1949. goto err_turn_radio_on;
  1950. }
  1951. } else
  1952. clear_bssid(usbdev);
  1953. length = params->ssid_len;
  1954. if (length > NDIS_802_11_LENGTH_SSID)
  1955. length = NDIS_802_11_LENGTH_SSID;
  1956. memset(&ssid, 0, sizeof(ssid));
  1957. ssid.length = cpu_to_le32(length);
  1958. memcpy(ssid.essid, params->ssid, length);
  1959. /* Don't need to pause rx queue for ad-hoc. */
  1960. usbnet_purge_paused_rxq(usbdev);
  1961. usbnet_resume_rx(usbdev);
  1962. ret = set_essid(usbdev, &ssid);
  1963. if (ret < 0)
  1964. netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
  1965. ret);
  1966. return ret;
  1967. err_turn_radio_on:
  1968. disassociate(usbdev, true);
  1969. return ret;
  1970. }
  1971. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1972. {
  1973. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1974. struct usbnet *usbdev = priv->usbdev;
  1975. netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
  1976. priv->connected = false;
  1977. memset(priv->bssid, 0, ETH_ALEN);
  1978. return deauthenticate(usbdev);
  1979. }
  1980. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  1981. u8 key_index, bool pairwise, const u8 *mac_addr,
  1982. struct key_params *params)
  1983. {
  1984. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1985. struct usbnet *usbdev = priv->usbdev;
  1986. __le32 flags;
  1987. netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
  1988. __func__, key_index, mac_addr, params->cipher);
  1989. switch (params->cipher) {
  1990. case WLAN_CIPHER_SUITE_WEP40:
  1991. case WLAN_CIPHER_SUITE_WEP104:
  1992. return add_wep_key(usbdev, params->key, params->key_len,
  1993. key_index);
  1994. case WLAN_CIPHER_SUITE_TKIP:
  1995. case WLAN_CIPHER_SUITE_CCMP:
  1996. flags = 0;
  1997. if (params->seq && params->seq_len > 0)
  1998. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  1999. if (mac_addr)
  2000. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
  2001. NDIS_80211_ADDKEY_TRANSMIT_KEY;
  2002. return add_wpa_key(usbdev, params->key, params->key_len,
  2003. key_index, mac_addr, params->seq,
  2004. params->seq_len, params->cipher, flags);
  2005. default:
  2006. netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
  2007. __func__, params->cipher);
  2008. return -ENOTSUPP;
  2009. }
  2010. }
  2011. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  2012. u8 key_index, bool pairwise, const u8 *mac_addr)
  2013. {
  2014. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2015. struct usbnet *usbdev = priv->usbdev;
  2016. netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
  2017. return remove_key(usbdev, key_index, mac_addr);
  2018. }
  2019. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  2020. u8 key_index, bool unicast, bool multicast)
  2021. {
  2022. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2023. struct usbnet *usbdev = priv->usbdev;
  2024. struct rndis_wlan_encr_key key;
  2025. netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
  2026. if (key_index >= RNDIS_WLAN_NUM_KEYS)
  2027. return -ENOENT;
  2028. priv->encr_tx_key_index = key_index;
  2029. if (is_wpa_key(priv, key_index))
  2030. return 0;
  2031. key = priv->encr_keys[key_index];
  2032. return add_wep_key(usbdev, key.material, key.len, key_index);
  2033. }
  2034. static void rndis_fill_station_info(struct usbnet *usbdev,
  2035. struct station_info *sinfo)
  2036. {
  2037. __le32 linkspeed, rssi;
  2038. int ret, len;
  2039. memset(sinfo, 0, sizeof(*sinfo));
  2040. len = sizeof(linkspeed);
  2041. ret = rndis_query_oid(usbdev, RNDIS_OID_GEN_LINK_SPEED, &linkspeed, &len);
  2042. if (ret == 0) {
  2043. sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
  2044. sinfo->filled |= STATION_INFO_TX_BITRATE;
  2045. }
  2046. len = sizeof(rssi);
  2047. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
  2048. &rssi, &len);
  2049. if (ret == 0) {
  2050. sinfo->signal = level_to_qual(le32_to_cpu(rssi));
  2051. sinfo->filled |= STATION_INFO_SIGNAL;
  2052. }
  2053. }
  2054. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  2055. u8 *mac, struct station_info *sinfo)
  2056. {
  2057. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2058. struct usbnet *usbdev = priv->usbdev;
  2059. if (!ether_addr_equal(priv->bssid, mac))
  2060. return -ENOENT;
  2061. rndis_fill_station_info(usbdev, sinfo);
  2062. return 0;
  2063. }
  2064. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  2065. int idx, u8 *mac, struct station_info *sinfo)
  2066. {
  2067. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2068. struct usbnet *usbdev = priv->usbdev;
  2069. if (idx != 0)
  2070. return -ENOENT;
  2071. memcpy(mac, priv->bssid, ETH_ALEN);
  2072. rndis_fill_station_info(usbdev, sinfo);
  2073. return 0;
  2074. }
  2075. static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  2076. struct cfg80211_pmksa *pmksa)
  2077. {
  2078. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2079. struct usbnet *usbdev = priv->usbdev;
  2080. struct ndis_80211_pmkid *pmkids;
  2081. u32 *tmp = (u32 *)pmksa->pmkid;
  2082. netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
  2083. pmksa->bssid,
  2084. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  2085. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  2086. pmkids = get_device_pmkids(usbdev);
  2087. if (IS_ERR(pmkids)) {
  2088. /* couldn't read PMKID cache from device */
  2089. return PTR_ERR(pmkids);
  2090. }
  2091. pmkids = update_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
  2092. if (IS_ERR(pmkids)) {
  2093. /* not found, list full, etc */
  2094. return PTR_ERR(pmkids);
  2095. }
  2096. return set_device_pmkids(usbdev, pmkids);
  2097. }
  2098. static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  2099. struct cfg80211_pmksa *pmksa)
  2100. {
  2101. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2102. struct usbnet *usbdev = priv->usbdev;
  2103. struct ndis_80211_pmkid *pmkids;
  2104. u32 *tmp = (u32 *)pmksa->pmkid;
  2105. netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
  2106. pmksa->bssid,
  2107. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  2108. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  2109. pmkids = get_device_pmkids(usbdev);
  2110. if (IS_ERR(pmkids)) {
  2111. /* Couldn't read PMKID cache from device */
  2112. return PTR_ERR(pmkids);
  2113. }
  2114. pmkids = remove_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
  2115. if (IS_ERR(pmkids)) {
  2116. /* not found, etc */
  2117. return PTR_ERR(pmkids);
  2118. }
  2119. return set_device_pmkids(usbdev, pmkids);
  2120. }
  2121. static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
  2122. {
  2123. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2124. struct usbnet *usbdev = priv->usbdev;
  2125. struct ndis_80211_pmkid pmkid;
  2126. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2127. memset(&pmkid, 0, sizeof(pmkid));
  2128. pmkid.length = cpu_to_le32(sizeof(pmkid));
  2129. pmkid.bssid_info_count = cpu_to_le32(0);
  2130. return rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID,
  2131. &pmkid, sizeof(pmkid));
  2132. }
  2133. static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
  2134. bool enabled, int timeout)
  2135. {
  2136. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2137. struct usbnet *usbdev = priv->usbdev;
  2138. int power_mode;
  2139. __le32 mode;
  2140. int ret;
  2141. if (priv->device_type != RNDIS_BCM4320B)
  2142. return -ENOTSUPP;
  2143. netdev_dbg(usbdev->net, "%s(): %s, %d\n", __func__,
  2144. enabled ? "enabled" : "disabled",
  2145. timeout);
  2146. if (enabled)
  2147. power_mode = NDIS_80211_POWER_MODE_FAST_PSP;
  2148. else
  2149. power_mode = NDIS_80211_POWER_MODE_CAM;
  2150. if (power_mode == priv->power_mode)
  2151. return 0;
  2152. priv->power_mode = power_mode;
  2153. mode = cpu_to_le32(power_mode);
  2154. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_POWER_MODE,
  2155. &mode, sizeof(mode));
  2156. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_POWER_MODE -> %d\n",
  2157. __func__, ret);
  2158. return ret;
  2159. }
  2160. static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
  2161. struct net_device *dev,
  2162. s32 rssi_thold, u32 rssi_hyst)
  2163. {
  2164. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2165. priv->cqm_rssi_thold = rssi_thold;
  2166. priv->cqm_rssi_hyst = rssi_hyst;
  2167. priv->last_cqm_event_rssi = 0;
  2168. return 0;
  2169. }
  2170. static void rndis_wlan_craft_connected_bss(struct usbnet *usbdev, u8 *bssid,
  2171. struct ndis_80211_assoc_info *info)
  2172. {
  2173. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2174. struct ieee80211_channel *channel;
  2175. struct ndis_80211_ssid ssid;
  2176. struct cfg80211_bss *bss;
  2177. s32 signal;
  2178. u64 timestamp;
  2179. u16 capability;
  2180. u32 beacon_period = 0;
  2181. __le32 rssi;
  2182. u8 ie_buf[34];
  2183. int len, ret, ie_len;
  2184. /* Get signal quality, in case of error use rssi=0 and ignore error. */
  2185. len = sizeof(rssi);
  2186. rssi = 0;
  2187. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
  2188. &rssi, &len);
  2189. signal = level_to_qual(le32_to_cpu(rssi));
  2190. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_RSSI -> %d, "
  2191. "rssi:%d, qual: %d\n", __func__, ret, le32_to_cpu(rssi),
  2192. level_to_qual(le32_to_cpu(rssi)));
  2193. /* Get AP capabilities */
  2194. if (info) {
  2195. capability = le16_to_cpu(info->resp_ie.capa);
  2196. } else {
  2197. /* Set atleast ESS/IBSS capability */
  2198. capability = (priv->infra_mode == NDIS_80211_INFRA_INFRA) ?
  2199. WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS;
  2200. }
  2201. /* Get channel and beacon interval */
  2202. channel = get_current_channel(usbdev, &beacon_period);
  2203. if (!channel) {
  2204. netdev_warn(usbdev->net, "%s(): could not get channel.\n",
  2205. __func__);
  2206. return;
  2207. }
  2208. /* Get SSID, in case of error, use zero length SSID and ignore error. */
  2209. len = sizeof(ssid);
  2210. memset(&ssid, 0, sizeof(ssid));
  2211. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_SSID,
  2212. &ssid, &len);
  2213. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_SSID -> %d, len: %d, ssid: "
  2214. "'%.32s'\n", __func__, ret,
  2215. le32_to_cpu(ssid.length), ssid.essid);
  2216. if (le32_to_cpu(ssid.length) > 32)
  2217. ssid.length = cpu_to_le32(32);
  2218. ie_buf[0] = WLAN_EID_SSID;
  2219. ie_buf[1] = le32_to_cpu(ssid.length);
  2220. memcpy(&ie_buf[2], ssid.essid, le32_to_cpu(ssid.length));
  2221. ie_len = le32_to_cpu(ssid.length) + 2;
  2222. /* no tsf */
  2223. timestamp = 0;
  2224. netdev_dbg(usbdev->net, "%s(): channel:%d(freq), bssid:[%pM], tsf:%d, "
  2225. "capa:%x, beacon int:%d, resp_ie(len:%d, essid:'%.32s'), "
  2226. "signal:%d\n", __func__, (channel ? channel->center_freq : -1),
  2227. bssid, (u32)timestamp, capability, beacon_period, ie_len,
  2228. ssid.essid, signal);
  2229. bss = cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid,
  2230. timestamp, capability, beacon_period, ie_buf, ie_len,
  2231. signal, GFP_KERNEL);
  2232. cfg80211_put_bss(priv->wdev.wiphy, bss);
  2233. }
  2234. /*
  2235. * workers, indication handlers, device poller
  2236. */
  2237. static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
  2238. {
  2239. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2240. struct ndis_80211_assoc_info *info = NULL;
  2241. u8 bssid[ETH_ALEN];
  2242. unsigned int resp_ie_len, req_ie_len;
  2243. unsigned int offset;
  2244. u8 *req_ie, *resp_ie;
  2245. int ret;
  2246. bool roamed = false;
  2247. bool match_bss;
  2248. if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
  2249. /* received media connect indication while connected, either
  2250. * device reassociated with same AP or roamed to new. */
  2251. roamed = true;
  2252. }
  2253. req_ie_len = 0;
  2254. resp_ie_len = 0;
  2255. req_ie = NULL;
  2256. resp_ie = NULL;
  2257. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  2258. info = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
  2259. if (!info) {
  2260. /* No memory? Try resume work later */
  2261. set_bit(WORK_LINK_UP, &priv->work_pending);
  2262. queue_work(priv->workqueue, &priv->work);
  2263. return;
  2264. }
  2265. /* Get association info IEs from device. */
  2266. ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
  2267. if (!ret) {
  2268. req_ie_len = le32_to_cpu(info->req_ie_length);
  2269. if (req_ie_len > CONTROL_BUFFER_SIZE)
  2270. req_ie_len = CONTROL_BUFFER_SIZE;
  2271. if (req_ie_len != 0) {
  2272. offset = le32_to_cpu(info->offset_req_ies);
  2273. if (offset > CONTROL_BUFFER_SIZE)
  2274. offset = CONTROL_BUFFER_SIZE;
  2275. req_ie = (u8 *)info + offset;
  2276. if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
  2277. req_ie_len =
  2278. CONTROL_BUFFER_SIZE - offset;
  2279. }
  2280. resp_ie_len = le32_to_cpu(info->resp_ie_length);
  2281. if (resp_ie_len > CONTROL_BUFFER_SIZE)
  2282. resp_ie_len = CONTROL_BUFFER_SIZE;
  2283. if (resp_ie_len != 0) {
  2284. offset = le32_to_cpu(info->offset_resp_ies);
  2285. if (offset > CONTROL_BUFFER_SIZE)
  2286. offset = CONTROL_BUFFER_SIZE;
  2287. resp_ie = (u8 *)info + offset;
  2288. if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
  2289. resp_ie_len =
  2290. CONTROL_BUFFER_SIZE - offset;
  2291. }
  2292. } else {
  2293. /* Since rndis_wlan_craft_connected_bss() might use info
  2294. * later and expects info to contain valid data if
  2295. * non-null, free info and set NULL here.
  2296. */
  2297. kfree(info);
  2298. info = NULL;
  2299. }
  2300. } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
  2301. return;
  2302. ret = get_bssid(usbdev, bssid);
  2303. if (ret < 0)
  2304. memset(bssid, 0, sizeof(bssid));
  2305. netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
  2306. bssid, roamed ? " roamed" : "");
  2307. /* Internal bss list in device should contain at least the currently
  2308. * connected bss and we can get it to cfg80211 with
  2309. * rndis_check_bssid_list().
  2310. *
  2311. * NDIS spec says: "If the device is associated, but the associated
  2312. * BSSID is not in its BSSID scan list, then the driver must add an
  2313. * entry for the BSSID at the end of the data that it returns in
  2314. * response to query of RNDIS_OID_802_11_BSSID_LIST."
  2315. *
  2316. * NOTE: Seems to be true for BCM4320b variant, but not BCM4320a.
  2317. */
  2318. match_bss = false;
  2319. rndis_check_bssid_list(usbdev, bssid, &match_bss);
  2320. if (!is_zero_ether_addr(bssid) && !match_bss) {
  2321. /* Couldn't get bss from device, we need to manually craft bss
  2322. * for cfg80211.
  2323. */
  2324. rndis_wlan_craft_connected_bss(usbdev, bssid, info);
  2325. }
  2326. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  2327. if (!roamed)
  2328. cfg80211_connect_result(usbdev->net, bssid, req_ie,
  2329. req_ie_len, resp_ie,
  2330. resp_ie_len, 0, GFP_KERNEL);
  2331. else
  2332. cfg80211_roamed(usbdev->net,
  2333. get_current_channel(usbdev, NULL),
  2334. bssid, req_ie, req_ie_len,
  2335. resp_ie, resp_ie_len, GFP_KERNEL);
  2336. } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  2337. cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
  2338. kfree(info);
  2339. priv->connected = true;
  2340. memcpy(priv->bssid, bssid, ETH_ALEN);
  2341. usbnet_resume_rx(usbdev);
  2342. netif_carrier_on(usbdev->net);
  2343. }
  2344. static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
  2345. {
  2346. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2347. if (priv->connected) {
  2348. priv->connected = false;
  2349. memset(priv->bssid, 0, ETH_ALEN);
  2350. deauthenticate(usbdev);
  2351. cfg80211_disconnected(usbdev->net, 0, NULL, 0, GFP_KERNEL);
  2352. }
  2353. netif_carrier_off(usbdev->net);
  2354. }
  2355. static void rndis_wlan_worker(struct work_struct *work)
  2356. {
  2357. struct rndis_wlan_private *priv =
  2358. container_of(work, struct rndis_wlan_private, work);
  2359. struct usbnet *usbdev = priv->usbdev;
  2360. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
  2361. rndis_wlan_do_link_up_work(usbdev);
  2362. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
  2363. rndis_wlan_do_link_down_work(usbdev);
  2364. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  2365. set_multicast_list(usbdev);
  2366. }
  2367. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  2368. {
  2369. struct usbnet *usbdev = netdev_priv(dev);
  2370. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2371. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  2372. return;
  2373. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  2374. queue_work(priv->workqueue, &priv->work);
  2375. }
  2376. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  2377. struct ndis_80211_status_indication *indication,
  2378. int len)
  2379. {
  2380. u8 *buf;
  2381. const char *type;
  2382. int flags, buflen, key_id;
  2383. bool pairwise_error, group_error;
  2384. struct ndis_80211_auth_request *auth_req;
  2385. enum nl80211_key_type key_type;
  2386. /* must have at least one array entry */
  2387. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2388. sizeof(struct ndis_80211_auth_request)) {
  2389. netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
  2390. len);
  2391. return;
  2392. }
  2393. buf = (void *)&indication->u.auth_request[0];
  2394. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  2395. while (buflen >= sizeof(*auth_req)) {
  2396. auth_req = (void *)buf;
  2397. type = "unknown";
  2398. flags = le32_to_cpu(auth_req->flags);
  2399. pairwise_error = false;
  2400. group_error = false;
  2401. if (flags & 0x1)
  2402. type = "reauth request";
  2403. if (flags & 0x2)
  2404. type = "key update request";
  2405. if (flags & 0x6) {
  2406. pairwise_error = true;
  2407. type = "pairwise_error";
  2408. }
  2409. if (flags & 0xe) {
  2410. group_error = true;
  2411. type = "group_error";
  2412. }
  2413. netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
  2414. type, le32_to_cpu(auth_req->flags));
  2415. if (pairwise_error) {
  2416. key_type = NL80211_KEYTYPE_PAIRWISE;
  2417. key_id = -1;
  2418. cfg80211_michael_mic_failure(usbdev->net,
  2419. auth_req->bssid,
  2420. key_type, key_id, NULL,
  2421. GFP_KERNEL);
  2422. }
  2423. if (group_error) {
  2424. key_type = NL80211_KEYTYPE_GROUP;
  2425. key_id = -1;
  2426. cfg80211_michael_mic_failure(usbdev->net,
  2427. auth_req->bssid,
  2428. key_type, key_id, NULL,
  2429. GFP_KERNEL);
  2430. }
  2431. buflen -= le32_to_cpu(auth_req->length);
  2432. buf += le32_to_cpu(auth_req->length);
  2433. }
  2434. }
  2435. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  2436. struct ndis_80211_status_indication *indication,
  2437. int len)
  2438. {
  2439. struct ndis_80211_pmkid_cand_list *cand_list;
  2440. int list_len, expected_len, i;
  2441. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2442. sizeof(struct ndis_80211_pmkid_cand_list)) {
  2443. netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
  2444. len);
  2445. return;
  2446. }
  2447. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  2448. sizeof(struct ndis_80211_pmkid_candidate);
  2449. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  2450. offsetof(struct ndis_80211_status_indication, u);
  2451. if (len < expected_len) {
  2452. netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
  2453. len, expected_len);
  2454. return;
  2455. }
  2456. cand_list = &indication->u.cand_list;
  2457. netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
  2458. le32_to_cpu(cand_list->version),
  2459. le32_to_cpu(cand_list->num_candidates));
  2460. if (le32_to_cpu(cand_list->version) != 1)
  2461. return;
  2462. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  2463. struct ndis_80211_pmkid_candidate *cand =
  2464. &cand_list->candidate_list[i];
  2465. bool preauth = !!(cand->flags & NDIS_80211_PMKID_CAND_PREAUTH);
  2466. netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, preauth: %d, bssid: %pM\n",
  2467. i, le32_to_cpu(cand->flags), preauth, cand->bssid);
  2468. cfg80211_pmksa_candidate_notify(usbdev->net, i, cand->bssid,
  2469. preauth, GFP_ATOMIC);
  2470. }
  2471. }
  2472. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  2473. struct rndis_indicate *msg, int buflen)
  2474. {
  2475. struct ndis_80211_status_indication *indication;
  2476. unsigned int len, offset;
  2477. offset = offsetof(struct rndis_indicate, status) +
  2478. le32_to_cpu(msg->offset);
  2479. len = le32_to_cpu(msg->length);
  2480. if (len < 8) {
  2481. netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
  2482. len);
  2483. return;
  2484. }
  2485. if (len > buflen || offset > buflen || offset + len > buflen) {
  2486. netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
  2487. offset + len, buflen);
  2488. return;
  2489. }
  2490. indication = (void *)((u8 *)msg + offset);
  2491. switch (le32_to_cpu(indication->status_type)) {
  2492. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  2493. netdev_info(usbdev->net, "radio state indication: %i\n",
  2494. le32_to_cpu(indication->u.radio_status));
  2495. return;
  2496. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  2497. netdev_info(usbdev->net, "media stream mode indication: %i\n",
  2498. le32_to_cpu(indication->u.media_stream_mode));
  2499. return;
  2500. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  2501. rndis_wlan_auth_indication(usbdev, indication, len);
  2502. return;
  2503. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2504. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2505. return;
  2506. default:
  2507. netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
  2508. le32_to_cpu(indication->status_type));
  2509. }
  2510. }
  2511. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2512. {
  2513. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2514. struct rndis_indicate *msg = ind;
  2515. switch (le32_to_cpu(msg->status)) {
  2516. case RNDIS_STATUS_MEDIA_CONNECT:
  2517. if (priv->current_command_oid == RNDIS_OID_802_11_ADD_KEY) {
  2518. /* RNDIS_OID_802_11_ADD_KEY causes sometimes extra
  2519. * "media connect" indications which confuses driver
  2520. * and userspace to think that device is
  2521. * roaming/reassociating when it isn't.
  2522. */
  2523. netdev_dbg(usbdev->net, "ignored RNDIS_OID_802_11_ADD_KEY triggered 'media connect'\n");
  2524. return;
  2525. }
  2526. usbnet_pause_rx(usbdev);
  2527. netdev_info(usbdev->net, "media connect\n");
  2528. /* queue work to avoid recursive calls into rndis_command */
  2529. set_bit(WORK_LINK_UP, &priv->work_pending);
  2530. queue_work(priv->workqueue, &priv->work);
  2531. break;
  2532. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2533. netdev_info(usbdev->net, "media disconnect\n");
  2534. /* queue work to avoid recursive calls into rndis_command */
  2535. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2536. queue_work(priv->workqueue, &priv->work);
  2537. break;
  2538. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2539. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2540. break;
  2541. default:
  2542. netdev_info(usbdev->net, "indication: 0x%08x\n",
  2543. le32_to_cpu(msg->status));
  2544. break;
  2545. }
  2546. }
  2547. static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
  2548. {
  2549. struct {
  2550. __le32 num_items;
  2551. __le32 items[8];
  2552. } networks_supported;
  2553. struct ndis_80211_capability *caps;
  2554. u8 caps_buf[sizeof(*caps) + sizeof(caps->auth_encr_pair) * 16];
  2555. int len, retval, i, n;
  2556. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2557. /* determine supported modes */
  2558. len = sizeof(networks_supported);
  2559. retval = rndis_query_oid(usbdev,
  2560. RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED,
  2561. &networks_supported, &len);
  2562. if (retval >= 0) {
  2563. n = le32_to_cpu(networks_supported.num_items);
  2564. if (n > 8)
  2565. n = 8;
  2566. for (i = 0; i < n; i++) {
  2567. switch (le32_to_cpu(networks_supported.items[i])) {
  2568. case NDIS_80211_TYPE_FREQ_HOP:
  2569. case NDIS_80211_TYPE_DIRECT_SEQ:
  2570. priv->caps |= CAP_MODE_80211B;
  2571. break;
  2572. case NDIS_80211_TYPE_OFDM_A:
  2573. priv->caps |= CAP_MODE_80211A;
  2574. break;
  2575. case NDIS_80211_TYPE_OFDM_G:
  2576. priv->caps |= CAP_MODE_80211G;
  2577. break;
  2578. }
  2579. }
  2580. }
  2581. /* get device 802.11 capabilities, number of PMKIDs */
  2582. caps = (struct ndis_80211_capability *)caps_buf;
  2583. len = sizeof(caps_buf);
  2584. retval = rndis_query_oid(usbdev,
  2585. RNDIS_OID_802_11_CAPABILITY,
  2586. caps, &len);
  2587. if (retval >= 0) {
  2588. netdev_dbg(usbdev->net, "RNDIS_OID_802_11_CAPABILITY -> len %d, "
  2589. "ver %d, pmkids %d, auth-encr-pairs %d\n",
  2590. le32_to_cpu(caps->length),
  2591. le32_to_cpu(caps->version),
  2592. le32_to_cpu(caps->num_pmkids),
  2593. le32_to_cpu(caps->num_auth_encr_pair));
  2594. wiphy->max_num_pmkids = le32_to_cpu(caps->num_pmkids);
  2595. } else
  2596. wiphy->max_num_pmkids = 0;
  2597. return retval;
  2598. }
  2599. static void rndis_do_cqm(struct usbnet *usbdev, s32 rssi)
  2600. {
  2601. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2602. enum nl80211_cqm_rssi_threshold_event event;
  2603. int thold, hyst, last_event;
  2604. if (priv->cqm_rssi_thold >= 0 || rssi >= 0)
  2605. return;
  2606. if (priv->infra_mode != NDIS_80211_INFRA_INFRA)
  2607. return;
  2608. last_event = priv->last_cqm_event_rssi;
  2609. thold = priv->cqm_rssi_thold;
  2610. hyst = priv->cqm_rssi_hyst;
  2611. if (rssi < thold && (last_event == 0 || rssi < last_event - hyst))
  2612. event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
  2613. else if (rssi > thold && (last_event == 0 || rssi > last_event + hyst))
  2614. event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
  2615. else
  2616. return;
  2617. priv->last_cqm_event_rssi = rssi;
  2618. cfg80211_cqm_rssi_notify(usbdev->net, event, GFP_KERNEL);
  2619. }
  2620. #define DEVICE_POLLER_JIFFIES (HZ)
  2621. static void rndis_device_poller(struct work_struct *work)
  2622. {
  2623. struct rndis_wlan_private *priv =
  2624. container_of(work, struct rndis_wlan_private,
  2625. dev_poller_work.work);
  2626. struct usbnet *usbdev = priv->usbdev;
  2627. __le32 rssi, tmp;
  2628. int len, ret, j;
  2629. int update_jiffies = DEVICE_POLLER_JIFFIES;
  2630. void *buf;
  2631. /* Only check/do workaround when connected. Calling is_associated()
  2632. * also polls device with rndis_command() and catches for media link
  2633. * indications.
  2634. */
  2635. if (!is_associated(usbdev)) {
  2636. /* Workaround bad scanning in BCM4320a devices with active
  2637. * background scanning when not associated.
  2638. */
  2639. if (priv->device_type == RNDIS_BCM4320A && priv->radio_on &&
  2640. !priv->scan_request) {
  2641. /* Get previous scan results */
  2642. rndis_check_bssid_list(usbdev, NULL, NULL);
  2643. /* Initiate new scan */
  2644. rndis_start_bssid_list_scan(usbdev);
  2645. }
  2646. goto end;
  2647. }
  2648. len = sizeof(rssi);
  2649. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
  2650. &rssi, &len);
  2651. if (ret == 0) {
  2652. priv->last_qual = level_to_qual(le32_to_cpu(rssi));
  2653. rndis_do_cqm(usbdev, le32_to_cpu(rssi));
  2654. }
  2655. netdev_dbg(usbdev->net, "dev-poller: RNDIS_OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
  2656. ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
  2657. /* Workaround transfer stalls on poor quality links.
  2658. * TODO: find right way to fix these stalls (as stalls do not happen
  2659. * with ndiswrapper/windows driver). */
  2660. if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
  2661. /* Decrease stats worker interval to catch stalls.
  2662. * faster. Faster than 400-500ms causes packet loss,
  2663. * Slower doesn't catch stalls fast enough.
  2664. */
  2665. j = msecs_to_jiffies(priv->param_workaround_interval);
  2666. if (j > DEVICE_POLLER_JIFFIES)
  2667. j = DEVICE_POLLER_JIFFIES;
  2668. else if (j <= 0)
  2669. j = 1;
  2670. update_jiffies = j;
  2671. /* Send scan OID. Use of both OIDs is required to get device
  2672. * working.
  2673. */
  2674. tmp = cpu_to_le32(1);
  2675. rndis_set_oid(usbdev,
  2676. RNDIS_OID_802_11_BSSID_LIST_SCAN,
  2677. &tmp, sizeof(tmp));
  2678. len = CONTROL_BUFFER_SIZE;
  2679. buf = kmalloc(len, GFP_KERNEL);
  2680. if (!buf)
  2681. goto end;
  2682. rndis_query_oid(usbdev,
  2683. RNDIS_OID_802_11_BSSID_LIST,
  2684. buf, &len);
  2685. kfree(buf);
  2686. }
  2687. end:
  2688. if (update_jiffies >= HZ)
  2689. update_jiffies = round_jiffies_relative(update_jiffies);
  2690. else {
  2691. j = round_jiffies_relative(update_jiffies);
  2692. if (abs(j - update_jiffies) <= 10)
  2693. update_jiffies = j;
  2694. }
  2695. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2696. update_jiffies);
  2697. }
  2698. /*
  2699. * driver/device initialization
  2700. */
  2701. static void rndis_copy_module_params(struct usbnet *usbdev, int device_type)
  2702. {
  2703. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2704. priv->device_type = device_type;
  2705. priv->param_country[0] = modparam_country[0];
  2706. priv->param_country[1] = modparam_country[1];
  2707. priv->param_country[2] = 0;
  2708. priv->param_frameburst = modparam_frameburst;
  2709. priv->param_afterburner = modparam_afterburner;
  2710. priv->param_power_save = modparam_power_save;
  2711. priv->param_power_output = modparam_power_output;
  2712. priv->param_roamtrigger = modparam_roamtrigger;
  2713. priv->param_roamdelta = modparam_roamdelta;
  2714. priv->param_country[0] = toupper(priv->param_country[0]);
  2715. priv->param_country[1] = toupper(priv->param_country[1]);
  2716. /* doesn't support EU as country code, use FI instead */
  2717. if (!strcmp(priv->param_country, "EU"))
  2718. strcpy(priv->param_country, "FI");
  2719. if (priv->param_power_save < 0)
  2720. priv->param_power_save = 0;
  2721. else if (priv->param_power_save > 2)
  2722. priv->param_power_save = 2;
  2723. if (priv->param_power_output < 0)
  2724. priv->param_power_output = 0;
  2725. else if (priv->param_power_output > 3)
  2726. priv->param_power_output = 3;
  2727. if (priv->param_roamtrigger < -80)
  2728. priv->param_roamtrigger = -80;
  2729. else if (priv->param_roamtrigger > -60)
  2730. priv->param_roamtrigger = -60;
  2731. if (priv->param_roamdelta < 0)
  2732. priv->param_roamdelta = 0;
  2733. else if (priv->param_roamdelta > 2)
  2734. priv->param_roamdelta = 2;
  2735. if (modparam_workaround_interval < 0)
  2736. priv->param_workaround_interval = 500;
  2737. else
  2738. priv->param_workaround_interval = modparam_workaround_interval;
  2739. }
  2740. static int unknown_early_init(struct usbnet *usbdev)
  2741. {
  2742. /* copy module parameters for unknown so that iwconfig reports txpower
  2743. * and workaround parameter is copied to private structure correctly.
  2744. */
  2745. rndis_copy_module_params(usbdev, RNDIS_UNKNOWN);
  2746. /* This is unknown device, so do not try set configuration parameters.
  2747. */
  2748. return 0;
  2749. }
  2750. static int bcm4320a_early_init(struct usbnet *usbdev)
  2751. {
  2752. /* copy module parameters for bcm4320a so that iwconfig reports txpower
  2753. * and workaround parameter is copied to private structure correctly.
  2754. */
  2755. rndis_copy_module_params(usbdev, RNDIS_BCM4320A);
  2756. /* bcm4320a doesn't handle configuration parameters well. Try
  2757. * set any and you get partially zeroed mac and broken device.
  2758. */
  2759. return 0;
  2760. }
  2761. static int bcm4320b_early_init(struct usbnet *usbdev)
  2762. {
  2763. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2764. char buf[8];
  2765. rndis_copy_module_params(usbdev, RNDIS_BCM4320B);
  2766. /* Early initialization settings, setting these won't have effect
  2767. * if called after generic_rndis_bind().
  2768. */
  2769. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2770. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2771. priv->param_frameburst ? "1" : "0");
  2772. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2773. priv->param_afterburner ? "1" : "0");
  2774. sprintf(buf, "%d", priv->param_power_save);
  2775. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2776. sprintf(buf, "%d", priv->param_power_output);
  2777. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2778. sprintf(buf, "%d", priv->param_roamtrigger);
  2779. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2780. sprintf(buf, "%d", priv->param_roamdelta);
  2781. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2782. return 0;
  2783. }
  2784. /* same as rndis_netdev_ops but with local multicast handler */
  2785. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2786. .ndo_open = usbnet_open,
  2787. .ndo_stop = usbnet_stop,
  2788. .ndo_start_xmit = usbnet_start_xmit,
  2789. .ndo_tx_timeout = usbnet_tx_timeout,
  2790. .ndo_set_mac_address = eth_mac_addr,
  2791. .ndo_validate_addr = eth_validate_addr,
  2792. .ndo_set_rx_mode = rndis_wlan_set_multicast_list,
  2793. };
  2794. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2795. {
  2796. struct wiphy *wiphy;
  2797. struct rndis_wlan_private *priv;
  2798. int retval, len;
  2799. __le32 tmp;
  2800. /* allocate wiphy and rndis private data
  2801. * NOTE: We only support a single virtual interface, so wiphy
  2802. * and wireless_dev are somewhat synonymous for this device.
  2803. */
  2804. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2805. if (!wiphy)
  2806. return -ENOMEM;
  2807. priv = wiphy_priv(wiphy);
  2808. usbdev->net->ieee80211_ptr = &priv->wdev;
  2809. priv->wdev.wiphy = wiphy;
  2810. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2811. /* These have to be initialized before calling generic_rndis_bind().
  2812. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2813. */
  2814. usbdev->driver_priv = priv;
  2815. priv->usbdev = usbdev;
  2816. mutex_init(&priv->command_lock);
  2817. /* because rndis_command() sleeps we need to use workqueue */
  2818. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2819. INIT_WORK(&priv->work, rndis_wlan_worker);
  2820. INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
  2821. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2822. /* try bind rndis_host */
  2823. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2824. if (retval < 0)
  2825. goto fail;
  2826. /* generic_rndis_bind set packet filter to multicast_all+
  2827. * promisc mode which doesn't work well for our devices (device
  2828. * picks up rssi to closest station instead of to access point).
  2829. *
  2830. * rndis_host wants to avoid all OID as much as possible
  2831. * so do promisc/multicast handling in rndis_wlan.
  2832. */
  2833. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2834. tmp = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST);
  2835. retval = rndis_set_oid(usbdev,
  2836. RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
  2837. &tmp, sizeof(tmp));
  2838. len = sizeof(tmp);
  2839. retval = rndis_query_oid(usbdev,
  2840. RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
  2841. &tmp, &len);
  2842. priv->multicast_size = le32_to_cpu(tmp);
  2843. if (retval < 0 || priv->multicast_size < 0)
  2844. priv->multicast_size = 0;
  2845. if (priv->multicast_size > 0)
  2846. usbdev->net->flags |= IFF_MULTICAST;
  2847. else
  2848. usbdev->net->flags &= ~IFF_MULTICAST;
  2849. /* fill-out wiphy structure and register w/ cfg80211 */
  2850. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2851. wiphy->privid = rndis_wiphy_privid;
  2852. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2853. | BIT(NL80211_IFTYPE_ADHOC);
  2854. wiphy->max_scan_ssids = 1;
  2855. /* TODO: fill-out band/encr information based on priv->caps */
  2856. rndis_wlan_get_caps(usbdev, wiphy);
  2857. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2858. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2859. priv->band.channels = priv->channels;
  2860. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2861. priv->band.bitrates = priv->rates;
  2862. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2863. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2864. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2865. memcpy(priv->cipher_suites, rndis_cipher_suites,
  2866. sizeof(rndis_cipher_suites));
  2867. wiphy->cipher_suites = priv->cipher_suites;
  2868. wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
  2869. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2870. if (wiphy_register(wiphy)) {
  2871. retval = -ENODEV;
  2872. goto fail;
  2873. }
  2874. set_default_iw_params(usbdev);
  2875. priv->power_mode = -1;
  2876. /* set default rts/frag */
  2877. rndis_set_wiphy_params(wiphy,
  2878. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2879. /* turn radio off on init */
  2880. priv->radio_on = false;
  2881. disassociate(usbdev, false);
  2882. netif_carrier_off(usbdev->net);
  2883. return 0;
  2884. fail:
  2885. cancel_delayed_work_sync(&priv->dev_poller_work);
  2886. cancel_delayed_work_sync(&priv->scan_work);
  2887. cancel_work_sync(&priv->work);
  2888. flush_workqueue(priv->workqueue);
  2889. destroy_workqueue(priv->workqueue);
  2890. wiphy_free(wiphy);
  2891. return retval;
  2892. }
  2893. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2894. {
  2895. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2896. /* turn radio off */
  2897. disassociate(usbdev, false);
  2898. cancel_delayed_work_sync(&priv->dev_poller_work);
  2899. cancel_delayed_work_sync(&priv->scan_work);
  2900. cancel_work_sync(&priv->work);
  2901. flush_workqueue(priv->workqueue);
  2902. destroy_workqueue(priv->workqueue);
  2903. rndis_unbind(usbdev, intf);
  2904. wiphy_unregister(priv->wdev.wiphy);
  2905. wiphy_free(priv->wdev.wiphy);
  2906. }
  2907. static int rndis_wlan_reset(struct usbnet *usbdev)
  2908. {
  2909. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2910. int retval;
  2911. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2912. retval = rndis_reset(usbdev);
  2913. if (retval)
  2914. netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
  2915. /* rndis_reset cleared multicast list, so restore here.
  2916. (set_multicast_list() also turns on current packet filter) */
  2917. set_multicast_list(usbdev);
  2918. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2919. round_jiffies_relative(DEVICE_POLLER_JIFFIES));
  2920. return deauthenticate(usbdev);
  2921. }
  2922. static int rndis_wlan_stop(struct usbnet *usbdev)
  2923. {
  2924. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2925. int retval;
  2926. __le32 filter;
  2927. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2928. retval = disassociate(usbdev, false);
  2929. priv->work_pending = 0;
  2930. cancel_delayed_work_sync(&priv->dev_poller_work);
  2931. cancel_delayed_work_sync(&priv->scan_work);
  2932. cancel_work_sync(&priv->work);
  2933. flush_workqueue(priv->workqueue);
  2934. if (priv->scan_request) {
  2935. cfg80211_scan_done(priv->scan_request, true);
  2936. priv->scan_request = NULL;
  2937. }
  2938. /* Set current packet filter zero to block receiving data packets from
  2939. device. */
  2940. filter = 0;
  2941. rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2942. sizeof(filter));
  2943. return retval;
  2944. }
  2945. static const struct driver_info bcm4320b_info = {
  2946. .description = "Wireless RNDIS device, BCM4320b based",
  2947. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2948. FLAG_AVOID_UNLINK_URBS,
  2949. .bind = rndis_wlan_bind,
  2950. .unbind = rndis_wlan_unbind,
  2951. .status = rndis_status,
  2952. .rx_fixup = rndis_rx_fixup,
  2953. .tx_fixup = rndis_tx_fixup,
  2954. .reset = rndis_wlan_reset,
  2955. .stop = rndis_wlan_stop,
  2956. .early_init = bcm4320b_early_init,
  2957. .indication = rndis_wlan_indication,
  2958. };
  2959. static const struct driver_info bcm4320a_info = {
  2960. .description = "Wireless RNDIS device, BCM4320a based",
  2961. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2962. FLAG_AVOID_UNLINK_URBS,
  2963. .bind = rndis_wlan_bind,
  2964. .unbind = rndis_wlan_unbind,
  2965. .status = rndis_status,
  2966. .rx_fixup = rndis_rx_fixup,
  2967. .tx_fixup = rndis_tx_fixup,
  2968. .reset = rndis_wlan_reset,
  2969. .stop = rndis_wlan_stop,
  2970. .early_init = bcm4320a_early_init,
  2971. .indication = rndis_wlan_indication,
  2972. };
  2973. static const struct driver_info rndis_wlan_info = {
  2974. .description = "Wireless RNDIS device",
  2975. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2976. FLAG_AVOID_UNLINK_URBS,
  2977. .bind = rndis_wlan_bind,
  2978. .unbind = rndis_wlan_unbind,
  2979. .status = rndis_status,
  2980. .rx_fixup = rndis_rx_fixup,
  2981. .tx_fixup = rndis_tx_fixup,
  2982. .reset = rndis_wlan_reset,
  2983. .stop = rndis_wlan_stop,
  2984. .early_init = unknown_early_init,
  2985. .indication = rndis_wlan_indication,
  2986. };
  2987. /*-------------------------------------------------------------------------*/
  2988. static const struct usb_device_id products [] = {
  2989. #define RNDIS_MASTER_INTERFACE \
  2990. .bInterfaceClass = USB_CLASS_COMM, \
  2991. .bInterfaceSubClass = 2 /* ACM */, \
  2992. .bInterfaceProtocol = 0x0ff
  2993. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2994. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  2995. */
  2996. {
  2997. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2998. | USB_DEVICE_ID_MATCH_DEVICE,
  2999. .idVendor = 0x0411,
  3000. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  3001. RNDIS_MASTER_INTERFACE,
  3002. .driver_info = (unsigned long) &bcm4320b_info,
  3003. }, {
  3004. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3005. | USB_DEVICE_ID_MATCH_DEVICE,
  3006. .idVendor = 0x0baf,
  3007. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  3008. RNDIS_MASTER_INTERFACE,
  3009. .driver_info = (unsigned long) &bcm4320b_info,
  3010. }, {
  3011. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3012. | USB_DEVICE_ID_MATCH_DEVICE,
  3013. .idVendor = 0x050d,
  3014. .idProduct = 0x011b, /* Belkin F5D7051 */
  3015. RNDIS_MASTER_INTERFACE,
  3016. .driver_info = (unsigned long) &bcm4320b_info,
  3017. }, {
  3018. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3019. | USB_DEVICE_ID_MATCH_DEVICE,
  3020. .idVendor = 0x1799, /* Belkin has two vendor ids */
  3021. .idProduct = 0x011b, /* Belkin F5D7051 */
  3022. RNDIS_MASTER_INTERFACE,
  3023. .driver_info = (unsigned long) &bcm4320b_info,
  3024. }, {
  3025. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3026. | USB_DEVICE_ID_MATCH_DEVICE,
  3027. .idVendor = 0x13b1,
  3028. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  3029. RNDIS_MASTER_INTERFACE,
  3030. .driver_info = (unsigned long) &bcm4320b_info,
  3031. }, {
  3032. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3033. | USB_DEVICE_ID_MATCH_DEVICE,
  3034. .idVendor = 0x13b1,
  3035. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  3036. RNDIS_MASTER_INTERFACE,
  3037. .driver_info = (unsigned long) &bcm4320b_info,
  3038. }, {
  3039. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3040. | USB_DEVICE_ID_MATCH_DEVICE,
  3041. .idVendor = 0x0b05,
  3042. .idProduct = 0x1717, /* Asus WL169gE */
  3043. RNDIS_MASTER_INTERFACE,
  3044. .driver_info = (unsigned long) &bcm4320b_info,
  3045. }, {
  3046. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3047. | USB_DEVICE_ID_MATCH_DEVICE,
  3048. .idVendor = 0x0a5c,
  3049. .idProduct = 0xd11b, /* Eminent EM4045 */
  3050. RNDIS_MASTER_INTERFACE,
  3051. .driver_info = (unsigned long) &bcm4320b_info,
  3052. }, {
  3053. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3054. | USB_DEVICE_ID_MATCH_DEVICE,
  3055. .idVendor = 0x1690,
  3056. .idProduct = 0x0715, /* BT Voyager 1055 */
  3057. RNDIS_MASTER_INTERFACE,
  3058. .driver_info = (unsigned long) &bcm4320b_info,
  3059. },
  3060. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  3061. * parameters available, hardware probably contain older firmware version with
  3062. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  3063. */
  3064. {
  3065. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3066. | USB_DEVICE_ID_MATCH_DEVICE,
  3067. .idVendor = 0x13b1,
  3068. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  3069. RNDIS_MASTER_INTERFACE,
  3070. .driver_info = (unsigned long) &bcm4320a_info,
  3071. }, {
  3072. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3073. | USB_DEVICE_ID_MATCH_DEVICE,
  3074. .idVendor = 0x0baf,
  3075. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  3076. RNDIS_MASTER_INTERFACE,
  3077. .driver_info = (unsigned long) &bcm4320a_info,
  3078. }, {
  3079. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3080. | USB_DEVICE_ID_MATCH_DEVICE,
  3081. .idVendor = 0x0411,
  3082. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  3083. RNDIS_MASTER_INTERFACE,
  3084. .driver_info = (unsigned long) &bcm4320a_info,
  3085. },
  3086. /* Generic Wireless RNDIS devices that we don't have exact
  3087. * idVendor/idProduct/chip yet.
  3088. */
  3089. {
  3090. /* RNDIS is MSFT's un-official variant of CDC ACM */
  3091. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  3092. .driver_info = (unsigned long) &rndis_wlan_info,
  3093. }, {
  3094. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  3095. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  3096. .driver_info = (unsigned long) &rndis_wlan_info,
  3097. },
  3098. { }, // END
  3099. };
  3100. MODULE_DEVICE_TABLE(usb, products);
  3101. static struct usb_driver rndis_wlan_driver = {
  3102. .name = "rndis_wlan",
  3103. .id_table = products,
  3104. .probe = usbnet_probe,
  3105. .disconnect = usbnet_disconnect,
  3106. .suspend = usbnet_suspend,
  3107. .resume = usbnet_resume,
  3108. .disable_hub_initiated_lpm = 1,
  3109. };
  3110. module_usb_driver(rndis_wlan_driver);
  3111. MODULE_AUTHOR("Bjorge Dijkstra");
  3112. MODULE_AUTHOR("Jussi Kivilinna");
  3113. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  3114. MODULE_LICENSE("GPL");