rndis_wlan.c 84 KB

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