rndis_wlan.c 84 KB

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