rndis_wlan.c 102 KB

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