rndis_wlan.c 97 KB

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