rndis_wlan.c 100 KB

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