rndis_wlan.c 99 KB

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