rndis_wlan.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251
  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 <net/iw_handler.h>
  43. #include <net/cfg80211.h>
  44. #include <linux/usb/usbnet.h>
  45. #include <linux/usb/rndis_host.h>
  46. /* NOTE: All these are settings for Broadcom chipset */
  47. static char modparam_country[4] = "EU";
  48. module_param_string(country, modparam_country, 4, 0444);
  49. MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
  50. static int modparam_frameburst = 1;
  51. module_param_named(frameburst, modparam_frameburst, int, 0444);
  52. MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
  53. static int modparam_afterburner = 0;
  54. module_param_named(afterburner, modparam_afterburner, int, 0444);
  55. MODULE_PARM_DESC(afterburner,
  56. "enable afterburner aka '125 High Speed Mode' (default: off)");
  57. static int modparam_power_save = 0;
  58. module_param_named(power_save, modparam_power_save, int, 0444);
  59. MODULE_PARM_DESC(power_save,
  60. "set power save mode: 0=off, 1=on, 2=fast (default: off)");
  61. static int modparam_power_output = 3;
  62. module_param_named(power_output, modparam_power_output, int, 0444);
  63. MODULE_PARM_DESC(power_output,
  64. "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
  65. static int modparam_roamtrigger = -70;
  66. module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
  67. MODULE_PARM_DESC(roamtrigger,
  68. "set roaming dBm trigger: -80=optimize for distance, "
  69. "-60=bandwidth (default: -70)");
  70. static int modparam_roamdelta = 1;
  71. module_param_named(roamdelta, modparam_roamdelta, int, 0444);
  72. MODULE_PARM_DESC(roamdelta,
  73. "set roaming tendency: 0=aggressive, 1=moderate, "
  74. "2=conservative (default: moderate)");
  75. static int modparam_workaround_interval = 500;
  76. module_param_named(workaround_interval, modparam_workaround_interval,
  77. int, 0444);
  78. MODULE_PARM_DESC(workaround_interval,
  79. "set stall workaround interval in msecs (default: 500)");
  80. /* various RNDIS OID defs */
  81. #define OID_GEN_LINK_SPEED cpu_to_le32(0x00010107)
  82. #define OID_GEN_RNDIS_CONFIG_PARAMETER cpu_to_le32(0x0001021b)
  83. #define OID_GEN_XMIT_OK cpu_to_le32(0x00020101)
  84. #define OID_GEN_RCV_OK cpu_to_le32(0x00020102)
  85. #define OID_GEN_XMIT_ERROR cpu_to_le32(0x00020103)
  86. #define OID_GEN_RCV_ERROR cpu_to_le32(0x00020104)
  87. #define OID_GEN_RCV_NO_BUFFER cpu_to_le32(0x00020105)
  88. #define OID_802_3_CURRENT_ADDRESS cpu_to_le32(0x01010102)
  89. #define OID_802_3_MULTICAST_LIST cpu_to_le32(0x01010103)
  90. #define OID_802_3_MAXIMUM_LIST_SIZE cpu_to_le32(0x01010104)
  91. #define OID_802_11_BSSID cpu_to_le32(0x0d010101)
  92. #define OID_802_11_SSID cpu_to_le32(0x0d010102)
  93. #define OID_802_11_INFRASTRUCTURE_MODE cpu_to_le32(0x0d010108)
  94. #define OID_802_11_ADD_WEP cpu_to_le32(0x0d010113)
  95. #define OID_802_11_REMOVE_WEP cpu_to_le32(0x0d010114)
  96. #define OID_802_11_DISASSOCIATE cpu_to_le32(0x0d010115)
  97. #define OID_802_11_AUTHENTICATION_MODE cpu_to_le32(0x0d010118)
  98. #define OID_802_11_PRIVACY_FILTER cpu_to_le32(0x0d010119)
  99. #define OID_802_11_BSSID_LIST_SCAN cpu_to_le32(0x0d01011a)
  100. #define OID_802_11_ENCRYPTION_STATUS cpu_to_le32(0x0d01011b)
  101. #define OID_802_11_ADD_KEY cpu_to_le32(0x0d01011d)
  102. #define OID_802_11_REMOVE_KEY cpu_to_le32(0x0d01011e)
  103. #define OID_802_11_ASSOCIATION_INFORMATION cpu_to_le32(0x0d01011f)
  104. #define OID_802_11_PMKID cpu_to_le32(0x0d010123)
  105. #define OID_802_11_NETWORK_TYPES_SUPPORTED cpu_to_le32(0x0d010203)
  106. #define OID_802_11_NETWORK_TYPE_IN_USE cpu_to_le32(0x0d010204)
  107. #define OID_802_11_TX_POWER_LEVEL cpu_to_le32(0x0d010205)
  108. #define OID_802_11_RSSI cpu_to_le32(0x0d010206)
  109. #define OID_802_11_RSSI_TRIGGER cpu_to_le32(0x0d010207)
  110. #define OID_802_11_FRAGMENTATION_THRESHOLD cpu_to_le32(0x0d010209)
  111. #define OID_802_11_RTS_THRESHOLD cpu_to_le32(0x0d01020a)
  112. #define OID_802_11_SUPPORTED_RATES cpu_to_le32(0x0d01020e)
  113. #define OID_802_11_CONFIGURATION cpu_to_le32(0x0d010211)
  114. #define OID_802_11_BSSID_LIST cpu_to_le32(0x0d010217)
  115. /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
  116. #define WL_NOISE -96 /* typical noise level in dBm */
  117. #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
  118. /* Assume that Broadcom 4320 (only chipset at time of writing known to be
  119. * based on wireless rndis) has default txpower of 13dBm.
  120. * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
  121. * 100% : 20 mW ~ 13dBm
  122. * 75% : 15 mW ~ 12dBm
  123. * 50% : 10 mW ~ 10dBm
  124. * 25% : 5 mW ~ 7dBm
  125. */
  126. #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
  127. #define BCM4320_DEFAULT_TXPOWER_DBM_75 12
  128. #define BCM4320_DEFAULT_TXPOWER_DBM_50 10
  129. #define BCM4320_DEFAULT_TXPOWER_DBM_25 7
  130. /* codes for "status" field of completion messages */
  131. #define RNDIS_STATUS_ADAPTER_NOT_READY cpu_to_le32(0xc0010011)
  132. #define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(0xc0010012)
  133. /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
  134. * slightly modified for datatype endianess, etc
  135. */
  136. #define NDIS_802_11_LENGTH_SSID 32
  137. #define NDIS_802_11_LENGTH_RATES 8
  138. #define NDIS_802_11_LENGTH_RATES_EX 16
  139. enum ndis_80211_net_type {
  140. NDIS_80211_TYPE_FREQ_HOP,
  141. NDIS_80211_TYPE_DIRECT_SEQ,
  142. NDIS_80211_TYPE_OFDM_A,
  143. NDIS_80211_TYPE_OFDM_G
  144. };
  145. enum ndis_80211_net_infra {
  146. NDIS_80211_INFRA_ADHOC,
  147. NDIS_80211_INFRA_INFRA,
  148. NDIS_80211_INFRA_AUTO_UNKNOWN
  149. };
  150. enum ndis_80211_auth_mode {
  151. NDIS_80211_AUTH_OPEN,
  152. NDIS_80211_AUTH_SHARED,
  153. NDIS_80211_AUTH_AUTO_SWITCH,
  154. NDIS_80211_AUTH_WPA,
  155. NDIS_80211_AUTH_WPA_PSK,
  156. NDIS_80211_AUTH_WPA_NONE,
  157. NDIS_80211_AUTH_WPA2,
  158. NDIS_80211_AUTH_WPA2_PSK
  159. };
  160. enum ndis_80211_encr_status {
  161. NDIS_80211_ENCR_WEP_ENABLED,
  162. NDIS_80211_ENCR_DISABLED,
  163. NDIS_80211_ENCR_WEP_KEY_ABSENT,
  164. NDIS_80211_ENCR_NOT_SUPPORTED,
  165. NDIS_80211_ENCR_TKIP_ENABLED,
  166. NDIS_80211_ENCR_TKIP_KEY_ABSENT,
  167. NDIS_80211_ENCR_CCMP_ENABLED,
  168. NDIS_80211_ENCR_CCMP_KEY_ABSENT
  169. };
  170. enum ndis_80211_priv_filter {
  171. NDIS_80211_PRIV_ACCEPT_ALL,
  172. NDIS_80211_PRIV_8021X_WEP
  173. };
  174. enum ndis_80211_status_type {
  175. NDIS_80211_STATUSTYPE_AUTHENTICATION,
  176. NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
  177. NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
  178. NDIS_80211_STATUSTYPE_RADIOSTATE,
  179. };
  180. enum ndis_80211_media_stream_mode {
  181. NDIS_80211_MEDIA_STREAM_OFF,
  182. NDIS_80211_MEDIA_STREAM_ON
  183. };
  184. enum ndis_80211_radio_status {
  185. NDIS_80211_RADIO_STATUS_ON,
  186. NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
  187. NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
  188. };
  189. enum ndis_80211_addkey_bits {
  190. NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
  191. NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
  192. NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
  193. NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  194. };
  195. enum ndis_80211_addwep_bits {
  196. NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
  197. NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  198. };
  199. struct ndis_80211_auth_request {
  200. __le32 length;
  201. u8 bssid[6];
  202. u8 padding[2];
  203. __le32 flags;
  204. } __attribute__((packed));
  205. struct ndis_80211_pmkid_candidate {
  206. u8 bssid[6];
  207. u8 padding[2];
  208. __le32 flags;
  209. } __attribute__((packed));
  210. struct ndis_80211_pmkid_cand_list {
  211. __le32 version;
  212. __le32 num_candidates;
  213. struct ndis_80211_pmkid_candidate candidate_list[0];
  214. } __attribute__((packed));
  215. struct ndis_80211_status_indication {
  216. __le32 status_type;
  217. union {
  218. enum ndis_80211_media_stream_mode media_stream_mode;
  219. enum ndis_80211_radio_status radio_status;
  220. struct ndis_80211_auth_request auth_request[0];
  221. struct ndis_80211_pmkid_cand_list cand_list;
  222. } u;
  223. } __attribute__((packed));
  224. struct ndis_80211_ssid {
  225. __le32 length;
  226. u8 essid[NDIS_802_11_LENGTH_SSID];
  227. } __attribute__((packed));
  228. struct ndis_80211_conf_freq_hop {
  229. __le32 length;
  230. __le32 hop_pattern;
  231. __le32 hop_set;
  232. __le32 dwell_time;
  233. } __attribute__((packed));
  234. struct ndis_80211_conf {
  235. __le32 length;
  236. __le32 beacon_period;
  237. __le32 atim_window;
  238. __le32 ds_config;
  239. struct ndis_80211_conf_freq_hop fh_config;
  240. } __attribute__((packed));
  241. struct ndis_80211_bssid_ex {
  242. __le32 length;
  243. u8 mac[6];
  244. u8 padding[2];
  245. struct ndis_80211_ssid ssid;
  246. __le32 privacy;
  247. __le32 rssi;
  248. __le32 net_type;
  249. struct ndis_80211_conf config;
  250. __le32 net_infra;
  251. u8 rates[NDIS_802_11_LENGTH_RATES_EX];
  252. __le32 ie_length;
  253. u8 ies[0];
  254. } __attribute__((packed));
  255. struct ndis_80211_bssid_list_ex {
  256. __le32 num_items;
  257. struct ndis_80211_bssid_ex bssid[0];
  258. } __attribute__((packed));
  259. struct ndis_80211_fixed_ies {
  260. u8 timestamp[8];
  261. __le16 beacon_interval;
  262. __le16 capabilities;
  263. } __attribute__((packed));
  264. struct ndis_80211_wep_key {
  265. __le32 size;
  266. __le32 index;
  267. __le32 length;
  268. u8 material[32];
  269. } __attribute__((packed));
  270. struct ndis_80211_key {
  271. __le32 size;
  272. __le32 index;
  273. __le32 length;
  274. u8 bssid[6];
  275. u8 padding[6];
  276. u8 rsc[8];
  277. u8 material[32];
  278. } __attribute__((packed));
  279. struct ndis_80211_remove_key {
  280. __le32 size;
  281. __le32 index;
  282. u8 bssid[6];
  283. u8 padding[2];
  284. } __attribute__((packed));
  285. struct ndis_config_param {
  286. __le32 name_offs;
  287. __le32 name_length;
  288. __le32 type;
  289. __le32 value_offs;
  290. __le32 value_length;
  291. } __attribute__((packed));
  292. struct ndis_80211_assoc_info {
  293. __le32 length;
  294. __le16 req_ies;
  295. struct req_ie {
  296. __le16 capa;
  297. __le16 listen_interval;
  298. u8 cur_ap_address[6];
  299. } req_ie;
  300. __le32 req_ie_length;
  301. __le32 offset_req_ies;
  302. __le16 resp_ies;
  303. struct resp_ie {
  304. __le16 capa;
  305. __le16 status_code;
  306. __le16 assoc_id;
  307. } resp_ie;
  308. __le32 resp_ie_length;
  309. __le32 offset_resp_ies;
  310. } __attribute__((packed));
  311. /* these have to match what is in wpa_supplicant */
  312. enum wpa_alg { WPA_ALG_NONE, WPA_ALG_WEP, WPA_ALG_TKIP, WPA_ALG_CCMP };
  313. enum wpa_cipher { CIPHER_NONE, CIPHER_WEP40, CIPHER_TKIP, CIPHER_CCMP,
  314. CIPHER_WEP104 };
  315. enum wpa_key_mgmt { KEY_MGMT_802_1X, KEY_MGMT_PSK, KEY_MGMT_NONE,
  316. KEY_MGMT_802_1X_NO_WPA, KEY_MGMT_WPA_NONE };
  317. /*
  318. * private data
  319. */
  320. #define NET_TYPE_11FB 0
  321. #define CAP_MODE_80211A 1
  322. #define CAP_MODE_80211B 2
  323. #define CAP_MODE_80211G 4
  324. #define CAP_MODE_MASK 7
  325. #define WORK_LINK_UP (1<<0)
  326. #define WORK_LINK_DOWN (1<<1)
  327. #define WORK_SET_MULTICAST_LIST (1<<2)
  328. #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
  329. static const struct ieee80211_channel rndis_channels[] = {
  330. { .center_freq = 2412 },
  331. { .center_freq = 2417 },
  332. { .center_freq = 2422 },
  333. { .center_freq = 2427 },
  334. { .center_freq = 2432 },
  335. { .center_freq = 2437 },
  336. { .center_freq = 2442 },
  337. { .center_freq = 2447 },
  338. { .center_freq = 2452 },
  339. { .center_freq = 2457 },
  340. { .center_freq = 2462 },
  341. { .center_freq = 2467 },
  342. { .center_freq = 2472 },
  343. { .center_freq = 2484 },
  344. };
  345. static const struct ieee80211_rate rndis_rates[] = {
  346. { .bitrate = 10 },
  347. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  348. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  349. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  350. { .bitrate = 60 },
  351. { .bitrate = 90 },
  352. { .bitrate = 120 },
  353. { .bitrate = 180 },
  354. { .bitrate = 240 },
  355. { .bitrate = 360 },
  356. { .bitrate = 480 },
  357. { .bitrate = 540 }
  358. };
  359. static const u32 rndis_cipher_suites[] = {
  360. WLAN_CIPHER_SUITE_WEP40,
  361. WLAN_CIPHER_SUITE_WEP104,
  362. WLAN_CIPHER_SUITE_TKIP,
  363. WLAN_CIPHER_SUITE_CCMP,
  364. };
  365. struct rndis_wlan_encr_key {
  366. int len;
  367. int cipher;
  368. u8 material[32];
  369. u8 bssid[ETH_ALEN];
  370. bool pairwise;
  371. bool tx_key;
  372. };
  373. /* RNDIS device private data */
  374. struct rndis_wlan_private {
  375. struct usbnet *usbdev;
  376. struct wireless_dev wdev;
  377. struct cfg80211_scan_request *scan_request;
  378. struct workqueue_struct *workqueue;
  379. struct delayed_work stats_work;
  380. struct delayed_work scan_work;
  381. struct work_struct work;
  382. struct mutex command_lock;
  383. spinlock_t stats_lock;
  384. unsigned long work_pending;
  385. struct ieee80211_supported_band band;
  386. struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
  387. struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
  388. u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
  389. struct iw_statistics iwstats;
  390. struct iw_statistics privstats;
  391. int caps;
  392. int multicast_size;
  393. /* module parameters */
  394. char param_country[4];
  395. int param_frameburst;
  396. int param_afterburner;
  397. int param_power_save;
  398. int param_power_output;
  399. int param_roamtrigger;
  400. int param_roamdelta;
  401. u32 param_workaround_interval;
  402. /* hardware state */
  403. int radio_on;
  404. int infra_mode;
  405. struct ndis_80211_ssid essid;
  406. __le32 current_command_oid;
  407. /* encryption stuff */
  408. int encr_tx_key_index;
  409. struct rndis_wlan_encr_key encr_keys[4];
  410. int wpa_version;
  411. int wpa_keymgmt;
  412. int wpa_authalg;
  413. int wpa_ie_len;
  414. u8 *wpa_ie;
  415. int wpa_cipher_pair;
  416. int wpa_cipher_group;
  417. u8 command_buffer[COMMAND_BUFFER_SIZE];
  418. };
  419. /*
  420. * cfg80211 ops
  421. */
  422. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  423. struct net_device *dev,
  424. enum nl80211_iftype type, u32 *flags,
  425. struct vif_params *params);
  426. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  427. struct cfg80211_scan_request *request);
  428. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
  429. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  430. int dbm);
  431. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
  432. static struct cfg80211_ops rndis_config_ops = {
  433. .change_virtual_intf = rndis_change_virtual_intf,
  434. .scan = rndis_scan,
  435. .set_wiphy_params = rndis_set_wiphy_params,
  436. .set_tx_power = rndis_set_tx_power,
  437. .get_tx_power = rndis_get_tx_power,
  438. };
  439. static void *rndis_wiphy_privid = &rndis_wiphy_privid;
  440. static const unsigned char zero_bssid[ETH_ALEN] = {0,};
  441. static const unsigned char ffff_bssid[ETH_ALEN] = { 0xff, 0xff, 0xff,
  442. 0xff, 0xff, 0xff };
  443. static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
  444. {
  445. return (struct rndis_wlan_private *)dev->driver_priv;
  446. }
  447. static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
  448. {
  449. switch (priv->param_power_output) {
  450. default:
  451. case 3:
  452. return BCM4320_DEFAULT_TXPOWER_DBM_100;
  453. case 2:
  454. return BCM4320_DEFAULT_TXPOWER_DBM_75;
  455. case 1:
  456. return BCM4320_DEFAULT_TXPOWER_DBM_50;
  457. case 0:
  458. return BCM4320_DEFAULT_TXPOWER_DBM_25;
  459. }
  460. }
  461. static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
  462. {
  463. int cipher = priv->encr_keys[idx].cipher;
  464. return (cipher == WLAN_CIPHER_SUITE_CCMP ||
  465. cipher == WLAN_CIPHER_SUITE_TKIP);
  466. }
  467. #ifdef DEBUG
  468. static const char *oid_to_string(__le32 oid)
  469. {
  470. switch (oid) {
  471. #define OID_STR(oid) case oid: return(#oid)
  472. /* from rndis_host.h */
  473. OID_STR(OID_802_3_PERMANENT_ADDRESS);
  474. OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
  475. OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
  476. OID_STR(OID_GEN_PHYSICAL_MEDIUM);
  477. /* from rndis_wlan.c */
  478. OID_STR(OID_GEN_LINK_SPEED);
  479. OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
  480. OID_STR(OID_GEN_XMIT_OK);
  481. OID_STR(OID_GEN_RCV_OK);
  482. OID_STR(OID_GEN_XMIT_ERROR);
  483. OID_STR(OID_GEN_RCV_ERROR);
  484. OID_STR(OID_GEN_RCV_NO_BUFFER);
  485. OID_STR(OID_802_3_CURRENT_ADDRESS);
  486. OID_STR(OID_802_3_MULTICAST_LIST);
  487. OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
  488. OID_STR(OID_802_11_BSSID);
  489. OID_STR(OID_802_11_SSID);
  490. OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
  491. OID_STR(OID_802_11_ADD_WEP);
  492. OID_STR(OID_802_11_REMOVE_WEP);
  493. OID_STR(OID_802_11_DISASSOCIATE);
  494. OID_STR(OID_802_11_AUTHENTICATION_MODE);
  495. OID_STR(OID_802_11_PRIVACY_FILTER);
  496. OID_STR(OID_802_11_BSSID_LIST_SCAN);
  497. OID_STR(OID_802_11_ENCRYPTION_STATUS);
  498. OID_STR(OID_802_11_ADD_KEY);
  499. OID_STR(OID_802_11_REMOVE_KEY);
  500. OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
  501. OID_STR(OID_802_11_PMKID);
  502. OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
  503. OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
  504. OID_STR(OID_802_11_TX_POWER_LEVEL);
  505. OID_STR(OID_802_11_RSSI);
  506. OID_STR(OID_802_11_RSSI_TRIGGER);
  507. OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
  508. OID_STR(OID_802_11_RTS_THRESHOLD);
  509. OID_STR(OID_802_11_SUPPORTED_RATES);
  510. OID_STR(OID_802_11_CONFIGURATION);
  511. OID_STR(OID_802_11_BSSID_LIST);
  512. #undef OID_STR
  513. }
  514. return "?";
  515. }
  516. #else
  517. static const char *oid_to_string(__le32 oid)
  518. {
  519. return "?";
  520. }
  521. #endif
  522. /* translate error code */
  523. static int rndis_error_status(__le32 rndis_status)
  524. {
  525. int ret = -EINVAL;
  526. switch (rndis_status) {
  527. case RNDIS_STATUS_SUCCESS:
  528. ret = 0;
  529. break;
  530. case RNDIS_STATUS_FAILURE:
  531. case RNDIS_STATUS_INVALID_DATA:
  532. ret = -EINVAL;
  533. break;
  534. case RNDIS_STATUS_NOT_SUPPORTED:
  535. ret = -EOPNOTSUPP;
  536. break;
  537. case RNDIS_STATUS_ADAPTER_NOT_READY:
  538. case RNDIS_STATUS_ADAPTER_NOT_OPEN:
  539. ret = -EBUSY;
  540. break;
  541. }
  542. return ret;
  543. }
  544. static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
  545. {
  546. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  547. union {
  548. void *buf;
  549. struct rndis_msg_hdr *header;
  550. struct rndis_query *get;
  551. struct rndis_query_c *get_c;
  552. } u;
  553. int ret, buflen;
  554. buflen = *len + sizeof(*u.get);
  555. if (buflen < CONTROL_BUFFER_SIZE)
  556. buflen = CONTROL_BUFFER_SIZE;
  557. if (buflen > COMMAND_BUFFER_SIZE) {
  558. u.buf = kmalloc(buflen, GFP_KERNEL);
  559. if (!u.buf)
  560. return -ENOMEM;
  561. } else {
  562. u.buf = priv->command_buffer;
  563. }
  564. mutex_lock(&priv->command_lock);
  565. memset(u.get, 0, sizeof *u.get);
  566. u.get->msg_type = RNDIS_MSG_QUERY;
  567. u.get->msg_len = cpu_to_le32(sizeof *u.get);
  568. u.get->oid = oid;
  569. priv->current_command_oid = oid;
  570. ret = rndis_command(dev, u.header, buflen);
  571. priv->current_command_oid = 0;
  572. if (ret < 0)
  573. devdbg(dev, "rndis_query_oid(%s): rndis_command() failed, %d "
  574. "(%08x)", oid_to_string(oid), ret,
  575. le32_to_cpu(u.get_c->status));
  576. if (ret == 0) {
  577. ret = le32_to_cpu(u.get_c->len);
  578. if (ret > *len)
  579. *len = ret;
  580. memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
  581. ret = rndis_error_status(u.get_c->status);
  582. if (ret < 0)
  583. devdbg(dev, "rndis_query_oid(%s): device returned "
  584. "error, 0x%08x (%d)", oid_to_string(oid),
  585. le32_to_cpu(u.get_c->status), ret);
  586. }
  587. mutex_unlock(&priv->command_lock);
  588. if (u.buf != priv->command_buffer)
  589. kfree(u.buf);
  590. return ret;
  591. }
  592. static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
  593. {
  594. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  595. union {
  596. void *buf;
  597. struct rndis_msg_hdr *header;
  598. struct rndis_set *set;
  599. struct rndis_set_c *set_c;
  600. } u;
  601. int ret, buflen;
  602. buflen = len + sizeof(*u.set);
  603. if (buflen < CONTROL_BUFFER_SIZE)
  604. buflen = CONTROL_BUFFER_SIZE;
  605. if (buflen > COMMAND_BUFFER_SIZE) {
  606. u.buf = kmalloc(buflen, GFP_KERNEL);
  607. if (!u.buf)
  608. return -ENOMEM;
  609. } else {
  610. u.buf = priv->command_buffer;
  611. }
  612. mutex_lock(&priv->command_lock);
  613. memset(u.set, 0, sizeof *u.set);
  614. u.set->msg_type = RNDIS_MSG_SET;
  615. u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
  616. u.set->oid = oid;
  617. u.set->len = cpu_to_le32(len);
  618. u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
  619. u.set->handle = cpu_to_le32(0);
  620. memcpy(u.buf + sizeof(*u.set), data, len);
  621. priv->current_command_oid = oid;
  622. ret = rndis_command(dev, u.header, buflen);
  623. priv->current_command_oid = 0;
  624. if (ret < 0)
  625. devdbg(dev, "rndis_set_oid(%s): rndis_command() failed, %d "
  626. "(%08x)", oid_to_string(oid), ret,
  627. le32_to_cpu(u.set_c->status));
  628. if (ret == 0) {
  629. ret = rndis_error_status(u.set_c->status);
  630. if (ret < 0)
  631. devdbg(dev, "rndis_set_oid(%s): device returned error, "
  632. "0x%08x (%d)", oid_to_string(oid),
  633. le32_to_cpu(u.set_c->status), ret);
  634. }
  635. mutex_unlock(&priv->command_lock);
  636. if (u.buf != priv->command_buffer)
  637. kfree(u.buf);
  638. return ret;
  639. }
  640. static int rndis_reset(struct usbnet *usbdev)
  641. {
  642. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  643. struct rndis_reset *reset;
  644. int ret;
  645. mutex_lock(&priv->command_lock);
  646. reset = (void *)priv->command_buffer;
  647. memset(reset, 0, sizeof(*reset));
  648. reset->msg_type = RNDIS_MSG_RESET;
  649. reset->msg_len = cpu_to_le32(sizeof(*reset));
  650. priv->current_command_oid = 0;
  651. ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
  652. mutex_unlock(&priv->command_lock);
  653. if (ret < 0)
  654. return ret;
  655. return 0;
  656. }
  657. /*
  658. * Specs say that we can only set config parameters only soon after device
  659. * initialization.
  660. * value_type: 0 = u32, 2 = unicode string
  661. */
  662. static int rndis_set_config_parameter(struct usbnet *dev, char *param,
  663. int value_type, void *value)
  664. {
  665. struct ndis_config_param *infobuf;
  666. int value_len, info_len, param_len, ret, i;
  667. __le16 *unibuf;
  668. __le32 *dst_value;
  669. if (value_type == 0)
  670. value_len = sizeof(__le32);
  671. else if (value_type == 2)
  672. value_len = strlen(value) * sizeof(__le16);
  673. else
  674. return -EINVAL;
  675. param_len = strlen(param) * sizeof(__le16);
  676. info_len = sizeof(*infobuf) + param_len + value_len;
  677. #ifdef DEBUG
  678. info_len += 12;
  679. #endif
  680. infobuf = kmalloc(info_len, GFP_KERNEL);
  681. if (!infobuf)
  682. return -ENOMEM;
  683. #ifdef DEBUG
  684. info_len -= 12;
  685. /* extra 12 bytes are for padding (debug output) */
  686. memset(infobuf, 0xCC, info_len + 12);
  687. #endif
  688. if (value_type == 2)
  689. devdbg(dev, "setting config parameter: %s, value: %s",
  690. param, (u8 *)value);
  691. else
  692. devdbg(dev, "setting config parameter: %s, value: %d",
  693. param, *(u32 *)value);
  694. infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
  695. infobuf->name_length = cpu_to_le32(param_len);
  696. infobuf->type = cpu_to_le32(value_type);
  697. infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
  698. infobuf->value_length = cpu_to_le32(value_len);
  699. /* simple string to unicode string conversion */
  700. unibuf = (void *)infobuf + sizeof(*infobuf);
  701. for (i = 0; i < param_len / sizeof(__le16); i++)
  702. unibuf[i] = cpu_to_le16(param[i]);
  703. if (value_type == 2) {
  704. unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
  705. for (i = 0; i < value_len / sizeof(__le16); i++)
  706. unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
  707. } else {
  708. dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
  709. *dst_value = cpu_to_le32(*(u32 *)value);
  710. }
  711. #ifdef DEBUG
  712. devdbg(dev, "info buffer (len: %d):", info_len);
  713. for (i = 0; i < info_len; i += 12) {
  714. u32 *tmp = (u32 *)((u8 *)infobuf + i);
  715. devdbg(dev, "%08X:%08X:%08X",
  716. cpu_to_be32(tmp[0]),
  717. cpu_to_be32(tmp[1]),
  718. cpu_to_be32(tmp[2]));
  719. }
  720. #endif
  721. ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
  722. infobuf, info_len);
  723. if (ret != 0)
  724. devdbg(dev, "setting rndis config parameter failed, %d.", ret);
  725. kfree(infobuf);
  726. return ret;
  727. }
  728. static int rndis_set_config_parameter_str(struct usbnet *dev,
  729. char *param, char *value)
  730. {
  731. return(rndis_set_config_parameter(dev, param, 2, value));
  732. }
  733. /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
  734. char *param, u32 value)
  735. {
  736. return(rndis_set_config_parameter(dev, param, 0, &value));
  737. }*/
  738. /*
  739. * data conversion functions
  740. */
  741. static int level_to_qual(int level)
  742. {
  743. int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
  744. return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
  745. }
  746. static void dsconfig_to_freq(unsigned int dsconfig, struct iw_freq *freq)
  747. {
  748. freq->e = 0;
  749. freq->i = 0;
  750. freq->flags = 0;
  751. /* see comment in wireless.h above the "struct iw_freq"
  752. * definition for an explanation of this if
  753. * NOTE: 1000000 is due to the kHz
  754. */
  755. if (dsconfig > 1000000) {
  756. freq->m = dsconfig / 10;
  757. freq->e = 1;
  758. } else
  759. freq->m = dsconfig;
  760. /* convert from kHz to Hz */
  761. freq->e += 3;
  762. }
  763. static int freq_to_dsconfig(struct iw_freq *freq, unsigned int *dsconfig)
  764. {
  765. if (freq->m < 1000 && freq->e == 0) {
  766. if (freq->m >= 1 && freq->m <= 14)
  767. *dsconfig = ieee80211_dsss_chan_to_freq(freq->m) * 1000;
  768. else
  769. return -1;
  770. } else {
  771. int i;
  772. *dsconfig = freq->m;
  773. for (i = freq->e; i > 0; i--)
  774. *dsconfig *= 10;
  775. *dsconfig /= 1000;
  776. }
  777. return 0;
  778. }
  779. /*
  780. * common functions
  781. */
  782. static void restore_keys(struct usbnet *usbdev);
  783. static int rndis_check_bssid_list(struct usbnet *usbdev);
  784. static int get_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  785. {
  786. int ret, len;
  787. len = sizeof(*ssid);
  788. ret = rndis_query_oid(usbdev, OID_802_11_SSID, ssid, &len);
  789. if (ret != 0)
  790. ssid->length = 0;
  791. #ifdef DEBUG
  792. {
  793. unsigned char tmp[NDIS_802_11_LENGTH_SSID + 1];
  794. memcpy(tmp, ssid->essid, le32_to_cpu(ssid->length));
  795. tmp[le32_to_cpu(ssid->length)] = 0;
  796. devdbg(usbdev, "get_essid: '%s', ret: %d", tmp, ret);
  797. }
  798. #endif
  799. return ret;
  800. }
  801. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  802. {
  803. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  804. int ret;
  805. ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
  806. if (ret == 0) {
  807. memcpy(&priv->essid, ssid, sizeof(priv->essid));
  808. priv->radio_on = 1;
  809. devdbg(usbdev, "set_essid: radio_on = 1");
  810. }
  811. return ret;
  812. }
  813. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  814. {
  815. int ret, len;
  816. len = ETH_ALEN;
  817. ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
  818. if (ret != 0)
  819. memset(bssid, 0, ETH_ALEN);
  820. return ret;
  821. }
  822. static int get_association_info(struct usbnet *usbdev,
  823. struct ndis_80211_assoc_info *info, int len)
  824. {
  825. return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
  826. info, &len);
  827. }
  828. static int is_associated(struct usbnet *usbdev)
  829. {
  830. u8 bssid[ETH_ALEN];
  831. int ret;
  832. ret = get_bssid(usbdev, bssid);
  833. return(ret == 0 && memcmp(bssid, zero_bssid, ETH_ALEN) != 0);
  834. }
  835. static int disassociate(struct usbnet *usbdev, int reset_ssid)
  836. {
  837. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  838. struct ndis_80211_ssid ssid;
  839. int i, ret = 0;
  840. if (priv->radio_on) {
  841. ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
  842. if (ret == 0) {
  843. priv->radio_on = 0;
  844. devdbg(usbdev, "disassociate: radio_on = 0");
  845. if (reset_ssid)
  846. msleep(100);
  847. }
  848. }
  849. /* disassociate causes radio to be turned off; if reset_ssid
  850. * is given, set random ssid to enable radio */
  851. if (reset_ssid) {
  852. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  853. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  854. ssid.essid[0] = 0x1;
  855. ssid.essid[1] = 0xff;
  856. for (i = 2; i < sizeof(ssid.essid); i++)
  857. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  858. ret = set_essid(usbdev, &ssid);
  859. }
  860. return ret;
  861. }
  862. static int set_auth_mode(struct usbnet *usbdev, int wpa_version, int authalg)
  863. {
  864. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  865. __le32 tmp;
  866. int auth_mode, ret;
  867. devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
  868. "keymgmt=0x%x", wpa_version, authalg, priv->wpa_keymgmt);
  869. if (wpa_version & IW_AUTH_WPA_VERSION_WPA2) {
  870. if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
  871. auth_mode = NDIS_80211_AUTH_WPA2;
  872. else
  873. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  874. } else if (wpa_version & IW_AUTH_WPA_VERSION_WPA) {
  875. if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
  876. auth_mode = NDIS_80211_AUTH_WPA;
  877. else if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_PSK)
  878. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  879. else
  880. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  881. } else if (authalg & IW_AUTH_ALG_SHARED_KEY) {
  882. if (authalg & IW_AUTH_ALG_OPEN_SYSTEM)
  883. auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
  884. else
  885. auth_mode = NDIS_80211_AUTH_SHARED;
  886. } else
  887. auth_mode = NDIS_80211_AUTH_OPEN;
  888. tmp = cpu_to_le32(auth_mode);
  889. ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
  890. sizeof(tmp));
  891. if (ret != 0) {
  892. devwarn(usbdev, "setting auth mode failed (%08X)", ret);
  893. return ret;
  894. }
  895. priv->wpa_version = wpa_version;
  896. priv->wpa_authalg = authalg;
  897. return 0;
  898. }
  899. static int set_priv_filter(struct usbnet *usbdev)
  900. {
  901. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  902. __le32 tmp;
  903. devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
  904. if (priv->wpa_version & IW_AUTH_WPA_VERSION_WPA2 ||
  905. priv->wpa_version & IW_AUTH_WPA_VERSION_WPA)
  906. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  907. else
  908. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  909. return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
  910. sizeof(tmp));
  911. }
  912. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  913. {
  914. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  915. __le32 tmp;
  916. int encr_mode, ret;
  917. devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
  918. pairwise,
  919. groupwise);
  920. if (pairwise & IW_AUTH_CIPHER_CCMP)
  921. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  922. else if (pairwise & IW_AUTH_CIPHER_TKIP)
  923. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  924. else if (pairwise &
  925. (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
  926. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  927. else if (groupwise & IW_AUTH_CIPHER_CCMP)
  928. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  929. else if (groupwise & IW_AUTH_CIPHER_TKIP)
  930. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  931. else
  932. encr_mode = NDIS_80211_ENCR_DISABLED;
  933. tmp = cpu_to_le32(encr_mode);
  934. ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
  935. sizeof(tmp));
  936. if (ret != 0) {
  937. devwarn(usbdev, "setting encr mode failed (%08X)", ret);
  938. return ret;
  939. }
  940. priv->wpa_cipher_pair = pairwise;
  941. priv->wpa_cipher_group = groupwise;
  942. return 0;
  943. }
  944. static int set_assoc_params(struct usbnet *usbdev)
  945. {
  946. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  947. set_auth_mode(usbdev, priv->wpa_version, priv->wpa_authalg);
  948. set_priv_filter(usbdev);
  949. set_encr_mode(usbdev, priv->wpa_cipher_pair, priv->wpa_cipher_group);
  950. return 0;
  951. }
  952. static int set_infra_mode(struct usbnet *usbdev, int mode)
  953. {
  954. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  955. __le32 tmp;
  956. int ret;
  957. devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
  958. tmp = cpu_to_le32(mode);
  959. ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
  960. sizeof(tmp));
  961. if (ret != 0) {
  962. devwarn(usbdev, "setting infra mode failed (%08X)", ret);
  963. return ret;
  964. }
  965. /* NDIS drivers clear keys when infrastructure mode is
  966. * changed. But Linux tools assume otherwise. So set the
  967. * keys */
  968. restore_keys(usbdev);
  969. priv->infra_mode = mode;
  970. return 0;
  971. }
  972. static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
  973. {
  974. __le32 tmp;
  975. devdbg(usbdev, "set_rts_threshold %i", rts_threshold);
  976. if (rts_threshold < 0 || rts_threshold > 2347)
  977. rts_threshold = 2347;
  978. tmp = cpu_to_le32(rts_threshold);
  979. return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
  980. sizeof(tmp));
  981. }
  982. static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
  983. {
  984. __le32 tmp;
  985. devdbg(usbdev, "set_frag_threshold %i", frag_threshold);
  986. if (frag_threshold < 256 || frag_threshold > 2346)
  987. frag_threshold = 2346;
  988. tmp = cpu_to_le32(frag_threshold);
  989. return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
  990. sizeof(tmp));
  991. }
  992. static void set_default_iw_params(struct usbnet *usbdev)
  993. {
  994. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  995. priv->wpa_keymgmt = 0;
  996. priv->wpa_version = 0;
  997. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  998. set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  999. IW_AUTH_ALG_OPEN_SYSTEM);
  1000. set_priv_filter(usbdev);
  1001. set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
  1002. }
  1003. static int deauthenticate(struct usbnet *usbdev)
  1004. {
  1005. int ret;
  1006. ret = disassociate(usbdev, 1);
  1007. set_default_iw_params(usbdev);
  1008. return ret;
  1009. }
  1010. /* index must be 0 - N, as per NDIS */
  1011. static int add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index)
  1012. {
  1013. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1014. struct ndis_80211_wep_key ndis_key;
  1015. int cipher, ret;
  1016. if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
  1017. return -EINVAL;
  1018. if (key_len == 5)
  1019. cipher = WLAN_CIPHER_SUITE_WEP40;
  1020. else
  1021. cipher = WLAN_CIPHER_SUITE_WEP104;
  1022. memset(&ndis_key, 0, sizeof(ndis_key));
  1023. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1024. ndis_key.length = cpu_to_le32(key_len);
  1025. ndis_key.index = cpu_to_le32(index);
  1026. memcpy(&ndis_key.material, key, key_len);
  1027. if (index == priv->encr_tx_key_index) {
  1028. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1029. ret = set_encr_mode(usbdev, IW_AUTH_CIPHER_WEP104,
  1030. IW_AUTH_CIPHER_NONE);
  1031. if (ret)
  1032. devwarn(usbdev, "encryption couldn't be enabled (%08X)",
  1033. ret);
  1034. }
  1035. ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
  1036. sizeof(ndis_key));
  1037. if (ret != 0) {
  1038. devwarn(usbdev, "adding encryption key %d failed (%08X)",
  1039. index+1, ret);
  1040. return ret;
  1041. }
  1042. priv->encr_keys[index].len = key_len;
  1043. priv->encr_keys[index].cipher = cipher;
  1044. memcpy(&priv->encr_keys[index].material, key, key_len);
  1045. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1046. return 0;
  1047. }
  1048. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1049. int index, const u8 *addr, const u8 *rx_seq, int cipher,
  1050. int flags)
  1051. {
  1052. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1053. struct ndis_80211_key ndis_key;
  1054. bool is_addr_ok;
  1055. int ret;
  1056. if (index < 0 || index >= 4) {
  1057. devdbg(usbdev, "add_wpa_key: index out of range (%i)", index);
  1058. return -EINVAL;
  1059. }
  1060. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1061. devdbg(usbdev, "add_wpa_key: key length out of range (%i)",
  1062. key_len);
  1063. return -EINVAL;
  1064. }
  1065. if ((flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) && !rx_seq) {
  1066. devdbg(usbdev, "add_wpa_key: recv seq flag without buffer");
  1067. return -EINVAL;
  1068. }
  1069. is_addr_ok = addr && memcmp(addr, zero_bssid, ETH_ALEN) != 0 &&
  1070. memcmp(addr, ffff_bssid, ETH_ALEN) != 0;
  1071. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1072. devdbg(usbdev, "add_wpa_key: pairwise but bssid invalid (%pM)",
  1073. addr);
  1074. return -EINVAL;
  1075. }
  1076. devdbg(usbdev, "add_wpa_key(%i): flags:%i%i%i", index,
  1077. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1078. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1079. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1080. memset(&ndis_key, 0, sizeof(ndis_key));
  1081. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1082. sizeof(ndis_key.material) + key_len);
  1083. ndis_key.length = cpu_to_le32(key_len);
  1084. ndis_key.index = cpu_to_le32(index) | flags;
  1085. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1086. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1087. * different order than NDIS spec, so swap the order here. */
  1088. memcpy(ndis_key.material, key, 16);
  1089. memcpy(ndis_key.material + 16, key + 24, 8);
  1090. memcpy(ndis_key.material + 24, key + 16, 8);
  1091. } else
  1092. memcpy(ndis_key.material, key, key_len);
  1093. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1094. memcpy(ndis_key.rsc, rx_seq, 6);
  1095. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1096. /* pairwise key */
  1097. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1098. } else {
  1099. /* group key */
  1100. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1101. memset(ndis_key.bssid, 0xff, ETH_ALEN);
  1102. else
  1103. get_bssid(usbdev, ndis_key.bssid);
  1104. }
  1105. ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
  1106. le32_to_cpu(ndis_key.size));
  1107. devdbg(usbdev, "add_wpa_key: OID_802_11_ADD_KEY -> %08X", ret);
  1108. if (ret != 0)
  1109. return ret;
  1110. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1111. priv->encr_keys[index].len = key_len;
  1112. priv->encr_keys[index].cipher = cipher;
  1113. memcpy(&priv->encr_keys[index].material, key, key_len);
  1114. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1115. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1116. else
  1117. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1118. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1119. priv->encr_tx_key_index = index;
  1120. return 0;
  1121. }
  1122. static int restore_key(struct usbnet *usbdev, int key_idx)
  1123. {
  1124. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1125. struct rndis_wlan_encr_key key;
  1126. int flags;
  1127. key = priv->encr_keys[key_idx];
  1128. devdbg(usbdev, "restore_key: %i:%s:%i", key_idx,
  1129. is_wpa_key(priv, key_idx) ? "wpa" : "wep",
  1130. key.len);
  1131. if (key.len == 0)
  1132. return 0;
  1133. if (is_wpa_key(priv, key_idx)) {
  1134. flags = 0;
  1135. /*if (priv->encr_tx_key_index == key_idx)
  1136. flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;*/
  1137. if (memcmp(key.bssid, zero_bssid, ETH_ALEN) != 0 &&
  1138. memcmp(key.bssid, ffff_bssid, ETH_ALEN) != 0)
  1139. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1140. return add_wpa_key(usbdev, key.material, key.len, key_idx,
  1141. key.bssid, NULL, key.cipher, flags);
  1142. }
  1143. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1144. }
  1145. static void restore_keys(struct usbnet *usbdev)
  1146. {
  1147. int i;
  1148. for (i = 0; i < 4; i++)
  1149. restore_key(usbdev, i);
  1150. }
  1151. static void clear_key(struct rndis_wlan_private *priv, int idx)
  1152. {
  1153. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1154. }
  1155. /* remove_key is for both wep and wpa */
  1156. static int remove_key(struct usbnet *usbdev, int index, u8 bssid[ETH_ALEN])
  1157. {
  1158. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1159. struct ndis_80211_remove_key remove_key;
  1160. __le32 keyindex;
  1161. bool is_wpa;
  1162. int ret;
  1163. if (priv->encr_keys[index].len == 0)
  1164. return 0;
  1165. is_wpa = is_wpa_key(priv, index);
  1166. devdbg(usbdev, "remove_key: %i:%s:%i", index, is_wpa ? "wpa" : "wep",
  1167. priv->encr_keys[index].len);
  1168. clear_key(priv, index);
  1169. if (is_wpa) {
  1170. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1171. remove_key.index = cpu_to_le32(index);
  1172. if (bssid) {
  1173. /* pairwise key */
  1174. if (memcmp(bssid, ffff_bssid, ETH_ALEN) != 0)
  1175. remove_key.index |=
  1176. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1177. memcpy(remove_key.bssid, bssid,
  1178. sizeof(remove_key.bssid));
  1179. } else
  1180. memset(remove_key.bssid, 0xff,
  1181. sizeof(remove_key.bssid));
  1182. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
  1183. sizeof(remove_key));
  1184. if (ret != 0)
  1185. return ret;
  1186. } else {
  1187. keyindex = cpu_to_le32(index);
  1188. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
  1189. sizeof(keyindex));
  1190. if (ret != 0) {
  1191. devwarn(usbdev,
  1192. "removing encryption key %d failed (%08X)",
  1193. index, ret);
  1194. return ret;
  1195. }
  1196. }
  1197. /* if it is transmit key, disable encryption */
  1198. if (index == priv->encr_tx_key_index)
  1199. set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
  1200. return 0;
  1201. }
  1202. static void set_multicast_list(struct usbnet *usbdev)
  1203. {
  1204. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1205. struct dev_mc_list *mclist;
  1206. __le32 filter;
  1207. int ret, i, size;
  1208. char *buf;
  1209. filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  1210. if (usbdev->net->flags & IFF_PROMISC) {
  1211. filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
  1212. RNDIS_PACKET_TYPE_ALL_LOCAL;
  1213. } else if (usbdev->net->flags & IFF_ALLMULTI ||
  1214. usbdev->net->mc_count > priv->multicast_size) {
  1215. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1216. } else if (usbdev->net->mc_count > 0) {
  1217. size = min(priv->multicast_size, usbdev->net->mc_count);
  1218. buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
  1219. if (!buf) {
  1220. devwarn(usbdev,
  1221. "couldn't alloc %d bytes of memory",
  1222. size * ETH_ALEN);
  1223. return;
  1224. }
  1225. mclist = usbdev->net->mc_list;
  1226. for (i = 0; i < size && mclist; mclist = mclist->next) {
  1227. if (mclist->dmi_addrlen != ETH_ALEN)
  1228. continue;
  1229. memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
  1230. i++;
  1231. }
  1232. ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
  1233. i * ETH_ALEN);
  1234. if (ret == 0 && i > 0)
  1235. filter |= RNDIS_PACKET_TYPE_MULTICAST;
  1236. else
  1237. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1238. devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
  1239. i, priv->multicast_size, ret);
  1240. kfree(buf);
  1241. }
  1242. ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1243. sizeof(filter));
  1244. if (ret < 0) {
  1245. devwarn(usbdev, "couldn't set packet filter: %08x",
  1246. le32_to_cpu(filter));
  1247. }
  1248. devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
  1249. le32_to_cpu(filter), ret);
  1250. }
  1251. /*
  1252. * cfg80211 ops
  1253. */
  1254. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1255. struct net_device *dev,
  1256. enum nl80211_iftype type, u32 *flags,
  1257. struct vif_params *params)
  1258. {
  1259. struct usbnet *usbdev = netdev_priv(dev);
  1260. int mode;
  1261. switch (type) {
  1262. case NL80211_IFTYPE_ADHOC:
  1263. mode = NDIS_80211_INFRA_ADHOC;
  1264. break;
  1265. case NL80211_IFTYPE_STATION:
  1266. mode = NDIS_80211_INFRA_INFRA;
  1267. break;
  1268. default:
  1269. return -EINVAL;
  1270. }
  1271. return set_infra_mode(usbdev, mode);
  1272. }
  1273. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1274. {
  1275. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1276. struct usbnet *usbdev = priv->usbdev;
  1277. int err;
  1278. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1279. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1280. if (err < 0)
  1281. return err;
  1282. }
  1283. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1284. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1285. if (err < 0)
  1286. return err;
  1287. }
  1288. return 0;
  1289. }
  1290. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  1291. int dbm)
  1292. {
  1293. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1294. struct usbnet *usbdev = priv->usbdev;
  1295. devdbg(usbdev, "rndis_set_tx_power type:0x%x dbm:%i", type, dbm);
  1296. /* Device doesn't support changing txpower after initialization, only
  1297. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1298. * currently used.
  1299. */
  1300. if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
  1301. if (!priv->radio_on)
  1302. disassociate(usbdev, 1); /* turn on radio */
  1303. return 0;
  1304. }
  1305. return -ENOTSUPP;
  1306. }
  1307. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
  1308. {
  1309. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1310. struct usbnet *usbdev = priv->usbdev;
  1311. *dbm = get_bcm4320_power_dbm(priv);
  1312. devdbg(usbdev, "rndis_get_tx_power dbm:%i", *dbm);
  1313. return 0;
  1314. }
  1315. #define SCAN_DELAY_JIFFIES (HZ)
  1316. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  1317. struct cfg80211_scan_request *request)
  1318. {
  1319. struct usbnet *usbdev = netdev_priv(dev);
  1320. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1321. int ret;
  1322. __le32 tmp;
  1323. devdbg(usbdev, "cfg80211.scan");
  1324. /* Get current bssid list from device before new scan, as new scan
  1325. * clears internal bssid list.
  1326. */
  1327. rndis_check_bssid_list(usbdev);
  1328. if (!request)
  1329. return -EINVAL;
  1330. if (priv->scan_request && priv->scan_request != request)
  1331. return -EBUSY;
  1332. priv->scan_request = request;
  1333. tmp = cpu_to_le32(1);
  1334. ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  1335. sizeof(tmp));
  1336. if (ret == 0) {
  1337. /* Wait before retrieving scan results from device */
  1338. queue_delayed_work(priv->workqueue, &priv->scan_work,
  1339. SCAN_DELAY_JIFFIES);
  1340. }
  1341. return ret;
  1342. }
  1343. static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
  1344. struct ndis_80211_bssid_ex *bssid)
  1345. {
  1346. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1347. struct ieee80211_channel *channel;
  1348. s32 signal;
  1349. u64 timestamp;
  1350. u16 capability;
  1351. u16 beacon_interval;
  1352. struct ndis_80211_fixed_ies *fixed;
  1353. int ie_len, bssid_len;
  1354. u8 *ie;
  1355. devdbg(usbdev, " found bssid: '%.32s' [%pM]", bssid->ssid.essid,
  1356. bssid->mac);
  1357. /* parse bssid structure */
  1358. bssid_len = le32_to_cpu(bssid->length);
  1359. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1360. sizeof(struct ndis_80211_fixed_ies))
  1361. return NULL;
  1362. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1363. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1364. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1365. (int)le32_to_cpu(bssid->ie_length));
  1366. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1367. if (ie_len < 0)
  1368. return NULL;
  1369. /* extract data for cfg80211_inform_bss */
  1370. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1371. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1372. if (!channel)
  1373. return NULL;
  1374. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1375. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1376. capability = le16_to_cpu(fixed->capabilities);
  1377. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1378. return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1379. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1380. GFP_KERNEL);
  1381. }
  1382. static int rndis_check_bssid_list(struct usbnet *usbdev)
  1383. {
  1384. void *buf = NULL;
  1385. struct ndis_80211_bssid_list_ex *bssid_list;
  1386. struct ndis_80211_bssid_ex *bssid;
  1387. int ret = -EINVAL, len, count, bssid_len;
  1388. bool resized = false;
  1389. devdbg(usbdev, "check_bssid_list");
  1390. len = CONTROL_BUFFER_SIZE;
  1391. resize_buf:
  1392. buf = kmalloc(len, GFP_KERNEL);
  1393. if (!buf) {
  1394. ret = -ENOMEM;
  1395. goto out;
  1396. }
  1397. ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  1398. if (ret != 0)
  1399. goto out;
  1400. if (!resized && len > CONTROL_BUFFER_SIZE) {
  1401. resized = true;
  1402. kfree(buf);
  1403. goto resize_buf;
  1404. }
  1405. bssid_list = buf;
  1406. bssid = bssid_list->bssid;
  1407. bssid_len = le32_to_cpu(bssid->length);
  1408. count = le32_to_cpu(bssid_list->num_items);
  1409. devdbg(usbdev, "check_bssid_list: %d BSSIDs found (buflen: %d)", count,
  1410. len);
  1411. while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
  1412. rndis_bss_info_update(usbdev, bssid);
  1413. bssid = (void *)bssid + bssid_len;
  1414. bssid_len = le32_to_cpu(bssid->length);
  1415. count--;
  1416. }
  1417. out:
  1418. kfree(buf);
  1419. return ret;
  1420. }
  1421. static void rndis_get_scan_results(struct work_struct *work)
  1422. {
  1423. struct rndis_wlan_private *priv =
  1424. container_of(work, struct rndis_wlan_private, scan_work.work);
  1425. struct usbnet *usbdev = priv->usbdev;
  1426. int ret;
  1427. devdbg(usbdev, "get_scan_results");
  1428. if (!priv->scan_request)
  1429. return;
  1430. ret = rndis_check_bssid_list(usbdev);
  1431. cfg80211_scan_done(priv->scan_request, ret < 0);
  1432. priv->scan_request = NULL;
  1433. }
  1434. /*
  1435. * wireless extension handlers
  1436. */
  1437. static int rndis_iw_commit(struct net_device *dev,
  1438. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1439. {
  1440. /* dummy op */
  1441. return 0;
  1442. }
  1443. static int rndis_iw_set_essid(struct net_device *dev,
  1444. struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
  1445. {
  1446. struct ndis_80211_ssid ssid;
  1447. int length = wrqu->essid.length;
  1448. struct usbnet *usbdev = netdev_priv(dev);
  1449. devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
  1450. wrqu->essid.flags, wrqu->essid.length, essid);
  1451. if (length > NDIS_802_11_LENGTH_SSID)
  1452. length = NDIS_802_11_LENGTH_SSID;
  1453. ssid.length = cpu_to_le32(length);
  1454. if (length > 0)
  1455. memcpy(ssid.essid, essid, length);
  1456. else
  1457. memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
  1458. set_assoc_params(usbdev);
  1459. if (!wrqu->essid.flags || length == 0)
  1460. return disassociate(usbdev, 1);
  1461. else {
  1462. /* Pause and purge rx queue, so we don't pass packets before
  1463. * 'media connect'-indication.
  1464. */
  1465. usbnet_pause_rx(usbdev);
  1466. usbnet_purge_paused_rxq(usbdev);
  1467. return set_essid(usbdev, &ssid);
  1468. }
  1469. }
  1470. static int rndis_iw_get_essid(struct net_device *dev,
  1471. struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
  1472. {
  1473. struct ndis_80211_ssid ssid;
  1474. struct usbnet *usbdev = netdev_priv(dev);
  1475. int ret;
  1476. ret = get_essid(usbdev, &ssid);
  1477. if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
  1478. wrqu->essid.flags = 1;
  1479. wrqu->essid.length = le32_to_cpu(ssid.length);
  1480. memcpy(essid, ssid.essid, wrqu->essid.length);
  1481. essid[wrqu->essid.length] = 0;
  1482. } else {
  1483. memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
  1484. wrqu->essid.flags = 0;
  1485. wrqu->essid.length = 0;
  1486. }
  1487. devdbg(usbdev, "SIOCGIWESSID: %s", essid);
  1488. return ret;
  1489. }
  1490. static int rndis_iw_get_bssid(struct net_device *dev,
  1491. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1492. {
  1493. struct usbnet *usbdev = netdev_priv(dev);
  1494. unsigned char bssid[ETH_ALEN];
  1495. int ret;
  1496. ret = get_bssid(usbdev, bssid);
  1497. if (ret == 0)
  1498. devdbg(usbdev, "SIOCGIWAP: %pM", bssid);
  1499. else
  1500. devdbg(usbdev, "SIOCGIWAP: <not associated>");
  1501. wrqu->ap_addr.sa_family = ARPHRD_ETHER;
  1502. memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
  1503. return ret;
  1504. }
  1505. static int rndis_iw_set_bssid(struct net_device *dev,
  1506. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1507. {
  1508. struct usbnet *usbdev = netdev_priv(dev);
  1509. u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
  1510. int ret;
  1511. devdbg(usbdev, "SIOCSIWAP: %pM", bssid);
  1512. ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
  1513. /* user apps may set ap's mac address, which is not required;
  1514. * they may fail to work if this function fails, so return
  1515. * success */
  1516. if (ret)
  1517. devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
  1518. return 0;
  1519. }
  1520. static int rndis_iw_set_auth(struct net_device *dev,
  1521. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1522. {
  1523. struct iw_param *p = &wrqu->param;
  1524. struct usbnet *usbdev = netdev_priv(dev);
  1525. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1526. int ret = -ENOTSUPP;
  1527. switch (p->flags & IW_AUTH_INDEX) {
  1528. case IW_AUTH_WPA_VERSION:
  1529. devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
  1530. priv->wpa_version = p->value;
  1531. ret = 0;
  1532. break;
  1533. case IW_AUTH_CIPHER_PAIRWISE:
  1534. devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
  1535. priv->wpa_cipher_pair = p->value;
  1536. ret = 0;
  1537. break;
  1538. case IW_AUTH_CIPHER_GROUP:
  1539. devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
  1540. priv->wpa_cipher_group = p->value;
  1541. ret = 0;
  1542. break;
  1543. case IW_AUTH_KEY_MGMT:
  1544. devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
  1545. priv->wpa_keymgmt = p->value;
  1546. ret = 0;
  1547. break;
  1548. case IW_AUTH_TKIP_COUNTERMEASURES:
  1549. devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
  1550. p->value);
  1551. ret = 0;
  1552. break;
  1553. case IW_AUTH_DROP_UNENCRYPTED:
  1554. devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
  1555. ret = 0;
  1556. break;
  1557. case IW_AUTH_80211_AUTH_ALG:
  1558. devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
  1559. priv->wpa_authalg = p->value;
  1560. ret = 0;
  1561. break;
  1562. case IW_AUTH_WPA_ENABLED:
  1563. devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
  1564. if (wrqu->param.value)
  1565. deauthenticate(usbdev);
  1566. ret = 0;
  1567. break;
  1568. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  1569. devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
  1570. p->value);
  1571. ret = 0;
  1572. break;
  1573. case IW_AUTH_ROAMING_CONTROL:
  1574. devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
  1575. ret = 0;
  1576. break;
  1577. case IW_AUTH_PRIVACY_INVOKED:
  1578. devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
  1579. wrqu->param.flags & IW_AUTH_INDEX);
  1580. return -EOPNOTSUPP;
  1581. default:
  1582. devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
  1583. p->flags & IW_AUTH_INDEX, p->value);
  1584. }
  1585. return ret;
  1586. }
  1587. static int rndis_iw_get_auth(struct net_device *dev,
  1588. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1589. {
  1590. struct iw_param *p = &wrqu->param;
  1591. struct usbnet *usbdev = netdev_priv(dev);
  1592. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1593. switch (p->flags & IW_AUTH_INDEX) {
  1594. case IW_AUTH_WPA_VERSION:
  1595. p->value = priv->wpa_version;
  1596. break;
  1597. case IW_AUTH_CIPHER_PAIRWISE:
  1598. p->value = priv->wpa_cipher_pair;
  1599. break;
  1600. case IW_AUTH_CIPHER_GROUP:
  1601. p->value = priv->wpa_cipher_group;
  1602. break;
  1603. case IW_AUTH_KEY_MGMT:
  1604. p->value = priv->wpa_keymgmt;
  1605. break;
  1606. case IW_AUTH_80211_AUTH_ALG:
  1607. p->value = priv->wpa_authalg;
  1608. break;
  1609. default:
  1610. devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
  1611. wrqu->param.flags & IW_AUTH_INDEX);
  1612. return -EOPNOTSUPP;
  1613. }
  1614. return 0;
  1615. }
  1616. static int rndis_iw_set_encode(struct net_device *dev,
  1617. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1618. {
  1619. struct usbnet *usbdev = netdev_priv(dev);
  1620. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1621. struct rndis_wlan_encr_key key;
  1622. int ret, index, key_len;
  1623. u8 *keybuf;
  1624. index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
  1625. /* iwconfig gives index as 1 - N */
  1626. if (index > 0)
  1627. index--;
  1628. else
  1629. index = priv->encr_tx_key_index;
  1630. if (index < 0 || index >= 4) {
  1631. devwarn(usbdev, "encryption index out of range (%u)", index);
  1632. return -EINVAL;
  1633. }
  1634. /* remove key if disabled */
  1635. if (wrqu->data.flags & IW_ENCODE_DISABLED) {
  1636. if (remove_key(usbdev, index, NULL))
  1637. return -EINVAL;
  1638. else
  1639. return 0;
  1640. }
  1641. /* global encryption state (for all keys) */
  1642. if (wrqu->data.flags & IW_ENCODE_OPEN)
  1643. ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1644. IW_AUTH_ALG_OPEN_SYSTEM);
  1645. else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
  1646. ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1647. IW_AUTH_ALG_SHARED_KEY);
  1648. if (ret != 0)
  1649. return ret;
  1650. if (wrqu->data.length > 0) {
  1651. key_len = wrqu->data.length;
  1652. keybuf = extra;
  1653. } else {
  1654. /* must be set as tx key */
  1655. if (priv->encr_keys[index].len == 0)
  1656. return -EINVAL;
  1657. key = priv->encr_keys[index];
  1658. key_len = key.len;
  1659. keybuf = key.material;
  1660. priv->encr_tx_key_index = index;
  1661. }
  1662. if (add_wep_key(usbdev, keybuf, key_len, index) != 0)
  1663. return -EINVAL;
  1664. if (index == priv->encr_tx_key_index)
  1665. /* ndis drivers want essid to be set after setting encr */
  1666. set_essid(usbdev, &priv->essid);
  1667. return 0;
  1668. }
  1669. static int rndis_iw_set_encode_ext(struct net_device *dev,
  1670. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1671. {
  1672. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1673. struct usbnet *usbdev = netdev_priv(dev);
  1674. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1675. int keyidx, flags, cipher;
  1676. keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
  1677. /* iwconfig gives index as 1 - N */
  1678. if (keyidx)
  1679. keyidx--;
  1680. else
  1681. keyidx = priv->encr_tx_key_index;
  1682. if (keyidx < 0 || keyidx >= 4) {
  1683. devwarn(usbdev, "encryption index out of range (%u)", keyidx);
  1684. return -EINVAL;
  1685. }
  1686. if (ext->alg == WPA_ALG_WEP) {
  1687. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
  1688. priv->encr_tx_key_index = keyidx;
  1689. return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
  1690. }
  1691. cipher = -1;
  1692. if (ext->alg == IW_ENCODE_ALG_TKIP)
  1693. cipher = WLAN_CIPHER_SUITE_TKIP;
  1694. else if (ext->alg == IW_ENCODE_ALG_CCMP)
  1695. cipher = WLAN_CIPHER_SUITE_CCMP;
  1696. if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
  1697. ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
  1698. return remove_key(usbdev, keyidx, NULL);
  1699. if (cipher == -1)
  1700. return -EOPNOTSUPP;
  1701. flags = 0;
  1702. if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
  1703. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  1704. if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY))
  1705. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1706. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
  1707. flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;
  1708. return add_wpa_key(usbdev, ext->key, ext->key_len, keyidx,
  1709. (u8 *)&ext->addr.sa_data, ext->rx_seq, cipher,
  1710. flags);
  1711. }
  1712. static int rndis_iw_set_genie(struct net_device *dev,
  1713. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1714. {
  1715. struct usbnet *usbdev = netdev_priv(dev);
  1716. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1717. int ret = 0;
  1718. #ifdef DEBUG
  1719. int j;
  1720. u8 *gie = extra;
  1721. for (j = 0; j < wrqu->data.length; j += 8)
  1722. devdbg(usbdev,
  1723. "SIOCSIWGENIE %04x - "
  1724. "%02x %02x %02x %02x %02x %02x %02x %02x", j,
  1725. gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
  1726. gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
  1727. #endif
  1728. /* clear existing IEs */
  1729. if (priv->wpa_ie_len) {
  1730. kfree(priv->wpa_ie);
  1731. priv->wpa_ie_len = 0;
  1732. }
  1733. /* set new IEs */
  1734. priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
  1735. if (priv->wpa_ie) {
  1736. priv->wpa_ie_len = wrqu->data.length;
  1737. memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
  1738. } else
  1739. ret = -ENOMEM;
  1740. return ret;
  1741. }
  1742. static int rndis_iw_get_genie(struct net_device *dev,
  1743. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1744. {
  1745. struct usbnet *usbdev = netdev_priv(dev);
  1746. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1747. devdbg(usbdev, "SIOCGIWGENIE");
  1748. if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
  1749. wrqu->data.length = 0;
  1750. return 0;
  1751. }
  1752. if (wrqu->data.length < priv->wpa_ie_len)
  1753. return -E2BIG;
  1754. wrqu->data.length = priv->wpa_ie_len;
  1755. memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
  1756. return 0;
  1757. }
  1758. static int rndis_iw_set_freq(struct net_device *dev,
  1759. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1760. {
  1761. struct usbnet *usbdev = netdev_priv(dev);
  1762. struct ndis_80211_conf config;
  1763. unsigned int dsconfig;
  1764. int len, ret;
  1765. /* this OID is valid only when not associated */
  1766. if (is_associated(usbdev))
  1767. return 0;
  1768. dsconfig = 0;
  1769. if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
  1770. return -EINVAL;
  1771. len = sizeof(config);
  1772. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1773. if (ret != 0) {
  1774. devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
  1775. return 0;
  1776. }
  1777. config.ds_config = cpu_to_le32(dsconfig);
  1778. devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
  1779. return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
  1780. sizeof(config));
  1781. }
  1782. static int rndis_iw_get_freq(struct net_device *dev,
  1783. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1784. {
  1785. struct usbnet *usbdev = netdev_priv(dev);
  1786. struct ndis_80211_conf config;
  1787. int len, ret;
  1788. len = sizeof(config);
  1789. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1790. if (ret == 0)
  1791. dsconfig_to_freq(le32_to_cpu(config.ds_config), &wrqu->freq);
  1792. devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
  1793. return ret;
  1794. }
  1795. static int rndis_iw_get_rate(struct net_device *dev,
  1796. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1797. {
  1798. struct usbnet *usbdev = netdev_priv(dev);
  1799. __le32 tmp;
  1800. int ret, len;
  1801. len = sizeof(tmp);
  1802. ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
  1803. if (ret == 0) {
  1804. wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
  1805. wrqu->bitrate.disabled = 0;
  1806. wrqu->bitrate.flags = 1;
  1807. }
  1808. return ret;
  1809. }
  1810. static int rndis_iw_set_mlme(struct net_device *dev,
  1811. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1812. {
  1813. struct usbnet *usbdev = netdev_priv(dev);
  1814. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1815. struct iw_mlme *mlme = (struct iw_mlme *)extra;
  1816. unsigned char bssid[ETH_ALEN];
  1817. get_bssid(usbdev, bssid);
  1818. if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
  1819. return -EINVAL;
  1820. switch (mlme->cmd) {
  1821. case IW_MLME_DEAUTH:
  1822. return deauthenticate(usbdev);
  1823. case IW_MLME_DISASSOC:
  1824. return disassociate(usbdev, priv->radio_on);
  1825. default:
  1826. return -EOPNOTSUPP;
  1827. }
  1828. return 0;
  1829. }
  1830. static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
  1831. {
  1832. struct usbnet *usbdev = netdev_priv(dev);
  1833. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1834. unsigned long flags;
  1835. spin_lock_irqsave(&priv->stats_lock, flags);
  1836. memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
  1837. spin_unlock_irqrestore(&priv->stats_lock, flags);
  1838. return &priv->iwstats;
  1839. }
  1840. #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
  1841. static const iw_handler rndis_iw_handler[] =
  1842. {
  1843. IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
  1844. IW_IOCTL(SIOCGIWNAME) = (iw_handler) cfg80211_wext_giwname,
  1845. IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
  1846. IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
  1847. IW_IOCTL(SIOCSIWMODE) = (iw_handler) cfg80211_wext_siwmode,
  1848. IW_IOCTL(SIOCGIWMODE) = (iw_handler) cfg80211_wext_giwmode,
  1849. IW_IOCTL(SIOCGIWRANGE) = (iw_handler) cfg80211_wext_giwrange,
  1850. IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
  1851. IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
  1852. IW_IOCTL(SIOCSIWSCAN) = (iw_handler) cfg80211_wext_siwscan,
  1853. IW_IOCTL(SIOCGIWSCAN) = (iw_handler) cfg80211_wext_giwscan,
  1854. IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
  1855. IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
  1856. IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
  1857. IW_IOCTL(SIOCSIWRTS) = (iw_handler) cfg80211_wext_siwrts,
  1858. IW_IOCTL(SIOCGIWRTS) = (iw_handler) cfg80211_wext_giwrts,
  1859. IW_IOCTL(SIOCSIWFRAG) = (iw_handler) cfg80211_wext_siwfrag,
  1860. IW_IOCTL(SIOCGIWFRAG) = (iw_handler) cfg80211_wext_giwfrag,
  1861. IW_IOCTL(SIOCSIWTXPOW) = (iw_handler) cfg80211_wext_siwtxpower,
  1862. IW_IOCTL(SIOCGIWTXPOW) = (iw_handler) cfg80211_wext_giwtxpower,
  1863. IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
  1864. IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
  1865. IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
  1866. IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
  1867. IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
  1868. IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
  1869. IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
  1870. };
  1871. static const iw_handler rndis_wlan_private_handler[] = {
  1872. };
  1873. static const struct iw_priv_args rndis_wlan_private_args[] = {
  1874. };
  1875. static const struct iw_handler_def rndis_iw_handlers = {
  1876. .num_standard = ARRAY_SIZE(rndis_iw_handler),
  1877. .num_private = ARRAY_SIZE(rndis_wlan_private_handler),
  1878. .num_private_args = ARRAY_SIZE(rndis_wlan_private_args),
  1879. .standard = (iw_handler *)rndis_iw_handler,
  1880. .private = (iw_handler *)rndis_wlan_private_handler,
  1881. .private_args = (struct iw_priv_args *)rndis_wlan_private_args,
  1882. .get_wireless_stats = rndis_get_wireless_stats,
  1883. };
  1884. static void rndis_wlan_worker(struct work_struct *work)
  1885. {
  1886. struct rndis_wlan_private *priv =
  1887. container_of(work, struct rndis_wlan_private, work);
  1888. struct usbnet *usbdev = priv->usbdev;
  1889. union iwreq_data evt;
  1890. unsigned char bssid[ETH_ALEN];
  1891. struct ndis_80211_assoc_info *info;
  1892. int assoc_size = sizeof(*info) + IW_CUSTOM_MAX + 32;
  1893. int ret, offset;
  1894. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending)) {
  1895. netif_carrier_on(usbdev->net);
  1896. info = kzalloc(assoc_size, GFP_KERNEL);
  1897. if (!info)
  1898. goto get_bssid;
  1899. /* Get association info IEs from device and send them back to
  1900. * userspace. */
  1901. ret = get_association_info(usbdev, info, assoc_size);
  1902. if (!ret) {
  1903. evt.data.length = le32_to_cpu(info->req_ie_length);
  1904. if (evt.data.length > 0) {
  1905. offset = le32_to_cpu(info->offset_req_ies);
  1906. wireless_send_event(usbdev->net,
  1907. IWEVASSOCREQIE, &evt,
  1908. (char *)info + offset);
  1909. }
  1910. evt.data.length = le32_to_cpu(info->resp_ie_length);
  1911. if (evt.data.length > 0) {
  1912. offset = le32_to_cpu(info->offset_resp_ies);
  1913. wireless_send_event(usbdev->net,
  1914. IWEVASSOCRESPIE, &evt,
  1915. (char *)info + offset);
  1916. }
  1917. }
  1918. kfree(info);
  1919. get_bssid:
  1920. ret = get_bssid(usbdev, bssid);
  1921. if (!ret) {
  1922. evt.data.flags = 0;
  1923. evt.data.length = 0;
  1924. memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
  1925. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1926. }
  1927. usbnet_resume_rx(usbdev);
  1928. }
  1929. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending)) {
  1930. netif_carrier_off(usbdev->net);
  1931. evt.data.flags = 0;
  1932. evt.data.length = 0;
  1933. memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
  1934. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1935. }
  1936. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1937. set_multicast_list(usbdev);
  1938. }
  1939. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  1940. {
  1941. struct usbnet *usbdev = netdev_priv(dev);
  1942. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1943. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1944. return;
  1945. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  1946. queue_work(priv->workqueue, &priv->work);
  1947. }
  1948. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  1949. struct ndis_80211_status_indication *indication,
  1950. int len)
  1951. {
  1952. u8 *buf;
  1953. const char *type;
  1954. int flags, buflen;
  1955. bool pairwise_error, group_error;
  1956. struct ndis_80211_auth_request *auth_req;
  1957. /* must have at least one array entry */
  1958. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1959. sizeof(struct ndis_80211_auth_request)) {
  1960. devinfo(usbdev, "authentication indication: "
  1961. "too short message (%i)", len);
  1962. return;
  1963. }
  1964. buf = (void *)&indication->u.auth_request[0];
  1965. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  1966. while (buflen >= sizeof(*auth_req)) {
  1967. auth_req = (void *)buf;
  1968. type = "unknown";
  1969. flags = le32_to_cpu(auth_req->flags);
  1970. pairwise_error = false;
  1971. group_error = false;
  1972. if (flags & 0x1)
  1973. type = "reauth request";
  1974. if (flags & 0x2)
  1975. type = "key update request";
  1976. if (flags & 0x6) {
  1977. pairwise_error = true;
  1978. type = "pairwise_error";
  1979. }
  1980. if (flags & 0xe) {
  1981. group_error = true;
  1982. type = "group_error";
  1983. }
  1984. devinfo(usbdev, "authentication indication: %s (0x%08x)", type,
  1985. le32_to_cpu(auth_req->flags));
  1986. if (pairwise_error || group_error) {
  1987. union iwreq_data wrqu;
  1988. struct iw_michaelmicfailure micfailure;
  1989. memset(&micfailure, 0, sizeof(micfailure));
  1990. if (pairwise_error)
  1991. micfailure.flags |= IW_MICFAILURE_PAIRWISE;
  1992. if (group_error)
  1993. micfailure.flags |= IW_MICFAILURE_GROUP;
  1994. memcpy(micfailure.src_addr.sa_data, auth_req->bssid,
  1995. ETH_ALEN);
  1996. memset(&wrqu, 0, sizeof(wrqu));
  1997. wrqu.data.length = sizeof(micfailure);
  1998. wireless_send_event(usbdev->net, IWEVMICHAELMICFAILURE,
  1999. &wrqu, (u8 *)&micfailure);
  2000. }
  2001. buflen -= le32_to_cpu(auth_req->length);
  2002. buf += le32_to_cpu(auth_req->length);
  2003. }
  2004. }
  2005. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  2006. struct ndis_80211_status_indication *indication,
  2007. int len)
  2008. {
  2009. struct ndis_80211_pmkid_cand_list *cand_list;
  2010. int list_len, expected_len, i;
  2011. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2012. sizeof(struct ndis_80211_pmkid_cand_list)) {
  2013. devinfo(usbdev, "pmkid candidate list indication: "
  2014. "too short message (%i)", len);
  2015. return;
  2016. }
  2017. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  2018. sizeof(struct ndis_80211_pmkid_candidate);
  2019. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  2020. offsetof(struct ndis_80211_status_indication, u);
  2021. if (len < expected_len) {
  2022. devinfo(usbdev, "pmkid candidate list indication: "
  2023. "list larger than buffer (%i < %i)",
  2024. len, expected_len);
  2025. return;
  2026. }
  2027. cand_list = &indication->u.cand_list;
  2028. devinfo(usbdev, "pmkid candidate list indication: "
  2029. "version %i, candidates %i",
  2030. le32_to_cpu(cand_list->version),
  2031. le32_to_cpu(cand_list->num_candidates));
  2032. if (le32_to_cpu(cand_list->version) != 1)
  2033. return;
  2034. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  2035. struct iw_pmkid_cand pcand;
  2036. union iwreq_data wrqu;
  2037. struct ndis_80211_pmkid_candidate *cand =
  2038. &cand_list->candidate_list[i];
  2039. devdbg(usbdev, "cand[%i]: flags: 0x%08x, bssid: %pM",
  2040. i, le32_to_cpu(cand->flags), cand->bssid);
  2041. memset(&pcand, 0, sizeof(pcand));
  2042. if (le32_to_cpu(cand->flags) & 0x01)
  2043. pcand.flags |= IW_PMKID_CAND_PREAUTH;
  2044. pcand.index = i;
  2045. memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
  2046. memset(&wrqu, 0, sizeof(wrqu));
  2047. wrqu.data.length = sizeof(pcand);
  2048. wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
  2049. (u8 *)&pcand);
  2050. }
  2051. }
  2052. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  2053. struct rndis_indicate *msg, int buflen)
  2054. {
  2055. struct ndis_80211_status_indication *indication;
  2056. int len, offset;
  2057. offset = offsetof(struct rndis_indicate, status) +
  2058. le32_to_cpu(msg->offset);
  2059. len = le32_to_cpu(msg->length);
  2060. if (len < 8) {
  2061. devinfo(usbdev, "media specific indication, "
  2062. "ignore too short message (%i < 8)", len);
  2063. return;
  2064. }
  2065. if (offset + len > buflen) {
  2066. devinfo(usbdev, "media specific indication, "
  2067. "too large to fit to buffer (%i > %i)",
  2068. offset + len, buflen);
  2069. return;
  2070. }
  2071. indication = (void *)((u8 *)msg + offset);
  2072. switch (le32_to_cpu(indication->status_type)) {
  2073. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  2074. devinfo(usbdev, "radio state indication: %i",
  2075. le32_to_cpu(indication->u.radio_status));
  2076. return;
  2077. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  2078. devinfo(usbdev, "media stream mode indication: %i",
  2079. le32_to_cpu(indication->u.media_stream_mode));
  2080. return;
  2081. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  2082. rndis_wlan_auth_indication(usbdev, indication, len);
  2083. return;
  2084. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2085. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2086. return;
  2087. default:
  2088. devinfo(usbdev, "media specific indication: "
  2089. "unknown status type 0x%08x",
  2090. le32_to_cpu(indication->status_type));
  2091. }
  2092. }
  2093. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2094. {
  2095. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2096. struct rndis_indicate *msg = ind;
  2097. switch (msg->status) {
  2098. case RNDIS_STATUS_MEDIA_CONNECT:
  2099. if (priv->current_command_oid == OID_802_11_ADD_KEY) {
  2100. /* OID_802_11_ADD_KEY causes sometimes extra
  2101. * "media connect" indications which confuses driver
  2102. * and userspace to think that device is
  2103. * roaming/reassociating when it isn't.
  2104. */
  2105. devdbg(usbdev, "ignored OID_802_11_ADD_KEY triggered "
  2106. "'media connect'");
  2107. return;
  2108. }
  2109. usbnet_pause_rx(usbdev);
  2110. devinfo(usbdev, "media connect");
  2111. /* queue work to avoid recursive calls into rndis_command */
  2112. set_bit(WORK_LINK_UP, &priv->work_pending);
  2113. queue_work(priv->workqueue, &priv->work);
  2114. break;
  2115. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2116. devinfo(usbdev, "media disconnect");
  2117. /* queue work to avoid recursive calls into rndis_command */
  2118. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2119. queue_work(priv->workqueue, &priv->work);
  2120. break;
  2121. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2122. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2123. break;
  2124. default:
  2125. devinfo(usbdev, "indication: 0x%08x",
  2126. le32_to_cpu(msg->status));
  2127. break;
  2128. }
  2129. }
  2130. static int rndis_wlan_get_caps(struct usbnet *usbdev)
  2131. {
  2132. struct {
  2133. __le32 num_items;
  2134. __le32 items[8];
  2135. } networks_supported;
  2136. int len, retval, i, n;
  2137. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2138. /* determine supported modes */
  2139. len = sizeof(networks_supported);
  2140. retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
  2141. &networks_supported, &len);
  2142. if (retval >= 0) {
  2143. n = le32_to_cpu(networks_supported.num_items);
  2144. if (n > 8)
  2145. n = 8;
  2146. for (i = 0; i < n; i++) {
  2147. switch (le32_to_cpu(networks_supported.items[i])) {
  2148. case NDIS_80211_TYPE_FREQ_HOP:
  2149. case NDIS_80211_TYPE_DIRECT_SEQ:
  2150. priv->caps |= CAP_MODE_80211B;
  2151. break;
  2152. case NDIS_80211_TYPE_OFDM_A:
  2153. priv->caps |= CAP_MODE_80211A;
  2154. break;
  2155. case NDIS_80211_TYPE_OFDM_G:
  2156. priv->caps |= CAP_MODE_80211G;
  2157. break;
  2158. }
  2159. }
  2160. }
  2161. return retval;
  2162. }
  2163. #define STATS_UPDATE_JIFFIES (HZ)
  2164. static void rndis_update_wireless_stats(struct work_struct *work)
  2165. {
  2166. struct rndis_wlan_private *priv =
  2167. container_of(work, struct rndis_wlan_private, stats_work.work);
  2168. struct usbnet *usbdev = priv->usbdev;
  2169. struct iw_statistics iwstats;
  2170. __le32 rssi, tmp;
  2171. int len, ret, j;
  2172. unsigned long flags;
  2173. int update_jiffies = STATS_UPDATE_JIFFIES;
  2174. void *buf;
  2175. spin_lock_irqsave(&priv->stats_lock, flags);
  2176. memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
  2177. spin_unlock_irqrestore(&priv->stats_lock, flags);
  2178. /* only update stats when connected */
  2179. if (!is_associated(usbdev)) {
  2180. iwstats.qual.qual = 0;
  2181. iwstats.qual.level = 0;
  2182. iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2183. | IW_QUAL_LEVEL_UPDATED
  2184. | IW_QUAL_NOISE_INVALID
  2185. | IW_QUAL_QUAL_INVALID
  2186. | IW_QUAL_LEVEL_INVALID;
  2187. goto end;
  2188. }
  2189. len = sizeof(rssi);
  2190. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  2191. devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
  2192. le32_to_cpu(rssi));
  2193. if (ret == 0) {
  2194. memset(&iwstats.qual, 0, sizeof(iwstats.qual));
  2195. iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
  2196. iwstats.qual.level = level_to_qual(le32_to_cpu(rssi));
  2197. iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2198. | IW_QUAL_LEVEL_UPDATED
  2199. | IW_QUAL_NOISE_INVALID;
  2200. }
  2201. memset(&iwstats.discard, 0, sizeof(iwstats.discard));
  2202. len = sizeof(tmp);
  2203. ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
  2204. if (ret == 0)
  2205. iwstats.discard.misc += le32_to_cpu(tmp);
  2206. len = sizeof(tmp);
  2207. ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
  2208. if (ret == 0)
  2209. iwstats.discard.misc += le32_to_cpu(tmp);
  2210. len = sizeof(tmp);
  2211. ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
  2212. if (ret == 0)
  2213. iwstats.discard.misc += le32_to_cpu(tmp);
  2214. /* Workaround transfer stalls on poor quality links.
  2215. * TODO: find right way to fix these stalls (as stalls do not happen
  2216. * with ndiswrapper/windows driver). */
  2217. if (iwstats.qual.qual <= 25) {
  2218. /* Decrease stats worker interval to catch stalls.
  2219. * faster. Faster than 400-500ms causes packet loss,
  2220. * Slower doesn't catch stalls fast enough.
  2221. */
  2222. j = msecs_to_jiffies(priv->param_workaround_interval);
  2223. if (j > STATS_UPDATE_JIFFIES)
  2224. j = STATS_UPDATE_JIFFIES;
  2225. else if (j <= 0)
  2226. j = 1;
  2227. update_jiffies = j;
  2228. /* Send scan OID. Use of both OIDs is required to get device
  2229. * working.
  2230. */
  2231. tmp = cpu_to_le32(1);
  2232. rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  2233. sizeof(tmp));
  2234. len = CONTROL_BUFFER_SIZE;
  2235. buf = kmalloc(len, GFP_KERNEL);
  2236. if (!buf)
  2237. goto end;
  2238. rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  2239. kfree(buf);
  2240. }
  2241. end:
  2242. spin_lock_irqsave(&priv->stats_lock, flags);
  2243. memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
  2244. spin_unlock_irqrestore(&priv->stats_lock, flags);
  2245. if (update_jiffies >= HZ)
  2246. update_jiffies = round_jiffies_relative(update_jiffies);
  2247. else {
  2248. j = round_jiffies_relative(update_jiffies);
  2249. if (abs(j - update_jiffies) <= 10)
  2250. update_jiffies = j;
  2251. }
  2252. queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
  2253. }
  2254. static int bcm4320a_early_init(struct usbnet *usbdev)
  2255. {
  2256. /* bcm4320a doesn't handle configuration parameters well. Try
  2257. * set any and you get partially zeroed mac and broken device.
  2258. */
  2259. return 0;
  2260. }
  2261. static int bcm4320b_early_init(struct usbnet *usbdev)
  2262. {
  2263. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2264. char buf[8];
  2265. /* Early initialization settings, setting these won't have effect
  2266. * if called after generic_rndis_bind().
  2267. */
  2268. priv->param_country[0] = modparam_country[0];
  2269. priv->param_country[1] = modparam_country[1];
  2270. priv->param_country[2] = 0;
  2271. priv->param_frameburst = modparam_frameburst;
  2272. priv->param_afterburner = modparam_afterburner;
  2273. priv->param_power_save = modparam_power_save;
  2274. priv->param_power_output = modparam_power_output;
  2275. priv->param_roamtrigger = modparam_roamtrigger;
  2276. priv->param_roamdelta = modparam_roamdelta;
  2277. priv->param_country[0] = toupper(priv->param_country[0]);
  2278. priv->param_country[1] = toupper(priv->param_country[1]);
  2279. /* doesn't support EU as country code, use FI instead */
  2280. if (!strcmp(priv->param_country, "EU"))
  2281. strcpy(priv->param_country, "FI");
  2282. if (priv->param_power_save < 0)
  2283. priv->param_power_save = 0;
  2284. else if (priv->param_power_save > 2)
  2285. priv->param_power_save = 2;
  2286. if (priv->param_power_output < 0)
  2287. priv->param_power_output = 0;
  2288. else if (priv->param_power_output > 3)
  2289. priv->param_power_output = 3;
  2290. if (priv->param_roamtrigger < -80)
  2291. priv->param_roamtrigger = -80;
  2292. else if (priv->param_roamtrigger > -60)
  2293. priv->param_roamtrigger = -60;
  2294. if (priv->param_roamdelta < 0)
  2295. priv->param_roamdelta = 0;
  2296. else if (priv->param_roamdelta > 2)
  2297. priv->param_roamdelta = 2;
  2298. if (modparam_workaround_interval < 0)
  2299. priv->param_workaround_interval = 500;
  2300. else
  2301. priv->param_workaround_interval = modparam_workaround_interval;
  2302. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2303. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2304. priv->param_frameburst ? "1" : "0");
  2305. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2306. priv->param_afterburner ? "1" : "0");
  2307. sprintf(buf, "%d", priv->param_power_save);
  2308. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2309. sprintf(buf, "%d", priv->param_power_output);
  2310. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2311. sprintf(buf, "%d", priv->param_roamtrigger);
  2312. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2313. sprintf(buf, "%d", priv->param_roamdelta);
  2314. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2315. return 0;
  2316. }
  2317. /* same as rndis_netdev_ops but with local multicast handler */
  2318. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2319. .ndo_open = usbnet_open,
  2320. .ndo_stop = usbnet_stop,
  2321. .ndo_start_xmit = usbnet_start_xmit,
  2322. .ndo_tx_timeout = usbnet_tx_timeout,
  2323. .ndo_set_mac_address = eth_mac_addr,
  2324. .ndo_validate_addr = eth_validate_addr,
  2325. .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
  2326. };
  2327. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2328. {
  2329. struct wiphy *wiphy;
  2330. struct rndis_wlan_private *priv;
  2331. int retval, len;
  2332. __le32 tmp;
  2333. /* allocate wiphy and rndis private data
  2334. * NOTE: We only support a single virtual interface, so wiphy
  2335. * and wireless_dev are somewhat synonymous for this device.
  2336. */
  2337. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2338. if (!wiphy)
  2339. return -ENOMEM;
  2340. priv = wiphy_priv(wiphy);
  2341. usbdev->net->ieee80211_ptr = &priv->wdev;
  2342. priv->wdev.wiphy = wiphy;
  2343. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2344. /* These have to be initialized before calling generic_rndis_bind().
  2345. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2346. */
  2347. usbdev->driver_priv = priv;
  2348. usbdev->net->wireless_handlers = &rndis_iw_handlers;
  2349. priv->usbdev = usbdev;
  2350. mutex_init(&priv->command_lock);
  2351. spin_lock_init(&priv->stats_lock);
  2352. /* because rndis_command() sleeps we need to use workqueue */
  2353. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2354. INIT_WORK(&priv->work, rndis_wlan_worker);
  2355. INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
  2356. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2357. /* try bind rndis_host */
  2358. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2359. if (retval < 0)
  2360. goto fail;
  2361. /* generic_rndis_bind set packet filter to multicast_all+
  2362. * promisc mode which doesn't work well for our devices (device
  2363. * picks up rssi to closest station instead of to access point).
  2364. *
  2365. * rndis_host wants to avoid all OID as much as possible
  2366. * so do promisc/multicast handling in rndis_wlan.
  2367. */
  2368. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2369. tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  2370. retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
  2371. sizeof(tmp));
  2372. len = sizeof(tmp);
  2373. retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
  2374. &len);
  2375. priv->multicast_size = le32_to_cpu(tmp);
  2376. if (retval < 0 || priv->multicast_size < 0)
  2377. priv->multicast_size = 0;
  2378. if (priv->multicast_size > 0)
  2379. usbdev->net->flags |= IFF_MULTICAST;
  2380. else
  2381. usbdev->net->flags &= ~IFF_MULTICAST;
  2382. priv->iwstats.qual.qual = 0;
  2383. priv->iwstats.qual.level = 0;
  2384. priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2385. | IW_QUAL_LEVEL_UPDATED
  2386. | IW_QUAL_NOISE_INVALID
  2387. | IW_QUAL_QUAL_INVALID
  2388. | IW_QUAL_LEVEL_INVALID;
  2389. /* fill-out wiphy structure and register w/ cfg80211 */
  2390. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2391. wiphy->privid = rndis_wiphy_privid;
  2392. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2393. | BIT(NL80211_IFTYPE_ADHOC);
  2394. wiphy->max_scan_ssids = 1;
  2395. /* TODO: fill-out band/encr information based on priv->caps */
  2396. rndis_wlan_get_caps(usbdev);
  2397. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2398. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2399. priv->band.channels = priv->channels;
  2400. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2401. priv->band.bitrates = priv->rates;
  2402. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2403. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2404. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2405. memcpy(priv->cipher_suites, rndis_cipher_suites,
  2406. sizeof(rndis_cipher_suites));
  2407. wiphy->cipher_suites = priv->cipher_suites;
  2408. wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
  2409. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2410. if (wiphy_register(wiphy)) {
  2411. retval = -ENODEV;
  2412. goto fail;
  2413. }
  2414. set_default_iw_params(usbdev);
  2415. /* set default rts/frag */
  2416. rndis_set_wiphy_params(wiphy,
  2417. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2418. /* turn radio on */
  2419. priv->radio_on = 1;
  2420. disassociate(usbdev, 1);
  2421. netif_carrier_off(usbdev->net);
  2422. return 0;
  2423. fail:
  2424. cancel_delayed_work_sync(&priv->stats_work);
  2425. cancel_delayed_work_sync(&priv->scan_work);
  2426. cancel_work_sync(&priv->work);
  2427. flush_workqueue(priv->workqueue);
  2428. destroy_workqueue(priv->workqueue);
  2429. wiphy_free(wiphy);
  2430. return retval;
  2431. }
  2432. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2433. {
  2434. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2435. /* turn radio off */
  2436. disassociate(usbdev, 0);
  2437. cancel_delayed_work_sync(&priv->stats_work);
  2438. cancel_delayed_work_sync(&priv->scan_work);
  2439. cancel_work_sync(&priv->work);
  2440. flush_workqueue(priv->workqueue);
  2441. destroy_workqueue(priv->workqueue);
  2442. if (priv && priv->wpa_ie_len)
  2443. kfree(priv->wpa_ie);
  2444. rndis_unbind(usbdev, intf);
  2445. wiphy_unregister(priv->wdev.wiphy);
  2446. wiphy_free(priv->wdev.wiphy);
  2447. }
  2448. static int rndis_wlan_reset(struct usbnet *usbdev)
  2449. {
  2450. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2451. int retval;
  2452. devdbg(usbdev, "rndis_wlan_reset");
  2453. retval = rndis_reset(usbdev);
  2454. if (retval)
  2455. devwarn(usbdev, "rndis_reset() failed: %d", retval);
  2456. /* rndis_reset cleared multicast list, so restore here.
  2457. (set_multicast_list() also turns on current packet filter) */
  2458. set_multicast_list(usbdev);
  2459. queue_delayed_work(priv->workqueue, &priv->stats_work,
  2460. round_jiffies_relative(STATS_UPDATE_JIFFIES));
  2461. return deauthenticate(usbdev);
  2462. }
  2463. static int rndis_wlan_stop(struct usbnet *usbdev)
  2464. {
  2465. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2466. int retval;
  2467. __le32 filter;
  2468. devdbg(usbdev, "rndis_wlan_stop");
  2469. retval = disassociate(usbdev, 0);
  2470. priv->work_pending = 0;
  2471. cancel_delayed_work_sync(&priv->stats_work);
  2472. cancel_delayed_work_sync(&priv->scan_work);
  2473. cancel_work_sync(&priv->work);
  2474. flush_workqueue(priv->workqueue);
  2475. if (priv->scan_request) {
  2476. cfg80211_scan_done(priv->scan_request, true);
  2477. priv->scan_request = NULL;
  2478. }
  2479. /* Set current packet filter zero to block receiving data packets from
  2480. device. */
  2481. filter = 0;
  2482. rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2483. sizeof(filter));
  2484. return retval;
  2485. }
  2486. static const struct driver_info bcm4320b_info = {
  2487. .description = "Wireless RNDIS device, BCM4320b based",
  2488. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2489. FLAG_AVOID_UNLINK_URBS,
  2490. .bind = rndis_wlan_bind,
  2491. .unbind = rndis_wlan_unbind,
  2492. .status = rndis_status,
  2493. .rx_fixup = rndis_rx_fixup,
  2494. .tx_fixup = rndis_tx_fixup,
  2495. .reset = rndis_wlan_reset,
  2496. .stop = rndis_wlan_stop,
  2497. .early_init = bcm4320b_early_init,
  2498. .indication = rndis_wlan_indication,
  2499. };
  2500. static const struct driver_info bcm4320a_info = {
  2501. .description = "Wireless RNDIS device, BCM4320a based",
  2502. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2503. FLAG_AVOID_UNLINK_URBS,
  2504. .bind = rndis_wlan_bind,
  2505. .unbind = rndis_wlan_unbind,
  2506. .status = rndis_status,
  2507. .rx_fixup = rndis_rx_fixup,
  2508. .tx_fixup = rndis_tx_fixup,
  2509. .reset = rndis_wlan_reset,
  2510. .stop = rndis_wlan_stop,
  2511. .early_init = bcm4320a_early_init,
  2512. .indication = rndis_wlan_indication,
  2513. };
  2514. static const struct driver_info rndis_wlan_info = {
  2515. .description = "Wireless RNDIS device",
  2516. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2517. FLAG_AVOID_UNLINK_URBS,
  2518. .bind = rndis_wlan_bind,
  2519. .unbind = rndis_wlan_unbind,
  2520. .status = rndis_status,
  2521. .rx_fixup = rndis_rx_fixup,
  2522. .tx_fixup = rndis_tx_fixup,
  2523. .reset = rndis_wlan_reset,
  2524. .stop = rndis_wlan_stop,
  2525. .early_init = bcm4320a_early_init,
  2526. .indication = rndis_wlan_indication,
  2527. };
  2528. /*-------------------------------------------------------------------------*/
  2529. static const struct usb_device_id products [] = {
  2530. #define RNDIS_MASTER_INTERFACE \
  2531. .bInterfaceClass = USB_CLASS_COMM, \
  2532. .bInterfaceSubClass = 2 /* ACM */, \
  2533. .bInterfaceProtocol = 0x0ff
  2534. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2535. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  2536. */
  2537. {
  2538. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2539. | USB_DEVICE_ID_MATCH_DEVICE,
  2540. .idVendor = 0x0411,
  2541. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  2542. RNDIS_MASTER_INTERFACE,
  2543. .driver_info = (unsigned long) &bcm4320b_info,
  2544. }, {
  2545. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2546. | USB_DEVICE_ID_MATCH_DEVICE,
  2547. .idVendor = 0x0baf,
  2548. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  2549. RNDIS_MASTER_INTERFACE,
  2550. .driver_info = (unsigned long) &bcm4320b_info,
  2551. }, {
  2552. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2553. | USB_DEVICE_ID_MATCH_DEVICE,
  2554. .idVendor = 0x050d,
  2555. .idProduct = 0x011b, /* Belkin F5D7051 */
  2556. RNDIS_MASTER_INTERFACE,
  2557. .driver_info = (unsigned long) &bcm4320b_info,
  2558. }, {
  2559. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2560. | USB_DEVICE_ID_MATCH_DEVICE,
  2561. .idVendor = 0x1799, /* Belkin has two vendor ids */
  2562. .idProduct = 0x011b, /* Belkin F5D7051 */
  2563. RNDIS_MASTER_INTERFACE,
  2564. .driver_info = (unsigned long) &bcm4320b_info,
  2565. }, {
  2566. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2567. | USB_DEVICE_ID_MATCH_DEVICE,
  2568. .idVendor = 0x13b1,
  2569. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  2570. RNDIS_MASTER_INTERFACE,
  2571. .driver_info = (unsigned long) &bcm4320b_info,
  2572. }, {
  2573. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2574. | USB_DEVICE_ID_MATCH_DEVICE,
  2575. .idVendor = 0x13b1,
  2576. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  2577. RNDIS_MASTER_INTERFACE,
  2578. .driver_info = (unsigned long) &bcm4320b_info,
  2579. }, {
  2580. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2581. | USB_DEVICE_ID_MATCH_DEVICE,
  2582. .idVendor = 0x0b05,
  2583. .idProduct = 0x1717, /* Asus WL169gE */
  2584. RNDIS_MASTER_INTERFACE,
  2585. .driver_info = (unsigned long) &bcm4320b_info,
  2586. }, {
  2587. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2588. | USB_DEVICE_ID_MATCH_DEVICE,
  2589. .idVendor = 0x0a5c,
  2590. .idProduct = 0xd11b, /* Eminent EM4045 */
  2591. RNDIS_MASTER_INTERFACE,
  2592. .driver_info = (unsigned long) &bcm4320b_info,
  2593. }, {
  2594. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2595. | USB_DEVICE_ID_MATCH_DEVICE,
  2596. .idVendor = 0x1690,
  2597. .idProduct = 0x0715, /* BT Voyager 1055 */
  2598. RNDIS_MASTER_INTERFACE,
  2599. .driver_info = (unsigned long) &bcm4320b_info,
  2600. },
  2601. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  2602. * parameters available, hardware probably contain older firmware version with
  2603. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  2604. */
  2605. {
  2606. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2607. | USB_DEVICE_ID_MATCH_DEVICE,
  2608. .idVendor = 0x13b1,
  2609. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  2610. RNDIS_MASTER_INTERFACE,
  2611. .driver_info = (unsigned long) &bcm4320a_info,
  2612. }, {
  2613. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2614. | USB_DEVICE_ID_MATCH_DEVICE,
  2615. .idVendor = 0x0baf,
  2616. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  2617. RNDIS_MASTER_INTERFACE,
  2618. .driver_info = (unsigned long) &bcm4320a_info,
  2619. }, {
  2620. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2621. | USB_DEVICE_ID_MATCH_DEVICE,
  2622. .idVendor = 0x0411,
  2623. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  2624. RNDIS_MASTER_INTERFACE,
  2625. .driver_info = (unsigned long) &bcm4320a_info,
  2626. },
  2627. /* Generic Wireless RNDIS devices that we don't have exact
  2628. * idVendor/idProduct/chip yet.
  2629. */
  2630. {
  2631. /* RNDIS is MSFT's un-official variant of CDC ACM */
  2632. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  2633. .driver_info = (unsigned long) &rndis_wlan_info,
  2634. }, {
  2635. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  2636. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  2637. .driver_info = (unsigned long) &rndis_wlan_info,
  2638. },
  2639. { }, // END
  2640. };
  2641. MODULE_DEVICE_TABLE(usb, products);
  2642. static struct usb_driver rndis_wlan_driver = {
  2643. .name = "rndis_wlan",
  2644. .id_table = products,
  2645. .probe = usbnet_probe,
  2646. .disconnect = usbnet_disconnect,
  2647. .suspend = usbnet_suspend,
  2648. .resume = usbnet_resume,
  2649. };
  2650. static int __init rndis_wlan_init(void)
  2651. {
  2652. return usb_register(&rndis_wlan_driver);
  2653. }
  2654. module_init(rndis_wlan_init);
  2655. static void __exit rndis_wlan_exit(void)
  2656. {
  2657. usb_deregister(&rndis_wlan_driver);
  2658. }
  2659. module_exit(rndis_wlan_exit);
  2660. MODULE_AUTHOR("Bjorge Dijkstra");
  2661. MODULE_AUTHOR("Jussi Kivilinna");
  2662. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  2663. MODULE_LICENSE("GPL");