rndis_wlan.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769
  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[ETH_ALEN];
  173. u8 padding[2];
  174. __le32 flags;
  175. } __packed;
  176. struct ndis_80211_pmkid_candidate {
  177. u8 bssid[ETH_ALEN];
  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[ETH_ALEN];
  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[ETH_ALEN];
  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[ETH_ALEN];
  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[ETH_ALEN];
  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[ETH_ALEN];
  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 0
  310. #define WORK_LINK_DOWN 1
  311. #define WORK_SET_MULTICAST_LIST 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. const 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. eth_zero_addr(bssid);
  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 = 1000 *
  1088. ieee80211_channel_to_frequency(channel, IEEE80211_BAND_2GHZ);
  1089. len = sizeof(config);
  1090. ret = rndis_query_oid(usbdev,
  1091. RNDIS_OID_802_11_CONFIGURATION,
  1092. &config, &len);
  1093. if (ret < 0) {
  1094. netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
  1095. __func__);
  1096. return ret;
  1097. }
  1098. config.ds_config = cpu_to_le32(dsconfig);
  1099. ret = rndis_set_oid(usbdev,
  1100. RNDIS_OID_802_11_CONFIGURATION,
  1101. &config, sizeof(config));
  1102. netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
  1103. return ret;
  1104. }
  1105. static struct ieee80211_channel *get_current_channel(struct usbnet *usbdev,
  1106. u32 *beacon_period)
  1107. {
  1108. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1109. struct ieee80211_channel *channel;
  1110. struct ndis_80211_conf config;
  1111. int len, ret;
  1112. /* Get channel and beacon interval */
  1113. len = sizeof(config);
  1114. ret = rndis_query_oid(usbdev,
  1115. RNDIS_OID_802_11_CONFIGURATION,
  1116. &config, &len);
  1117. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_CONFIGURATION -> %d\n",
  1118. __func__, ret);
  1119. if (ret < 0)
  1120. return NULL;
  1121. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1122. KHZ_TO_MHZ(le32_to_cpu(config.ds_config)));
  1123. if (!channel)
  1124. return NULL;
  1125. if (beacon_period)
  1126. *beacon_period = le32_to_cpu(config.beacon_period);
  1127. return channel;
  1128. }
  1129. /* index must be 0 - N, as per NDIS */
  1130. static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1131. u8 index)
  1132. {
  1133. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1134. struct ndis_80211_wep_key ndis_key;
  1135. u32 cipher;
  1136. int ret;
  1137. netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
  1138. __func__, index, key_len);
  1139. if (index >= RNDIS_WLAN_NUM_KEYS)
  1140. return -EINVAL;
  1141. if (key_len == 5)
  1142. cipher = WLAN_CIPHER_SUITE_WEP40;
  1143. else if (key_len == 13)
  1144. cipher = WLAN_CIPHER_SUITE_WEP104;
  1145. else
  1146. return -EINVAL;
  1147. memset(&ndis_key, 0, sizeof(ndis_key));
  1148. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1149. ndis_key.length = cpu_to_le32(key_len);
  1150. ndis_key.index = cpu_to_le32(index);
  1151. memcpy(&ndis_key.material, key, key_len);
  1152. if (index == priv->encr_tx_key_index) {
  1153. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1154. ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
  1155. RNDIS_WLAN_ALG_NONE);
  1156. if (ret)
  1157. netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
  1158. ret);
  1159. }
  1160. ret = rndis_set_oid(usbdev,
  1161. RNDIS_OID_802_11_ADD_WEP, &ndis_key,
  1162. sizeof(ndis_key));
  1163. if (ret != 0) {
  1164. netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
  1165. index + 1, ret);
  1166. return ret;
  1167. }
  1168. priv->encr_keys[index].len = key_len;
  1169. priv->encr_keys[index].cipher = cipher;
  1170. memcpy(&priv->encr_keys[index].material, key, key_len);
  1171. eth_broadcast_addr(priv->encr_keys[index].bssid);
  1172. return 0;
  1173. }
  1174. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1175. u8 index, const u8 *addr, const u8 *rx_seq,
  1176. int seq_len, u32 cipher, __le32 flags)
  1177. {
  1178. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1179. struct ndis_80211_key ndis_key;
  1180. bool is_addr_ok;
  1181. int ret;
  1182. if (index >= RNDIS_WLAN_NUM_KEYS) {
  1183. netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
  1184. __func__, index);
  1185. return -EINVAL;
  1186. }
  1187. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1188. netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
  1189. __func__, key_len);
  1190. return -EINVAL;
  1191. }
  1192. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
  1193. if (!rx_seq || seq_len <= 0) {
  1194. netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
  1195. __func__);
  1196. return -EINVAL;
  1197. }
  1198. if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
  1199. netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
  1200. return -EINVAL;
  1201. }
  1202. }
  1203. is_addr_ok = addr && !is_zero_ether_addr(addr) &&
  1204. !is_broadcast_ether_addr(addr);
  1205. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1206. netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
  1207. __func__, addr);
  1208. return -EINVAL;
  1209. }
  1210. netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
  1211. __func__, index,
  1212. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1213. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1214. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1215. memset(&ndis_key, 0, sizeof(ndis_key));
  1216. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1217. sizeof(ndis_key.material) + key_len);
  1218. ndis_key.length = cpu_to_le32(key_len);
  1219. ndis_key.index = cpu_to_le32(index) | flags;
  1220. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1221. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1222. * different order than NDIS spec, so swap the order here. */
  1223. memcpy(ndis_key.material, key, 16);
  1224. memcpy(ndis_key.material + 16, key + 24, 8);
  1225. memcpy(ndis_key.material + 24, key + 16, 8);
  1226. } else
  1227. memcpy(ndis_key.material, key, key_len);
  1228. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1229. memcpy(ndis_key.rsc, rx_seq, seq_len);
  1230. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1231. /* pairwise key */
  1232. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1233. } else {
  1234. /* group key */
  1235. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1236. eth_broadcast_addr(ndis_key.bssid);
  1237. else
  1238. get_bssid(usbdev, ndis_key.bssid);
  1239. }
  1240. ret = rndis_set_oid(usbdev,
  1241. RNDIS_OID_802_11_ADD_KEY, &ndis_key,
  1242. le32_to_cpu(ndis_key.size));
  1243. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_ADD_KEY -> %08X\n",
  1244. __func__, ret);
  1245. if (ret != 0)
  1246. return ret;
  1247. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1248. priv->encr_keys[index].len = key_len;
  1249. priv->encr_keys[index].cipher = cipher;
  1250. memcpy(&priv->encr_keys[index].material, key, key_len);
  1251. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1252. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1253. else
  1254. eth_broadcast_addr(priv->encr_keys[index].bssid);
  1255. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1256. priv->encr_tx_key_index = index;
  1257. return 0;
  1258. }
  1259. static int restore_key(struct usbnet *usbdev, u8 key_idx)
  1260. {
  1261. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1262. struct rndis_wlan_encr_key key;
  1263. if (is_wpa_key(priv, key_idx))
  1264. return 0;
  1265. key = priv->encr_keys[key_idx];
  1266. netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
  1267. if (key.len == 0)
  1268. return 0;
  1269. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1270. }
  1271. static void restore_keys(struct usbnet *usbdev)
  1272. {
  1273. int i;
  1274. for (i = 0; i < 4; i++)
  1275. restore_key(usbdev, i);
  1276. }
  1277. static void clear_key(struct rndis_wlan_private *priv, u8 idx)
  1278. {
  1279. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1280. }
  1281. /* remove_key is for both wep and wpa */
  1282. static int remove_key(struct usbnet *usbdev, u8 index, const u8 *bssid)
  1283. {
  1284. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1285. struct ndis_80211_remove_key remove_key;
  1286. __le32 keyindex;
  1287. bool is_wpa;
  1288. int ret;
  1289. if (index >= RNDIS_WLAN_NUM_KEYS)
  1290. return -ENOENT;
  1291. if (priv->encr_keys[index].len == 0)
  1292. return 0;
  1293. is_wpa = is_wpa_key(priv, index);
  1294. netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
  1295. __func__, index, is_wpa ? "wpa" : "wep",
  1296. priv->encr_keys[index].len);
  1297. clear_key(priv, index);
  1298. if (is_wpa) {
  1299. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1300. remove_key.index = cpu_to_le32(index);
  1301. if (bssid) {
  1302. /* pairwise key */
  1303. if (!is_broadcast_ether_addr(bssid))
  1304. remove_key.index |=
  1305. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1306. memcpy(remove_key.bssid, bssid,
  1307. sizeof(remove_key.bssid));
  1308. } else
  1309. memset(remove_key.bssid, 0xff,
  1310. sizeof(remove_key.bssid));
  1311. ret = rndis_set_oid(usbdev,
  1312. RNDIS_OID_802_11_REMOVE_KEY,
  1313. &remove_key, sizeof(remove_key));
  1314. if (ret != 0)
  1315. return ret;
  1316. } else {
  1317. keyindex = cpu_to_le32(index);
  1318. ret = rndis_set_oid(usbdev,
  1319. RNDIS_OID_802_11_REMOVE_WEP,
  1320. &keyindex, sizeof(keyindex));
  1321. if (ret != 0) {
  1322. netdev_warn(usbdev->net,
  1323. "removing encryption key %d failed (%08X)\n",
  1324. index, ret);
  1325. return ret;
  1326. }
  1327. }
  1328. /* if it is transmit key, disable encryption */
  1329. if (index == priv->encr_tx_key_index)
  1330. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1331. return 0;
  1332. }
  1333. static void set_multicast_list(struct usbnet *usbdev)
  1334. {
  1335. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1336. struct netdev_hw_addr *ha;
  1337. __le32 filter, basefilter;
  1338. int ret;
  1339. char *mc_addrs = NULL;
  1340. int mc_count;
  1341. basefilter = filter = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED |
  1342. RNDIS_PACKET_TYPE_BROADCAST);
  1343. if (usbdev->net->flags & IFF_PROMISC) {
  1344. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_PROMISCUOUS |
  1345. RNDIS_PACKET_TYPE_ALL_LOCAL);
  1346. } else if (usbdev->net->flags & IFF_ALLMULTI) {
  1347. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1348. }
  1349. if (filter != basefilter)
  1350. goto set_filter;
  1351. /*
  1352. * mc_list should be accessed holding the lock, so copy addresses to
  1353. * local buffer first.
  1354. */
  1355. netif_addr_lock_bh(usbdev->net);
  1356. mc_count = netdev_mc_count(usbdev->net);
  1357. if (mc_count > priv->multicast_size) {
  1358. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1359. } else if (mc_count) {
  1360. int i = 0;
  1361. mc_addrs = kmalloc_array(mc_count, ETH_ALEN, GFP_ATOMIC);
  1362. if (!mc_addrs) {
  1363. netif_addr_unlock_bh(usbdev->net);
  1364. return;
  1365. }
  1366. netdev_for_each_mc_addr(ha, usbdev->net)
  1367. memcpy(mc_addrs + i++ * ETH_ALEN,
  1368. ha->addr, ETH_ALEN);
  1369. }
  1370. netif_addr_unlock_bh(usbdev->net);
  1371. if (filter != basefilter)
  1372. goto set_filter;
  1373. if (mc_count) {
  1374. ret = rndis_set_oid(usbdev,
  1375. RNDIS_OID_802_3_MULTICAST_LIST,
  1376. mc_addrs, mc_count * ETH_ALEN);
  1377. kfree(mc_addrs);
  1378. if (ret == 0)
  1379. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_MULTICAST);
  1380. else
  1381. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1382. netdev_dbg(usbdev->net, "RNDIS_OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
  1383. mc_count, priv->multicast_size, ret);
  1384. }
  1385. set_filter:
  1386. ret = rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1387. sizeof(filter));
  1388. if (ret < 0) {
  1389. netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
  1390. le32_to_cpu(filter));
  1391. }
  1392. netdev_dbg(usbdev->net, "RNDIS_OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
  1393. le32_to_cpu(filter), ret);
  1394. }
  1395. #ifdef DEBUG
  1396. static void debug_print_pmkids(struct usbnet *usbdev,
  1397. struct ndis_80211_pmkid *pmkids,
  1398. const char *func_str)
  1399. {
  1400. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1401. int i, len, count, max_pmkids, entry_len;
  1402. max_pmkids = priv->wdev.wiphy->max_num_pmkids;
  1403. len = le32_to_cpu(pmkids->length);
  1404. count = le32_to_cpu(pmkids->bssid_info_count);
  1405. entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
  1406. netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
  1407. "%d)\n", func_str, count, len, entry_len);
  1408. if (count > max_pmkids)
  1409. count = max_pmkids;
  1410. for (i = 0; i < count; i++) {
  1411. u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
  1412. netdev_dbg(usbdev->net, "%s(): bssid: %pM, "
  1413. "pmkid: %08X:%08X:%08X:%08X\n",
  1414. func_str, pmkids->bssid_info[i].bssid,
  1415. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  1416. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  1417. }
  1418. }
  1419. #else
  1420. static void debug_print_pmkids(struct usbnet *usbdev,
  1421. struct ndis_80211_pmkid *pmkids,
  1422. const char *func_str)
  1423. {
  1424. return;
  1425. }
  1426. #endif
  1427. static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
  1428. {
  1429. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1430. struct ndis_80211_pmkid *pmkids;
  1431. int len, ret, max_pmkids;
  1432. max_pmkids = priv->wdev.wiphy->max_num_pmkids;
  1433. len = sizeof(*pmkids) + max_pmkids * sizeof(pmkids->bssid_info[0]);
  1434. pmkids = kzalloc(len, GFP_KERNEL);
  1435. if (!pmkids)
  1436. return ERR_PTR(-ENOMEM);
  1437. pmkids->length = cpu_to_le32(len);
  1438. pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
  1439. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_PMKID,
  1440. pmkids, &len);
  1441. if (ret < 0) {
  1442. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d)"
  1443. " -> %d\n", __func__, len, max_pmkids, ret);
  1444. kfree(pmkids);
  1445. return ERR_PTR(ret);
  1446. }
  1447. if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
  1448. pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
  1449. debug_print_pmkids(usbdev, pmkids, __func__);
  1450. return pmkids;
  1451. }
  1452. static int set_device_pmkids(struct usbnet *usbdev,
  1453. struct ndis_80211_pmkid *pmkids)
  1454. {
  1455. int ret, len, num_pmkids;
  1456. num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
  1457. len = sizeof(*pmkids) + num_pmkids * sizeof(pmkids->bssid_info[0]);
  1458. pmkids->length = cpu_to_le32(len);
  1459. debug_print_pmkids(usbdev, pmkids, __func__);
  1460. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID, pmkids,
  1461. le32_to_cpu(pmkids->length));
  1462. if (ret < 0) {
  1463. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d) -> %d"
  1464. "\n", __func__, len, num_pmkids, ret);
  1465. }
  1466. kfree(pmkids);
  1467. return ret;
  1468. }
  1469. static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
  1470. struct ndis_80211_pmkid *pmkids,
  1471. struct cfg80211_pmksa *pmksa,
  1472. int max_pmkids)
  1473. {
  1474. int i, newlen, err;
  1475. unsigned int count;
  1476. count = le32_to_cpu(pmkids->bssid_info_count);
  1477. if (count > max_pmkids)
  1478. count = max_pmkids;
  1479. for (i = 0; i < count; i++)
  1480. if (ether_addr_equal(pmkids->bssid_info[i].bssid,
  1481. pmksa->bssid))
  1482. break;
  1483. /* pmkid not found */
  1484. if (i == count) {
  1485. netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
  1486. __func__, pmksa->bssid);
  1487. err = -ENOENT;
  1488. goto error;
  1489. }
  1490. for (; i + 1 < count; i++)
  1491. pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
  1492. count--;
  1493. newlen = sizeof(*pmkids) + count * sizeof(pmkids->bssid_info[0]);
  1494. pmkids->length = cpu_to_le32(newlen);
  1495. pmkids->bssid_info_count = cpu_to_le32(count);
  1496. return pmkids;
  1497. error:
  1498. kfree(pmkids);
  1499. return ERR_PTR(err);
  1500. }
  1501. static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
  1502. struct ndis_80211_pmkid *pmkids,
  1503. struct cfg80211_pmksa *pmksa,
  1504. int max_pmkids)
  1505. {
  1506. struct ndis_80211_pmkid *new_pmkids;
  1507. int i, err, newlen;
  1508. unsigned int count;
  1509. count = le32_to_cpu(pmkids->bssid_info_count);
  1510. if (count > max_pmkids)
  1511. count = max_pmkids;
  1512. /* update with new pmkid */
  1513. for (i = 0; i < count; i++) {
  1514. if (!ether_addr_equal(pmkids->bssid_info[i].bssid,
  1515. pmksa->bssid))
  1516. continue;
  1517. memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
  1518. WLAN_PMKID_LEN);
  1519. return pmkids;
  1520. }
  1521. /* out of space, return error */
  1522. if (i == max_pmkids) {
  1523. netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
  1524. err = -ENOSPC;
  1525. goto error;
  1526. }
  1527. /* add new pmkid */
  1528. newlen = sizeof(*pmkids) + (count + 1) * sizeof(pmkids->bssid_info[0]);
  1529. new_pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
  1530. if (!new_pmkids) {
  1531. err = -ENOMEM;
  1532. goto error;
  1533. }
  1534. pmkids = new_pmkids;
  1535. pmkids->length = cpu_to_le32(newlen);
  1536. pmkids->bssid_info_count = cpu_to_le32(count + 1);
  1537. memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
  1538. memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
  1539. return pmkids;
  1540. error:
  1541. kfree(pmkids);
  1542. return ERR_PTR(err);
  1543. }
  1544. /*
  1545. * cfg80211 ops
  1546. */
  1547. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1548. struct net_device *dev,
  1549. enum nl80211_iftype type, u32 *flags,
  1550. struct vif_params *params)
  1551. {
  1552. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1553. struct usbnet *usbdev = priv->usbdev;
  1554. int mode;
  1555. switch (type) {
  1556. case NL80211_IFTYPE_ADHOC:
  1557. mode = NDIS_80211_INFRA_ADHOC;
  1558. break;
  1559. case NL80211_IFTYPE_STATION:
  1560. mode = NDIS_80211_INFRA_INFRA;
  1561. break;
  1562. default:
  1563. return -EINVAL;
  1564. }
  1565. priv->wdev.iftype = type;
  1566. return set_infra_mode(usbdev, mode);
  1567. }
  1568. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1569. {
  1570. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1571. struct usbnet *usbdev = priv->usbdev;
  1572. int err;
  1573. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1574. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1575. if (err < 0)
  1576. return err;
  1577. }
  1578. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1579. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1580. if (err < 0)
  1581. return err;
  1582. }
  1583. return 0;
  1584. }
  1585. static int rndis_set_tx_power(struct wiphy *wiphy,
  1586. struct wireless_dev *wdev,
  1587. enum nl80211_tx_power_setting type,
  1588. int mbm)
  1589. {
  1590. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1591. struct usbnet *usbdev = priv->usbdev;
  1592. netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
  1593. __func__, type, mbm);
  1594. if (mbm < 0 || (mbm % 100))
  1595. return -ENOTSUPP;
  1596. /* Device doesn't support changing txpower after initialization, only
  1597. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1598. * currently used.
  1599. */
  1600. if (type == NL80211_TX_POWER_AUTOMATIC ||
  1601. MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
  1602. if (!priv->radio_on)
  1603. disassociate(usbdev, true); /* turn on radio */
  1604. return 0;
  1605. }
  1606. return -ENOTSUPP;
  1607. }
  1608. static int rndis_get_tx_power(struct wiphy *wiphy,
  1609. struct wireless_dev *wdev,
  1610. int *dbm)
  1611. {
  1612. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1613. struct usbnet *usbdev = priv->usbdev;
  1614. *dbm = get_bcm4320_power_dbm(priv);
  1615. netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
  1616. return 0;
  1617. }
  1618. #define SCAN_DELAY_JIFFIES (6 * HZ)
  1619. static int rndis_scan(struct wiphy *wiphy,
  1620. struct cfg80211_scan_request *request)
  1621. {
  1622. struct net_device *dev = request->wdev->netdev;
  1623. struct usbnet *usbdev = netdev_priv(dev);
  1624. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1625. int ret;
  1626. int delay = SCAN_DELAY_JIFFIES;
  1627. netdev_dbg(usbdev->net, "cfg80211.scan\n");
  1628. /* Get current bssid list from device before new scan, as new scan
  1629. * clears internal bssid list.
  1630. */
  1631. rndis_check_bssid_list(usbdev, NULL, NULL);
  1632. if (priv->scan_request && priv->scan_request != request)
  1633. return -EBUSY;
  1634. priv->scan_request = request;
  1635. ret = rndis_start_bssid_list_scan(usbdev);
  1636. if (ret == 0) {
  1637. if (priv->device_type == RNDIS_BCM4320A)
  1638. delay = HZ;
  1639. /* Wait before retrieving scan results from device */
  1640. queue_delayed_work(priv->workqueue, &priv->scan_work, delay);
  1641. }
  1642. return ret;
  1643. }
  1644. static bool rndis_bss_info_update(struct usbnet *usbdev,
  1645. struct ndis_80211_bssid_ex *bssid)
  1646. {
  1647. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1648. struct ieee80211_channel *channel;
  1649. struct cfg80211_bss *bss;
  1650. s32 signal;
  1651. u64 timestamp;
  1652. u16 capability;
  1653. u16 beacon_interval;
  1654. struct ndis_80211_fixed_ies *fixed;
  1655. int ie_len, bssid_len;
  1656. u8 *ie;
  1657. netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM], len: %d\n",
  1658. bssid->ssid.essid, bssid->mac, le32_to_cpu(bssid->length));
  1659. /* parse bssid structure */
  1660. bssid_len = le32_to_cpu(bssid->length);
  1661. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1662. sizeof(struct ndis_80211_fixed_ies))
  1663. return NULL;
  1664. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1665. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1666. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1667. (int)le32_to_cpu(bssid->ie_length));
  1668. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1669. if (ie_len < 0)
  1670. return NULL;
  1671. /* extract data for cfg80211_inform_bss */
  1672. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1673. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1674. if (!channel)
  1675. return NULL;
  1676. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1677. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1678. capability = le16_to_cpu(fixed->capabilities);
  1679. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1680. bss = cfg80211_inform_bss(priv->wdev.wiphy, channel,
  1681. CFG80211_BSS_FTYPE_UNKNOWN, bssid->mac,
  1682. timestamp, capability, beacon_interval,
  1683. ie, ie_len, signal, GFP_KERNEL);
  1684. cfg80211_put_bss(priv->wdev.wiphy, bss);
  1685. return (bss != NULL);
  1686. }
  1687. static struct ndis_80211_bssid_ex *next_bssid_list_item(
  1688. struct ndis_80211_bssid_ex *bssid,
  1689. int *bssid_len, void *buf, int len)
  1690. {
  1691. void *buf_end, *bssid_end;
  1692. buf_end = (char *)buf + len;
  1693. bssid_end = (char *)bssid + *bssid_len;
  1694. if ((int)(buf_end - bssid_end) < sizeof(bssid->length)) {
  1695. *bssid_len = 0;
  1696. return NULL;
  1697. } else {
  1698. bssid = (void *)((char *)bssid + *bssid_len);
  1699. *bssid_len = le32_to_cpu(bssid->length);
  1700. return bssid;
  1701. }
  1702. }
  1703. static bool check_bssid_list_item(struct ndis_80211_bssid_ex *bssid,
  1704. int bssid_len, void *buf, int len)
  1705. {
  1706. void *buf_end, *bssid_end;
  1707. if (!bssid || bssid_len <= 0 || bssid_len > len)
  1708. return false;
  1709. buf_end = (char *)buf + len;
  1710. bssid_end = (char *)bssid + bssid_len;
  1711. return (int)(buf_end - bssid_end) >= 0 && (int)(bssid_end - buf) >= 0;
  1712. }
  1713. static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
  1714. bool *matched)
  1715. {
  1716. void *buf = NULL;
  1717. struct ndis_80211_bssid_list_ex *bssid_list;
  1718. struct ndis_80211_bssid_ex *bssid;
  1719. int ret = -EINVAL, len, count, bssid_len, real_count, new_len;
  1720. netdev_dbg(usbdev->net, "%s()\n", __func__);
  1721. len = CONTROL_BUFFER_SIZE;
  1722. resize_buf:
  1723. buf = kzalloc(len, GFP_KERNEL);
  1724. if (!buf) {
  1725. ret = -ENOMEM;
  1726. goto out;
  1727. }
  1728. /* BSSID-list might have got bigger last time we checked, keep
  1729. * resizing until it won't get any bigger.
  1730. */
  1731. new_len = len;
  1732. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST,
  1733. buf, &new_len);
  1734. if (ret != 0 || new_len < sizeof(struct ndis_80211_bssid_list_ex))
  1735. goto out;
  1736. if (new_len > len) {
  1737. len = new_len;
  1738. kfree(buf);
  1739. goto resize_buf;
  1740. }
  1741. len = new_len;
  1742. bssid_list = buf;
  1743. count = le32_to_cpu(bssid_list->num_items);
  1744. real_count = 0;
  1745. netdev_dbg(usbdev->net, "%s(): buflen: %d\n", __func__, len);
  1746. bssid_len = 0;
  1747. bssid = next_bssid_list_item(bssid_list->bssid, &bssid_len, buf, len);
  1748. /* Device returns incorrect 'num_items'. Workaround by ignoring the
  1749. * received 'num_items' and walking through full bssid buffer instead.
  1750. */
  1751. while (check_bssid_list_item(bssid, bssid_len, buf, len)) {
  1752. if (rndis_bss_info_update(usbdev, bssid) && match_bssid &&
  1753. matched) {
  1754. if (ether_addr_equal(bssid->mac, match_bssid))
  1755. *matched = true;
  1756. }
  1757. real_count++;
  1758. bssid = next_bssid_list_item(bssid, &bssid_len, buf, len);
  1759. }
  1760. netdev_dbg(usbdev->net, "%s(): num_items from device: %d, really found:"
  1761. " %d\n", __func__, count, real_count);
  1762. out:
  1763. kfree(buf);
  1764. return ret;
  1765. }
  1766. static void rndis_get_scan_results(struct work_struct *work)
  1767. {
  1768. struct rndis_wlan_private *priv =
  1769. container_of(work, struct rndis_wlan_private, scan_work.work);
  1770. struct usbnet *usbdev = priv->usbdev;
  1771. int ret;
  1772. netdev_dbg(usbdev->net, "get_scan_results\n");
  1773. if (!priv->scan_request)
  1774. return;
  1775. ret = rndis_check_bssid_list(usbdev, NULL, NULL);
  1776. cfg80211_scan_done(priv->scan_request, ret < 0);
  1777. priv->scan_request = NULL;
  1778. }
  1779. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  1780. struct cfg80211_connect_params *sme)
  1781. {
  1782. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1783. struct usbnet *usbdev = priv->usbdev;
  1784. struct ieee80211_channel *channel = sme->channel;
  1785. struct ndis_80211_ssid ssid;
  1786. int pairwise = RNDIS_WLAN_ALG_NONE;
  1787. int groupwise = RNDIS_WLAN_ALG_NONE;
  1788. int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
  1789. int length, i, ret, chan = -1;
  1790. if (channel)
  1791. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1792. groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
  1793. for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
  1794. pairwise |=
  1795. rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
  1796. if (sme->crypto.n_ciphers_pairwise > 0 &&
  1797. pairwise == RNDIS_WLAN_ALG_NONE) {
  1798. netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
  1799. return -ENOTSUPP;
  1800. }
  1801. for (i = 0; i < sme->crypto.n_akm_suites; i++)
  1802. keymgmt |=
  1803. rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
  1804. if (sme->crypto.n_akm_suites > 0 &&
  1805. keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
  1806. netdev_err(usbdev->net, "Invalid keymgmt\n");
  1807. return -ENOTSUPP;
  1808. }
  1809. netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
  1810. sme->ssid, sme->bssid, chan,
  1811. sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
  1812. groupwise, pairwise, keymgmt);
  1813. if (is_associated(usbdev))
  1814. disassociate(usbdev, false);
  1815. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1816. if (ret < 0) {
  1817. netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
  1818. ret);
  1819. goto err_turn_radio_on;
  1820. }
  1821. ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
  1822. keymgmt);
  1823. if (ret < 0) {
  1824. netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
  1825. ret);
  1826. goto err_turn_radio_on;
  1827. }
  1828. set_priv_filter(usbdev);
  1829. ret = set_encr_mode(usbdev, pairwise, groupwise);
  1830. if (ret < 0) {
  1831. netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
  1832. ret);
  1833. goto err_turn_radio_on;
  1834. }
  1835. if (channel) {
  1836. ret = set_channel(usbdev, chan);
  1837. if (ret < 0) {
  1838. netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
  1839. ret);
  1840. goto err_turn_radio_on;
  1841. }
  1842. }
  1843. if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
  1844. priv->encr_tx_key_index = sme->key_idx;
  1845. ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
  1846. if (ret < 0) {
  1847. netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
  1848. ret, sme->key_len, sme->key_idx);
  1849. goto err_turn_radio_on;
  1850. }
  1851. }
  1852. if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
  1853. !is_broadcast_ether_addr(sme->bssid)) {
  1854. ret = set_bssid(usbdev, sme->bssid);
  1855. if (ret < 0) {
  1856. netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
  1857. ret);
  1858. goto err_turn_radio_on;
  1859. }
  1860. } else
  1861. clear_bssid(usbdev);
  1862. length = sme->ssid_len;
  1863. if (length > NDIS_802_11_LENGTH_SSID)
  1864. length = NDIS_802_11_LENGTH_SSID;
  1865. memset(&ssid, 0, sizeof(ssid));
  1866. ssid.length = cpu_to_le32(length);
  1867. memcpy(ssid.essid, sme->ssid, length);
  1868. /* Pause and purge rx queue, so we don't pass packets before
  1869. * 'media connect'-indication.
  1870. */
  1871. usbnet_pause_rx(usbdev);
  1872. usbnet_purge_paused_rxq(usbdev);
  1873. ret = set_essid(usbdev, &ssid);
  1874. if (ret < 0)
  1875. netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
  1876. return ret;
  1877. err_turn_radio_on:
  1878. disassociate(usbdev, true);
  1879. return ret;
  1880. }
  1881. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1882. u16 reason_code)
  1883. {
  1884. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1885. struct usbnet *usbdev = priv->usbdev;
  1886. netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
  1887. priv->connected = false;
  1888. eth_zero_addr(priv->bssid);
  1889. return deauthenticate(usbdev);
  1890. }
  1891. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1892. struct cfg80211_ibss_params *params)
  1893. {
  1894. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1895. struct usbnet *usbdev = priv->usbdev;
  1896. struct ieee80211_channel *channel = params->chandef.chan;
  1897. struct ndis_80211_ssid ssid;
  1898. enum nl80211_auth_type auth_type;
  1899. int ret, alg, length, chan = -1;
  1900. if (channel)
  1901. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1902. /* TODO: How to handle ad-hoc encryption?
  1903. * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
  1904. * pre-shared for encryption (open/shared/wpa), is key set before
  1905. * join_ibss? Which auth_type to use (not in params)? What about WPA?
  1906. */
  1907. if (params->privacy) {
  1908. auth_type = NL80211_AUTHTYPE_SHARED_KEY;
  1909. alg = RNDIS_WLAN_ALG_WEP;
  1910. } else {
  1911. auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  1912. alg = RNDIS_WLAN_ALG_NONE;
  1913. }
  1914. netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
  1915. params->ssid, params->bssid, chan, params->privacy);
  1916. if (is_associated(usbdev))
  1917. disassociate(usbdev, false);
  1918. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
  1919. if (ret < 0) {
  1920. netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
  1921. ret);
  1922. goto err_turn_radio_on;
  1923. }
  1924. ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
  1925. if (ret < 0) {
  1926. netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
  1927. ret);
  1928. goto err_turn_radio_on;
  1929. }
  1930. set_priv_filter(usbdev);
  1931. ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
  1932. if (ret < 0) {
  1933. netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
  1934. ret);
  1935. goto err_turn_radio_on;
  1936. }
  1937. if (channel) {
  1938. ret = set_channel(usbdev, chan);
  1939. if (ret < 0) {
  1940. netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
  1941. ret);
  1942. goto err_turn_radio_on;
  1943. }
  1944. }
  1945. if (params->bssid && !is_zero_ether_addr(params->bssid) &&
  1946. !is_broadcast_ether_addr(params->bssid)) {
  1947. ret = set_bssid(usbdev, params->bssid);
  1948. if (ret < 0) {
  1949. netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
  1950. ret);
  1951. goto err_turn_radio_on;
  1952. }
  1953. } else
  1954. clear_bssid(usbdev);
  1955. length = params->ssid_len;
  1956. if (length > NDIS_802_11_LENGTH_SSID)
  1957. length = NDIS_802_11_LENGTH_SSID;
  1958. memset(&ssid, 0, sizeof(ssid));
  1959. ssid.length = cpu_to_le32(length);
  1960. memcpy(ssid.essid, params->ssid, length);
  1961. /* Don't need to pause rx queue for ad-hoc. */
  1962. usbnet_purge_paused_rxq(usbdev);
  1963. usbnet_resume_rx(usbdev);
  1964. ret = set_essid(usbdev, &ssid);
  1965. if (ret < 0)
  1966. netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
  1967. ret);
  1968. return ret;
  1969. err_turn_radio_on:
  1970. disassociate(usbdev, true);
  1971. return ret;
  1972. }
  1973. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1974. {
  1975. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1976. struct usbnet *usbdev = priv->usbdev;
  1977. netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
  1978. priv->connected = false;
  1979. eth_zero_addr(priv->bssid);
  1980. return deauthenticate(usbdev);
  1981. }
  1982. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  1983. u8 key_index, bool pairwise, const u8 *mac_addr,
  1984. struct key_params *params)
  1985. {
  1986. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1987. struct usbnet *usbdev = priv->usbdev;
  1988. __le32 flags;
  1989. netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
  1990. __func__, key_index, mac_addr, params->cipher);
  1991. switch (params->cipher) {
  1992. case WLAN_CIPHER_SUITE_WEP40:
  1993. case WLAN_CIPHER_SUITE_WEP104:
  1994. return add_wep_key(usbdev, params->key, params->key_len,
  1995. key_index);
  1996. case WLAN_CIPHER_SUITE_TKIP:
  1997. case WLAN_CIPHER_SUITE_CCMP:
  1998. flags = 0;
  1999. if (params->seq && params->seq_len > 0)
  2000. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  2001. if (mac_addr)
  2002. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
  2003. NDIS_80211_ADDKEY_TRANSMIT_KEY;
  2004. return add_wpa_key(usbdev, params->key, params->key_len,
  2005. key_index, mac_addr, params->seq,
  2006. params->seq_len, params->cipher, flags);
  2007. default:
  2008. netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
  2009. __func__, params->cipher);
  2010. return -ENOTSUPP;
  2011. }
  2012. }
  2013. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  2014. u8 key_index, bool pairwise, const u8 *mac_addr)
  2015. {
  2016. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2017. struct usbnet *usbdev = priv->usbdev;
  2018. netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
  2019. return remove_key(usbdev, key_index, mac_addr);
  2020. }
  2021. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  2022. u8 key_index, bool unicast, bool multicast)
  2023. {
  2024. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2025. struct usbnet *usbdev = priv->usbdev;
  2026. struct rndis_wlan_encr_key key;
  2027. netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
  2028. if (key_index >= RNDIS_WLAN_NUM_KEYS)
  2029. return -ENOENT;
  2030. priv->encr_tx_key_index = key_index;
  2031. if (is_wpa_key(priv, key_index))
  2032. return 0;
  2033. key = priv->encr_keys[key_index];
  2034. return add_wep_key(usbdev, key.material, key.len, key_index);
  2035. }
  2036. static void rndis_fill_station_info(struct usbnet *usbdev,
  2037. struct station_info *sinfo)
  2038. {
  2039. __le32 linkspeed, rssi;
  2040. int ret, len;
  2041. memset(sinfo, 0, sizeof(*sinfo));
  2042. len = sizeof(linkspeed);
  2043. ret = rndis_query_oid(usbdev, RNDIS_OID_GEN_LINK_SPEED, &linkspeed, &len);
  2044. if (ret == 0) {
  2045. sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
  2046. sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
  2047. }
  2048. len = sizeof(rssi);
  2049. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
  2050. &rssi, &len);
  2051. if (ret == 0) {
  2052. sinfo->signal = level_to_qual(le32_to_cpu(rssi));
  2053. sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
  2054. }
  2055. }
  2056. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  2057. const u8 *mac, struct station_info *sinfo)
  2058. {
  2059. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2060. struct usbnet *usbdev = priv->usbdev;
  2061. if (!ether_addr_equal(priv->bssid, mac))
  2062. return -ENOENT;
  2063. rndis_fill_station_info(usbdev, sinfo);
  2064. return 0;
  2065. }
  2066. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  2067. int idx, u8 *mac, struct station_info *sinfo)
  2068. {
  2069. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2070. struct usbnet *usbdev = priv->usbdev;
  2071. if (idx != 0)
  2072. return -ENOENT;
  2073. memcpy(mac, priv->bssid, ETH_ALEN);
  2074. rndis_fill_station_info(usbdev, sinfo);
  2075. return 0;
  2076. }
  2077. static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  2078. struct cfg80211_pmksa *pmksa)
  2079. {
  2080. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2081. struct usbnet *usbdev = priv->usbdev;
  2082. struct ndis_80211_pmkid *pmkids;
  2083. u32 *tmp = (u32 *)pmksa->pmkid;
  2084. netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
  2085. pmksa->bssid,
  2086. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  2087. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  2088. pmkids = get_device_pmkids(usbdev);
  2089. if (IS_ERR(pmkids)) {
  2090. /* couldn't read PMKID cache from device */
  2091. return PTR_ERR(pmkids);
  2092. }
  2093. pmkids = update_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
  2094. if (IS_ERR(pmkids)) {
  2095. /* not found, list full, etc */
  2096. return PTR_ERR(pmkids);
  2097. }
  2098. return set_device_pmkids(usbdev, pmkids);
  2099. }
  2100. static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  2101. struct cfg80211_pmksa *pmksa)
  2102. {
  2103. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2104. struct usbnet *usbdev = priv->usbdev;
  2105. struct ndis_80211_pmkid *pmkids;
  2106. u32 *tmp = (u32 *)pmksa->pmkid;
  2107. netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
  2108. pmksa->bssid,
  2109. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  2110. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  2111. pmkids = get_device_pmkids(usbdev);
  2112. if (IS_ERR(pmkids)) {
  2113. /* Couldn't read PMKID cache from device */
  2114. return PTR_ERR(pmkids);
  2115. }
  2116. pmkids = remove_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
  2117. if (IS_ERR(pmkids)) {
  2118. /* not found, etc */
  2119. return PTR_ERR(pmkids);
  2120. }
  2121. return set_device_pmkids(usbdev, pmkids);
  2122. }
  2123. static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
  2124. {
  2125. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2126. struct usbnet *usbdev = priv->usbdev;
  2127. struct ndis_80211_pmkid pmkid;
  2128. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2129. memset(&pmkid, 0, sizeof(pmkid));
  2130. pmkid.length = cpu_to_le32(sizeof(pmkid));
  2131. pmkid.bssid_info_count = cpu_to_le32(0);
  2132. return rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID,
  2133. &pmkid, sizeof(pmkid));
  2134. }
  2135. static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
  2136. bool enabled, int timeout)
  2137. {
  2138. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2139. struct usbnet *usbdev = priv->usbdev;
  2140. int power_mode;
  2141. __le32 mode;
  2142. int ret;
  2143. if (priv->device_type != RNDIS_BCM4320B)
  2144. return -ENOTSUPP;
  2145. netdev_dbg(usbdev->net, "%s(): %s, %d\n", __func__,
  2146. enabled ? "enabled" : "disabled",
  2147. timeout);
  2148. if (enabled)
  2149. power_mode = NDIS_80211_POWER_MODE_FAST_PSP;
  2150. else
  2151. power_mode = NDIS_80211_POWER_MODE_CAM;
  2152. if (power_mode == priv->power_mode)
  2153. return 0;
  2154. priv->power_mode = power_mode;
  2155. mode = cpu_to_le32(power_mode);
  2156. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_POWER_MODE,
  2157. &mode, sizeof(mode));
  2158. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_POWER_MODE -> %d\n",
  2159. __func__, ret);
  2160. return ret;
  2161. }
  2162. static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
  2163. struct net_device *dev,
  2164. s32 rssi_thold, u32 rssi_hyst)
  2165. {
  2166. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2167. priv->cqm_rssi_thold = rssi_thold;
  2168. priv->cqm_rssi_hyst = rssi_hyst;
  2169. priv->last_cqm_event_rssi = 0;
  2170. return 0;
  2171. }
  2172. static void rndis_wlan_craft_connected_bss(struct usbnet *usbdev, u8 *bssid,
  2173. struct ndis_80211_assoc_info *info)
  2174. {
  2175. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2176. struct ieee80211_channel *channel;
  2177. struct ndis_80211_ssid ssid;
  2178. struct cfg80211_bss *bss;
  2179. s32 signal;
  2180. u64 timestamp;
  2181. u16 capability;
  2182. u32 beacon_period = 0;
  2183. __le32 rssi;
  2184. u8 ie_buf[34];
  2185. int len, ret, ie_len;
  2186. /* Get signal quality, in case of error use rssi=0 and ignore error. */
  2187. len = sizeof(rssi);
  2188. rssi = 0;
  2189. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
  2190. &rssi, &len);
  2191. signal = level_to_qual(le32_to_cpu(rssi));
  2192. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_RSSI -> %d, "
  2193. "rssi:%d, qual: %d\n", __func__, ret, le32_to_cpu(rssi),
  2194. level_to_qual(le32_to_cpu(rssi)));
  2195. /* Get AP capabilities */
  2196. if (info) {
  2197. capability = le16_to_cpu(info->resp_ie.capa);
  2198. } else {
  2199. /* Set atleast ESS/IBSS capability */
  2200. capability = (priv->infra_mode == NDIS_80211_INFRA_INFRA) ?
  2201. WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS;
  2202. }
  2203. /* Get channel and beacon interval */
  2204. channel = get_current_channel(usbdev, &beacon_period);
  2205. if (!channel) {
  2206. netdev_warn(usbdev->net, "%s(): could not get channel.\n",
  2207. __func__);
  2208. return;
  2209. }
  2210. /* Get SSID, in case of error, use zero length SSID and ignore error. */
  2211. len = sizeof(ssid);
  2212. memset(&ssid, 0, sizeof(ssid));
  2213. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_SSID,
  2214. &ssid, &len);
  2215. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_SSID -> %d, len: %d, ssid: "
  2216. "'%.32s'\n", __func__, ret,
  2217. le32_to_cpu(ssid.length), ssid.essid);
  2218. if (le32_to_cpu(ssid.length) > 32)
  2219. ssid.length = cpu_to_le32(32);
  2220. ie_buf[0] = WLAN_EID_SSID;
  2221. ie_buf[1] = le32_to_cpu(ssid.length);
  2222. memcpy(&ie_buf[2], ssid.essid, le32_to_cpu(ssid.length));
  2223. ie_len = le32_to_cpu(ssid.length) + 2;
  2224. /* no tsf */
  2225. timestamp = 0;
  2226. netdev_dbg(usbdev->net, "%s(): channel:%d(freq), bssid:[%pM], tsf:%d, "
  2227. "capa:%x, beacon int:%d, resp_ie(len:%d, essid:'%.32s'), "
  2228. "signal:%d\n", __func__, (channel ? channel->center_freq : -1),
  2229. bssid, (u32)timestamp, capability, beacon_period, ie_len,
  2230. ssid.essid, signal);
  2231. bss = cfg80211_inform_bss(priv->wdev.wiphy, channel,
  2232. CFG80211_BSS_FTYPE_UNKNOWN, bssid,
  2233. timestamp, capability, beacon_period,
  2234. ie_buf, ie_len, signal, GFP_KERNEL);
  2235. cfg80211_put_bss(priv->wdev.wiphy, bss);
  2236. }
  2237. /*
  2238. * workers, indication handlers, device poller
  2239. */
  2240. static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
  2241. {
  2242. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2243. struct ndis_80211_assoc_info *info = NULL;
  2244. u8 bssid[ETH_ALEN];
  2245. unsigned int resp_ie_len, req_ie_len;
  2246. unsigned int offset;
  2247. u8 *req_ie, *resp_ie;
  2248. int ret;
  2249. bool roamed = false;
  2250. bool match_bss;
  2251. if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
  2252. /* received media connect indication while connected, either
  2253. * device reassociated with same AP or roamed to new. */
  2254. roamed = true;
  2255. }
  2256. req_ie_len = 0;
  2257. resp_ie_len = 0;
  2258. req_ie = NULL;
  2259. resp_ie = NULL;
  2260. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  2261. info = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
  2262. if (!info) {
  2263. /* No memory? Try resume work later */
  2264. set_bit(WORK_LINK_UP, &priv->work_pending);
  2265. queue_work(priv->workqueue, &priv->work);
  2266. return;
  2267. }
  2268. /* Get association info IEs from device. */
  2269. ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
  2270. if (!ret) {
  2271. req_ie_len = le32_to_cpu(info->req_ie_length);
  2272. if (req_ie_len > CONTROL_BUFFER_SIZE)
  2273. req_ie_len = CONTROL_BUFFER_SIZE;
  2274. if (req_ie_len != 0) {
  2275. offset = le32_to_cpu(info->offset_req_ies);
  2276. if (offset > CONTROL_BUFFER_SIZE)
  2277. offset = CONTROL_BUFFER_SIZE;
  2278. req_ie = (u8 *)info + offset;
  2279. if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
  2280. req_ie_len =
  2281. CONTROL_BUFFER_SIZE - offset;
  2282. }
  2283. resp_ie_len = le32_to_cpu(info->resp_ie_length);
  2284. if (resp_ie_len > CONTROL_BUFFER_SIZE)
  2285. resp_ie_len = CONTROL_BUFFER_SIZE;
  2286. if (resp_ie_len != 0) {
  2287. offset = le32_to_cpu(info->offset_resp_ies);
  2288. if (offset > CONTROL_BUFFER_SIZE)
  2289. offset = CONTROL_BUFFER_SIZE;
  2290. resp_ie = (u8 *)info + offset;
  2291. if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
  2292. resp_ie_len =
  2293. CONTROL_BUFFER_SIZE - offset;
  2294. }
  2295. } else {
  2296. /* Since rndis_wlan_craft_connected_bss() might use info
  2297. * later and expects info to contain valid data if
  2298. * non-null, free info and set NULL here.
  2299. */
  2300. kfree(info);
  2301. info = NULL;
  2302. }
  2303. } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
  2304. return;
  2305. ret = get_bssid(usbdev, bssid);
  2306. if (ret < 0)
  2307. memset(bssid, 0, sizeof(bssid));
  2308. netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
  2309. bssid, roamed ? " roamed" : "");
  2310. /* Internal bss list in device should contain at least the currently
  2311. * connected bss and we can get it to cfg80211 with
  2312. * rndis_check_bssid_list().
  2313. *
  2314. * NDIS spec says: "If the device is associated, but the associated
  2315. * BSSID is not in its BSSID scan list, then the driver must add an
  2316. * entry for the BSSID at the end of the data that it returns in
  2317. * response to query of RNDIS_OID_802_11_BSSID_LIST."
  2318. *
  2319. * NOTE: Seems to be true for BCM4320b variant, but not BCM4320a.
  2320. */
  2321. match_bss = false;
  2322. rndis_check_bssid_list(usbdev, bssid, &match_bss);
  2323. if (!is_zero_ether_addr(bssid) && !match_bss) {
  2324. /* Couldn't get bss from device, we need to manually craft bss
  2325. * for cfg80211.
  2326. */
  2327. rndis_wlan_craft_connected_bss(usbdev, bssid, info);
  2328. }
  2329. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  2330. if (!roamed)
  2331. cfg80211_connect_result(usbdev->net, bssid, req_ie,
  2332. req_ie_len, resp_ie,
  2333. resp_ie_len, 0, GFP_KERNEL);
  2334. else
  2335. cfg80211_roamed(usbdev->net,
  2336. get_current_channel(usbdev, NULL),
  2337. bssid, req_ie, req_ie_len,
  2338. resp_ie, resp_ie_len, GFP_KERNEL);
  2339. } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  2340. cfg80211_ibss_joined(usbdev->net, bssid,
  2341. get_current_channel(usbdev, NULL),
  2342. GFP_KERNEL);
  2343. kfree(info);
  2344. priv->connected = true;
  2345. memcpy(priv->bssid, bssid, ETH_ALEN);
  2346. usbnet_resume_rx(usbdev);
  2347. netif_carrier_on(usbdev->net);
  2348. }
  2349. static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
  2350. {
  2351. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2352. if (priv->connected) {
  2353. priv->connected = false;
  2354. eth_zero_addr(priv->bssid);
  2355. deauthenticate(usbdev);
  2356. cfg80211_disconnected(usbdev->net, 0, NULL, 0, true, GFP_KERNEL);
  2357. }
  2358. netif_carrier_off(usbdev->net);
  2359. }
  2360. static void rndis_wlan_worker(struct work_struct *work)
  2361. {
  2362. struct rndis_wlan_private *priv =
  2363. container_of(work, struct rndis_wlan_private, work);
  2364. struct usbnet *usbdev = priv->usbdev;
  2365. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
  2366. rndis_wlan_do_link_up_work(usbdev);
  2367. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
  2368. rndis_wlan_do_link_down_work(usbdev);
  2369. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  2370. set_multicast_list(usbdev);
  2371. }
  2372. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  2373. {
  2374. struct usbnet *usbdev = netdev_priv(dev);
  2375. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2376. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  2377. return;
  2378. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  2379. queue_work(priv->workqueue, &priv->work);
  2380. }
  2381. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  2382. struct ndis_80211_status_indication *indication,
  2383. int len)
  2384. {
  2385. u8 *buf;
  2386. const char *type;
  2387. int flags, buflen, key_id;
  2388. bool pairwise_error, group_error;
  2389. struct ndis_80211_auth_request *auth_req;
  2390. enum nl80211_key_type key_type;
  2391. /* must have at least one array entry */
  2392. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2393. sizeof(struct ndis_80211_auth_request)) {
  2394. netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
  2395. len);
  2396. return;
  2397. }
  2398. buf = (void *)&indication->u.auth_request[0];
  2399. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  2400. while (buflen >= sizeof(*auth_req)) {
  2401. auth_req = (void *)buf;
  2402. type = "unknown";
  2403. flags = le32_to_cpu(auth_req->flags);
  2404. pairwise_error = false;
  2405. group_error = false;
  2406. if (flags & 0x1)
  2407. type = "reauth request";
  2408. if (flags & 0x2)
  2409. type = "key update request";
  2410. if (flags & 0x6) {
  2411. pairwise_error = true;
  2412. type = "pairwise_error";
  2413. }
  2414. if (flags & 0xe) {
  2415. group_error = true;
  2416. type = "group_error";
  2417. }
  2418. netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
  2419. type, le32_to_cpu(auth_req->flags));
  2420. if (pairwise_error) {
  2421. key_type = NL80211_KEYTYPE_PAIRWISE;
  2422. key_id = -1;
  2423. cfg80211_michael_mic_failure(usbdev->net,
  2424. auth_req->bssid,
  2425. key_type, key_id, NULL,
  2426. GFP_KERNEL);
  2427. }
  2428. if (group_error) {
  2429. key_type = NL80211_KEYTYPE_GROUP;
  2430. key_id = -1;
  2431. cfg80211_michael_mic_failure(usbdev->net,
  2432. auth_req->bssid,
  2433. key_type, key_id, NULL,
  2434. GFP_KERNEL);
  2435. }
  2436. buflen -= le32_to_cpu(auth_req->length);
  2437. buf += le32_to_cpu(auth_req->length);
  2438. }
  2439. }
  2440. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  2441. struct ndis_80211_status_indication *indication,
  2442. int len)
  2443. {
  2444. struct ndis_80211_pmkid_cand_list *cand_list;
  2445. int list_len, expected_len, i;
  2446. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2447. sizeof(struct ndis_80211_pmkid_cand_list)) {
  2448. netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
  2449. len);
  2450. return;
  2451. }
  2452. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  2453. sizeof(struct ndis_80211_pmkid_candidate);
  2454. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  2455. offsetof(struct ndis_80211_status_indication, u);
  2456. if (len < expected_len) {
  2457. netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
  2458. len, expected_len);
  2459. return;
  2460. }
  2461. cand_list = &indication->u.cand_list;
  2462. netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
  2463. le32_to_cpu(cand_list->version),
  2464. le32_to_cpu(cand_list->num_candidates));
  2465. if (le32_to_cpu(cand_list->version) != 1)
  2466. return;
  2467. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  2468. struct ndis_80211_pmkid_candidate *cand =
  2469. &cand_list->candidate_list[i];
  2470. bool preauth = !!(cand->flags & NDIS_80211_PMKID_CAND_PREAUTH);
  2471. netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, preauth: %d, bssid: %pM\n",
  2472. i, le32_to_cpu(cand->flags), preauth, cand->bssid);
  2473. cfg80211_pmksa_candidate_notify(usbdev->net, i, cand->bssid,
  2474. preauth, GFP_ATOMIC);
  2475. }
  2476. }
  2477. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  2478. struct rndis_indicate *msg, int buflen)
  2479. {
  2480. struct ndis_80211_status_indication *indication;
  2481. unsigned int len, offset;
  2482. offset = offsetof(struct rndis_indicate, status) +
  2483. le32_to_cpu(msg->offset);
  2484. len = le32_to_cpu(msg->length);
  2485. if (len < 8) {
  2486. netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
  2487. len);
  2488. return;
  2489. }
  2490. if (len > buflen || offset > buflen || offset + len > buflen) {
  2491. netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
  2492. offset + len, buflen);
  2493. return;
  2494. }
  2495. indication = (void *)((u8 *)msg + offset);
  2496. switch (le32_to_cpu(indication->status_type)) {
  2497. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  2498. netdev_info(usbdev->net, "radio state indication: %i\n",
  2499. le32_to_cpu(indication->u.radio_status));
  2500. return;
  2501. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  2502. netdev_info(usbdev->net, "media stream mode indication: %i\n",
  2503. le32_to_cpu(indication->u.media_stream_mode));
  2504. return;
  2505. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  2506. rndis_wlan_auth_indication(usbdev, indication, len);
  2507. return;
  2508. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2509. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2510. return;
  2511. default:
  2512. netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
  2513. le32_to_cpu(indication->status_type));
  2514. }
  2515. }
  2516. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2517. {
  2518. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2519. struct rndis_indicate *msg = ind;
  2520. switch (le32_to_cpu(msg->status)) {
  2521. case RNDIS_STATUS_MEDIA_CONNECT:
  2522. if (priv->current_command_oid == RNDIS_OID_802_11_ADD_KEY) {
  2523. /* RNDIS_OID_802_11_ADD_KEY causes sometimes extra
  2524. * "media connect" indications which confuses driver
  2525. * and userspace to think that device is
  2526. * roaming/reassociating when it isn't.
  2527. */
  2528. netdev_dbg(usbdev->net, "ignored RNDIS_OID_802_11_ADD_KEY triggered 'media connect'\n");
  2529. return;
  2530. }
  2531. usbnet_pause_rx(usbdev);
  2532. netdev_info(usbdev->net, "media connect\n");
  2533. /* queue work to avoid recursive calls into rndis_command */
  2534. set_bit(WORK_LINK_UP, &priv->work_pending);
  2535. queue_work(priv->workqueue, &priv->work);
  2536. break;
  2537. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2538. netdev_info(usbdev->net, "media disconnect\n");
  2539. /* queue work to avoid recursive calls into rndis_command */
  2540. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2541. queue_work(priv->workqueue, &priv->work);
  2542. break;
  2543. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2544. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2545. break;
  2546. default:
  2547. netdev_info(usbdev->net, "indication: 0x%08x\n",
  2548. le32_to_cpu(msg->status));
  2549. break;
  2550. }
  2551. }
  2552. static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
  2553. {
  2554. struct {
  2555. __le32 num_items;
  2556. __le32 items[8];
  2557. } networks_supported;
  2558. struct ndis_80211_capability *caps;
  2559. u8 caps_buf[sizeof(*caps) + sizeof(caps->auth_encr_pair) * 16];
  2560. int len, retval, i, n;
  2561. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2562. /* determine supported modes */
  2563. len = sizeof(networks_supported);
  2564. retval = rndis_query_oid(usbdev,
  2565. RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED,
  2566. &networks_supported, &len);
  2567. if (retval >= 0) {
  2568. n = le32_to_cpu(networks_supported.num_items);
  2569. if (n > 8)
  2570. n = 8;
  2571. for (i = 0; i < n; i++) {
  2572. switch (le32_to_cpu(networks_supported.items[i])) {
  2573. case NDIS_80211_TYPE_FREQ_HOP:
  2574. case NDIS_80211_TYPE_DIRECT_SEQ:
  2575. priv->caps |= CAP_MODE_80211B;
  2576. break;
  2577. case NDIS_80211_TYPE_OFDM_A:
  2578. priv->caps |= CAP_MODE_80211A;
  2579. break;
  2580. case NDIS_80211_TYPE_OFDM_G:
  2581. priv->caps |= CAP_MODE_80211G;
  2582. break;
  2583. }
  2584. }
  2585. }
  2586. /* get device 802.11 capabilities, number of PMKIDs */
  2587. caps = (struct ndis_80211_capability *)caps_buf;
  2588. len = sizeof(caps_buf);
  2589. retval = rndis_query_oid(usbdev,
  2590. RNDIS_OID_802_11_CAPABILITY,
  2591. caps, &len);
  2592. if (retval >= 0) {
  2593. netdev_dbg(usbdev->net, "RNDIS_OID_802_11_CAPABILITY -> len %d, "
  2594. "ver %d, pmkids %d, auth-encr-pairs %d\n",
  2595. le32_to_cpu(caps->length),
  2596. le32_to_cpu(caps->version),
  2597. le32_to_cpu(caps->num_pmkids),
  2598. le32_to_cpu(caps->num_auth_encr_pair));
  2599. wiphy->max_num_pmkids = le32_to_cpu(caps->num_pmkids);
  2600. } else
  2601. wiphy->max_num_pmkids = 0;
  2602. return retval;
  2603. }
  2604. static void rndis_do_cqm(struct usbnet *usbdev, s32 rssi)
  2605. {
  2606. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2607. enum nl80211_cqm_rssi_threshold_event event;
  2608. int thold, hyst, last_event;
  2609. if (priv->cqm_rssi_thold >= 0 || rssi >= 0)
  2610. return;
  2611. if (priv->infra_mode != NDIS_80211_INFRA_INFRA)
  2612. return;
  2613. last_event = priv->last_cqm_event_rssi;
  2614. thold = priv->cqm_rssi_thold;
  2615. hyst = priv->cqm_rssi_hyst;
  2616. if (rssi < thold && (last_event == 0 || rssi < last_event - hyst))
  2617. event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
  2618. else if (rssi > thold && (last_event == 0 || rssi > last_event + hyst))
  2619. event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
  2620. else
  2621. return;
  2622. priv->last_cqm_event_rssi = rssi;
  2623. cfg80211_cqm_rssi_notify(usbdev->net, event, GFP_KERNEL);
  2624. }
  2625. #define DEVICE_POLLER_JIFFIES (HZ)
  2626. static void rndis_device_poller(struct work_struct *work)
  2627. {
  2628. struct rndis_wlan_private *priv =
  2629. container_of(work, struct rndis_wlan_private,
  2630. dev_poller_work.work);
  2631. struct usbnet *usbdev = priv->usbdev;
  2632. __le32 rssi, tmp;
  2633. int len, ret, j;
  2634. int update_jiffies = DEVICE_POLLER_JIFFIES;
  2635. void *buf;
  2636. /* Only check/do workaround when connected. Calling is_associated()
  2637. * also polls device with rndis_command() and catches for media link
  2638. * indications.
  2639. */
  2640. if (!is_associated(usbdev)) {
  2641. /* Workaround bad scanning in BCM4320a devices with active
  2642. * background scanning when not associated.
  2643. */
  2644. if (priv->device_type == RNDIS_BCM4320A && priv->radio_on &&
  2645. !priv->scan_request) {
  2646. /* Get previous scan results */
  2647. rndis_check_bssid_list(usbdev, NULL, NULL);
  2648. /* Initiate new scan */
  2649. rndis_start_bssid_list_scan(usbdev);
  2650. }
  2651. goto end;
  2652. }
  2653. len = sizeof(rssi);
  2654. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
  2655. &rssi, &len);
  2656. if (ret == 0) {
  2657. priv->last_qual = level_to_qual(le32_to_cpu(rssi));
  2658. rndis_do_cqm(usbdev, le32_to_cpu(rssi));
  2659. }
  2660. netdev_dbg(usbdev->net, "dev-poller: RNDIS_OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
  2661. ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
  2662. /* Workaround transfer stalls on poor quality links.
  2663. * TODO: find right way to fix these stalls (as stalls do not happen
  2664. * with ndiswrapper/windows driver). */
  2665. if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
  2666. /* Decrease stats worker interval to catch stalls.
  2667. * faster. Faster than 400-500ms causes packet loss,
  2668. * Slower doesn't catch stalls fast enough.
  2669. */
  2670. j = msecs_to_jiffies(priv->param_workaround_interval);
  2671. if (j > DEVICE_POLLER_JIFFIES)
  2672. j = DEVICE_POLLER_JIFFIES;
  2673. else if (j <= 0)
  2674. j = 1;
  2675. update_jiffies = j;
  2676. /* Send scan OID. Use of both OIDs is required to get device
  2677. * working.
  2678. */
  2679. tmp = cpu_to_le32(1);
  2680. rndis_set_oid(usbdev,
  2681. RNDIS_OID_802_11_BSSID_LIST_SCAN,
  2682. &tmp, sizeof(tmp));
  2683. len = CONTROL_BUFFER_SIZE;
  2684. buf = kmalloc(len, GFP_KERNEL);
  2685. if (!buf)
  2686. goto end;
  2687. rndis_query_oid(usbdev,
  2688. RNDIS_OID_802_11_BSSID_LIST,
  2689. buf, &len);
  2690. kfree(buf);
  2691. }
  2692. end:
  2693. if (update_jiffies >= HZ)
  2694. update_jiffies = round_jiffies_relative(update_jiffies);
  2695. else {
  2696. j = round_jiffies_relative(update_jiffies);
  2697. if (abs(j - update_jiffies) <= 10)
  2698. update_jiffies = j;
  2699. }
  2700. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2701. update_jiffies);
  2702. }
  2703. /*
  2704. * driver/device initialization
  2705. */
  2706. static void rndis_copy_module_params(struct usbnet *usbdev, int device_type)
  2707. {
  2708. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2709. priv->device_type = device_type;
  2710. priv->param_country[0] = modparam_country[0];
  2711. priv->param_country[1] = modparam_country[1];
  2712. priv->param_country[2] = 0;
  2713. priv->param_frameburst = modparam_frameburst;
  2714. priv->param_afterburner = modparam_afterburner;
  2715. priv->param_power_save = modparam_power_save;
  2716. priv->param_power_output = modparam_power_output;
  2717. priv->param_roamtrigger = modparam_roamtrigger;
  2718. priv->param_roamdelta = modparam_roamdelta;
  2719. priv->param_country[0] = toupper(priv->param_country[0]);
  2720. priv->param_country[1] = toupper(priv->param_country[1]);
  2721. /* doesn't support EU as country code, use FI instead */
  2722. if (!strcmp(priv->param_country, "EU"))
  2723. strcpy(priv->param_country, "FI");
  2724. if (priv->param_power_save < 0)
  2725. priv->param_power_save = 0;
  2726. else if (priv->param_power_save > 2)
  2727. priv->param_power_save = 2;
  2728. if (priv->param_power_output < 0)
  2729. priv->param_power_output = 0;
  2730. else if (priv->param_power_output > 3)
  2731. priv->param_power_output = 3;
  2732. if (priv->param_roamtrigger < -80)
  2733. priv->param_roamtrigger = -80;
  2734. else if (priv->param_roamtrigger > -60)
  2735. priv->param_roamtrigger = -60;
  2736. if (priv->param_roamdelta < 0)
  2737. priv->param_roamdelta = 0;
  2738. else if (priv->param_roamdelta > 2)
  2739. priv->param_roamdelta = 2;
  2740. if (modparam_workaround_interval < 0)
  2741. priv->param_workaround_interval = 500;
  2742. else
  2743. priv->param_workaround_interval = modparam_workaround_interval;
  2744. }
  2745. static int unknown_early_init(struct usbnet *usbdev)
  2746. {
  2747. /* copy module parameters for unknown so that iwconfig reports txpower
  2748. * and workaround parameter is copied to private structure correctly.
  2749. */
  2750. rndis_copy_module_params(usbdev, RNDIS_UNKNOWN);
  2751. /* This is unknown device, so do not try set configuration parameters.
  2752. */
  2753. return 0;
  2754. }
  2755. static int bcm4320a_early_init(struct usbnet *usbdev)
  2756. {
  2757. /* copy module parameters for bcm4320a so that iwconfig reports txpower
  2758. * and workaround parameter is copied to private structure correctly.
  2759. */
  2760. rndis_copy_module_params(usbdev, RNDIS_BCM4320A);
  2761. /* bcm4320a doesn't handle configuration parameters well. Try
  2762. * set any and you get partially zeroed mac and broken device.
  2763. */
  2764. return 0;
  2765. }
  2766. static int bcm4320b_early_init(struct usbnet *usbdev)
  2767. {
  2768. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2769. char buf[8];
  2770. rndis_copy_module_params(usbdev, RNDIS_BCM4320B);
  2771. /* Early initialization settings, setting these won't have effect
  2772. * if called after generic_rndis_bind().
  2773. */
  2774. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2775. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2776. priv->param_frameburst ? "1" : "0");
  2777. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2778. priv->param_afterburner ? "1" : "0");
  2779. sprintf(buf, "%d", priv->param_power_save);
  2780. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2781. sprintf(buf, "%d", priv->param_power_output);
  2782. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2783. sprintf(buf, "%d", priv->param_roamtrigger);
  2784. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2785. sprintf(buf, "%d", priv->param_roamdelta);
  2786. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2787. return 0;
  2788. }
  2789. /* same as rndis_netdev_ops but with local multicast handler */
  2790. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2791. .ndo_open = usbnet_open,
  2792. .ndo_stop = usbnet_stop,
  2793. .ndo_start_xmit = usbnet_start_xmit,
  2794. .ndo_tx_timeout = usbnet_tx_timeout,
  2795. .ndo_set_mac_address = eth_mac_addr,
  2796. .ndo_validate_addr = eth_validate_addr,
  2797. .ndo_set_rx_mode = rndis_wlan_set_multicast_list,
  2798. };
  2799. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2800. {
  2801. struct wiphy *wiphy;
  2802. struct rndis_wlan_private *priv;
  2803. int retval, len;
  2804. __le32 tmp;
  2805. /* allocate wiphy and rndis private data
  2806. * NOTE: We only support a single virtual interface, so wiphy
  2807. * and wireless_dev are somewhat synonymous for this device.
  2808. */
  2809. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2810. if (!wiphy)
  2811. return -ENOMEM;
  2812. priv = wiphy_priv(wiphy);
  2813. usbdev->net->ieee80211_ptr = &priv->wdev;
  2814. priv->wdev.wiphy = wiphy;
  2815. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2816. /* These have to be initialized before calling generic_rndis_bind().
  2817. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2818. */
  2819. usbdev->driver_priv = priv;
  2820. priv->usbdev = usbdev;
  2821. mutex_init(&priv->command_lock);
  2822. /* because rndis_command() sleeps we need to use workqueue */
  2823. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2824. INIT_WORK(&priv->work, rndis_wlan_worker);
  2825. INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
  2826. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2827. /* try bind rndis_host */
  2828. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2829. if (retval < 0)
  2830. goto fail;
  2831. /* generic_rndis_bind set packet filter to multicast_all+
  2832. * promisc mode which doesn't work well for our devices (device
  2833. * picks up rssi to closest station instead of to access point).
  2834. *
  2835. * rndis_host wants to avoid all OID as much as possible
  2836. * so do promisc/multicast handling in rndis_wlan.
  2837. */
  2838. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2839. tmp = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST);
  2840. retval = rndis_set_oid(usbdev,
  2841. RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
  2842. &tmp, sizeof(tmp));
  2843. len = sizeof(tmp);
  2844. retval = rndis_query_oid(usbdev,
  2845. RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
  2846. &tmp, &len);
  2847. priv->multicast_size = le32_to_cpu(tmp);
  2848. if (retval < 0 || priv->multicast_size < 0)
  2849. priv->multicast_size = 0;
  2850. if (priv->multicast_size > 0)
  2851. usbdev->net->flags |= IFF_MULTICAST;
  2852. else
  2853. usbdev->net->flags &= ~IFF_MULTICAST;
  2854. /* fill-out wiphy structure and register w/ cfg80211 */
  2855. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2856. wiphy->privid = rndis_wiphy_privid;
  2857. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2858. | BIT(NL80211_IFTYPE_ADHOC);
  2859. wiphy->max_scan_ssids = 1;
  2860. /* TODO: fill-out band/encr information based on priv->caps */
  2861. rndis_wlan_get_caps(usbdev, wiphy);
  2862. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2863. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2864. priv->band.channels = priv->channels;
  2865. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2866. priv->band.bitrates = priv->rates;
  2867. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2868. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2869. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2870. memcpy(priv->cipher_suites, rndis_cipher_suites,
  2871. sizeof(rndis_cipher_suites));
  2872. wiphy->cipher_suites = priv->cipher_suites;
  2873. wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
  2874. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2875. if (wiphy_register(wiphy)) {
  2876. retval = -ENODEV;
  2877. goto fail;
  2878. }
  2879. set_default_iw_params(usbdev);
  2880. priv->power_mode = -1;
  2881. /* set default rts/frag */
  2882. rndis_set_wiphy_params(wiphy,
  2883. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2884. /* turn radio off on init */
  2885. priv->radio_on = false;
  2886. disassociate(usbdev, false);
  2887. netif_carrier_off(usbdev->net);
  2888. return 0;
  2889. fail:
  2890. cancel_delayed_work_sync(&priv->dev_poller_work);
  2891. cancel_delayed_work_sync(&priv->scan_work);
  2892. cancel_work_sync(&priv->work);
  2893. flush_workqueue(priv->workqueue);
  2894. destroy_workqueue(priv->workqueue);
  2895. wiphy_free(wiphy);
  2896. return retval;
  2897. }
  2898. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2899. {
  2900. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2901. /* turn radio off */
  2902. disassociate(usbdev, false);
  2903. cancel_delayed_work_sync(&priv->dev_poller_work);
  2904. cancel_delayed_work_sync(&priv->scan_work);
  2905. cancel_work_sync(&priv->work);
  2906. flush_workqueue(priv->workqueue);
  2907. destroy_workqueue(priv->workqueue);
  2908. rndis_unbind(usbdev, intf);
  2909. wiphy_unregister(priv->wdev.wiphy);
  2910. wiphy_free(priv->wdev.wiphy);
  2911. }
  2912. static int rndis_wlan_reset(struct usbnet *usbdev)
  2913. {
  2914. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2915. int retval;
  2916. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2917. retval = rndis_reset(usbdev);
  2918. if (retval)
  2919. netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
  2920. /* rndis_reset cleared multicast list, so restore here.
  2921. (set_multicast_list() also turns on current packet filter) */
  2922. set_multicast_list(usbdev);
  2923. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2924. round_jiffies_relative(DEVICE_POLLER_JIFFIES));
  2925. return deauthenticate(usbdev);
  2926. }
  2927. static int rndis_wlan_stop(struct usbnet *usbdev)
  2928. {
  2929. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2930. int retval;
  2931. __le32 filter;
  2932. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2933. retval = disassociate(usbdev, false);
  2934. priv->work_pending = 0;
  2935. cancel_delayed_work_sync(&priv->dev_poller_work);
  2936. cancel_delayed_work_sync(&priv->scan_work);
  2937. cancel_work_sync(&priv->work);
  2938. flush_workqueue(priv->workqueue);
  2939. if (priv->scan_request) {
  2940. cfg80211_scan_done(priv->scan_request, true);
  2941. priv->scan_request = NULL;
  2942. }
  2943. /* Set current packet filter zero to block receiving data packets from
  2944. device. */
  2945. filter = 0;
  2946. rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2947. sizeof(filter));
  2948. return retval;
  2949. }
  2950. static const struct driver_info bcm4320b_info = {
  2951. .description = "Wireless RNDIS device, BCM4320b based",
  2952. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2953. FLAG_AVOID_UNLINK_URBS,
  2954. .bind = rndis_wlan_bind,
  2955. .unbind = rndis_wlan_unbind,
  2956. .status = rndis_status,
  2957. .rx_fixup = rndis_rx_fixup,
  2958. .tx_fixup = rndis_tx_fixup,
  2959. .reset = rndis_wlan_reset,
  2960. .stop = rndis_wlan_stop,
  2961. .early_init = bcm4320b_early_init,
  2962. .indication = rndis_wlan_indication,
  2963. };
  2964. static const struct driver_info bcm4320a_info = {
  2965. .description = "Wireless RNDIS device, BCM4320a based",
  2966. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2967. FLAG_AVOID_UNLINK_URBS,
  2968. .bind = rndis_wlan_bind,
  2969. .unbind = rndis_wlan_unbind,
  2970. .status = rndis_status,
  2971. .rx_fixup = rndis_rx_fixup,
  2972. .tx_fixup = rndis_tx_fixup,
  2973. .reset = rndis_wlan_reset,
  2974. .stop = rndis_wlan_stop,
  2975. .early_init = bcm4320a_early_init,
  2976. .indication = rndis_wlan_indication,
  2977. };
  2978. static const struct driver_info rndis_wlan_info = {
  2979. .description = "Wireless RNDIS device",
  2980. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2981. FLAG_AVOID_UNLINK_URBS,
  2982. .bind = rndis_wlan_bind,
  2983. .unbind = rndis_wlan_unbind,
  2984. .status = rndis_status,
  2985. .rx_fixup = rndis_rx_fixup,
  2986. .tx_fixup = rndis_tx_fixup,
  2987. .reset = rndis_wlan_reset,
  2988. .stop = rndis_wlan_stop,
  2989. .early_init = unknown_early_init,
  2990. .indication = rndis_wlan_indication,
  2991. };
  2992. /*-------------------------------------------------------------------------*/
  2993. static const struct usb_device_id products [] = {
  2994. #define RNDIS_MASTER_INTERFACE \
  2995. .bInterfaceClass = USB_CLASS_COMM, \
  2996. .bInterfaceSubClass = 2 /* ACM */, \
  2997. .bInterfaceProtocol = 0x0ff
  2998. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2999. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  3000. */
  3001. {
  3002. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3003. | USB_DEVICE_ID_MATCH_DEVICE,
  3004. .idVendor = 0x0411,
  3005. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  3006. RNDIS_MASTER_INTERFACE,
  3007. .driver_info = (unsigned long) &bcm4320b_info,
  3008. }, {
  3009. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3010. | USB_DEVICE_ID_MATCH_DEVICE,
  3011. .idVendor = 0x0baf,
  3012. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  3013. RNDIS_MASTER_INTERFACE,
  3014. .driver_info = (unsigned long) &bcm4320b_info,
  3015. }, {
  3016. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3017. | USB_DEVICE_ID_MATCH_DEVICE,
  3018. .idVendor = 0x050d,
  3019. .idProduct = 0x011b, /* Belkin F5D7051 */
  3020. RNDIS_MASTER_INTERFACE,
  3021. .driver_info = (unsigned long) &bcm4320b_info,
  3022. }, {
  3023. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3024. | USB_DEVICE_ID_MATCH_DEVICE,
  3025. .idVendor = 0x1799, /* Belkin has two vendor ids */
  3026. .idProduct = 0x011b, /* Belkin F5D7051 */
  3027. RNDIS_MASTER_INTERFACE,
  3028. .driver_info = (unsigned long) &bcm4320b_info,
  3029. }, {
  3030. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3031. | USB_DEVICE_ID_MATCH_DEVICE,
  3032. .idVendor = 0x13b1,
  3033. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  3034. RNDIS_MASTER_INTERFACE,
  3035. .driver_info = (unsigned long) &bcm4320b_info,
  3036. }, {
  3037. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3038. | USB_DEVICE_ID_MATCH_DEVICE,
  3039. .idVendor = 0x13b1,
  3040. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  3041. RNDIS_MASTER_INTERFACE,
  3042. .driver_info = (unsigned long) &bcm4320b_info,
  3043. }, {
  3044. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3045. | USB_DEVICE_ID_MATCH_DEVICE,
  3046. .idVendor = 0x0b05,
  3047. .idProduct = 0x1717, /* Asus WL169gE */
  3048. RNDIS_MASTER_INTERFACE,
  3049. .driver_info = (unsigned long) &bcm4320b_info,
  3050. }, {
  3051. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3052. | USB_DEVICE_ID_MATCH_DEVICE,
  3053. .idVendor = 0x0a5c,
  3054. .idProduct = 0xd11b, /* Eminent EM4045 */
  3055. RNDIS_MASTER_INTERFACE,
  3056. .driver_info = (unsigned long) &bcm4320b_info,
  3057. }, {
  3058. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3059. | USB_DEVICE_ID_MATCH_DEVICE,
  3060. .idVendor = 0x1690,
  3061. .idProduct = 0x0715, /* BT Voyager 1055 */
  3062. RNDIS_MASTER_INTERFACE,
  3063. .driver_info = (unsigned long) &bcm4320b_info,
  3064. },
  3065. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  3066. * parameters available, hardware probably contain older firmware version with
  3067. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  3068. */
  3069. {
  3070. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3071. | USB_DEVICE_ID_MATCH_DEVICE,
  3072. .idVendor = 0x13b1,
  3073. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  3074. RNDIS_MASTER_INTERFACE,
  3075. .driver_info = (unsigned long) &bcm4320a_info,
  3076. }, {
  3077. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3078. | USB_DEVICE_ID_MATCH_DEVICE,
  3079. .idVendor = 0x0baf,
  3080. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  3081. RNDIS_MASTER_INTERFACE,
  3082. .driver_info = (unsigned long) &bcm4320a_info,
  3083. }, {
  3084. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3085. | USB_DEVICE_ID_MATCH_DEVICE,
  3086. .idVendor = 0x0411,
  3087. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  3088. RNDIS_MASTER_INTERFACE,
  3089. .driver_info = (unsigned long) &bcm4320a_info,
  3090. },
  3091. /* Generic Wireless RNDIS devices that we don't have exact
  3092. * idVendor/idProduct/chip yet.
  3093. */
  3094. {
  3095. /* RNDIS is MSFT's un-official variant of CDC ACM */
  3096. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  3097. .driver_info = (unsigned long) &rndis_wlan_info,
  3098. }, {
  3099. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  3100. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  3101. .driver_info = (unsigned long) &rndis_wlan_info,
  3102. },
  3103. { }, // END
  3104. };
  3105. MODULE_DEVICE_TABLE(usb, products);
  3106. static struct usb_driver rndis_wlan_driver = {
  3107. .name = "rndis_wlan",
  3108. .id_table = products,
  3109. .probe = usbnet_probe,
  3110. .disconnect = usbnet_disconnect,
  3111. .suspend = usbnet_suspend,
  3112. .resume = usbnet_resume,
  3113. .disable_hub_initiated_lpm = 1,
  3114. };
  3115. module_usb_driver(rndis_wlan_driver);
  3116. MODULE_AUTHOR("Bjorge Dijkstra");
  3117. MODULE_AUTHOR("Jussi Kivilinna");
  3118. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  3119. MODULE_LICENSE("GPL");