rndis_wlan.c 101 KB

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