rndis_wlan.c 101 KB

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