rndis_wlan.c 97 KB

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