rndis_wlan.c 99 KB

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