rndis_wlan.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062
  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. devdbg(dev, "rndis_query_oid(%s): rndis_command() failed, %d "
  627. "(%08x)", 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. devdbg(dev, "rndis_query_oid(%s): device returned "
  637. "error, 0x%08x (%d)", 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. devdbg(dev, "rndis_set_oid(%s): rndis_command() failed, %d "
  679. "(%08x)", 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. devdbg(dev, "rndis_set_oid(%s): device returned error, "
  685. "0x%08x (%d)", 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. devdbg(dev, "setting config parameter: %s, value: %s",
  743. param, (u8 *)value);
  744. else
  745. devdbg(dev, "setting config parameter: %s, value: %d",
  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. devdbg(dev, "info buffer (len: %d):", info_len);
  766. for (i = 0; i < info_len; i += 12) {
  767. u32 *tmp = (u32 *)((u8 *)infobuf + i);
  768. devdbg(dev, "%08X:%08X:%08X",
  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. devdbg(dev, "setting rndis config parameter failed, %d.", ret);
  778. kfree(infobuf);
  779. return ret;
  780. }
  781. static int rndis_set_config_parameter_str(struct usbnet *dev,
  782. char *param, char *value)
  783. {
  784. return rndis_set_config_parameter(dev, param, 2, value);
  785. }
  786. /*
  787. * data conversion functions
  788. */
  789. static int level_to_qual(int level)
  790. {
  791. int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
  792. return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
  793. }
  794. /*
  795. * common functions
  796. */
  797. static int set_infra_mode(struct usbnet *usbdev, int mode);
  798. static void restore_keys(struct usbnet *usbdev);
  799. static int rndis_check_bssid_list(struct usbnet *usbdev);
  800. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  801. {
  802. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  803. int ret;
  804. ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
  805. if (ret < 0) {
  806. devwarn(usbdev, "setting SSID failed (%08X)", ret);
  807. return ret;
  808. }
  809. if (ret == 0) {
  810. memcpy(&priv->essid, ssid, sizeof(priv->essid));
  811. priv->radio_on = true;
  812. devdbg(usbdev, "set_essid: radio_on = true");
  813. }
  814. return ret;
  815. }
  816. static int set_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  817. {
  818. int ret;
  819. ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
  820. if (ret < 0) {
  821. devwarn(usbdev, "setting BSSID[%pM] failed (%08X)", bssid, ret);
  822. return ret;
  823. }
  824. return ret;
  825. }
  826. static int clear_bssid(struct usbnet *usbdev)
  827. {
  828. u8 broadcast_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  829. return set_bssid(usbdev, broadcast_mac);
  830. }
  831. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  832. {
  833. int ret, len;
  834. len = ETH_ALEN;
  835. ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
  836. if (ret != 0)
  837. memset(bssid, 0, ETH_ALEN);
  838. return ret;
  839. }
  840. static int get_association_info(struct usbnet *usbdev,
  841. struct ndis_80211_assoc_info *info, int len)
  842. {
  843. return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
  844. info, &len);
  845. }
  846. static bool is_associated(struct usbnet *usbdev)
  847. {
  848. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  849. u8 bssid[ETH_ALEN];
  850. int ret;
  851. if (!priv->radio_on)
  852. return false;
  853. ret = get_bssid(usbdev, bssid);
  854. return (ret == 0 && !is_zero_ether_addr(bssid));
  855. }
  856. static int disassociate(struct usbnet *usbdev, bool reset_ssid)
  857. {
  858. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  859. struct ndis_80211_ssid ssid;
  860. int i, ret = 0;
  861. if (priv->radio_on) {
  862. ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
  863. if (ret == 0) {
  864. priv->radio_on = false;
  865. devdbg(usbdev, "disassociate: radio_on = false");
  866. if (reset_ssid)
  867. msleep(100);
  868. }
  869. }
  870. /* disassociate causes radio to be turned off; if reset_ssid
  871. * is given, set random ssid to enable radio */
  872. if (reset_ssid) {
  873. /* Set device to infrastructure mode so we don't get ad-hoc
  874. * 'media connect' indications with the random ssid.
  875. */
  876. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  877. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  878. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  879. ssid.essid[0] = 0x1;
  880. ssid.essid[1] = 0xff;
  881. for (i = 2; i < sizeof(ssid.essid); i++)
  882. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  883. ret = set_essid(usbdev, &ssid);
  884. }
  885. return ret;
  886. }
  887. static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
  888. enum nl80211_auth_type auth_type, int keymgmt)
  889. {
  890. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  891. __le32 tmp;
  892. int auth_mode, ret;
  893. devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
  894. "keymgmt=0x%x", wpa_version, auth_type, keymgmt);
  895. if (wpa_version & NL80211_WPA_VERSION_2) {
  896. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  897. auth_mode = NDIS_80211_AUTH_WPA2;
  898. else
  899. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  900. } else if (wpa_version & NL80211_WPA_VERSION_1) {
  901. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  902. auth_mode = NDIS_80211_AUTH_WPA;
  903. else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
  904. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  905. else
  906. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  907. } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  908. auth_mode = NDIS_80211_AUTH_SHARED;
  909. else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
  910. auth_mode = NDIS_80211_AUTH_OPEN;
  911. else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
  912. auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
  913. else
  914. return -ENOTSUPP;
  915. tmp = cpu_to_le32(auth_mode);
  916. ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
  917. sizeof(tmp));
  918. if (ret != 0) {
  919. devwarn(usbdev, "setting auth mode failed (%08X)", ret);
  920. return ret;
  921. }
  922. priv->wpa_version = wpa_version;
  923. priv->wpa_auth_type = auth_type;
  924. priv->wpa_keymgmt = keymgmt;
  925. return 0;
  926. }
  927. static int set_priv_filter(struct usbnet *usbdev)
  928. {
  929. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  930. __le32 tmp;
  931. devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
  932. if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
  933. priv->wpa_version & NL80211_WPA_VERSION_1)
  934. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  935. else
  936. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  937. return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
  938. sizeof(tmp));
  939. }
  940. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  941. {
  942. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  943. __le32 tmp;
  944. int encr_mode, ret;
  945. devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
  946. pairwise, groupwise);
  947. if (pairwise & RNDIS_WLAN_ALG_CCMP)
  948. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  949. else if (pairwise & RNDIS_WLAN_ALG_TKIP)
  950. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  951. else if (pairwise & RNDIS_WLAN_ALG_WEP)
  952. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  953. else if (groupwise & RNDIS_WLAN_ALG_CCMP)
  954. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  955. else if (groupwise & RNDIS_WLAN_ALG_TKIP)
  956. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  957. else
  958. encr_mode = NDIS_80211_ENCR_DISABLED;
  959. tmp = cpu_to_le32(encr_mode);
  960. ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
  961. sizeof(tmp));
  962. if (ret != 0) {
  963. devwarn(usbdev, "setting encr mode failed (%08X)", ret);
  964. return ret;
  965. }
  966. priv->wpa_cipher_pair = pairwise;
  967. priv->wpa_cipher_group = groupwise;
  968. return 0;
  969. }
  970. static int set_infra_mode(struct usbnet *usbdev, int mode)
  971. {
  972. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  973. __le32 tmp;
  974. int ret;
  975. devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
  976. tmp = cpu_to_le32(mode);
  977. ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
  978. sizeof(tmp));
  979. if (ret != 0) {
  980. devwarn(usbdev, "setting infra mode failed (%08X)", ret);
  981. return ret;
  982. }
  983. /* NDIS drivers clear keys when infrastructure mode is
  984. * changed. But Linux tools assume otherwise. So set the
  985. * keys */
  986. restore_keys(usbdev);
  987. priv->infra_mode = mode;
  988. return 0;
  989. }
  990. static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
  991. {
  992. __le32 tmp;
  993. devdbg(usbdev, "set_rts_threshold %i", rts_threshold);
  994. if (rts_threshold < 0 || rts_threshold > 2347)
  995. rts_threshold = 2347;
  996. tmp = cpu_to_le32(rts_threshold);
  997. return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
  998. sizeof(tmp));
  999. }
  1000. static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
  1001. {
  1002. __le32 tmp;
  1003. devdbg(usbdev, "set_frag_threshold %i", frag_threshold);
  1004. if (frag_threshold < 256 || frag_threshold > 2346)
  1005. frag_threshold = 2346;
  1006. tmp = cpu_to_le32(frag_threshold);
  1007. return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
  1008. sizeof(tmp));
  1009. }
  1010. static void set_default_iw_params(struct usbnet *usbdev)
  1011. {
  1012. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1013. set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
  1014. RNDIS_WLAN_KEY_MGMT_NONE);
  1015. set_priv_filter(usbdev);
  1016. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1017. }
  1018. static int deauthenticate(struct usbnet *usbdev)
  1019. {
  1020. int ret;
  1021. ret = disassociate(usbdev, true);
  1022. set_default_iw_params(usbdev);
  1023. return ret;
  1024. }
  1025. static int set_channel(struct usbnet *usbdev, int channel)
  1026. {
  1027. struct ndis_80211_conf config;
  1028. unsigned int dsconfig;
  1029. int len, ret;
  1030. devdbg(usbdev, "set_channel(%d)", channel);
  1031. /* this OID is valid only when not associated */
  1032. if (is_associated(usbdev))
  1033. return 0;
  1034. dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
  1035. len = sizeof(config);
  1036. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1037. if (ret < 0) {
  1038. devdbg(usbdev, "set_channel: querying configuration failed");
  1039. return ret;
  1040. }
  1041. config.ds_config = cpu_to_le32(dsconfig);
  1042. ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
  1043. sizeof(config));
  1044. devdbg(usbdev, "set_channel: %d -> %d", channel, ret);
  1045. return ret;
  1046. }
  1047. /* index must be 0 - N, as per NDIS */
  1048. static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1049. int index)
  1050. {
  1051. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1052. struct ndis_80211_wep_key ndis_key;
  1053. u32 cipher;
  1054. int ret;
  1055. devdbg(usbdev, "add_wep_key(idx: %d, len: %d)", index, key_len);
  1056. if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
  1057. return -EINVAL;
  1058. if (key_len == 5)
  1059. cipher = WLAN_CIPHER_SUITE_WEP40;
  1060. else
  1061. cipher = WLAN_CIPHER_SUITE_WEP104;
  1062. memset(&ndis_key, 0, sizeof(ndis_key));
  1063. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1064. ndis_key.length = cpu_to_le32(key_len);
  1065. ndis_key.index = cpu_to_le32(index);
  1066. memcpy(&ndis_key.material, key, key_len);
  1067. if (index == priv->encr_tx_key_index) {
  1068. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1069. ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
  1070. RNDIS_WLAN_ALG_NONE);
  1071. if (ret)
  1072. devwarn(usbdev, "encryption couldn't be enabled (%08X)",
  1073. ret);
  1074. }
  1075. ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
  1076. sizeof(ndis_key));
  1077. if (ret != 0) {
  1078. devwarn(usbdev, "adding encryption key %d failed (%08X)",
  1079. index+1, ret);
  1080. return ret;
  1081. }
  1082. priv->encr_keys[index].len = key_len;
  1083. priv->encr_keys[index].cipher = cipher;
  1084. memcpy(&priv->encr_keys[index].material, key, key_len);
  1085. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1086. return 0;
  1087. }
  1088. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1089. int index, const u8 *addr, const u8 *rx_seq,
  1090. int seq_len, u32 cipher, __le32 flags)
  1091. {
  1092. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1093. struct ndis_80211_key ndis_key;
  1094. bool is_addr_ok;
  1095. int ret;
  1096. if (index < 0 || index >= 4) {
  1097. devdbg(usbdev, "add_wpa_key: index out of range (%i)", index);
  1098. return -EINVAL;
  1099. }
  1100. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1101. devdbg(usbdev, "add_wpa_key: key length out of range (%i)",
  1102. key_len);
  1103. return -EINVAL;
  1104. }
  1105. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
  1106. if (!rx_seq || seq_len <= 0) {
  1107. devdbg(usbdev, "add_wpa_key: recv seq flag without"
  1108. "buffer");
  1109. return -EINVAL;
  1110. }
  1111. if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
  1112. devdbg(usbdev, "add_wpa_key: too big recv seq buffer");
  1113. return -EINVAL;
  1114. }
  1115. }
  1116. is_addr_ok = addr && !is_zero_ether_addr(addr) &&
  1117. !is_broadcast_ether_addr(addr);
  1118. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1119. devdbg(usbdev, "add_wpa_key: pairwise but bssid invalid (%pM)",
  1120. addr);
  1121. return -EINVAL;
  1122. }
  1123. devdbg(usbdev, "add_wpa_key(%i): flags:%i%i%i", index,
  1124. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1125. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1126. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1127. memset(&ndis_key, 0, sizeof(ndis_key));
  1128. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1129. sizeof(ndis_key.material) + key_len);
  1130. ndis_key.length = cpu_to_le32(key_len);
  1131. ndis_key.index = cpu_to_le32(index) | flags;
  1132. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1133. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1134. * different order than NDIS spec, so swap the order here. */
  1135. memcpy(ndis_key.material, key, 16);
  1136. memcpy(ndis_key.material + 16, key + 24, 8);
  1137. memcpy(ndis_key.material + 24, key + 16, 8);
  1138. } else
  1139. memcpy(ndis_key.material, key, key_len);
  1140. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1141. memcpy(ndis_key.rsc, rx_seq, seq_len);
  1142. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1143. /* pairwise key */
  1144. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1145. } else {
  1146. /* group key */
  1147. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1148. memset(ndis_key.bssid, 0xff, ETH_ALEN);
  1149. else
  1150. get_bssid(usbdev, ndis_key.bssid);
  1151. }
  1152. ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
  1153. le32_to_cpu(ndis_key.size));
  1154. devdbg(usbdev, "add_wpa_key: OID_802_11_ADD_KEY -> %08X", ret);
  1155. if (ret != 0)
  1156. return ret;
  1157. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1158. priv->encr_keys[index].len = key_len;
  1159. priv->encr_keys[index].cipher = cipher;
  1160. memcpy(&priv->encr_keys[index].material, key, key_len);
  1161. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1162. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1163. else
  1164. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1165. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1166. priv->encr_tx_key_index = index;
  1167. return 0;
  1168. }
  1169. static int restore_key(struct usbnet *usbdev, int key_idx)
  1170. {
  1171. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1172. struct rndis_wlan_encr_key key;
  1173. if (is_wpa_key(priv, key_idx))
  1174. return 0;
  1175. key = priv->encr_keys[key_idx];
  1176. devdbg(usbdev, "restore_key: %i:%i", key_idx, key.len);
  1177. if (key.len == 0)
  1178. return 0;
  1179. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1180. }
  1181. static void restore_keys(struct usbnet *usbdev)
  1182. {
  1183. int i;
  1184. for (i = 0; i < 4; i++)
  1185. restore_key(usbdev, i);
  1186. }
  1187. static void clear_key(struct rndis_wlan_private *priv, int idx)
  1188. {
  1189. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1190. }
  1191. /* remove_key is for both wep and wpa */
  1192. static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
  1193. {
  1194. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1195. struct ndis_80211_remove_key remove_key;
  1196. __le32 keyindex;
  1197. bool is_wpa;
  1198. int ret;
  1199. if (priv->encr_keys[index].len == 0)
  1200. return 0;
  1201. is_wpa = is_wpa_key(priv, index);
  1202. devdbg(usbdev, "remove_key: %i:%s:%i", index, is_wpa ? "wpa" : "wep",
  1203. priv->encr_keys[index].len);
  1204. clear_key(priv, index);
  1205. if (is_wpa) {
  1206. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1207. remove_key.index = cpu_to_le32(index);
  1208. if (bssid) {
  1209. /* pairwise key */
  1210. if (!is_broadcast_ether_addr(bssid))
  1211. remove_key.index |=
  1212. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1213. memcpy(remove_key.bssid, bssid,
  1214. sizeof(remove_key.bssid));
  1215. } else
  1216. memset(remove_key.bssid, 0xff,
  1217. sizeof(remove_key.bssid));
  1218. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
  1219. sizeof(remove_key));
  1220. if (ret != 0)
  1221. return ret;
  1222. } else {
  1223. keyindex = cpu_to_le32(index);
  1224. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
  1225. sizeof(keyindex));
  1226. if (ret != 0) {
  1227. devwarn(usbdev,
  1228. "removing encryption key %d failed (%08X)",
  1229. index, ret);
  1230. return ret;
  1231. }
  1232. }
  1233. /* if it is transmit key, disable encryption */
  1234. if (index == priv->encr_tx_key_index)
  1235. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1236. return 0;
  1237. }
  1238. static void set_multicast_list(struct usbnet *usbdev)
  1239. {
  1240. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1241. struct dev_mc_list *mclist;
  1242. __le32 filter;
  1243. int ret, i, size;
  1244. char *buf;
  1245. filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  1246. if (usbdev->net->flags & IFF_PROMISC) {
  1247. filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
  1248. RNDIS_PACKET_TYPE_ALL_LOCAL;
  1249. } else if (usbdev->net->flags & IFF_ALLMULTI ||
  1250. usbdev->net->mc_count > priv->multicast_size) {
  1251. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1252. } else if (usbdev->net->mc_count > 0) {
  1253. size = min(priv->multicast_size, usbdev->net->mc_count);
  1254. buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
  1255. if (!buf) {
  1256. devwarn(usbdev,
  1257. "couldn't alloc %d bytes of memory",
  1258. size * ETH_ALEN);
  1259. return;
  1260. }
  1261. mclist = usbdev->net->mc_list;
  1262. for (i = 0; i < size && mclist; mclist = mclist->next) {
  1263. if (mclist->dmi_addrlen != ETH_ALEN)
  1264. continue;
  1265. memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
  1266. i++;
  1267. }
  1268. ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
  1269. i * ETH_ALEN);
  1270. if (ret == 0 && i > 0)
  1271. filter |= RNDIS_PACKET_TYPE_MULTICAST;
  1272. else
  1273. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1274. devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
  1275. i, priv->multicast_size, ret);
  1276. kfree(buf);
  1277. }
  1278. ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1279. sizeof(filter));
  1280. if (ret < 0) {
  1281. devwarn(usbdev, "couldn't set packet filter: %08x",
  1282. le32_to_cpu(filter));
  1283. }
  1284. devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
  1285. le32_to_cpu(filter), ret);
  1286. }
  1287. /*
  1288. * cfg80211 ops
  1289. */
  1290. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1291. struct net_device *dev,
  1292. enum nl80211_iftype type, u32 *flags,
  1293. struct vif_params *params)
  1294. {
  1295. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1296. struct usbnet *usbdev = priv->usbdev;
  1297. int mode;
  1298. switch (type) {
  1299. case NL80211_IFTYPE_ADHOC:
  1300. mode = NDIS_80211_INFRA_ADHOC;
  1301. break;
  1302. case NL80211_IFTYPE_STATION:
  1303. mode = NDIS_80211_INFRA_INFRA;
  1304. break;
  1305. default:
  1306. return -EINVAL;
  1307. }
  1308. priv->wdev.iftype = type;
  1309. return set_infra_mode(usbdev, mode);
  1310. }
  1311. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1312. {
  1313. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1314. struct usbnet *usbdev = priv->usbdev;
  1315. int err;
  1316. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1317. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1318. if (err < 0)
  1319. return err;
  1320. }
  1321. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1322. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1323. if (err < 0)
  1324. return err;
  1325. }
  1326. return 0;
  1327. }
  1328. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  1329. int dbm)
  1330. {
  1331. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1332. struct usbnet *usbdev = priv->usbdev;
  1333. devdbg(usbdev, "rndis_set_tx_power type:0x%x dbm:%i", type, dbm);
  1334. /* Device doesn't support changing txpower after initialization, only
  1335. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1336. * currently used.
  1337. */
  1338. if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
  1339. if (!priv->radio_on)
  1340. disassociate(usbdev, true); /* turn on radio */
  1341. return 0;
  1342. }
  1343. return -ENOTSUPP;
  1344. }
  1345. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
  1346. {
  1347. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1348. struct usbnet *usbdev = priv->usbdev;
  1349. *dbm = get_bcm4320_power_dbm(priv);
  1350. devdbg(usbdev, "rndis_get_tx_power dbm:%i", *dbm);
  1351. return 0;
  1352. }
  1353. #define SCAN_DELAY_JIFFIES (6 * HZ)
  1354. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  1355. struct cfg80211_scan_request *request)
  1356. {
  1357. struct usbnet *usbdev = netdev_priv(dev);
  1358. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1359. int ret;
  1360. __le32 tmp;
  1361. devdbg(usbdev, "cfg80211.scan");
  1362. /* Get current bssid list from device before new scan, as new scan
  1363. * clears internal bssid list.
  1364. */
  1365. rndis_check_bssid_list(usbdev);
  1366. if (!request)
  1367. return -EINVAL;
  1368. if (priv->scan_request && priv->scan_request != request)
  1369. return -EBUSY;
  1370. priv->scan_request = request;
  1371. tmp = cpu_to_le32(1);
  1372. ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  1373. sizeof(tmp));
  1374. if (ret == 0) {
  1375. /* Wait before retrieving scan results from device */
  1376. queue_delayed_work(priv->workqueue, &priv->scan_work,
  1377. SCAN_DELAY_JIFFIES);
  1378. }
  1379. return ret;
  1380. }
  1381. static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
  1382. struct ndis_80211_bssid_ex *bssid)
  1383. {
  1384. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1385. struct ieee80211_channel *channel;
  1386. s32 signal;
  1387. u64 timestamp;
  1388. u16 capability;
  1389. u16 beacon_interval;
  1390. struct ndis_80211_fixed_ies *fixed;
  1391. int ie_len, bssid_len;
  1392. u8 *ie;
  1393. devdbg(usbdev, " found bssid: '%.32s' [%pM]", bssid->ssid.essid,
  1394. bssid->mac);
  1395. /* parse bssid structure */
  1396. bssid_len = le32_to_cpu(bssid->length);
  1397. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1398. sizeof(struct ndis_80211_fixed_ies))
  1399. return NULL;
  1400. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1401. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1402. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1403. (int)le32_to_cpu(bssid->ie_length));
  1404. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1405. if (ie_len < 0)
  1406. return NULL;
  1407. /* extract data for cfg80211_inform_bss */
  1408. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1409. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1410. if (!channel)
  1411. return NULL;
  1412. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1413. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1414. capability = le16_to_cpu(fixed->capabilities);
  1415. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1416. return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1417. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1418. GFP_KERNEL);
  1419. }
  1420. static int rndis_check_bssid_list(struct usbnet *usbdev)
  1421. {
  1422. void *buf = NULL;
  1423. struct ndis_80211_bssid_list_ex *bssid_list;
  1424. struct ndis_80211_bssid_ex *bssid;
  1425. int ret = -EINVAL, len, count, bssid_len;
  1426. bool resized = false;
  1427. devdbg(usbdev, "check_bssid_list");
  1428. len = CONTROL_BUFFER_SIZE;
  1429. resize_buf:
  1430. buf = kmalloc(len, GFP_KERNEL);
  1431. if (!buf) {
  1432. ret = -ENOMEM;
  1433. goto out;
  1434. }
  1435. ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  1436. if (ret != 0)
  1437. goto out;
  1438. if (!resized && len > CONTROL_BUFFER_SIZE) {
  1439. resized = true;
  1440. kfree(buf);
  1441. goto resize_buf;
  1442. }
  1443. bssid_list = buf;
  1444. bssid = bssid_list->bssid;
  1445. bssid_len = le32_to_cpu(bssid->length);
  1446. count = le32_to_cpu(bssid_list->num_items);
  1447. devdbg(usbdev, "check_bssid_list: %d BSSIDs found (buflen: %d)", count,
  1448. len);
  1449. while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
  1450. rndis_bss_info_update(usbdev, bssid);
  1451. bssid = (void *)bssid + bssid_len;
  1452. bssid_len = le32_to_cpu(bssid->length);
  1453. count--;
  1454. }
  1455. out:
  1456. kfree(buf);
  1457. return ret;
  1458. }
  1459. static void rndis_get_scan_results(struct work_struct *work)
  1460. {
  1461. struct rndis_wlan_private *priv =
  1462. container_of(work, struct rndis_wlan_private, scan_work.work);
  1463. struct usbnet *usbdev = priv->usbdev;
  1464. int ret;
  1465. devdbg(usbdev, "get_scan_results");
  1466. if (!priv->scan_request)
  1467. return;
  1468. ret = rndis_check_bssid_list(usbdev);
  1469. cfg80211_scan_done(priv->scan_request, ret < 0);
  1470. priv->scan_request = NULL;
  1471. }
  1472. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  1473. struct cfg80211_connect_params *sme)
  1474. {
  1475. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1476. struct usbnet *usbdev = priv->usbdev;
  1477. struct ieee80211_channel *channel = sme->channel;
  1478. struct ndis_80211_ssid ssid;
  1479. int pairwise = RNDIS_WLAN_ALG_NONE;
  1480. int groupwise = RNDIS_WLAN_ALG_NONE;
  1481. int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
  1482. int length, i, ret, chan = -1;
  1483. if (channel)
  1484. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1485. groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
  1486. for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
  1487. pairwise |=
  1488. rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
  1489. if (sme->crypto.n_ciphers_pairwise > 0 &&
  1490. pairwise == RNDIS_WLAN_ALG_NONE) {
  1491. deverr(usbdev, "Unsupported pairwise cipher");
  1492. return -ENOTSUPP;
  1493. }
  1494. for (i = 0; i < sme->crypto.n_akm_suites; i++)
  1495. keymgmt |=
  1496. rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
  1497. if (sme->crypto.n_akm_suites > 0 &&
  1498. keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
  1499. deverr(usbdev, "Invalid keymgmt");
  1500. return -ENOTSUPP;
  1501. }
  1502. devdbg(usbdev, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:"
  1503. "0x%x]:0x%x)", sme->ssid, sme->bssid, chan,
  1504. sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
  1505. groupwise, pairwise, keymgmt);
  1506. if (is_associated(usbdev))
  1507. disassociate(usbdev, false);
  1508. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1509. if (ret < 0) {
  1510. devdbg(usbdev, "connect: set_infra_mode failed, %d", ret);
  1511. goto err_turn_radio_on;
  1512. }
  1513. ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
  1514. keymgmt);
  1515. if (ret < 0) {
  1516. devdbg(usbdev, "connect: set_auth_mode failed, %d", ret);
  1517. goto err_turn_radio_on;
  1518. }
  1519. set_priv_filter(usbdev);
  1520. ret = set_encr_mode(usbdev, pairwise, groupwise);
  1521. if (ret < 0) {
  1522. devdbg(usbdev, "connect: set_encr_mode failed, %d", ret);
  1523. goto err_turn_radio_on;
  1524. }
  1525. if (channel) {
  1526. ret = set_channel(usbdev, chan);
  1527. if (ret < 0) {
  1528. devdbg(usbdev, "connect: set_channel failed, %d", ret);
  1529. goto err_turn_radio_on;
  1530. }
  1531. }
  1532. if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
  1533. priv->encr_tx_key_index = sme->key_idx;
  1534. ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
  1535. if (ret < 0) {
  1536. devdbg(usbdev, "connect: add_wep_key failed, %d "
  1537. "(%d, %d)", ret, sme->key_len, sme->key_idx);
  1538. goto err_turn_radio_on;
  1539. }
  1540. }
  1541. if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
  1542. !is_broadcast_ether_addr(sme->bssid)) {
  1543. ret = set_bssid(usbdev, sme->bssid);
  1544. if (ret < 0) {
  1545. devdbg(usbdev, "connect: set_bssid failed, %d", ret);
  1546. goto err_turn_radio_on;
  1547. }
  1548. } else
  1549. clear_bssid(usbdev);
  1550. length = sme->ssid_len;
  1551. if (length > NDIS_802_11_LENGTH_SSID)
  1552. length = NDIS_802_11_LENGTH_SSID;
  1553. memset(&ssid, 0, sizeof(ssid));
  1554. ssid.length = cpu_to_le32(length);
  1555. memcpy(ssid.essid, sme->ssid, length);
  1556. /* Pause and purge rx queue, so we don't pass packets before
  1557. * 'media connect'-indication.
  1558. */
  1559. usbnet_pause_rx(usbdev);
  1560. usbnet_purge_paused_rxq(usbdev);
  1561. ret = set_essid(usbdev, &ssid);
  1562. if (ret < 0)
  1563. devdbg(usbdev, "connect: set_essid failed, %d", ret);
  1564. return ret;
  1565. err_turn_radio_on:
  1566. disassociate(usbdev, true);
  1567. return ret;
  1568. }
  1569. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1570. u16 reason_code)
  1571. {
  1572. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1573. struct usbnet *usbdev = priv->usbdev;
  1574. devdbg(usbdev, "cfg80211.disconnect(%d)", reason_code);
  1575. priv->connected = false;
  1576. memset(priv->bssid, 0, ETH_ALEN);
  1577. return deauthenticate(usbdev);
  1578. }
  1579. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1580. struct cfg80211_ibss_params *params)
  1581. {
  1582. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1583. struct usbnet *usbdev = priv->usbdev;
  1584. struct ieee80211_channel *channel = params->channel;
  1585. struct ndis_80211_ssid ssid;
  1586. enum nl80211_auth_type auth_type;
  1587. int ret, alg, length, chan = -1;
  1588. if (channel)
  1589. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1590. /* TODO: How to handle ad-hoc encryption?
  1591. * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
  1592. * pre-shared for encryption (open/shared/wpa), is key set before
  1593. * join_ibss? Which auth_type to use (not in params)? What about WPA?
  1594. */
  1595. if (params->privacy) {
  1596. auth_type = NL80211_AUTHTYPE_SHARED_KEY;
  1597. alg = RNDIS_WLAN_ALG_WEP;
  1598. } else {
  1599. auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  1600. alg = RNDIS_WLAN_ALG_NONE;
  1601. }
  1602. devdbg(usbdev, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)", params->ssid,
  1603. params->bssid, chan, params->privacy);
  1604. if (is_associated(usbdev))
  1605. disassociate(usbdev, false);
  1606. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
  1607. if (ret < 0) {
  1608. devdbg(usbdev, "join_ibss: set_infra_mode failed, %d", ret);
  1609. goto err_turn_radio_on;
  1610. }
  1611. ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
  1612. if (ret < 0) {
  1613. devdbg(usbdev, "join_ibss: set_auth_mode failed, %d", ret);
  1614. goto err_turn_radio_on;
  1615. }
  1616. set_priv_filter(usbdev);
  1617. ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
  1618. if (ret < 0) {
  1619. devdbg(usbdev, "join_ibss: set_encr_mode failed, %d", ret);
  1620. goto err_turn_radio_on;
  1621. }
  1622. if (channel) {
  1623. ret = set_channel(usbdev, chan);
  1624. if (ret < 0) {
  1625. devdbg(usbdev, "join_ibss: set_channel failed, %d",
  1626. ret);
  1627. goto err_turn_radio_on;
  1628. }
  1629. }
  1630. if (params->bssid && !is_zero_ether_addr(params->bssid) &&
  1631. !is_broadcast_ether_addr(params->bssid)) {
  1632. ret = set_bssid(usbdev, params->bssid);
  1633. if (ret < 0) {
  1634. devdbg(usbdev, "join_ibss: set_bssid failed, %d", ret);
  1635. goto err_turn_radio_on;
  1636. }
  1637. } else
  1638. clear_bssid(usbdev);
  1639. length = params->ssid_len;
  1640. if (length > NDIS_802_11_LENGTH_SSID)
  1641. length = NDIS_802_11_LENGTH_SSID;
  1642. memset(&ssid, 0, sizeof(ssid));
  1643. ssid.length = cpu_to_le32(length);
  1644. memcpy(ssid.essid, params->ssid, length);
  1645. /* Don't need to pause rx queue for ad-hoc. */
  1646. usbnet_purge_paused_rxq(usbdev);
  1647. usbnet_resume_rx(usbdev);
  1648. ret = set_essid(usbdev, &ssid);
  1649. if (ret < 0)
  1650. devdbg(usbdev, "join_ibss: set_essid failed, %d", ret);
  1651. return ret;
  1652. err_turn_radio_on:
  1653. disassociate(usbdev, true);
  1654. return ret;
  1655. }
  1656. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1657. {
  1658. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1659. struct usbnet *usbdev = priv->usbdev;
  1660. devdbg(usbdev, "cfg80211.leave_ibss()");
  1661. priv->connected = false;
  1662. memset(priv->bssid, 0, ETH_ALEN);
  1663. return deauthenticate(usbdev);
  1664. }
  1665. static int rndis_set_channel(struct wiphy *wiphy,
  1666. struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
  1667. {
  1668. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1669. struct usbnet *usbdev = priv->usbdev;
  1670. return set_channel(usbdev,
  1671. ieee80211_frequency_to_channel(chan->center_freq));
  1672. }
  1673. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  1674. u8 key_index, const u8 *mac_addr,
  1675. struct key_params *params)
  1676. {
  1677. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1678. struct usbnet *usbdev = priv->usbdev;
  1679. __le32 flags;
  1680. devdbg(usbdev, "rndis_add_key(%i, %pM, %08x)", key_index, mac_addr,
  1681. params->cipher);
  1682. switch (params->cipher) {
  1683. case WLAN_CIPHER_SUITE_WEP40:
  1684. case WLAN_CIPHER_SUITE_WEP104:
  1685. return add_wep_key(usbdev, params->key, params->key_len,
  1686. key_index);
  1687. case WLAN_CIPHER_SUITE_TKIP:
  1688. case WLAN_CIPHER_SUITE_CCMP:
  1689. flags = 0;
  1690. if (params->seq && params->seq_len > 0)
  1691. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  1692. if (mac_addr)
  1693. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
  1694. NDIS_80211_ADDKEY_TRANSMIT_KEY;
  1695. return add_wpa_key(usbdev, params->key, params->key_len,
  1696. key_index, mac_addr, params->seq,
  1697. params->seq_len, params->cipher, flags);
  1698. default:
  1699. devdbg(usbdev, "rndis_add_key: unsupported cipher %08x",
  1700. params->cipher);
  1701. return -ENOTSUPP;
  1702. }
  1703. }
  1704. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  1705. u8 key_index, const u8 *mac_addr)
  1706. {
  1707. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1708. struct usbnet *usbdev = priv->usbdev;
  1709. devdbg(usbdev, "rndis_del_key(%i, %pM)", key_index, mac_addr);
  1710. return remove_key(usbdev, key_index, mac_addr);
  1711. }
  1712. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  1713. u8 key_index)
  1714. {
  1715. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1716. struct usbnet *usbdev = priv->usbdev;
  1717. struct rndis_wlan_encr_key key;
  1718. devdbg(usbdev, "rndis_set_default_key(%i)", key_index);
  1719. priv->encr_tx_key_index = key_index;
  1720. key = priv->encr_keys[key_index];
  1721. return add_wep_key(usbdev, key.material, key.len, key_index);
  1722. }
  1723. static void rndis_fill_station_info(struct usbnet *usbdev,
  1724. struct station_info *sinfo)
  1725. {
  1726. __le32 linkspeed, rssi;
  1727. int ret, len;
  1728. memset(sinfo, 0, sizeof(*sinfo));
  1729. len = sizeof(linkspeed);
  1730. ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
  1731. if (ret == 0) {
  1732. sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
  1733. sinfo->filled |= STATION_INFO_TX_BITRATE;
  1734. }
  1735. len = sizeof(rssi);
  1736. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  1737. if (ret == 0) {
  1738. sinfo->signal = level_to_qual(le32_to_cpu(rssi));
  1739. sinfo->filled |= STATION_INFO_SIGNAL;
  1740. }
  1741. }
  1742. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  1743. u8 *mac, struct station_info *sinfo)
  1744. {
  1745. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1746. struct usbnet *usbdev = priv->usbdev;
  1747. if (compare_ether_addr(priv->bssid, mac))
  1748. return -ENOENT;
  1749. rndis_fill_station_info(usbdev, sinfo);
  1750. return 0;
  1751. }
  1752. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  1753. int idx, u8 *mac, struct station_info *sinfo)
  1754. {
  1755. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1756. struct usbnet *usbdev = priv->usbdev;
  1757. if (idx != 0)
  1758. return -ENOENT;
  1759. memcpy(mac, priv->bssid, ETH_ALEN);
  1760. rndis_fill_station_info(usbdev, sinfo);
  1761. return 0;
  1762. }
  1763. /*
  1764. * workers, indication handlers, device poller
  1765. */
  1766. static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
  1767. {
  1768. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1769. struct ndis_80211_assoc_info *info;
  1770. u8 assoc_buf[sizeof(*info) + IW_CUSTOM_MAX + 32];
  1771. u8 bssid[ETH_ALEN];
  1772. int resp_ie_len, req_ie_len;
  1773. u8 *req_ie, *resp_ie;
  1774. int ret, offset;
  1775. bool roamed = false;
  1776. if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
  1777. /* received media connect indication while connected, either
  1778. * device reassociated with same AP or roamed to new. */
  1779. roamed = true;
  1780. }
  1781. req_ie_len = 0;
  1782. resp_ie_len = 0;
  1783. req_ie = NULL;
  1784. resp_ie = NULL;
  1785. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  1786. memset(assoc_buf, 0, sizeof(assoc_buf));
  1787. info = (void *)assoc_buf;
  1788. /* Get association info IEs from device and send them back to
  1789. * userspace. */
  1790. ret = get_association_info(usbdev, info, sizeof(assoc_buf));
  1791. if (!ret) {
  1792. req_ie_len = le32_to_cpu(info->req_ie_length);
  1793. if (req_ie_len > 0) {
  1794. offset = le32_to_cpu(info->offset_req_ies);
  1795. req_ie = (u8 *)info + offset;
  1796. }
  1797. resp_ie_len = le32_to_cpu(info->resp_ie_length);
  1798. if (resp_ie_len > 0) {
  1799. offset = le32_to_cpu(info->offset_resp_ies);
  1800. resp_ie = (u8 *)info + offset;
  1801. }
  1802. }
  1803. } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
  1804. return;
  1805. ret = get_bssid(usbdev, bssid);
  1806. if (ret < 0)
  1807. memset(bssid, 0, sizeof(bssid));
  1808. devdbg(usbdev, "link up work: [%pM] %s", bssid, roamed ? "roamed" : "");
  1809. /* Internal bss list in device always contains at least the currently
  1810. * connected bss and we can get it to cfg80211 with
  1811. * rndis_check_bssid_list().
  1812. * NOTE: This is true for Broadcom chip, but not mentioned in RNDIS
  1813. * spec.
  1814. */
  1815. rndis_check_bssid_list(usbdev);
  1816. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  1817. if (!roamed)
  1818. cfg80211_connect_result(usbdev->net, bssid, req_ie,
  1819. req_ie_len, resp_ie,
  1820. resp_ie_len, 0, GFP_KERNEL);
  1821. else
  1822. cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
  1823. resp_ie, resp_ie_len, GFP_KERNEL);
  1824. } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1825. cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
  1826. priv->connected = true;
  1827. memcpy(priv->bssid, bssid, ETH_ALEN);
  1828. usbnet_resume_rx(usbdev);
  1829. netif_carrier_on(usbdev->net);
  1830. }
  1831. static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
  1832. {
  1833. union iwreq_data evt;
  1834. netif_carrier_off(usbdev->net);
  1835. evt.data.flags = 0;
  1836. evt.data.length = 0;
  1837. memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
  1838. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1839. }
  1840. static void rndis_wlan_worker(struct work_struct *work)
  1841. {
  1842. struct rndis_wlan_private *priv =
  1843. container_of(work, struct rndis_wlan_private, work);
  1844. struct usbnet *usbdev = priv->usbdev;
  1845. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
  1846. rndis_wlan_do_link_up_work(usbdev);
  1847. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
  1848. rndis_wlan_do_link_down_work(usbdev);
  1849. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1850. set_multicast_list(usbdev);
  1851. }
  1852. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  1853. {
  1854. struct usbnet *usbdev = netdev_priv(dev);
  1855. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1856. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1857. return;
  1858. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  1859. queue_work(priv->workqueue, &priv->work);
  1860. }
  1861. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  1862. struct ndis_80211_status_indication *indication,
  1863. int len)
  1864. {
  1865. u8 *buf;
  1866. const char *type;
  1867. int flags, buflen, key_id;
  1868. bool pairwise_error, group_error;
  1869. struct ndis_80211_auth_request *auth_req;
  1870. enum nl80211_key_type key_type;
  1871. /* must have at least one array entry */
  1872. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1873. sizeof(struct ndis_80211_auth_request)) {
  1874. devinfo(usbdev, "authentication indication: "
  1875. "too short message (%i)", len);
  1876. return;
  1877. }
  1878. buf = (void *)&indication->u.auth_request[0];
  1879. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  1880. while (buflen >= sizeof(*auth_req)) {
  1881. auth_req = (void *)buf;
  1882. type = "unknown";
  1883. flags = le32_to_cpu(auth_req->flags);
  1884. pairwise_error = false;
  1885. group_error = false;
  1886. if (flags & 0x1)
  1887. type = "reauth request";
  1888. if (flags & 0x2)
  1889. type = "key update request";
  1890. if (flags & 0x6) {
  1891. pairwise_error = true;
  1892. type = "pairwise_error";
  1893. }
  1894. if (flags & 0xe) {
  1895. group_error = true;
  1896. type = "group_error";
  1897. }
  1898. devinfo(usbdev, "authentication indication: %s (0x%08x)", type,
  1899. le32_to_cpu(auth_req->flags));
  1900. if (pairwise_error) {
  1901. key_type = NL80211_KEYTYPE_PAIRWISE;
  1902. key_id = -1;
  1903. cfg80211_michael_mic_failure(usbdev->net,
  1904. auth_req->bssid,
  1905. key_type, key_id, NULL,
  1906. GFP_KERNEL);
  1907. }
  1908. if (group_error) {
  1909. key_type = NL80211_KEYTYPE_GROUP;
  1910. key_id = -1;
  1911. cfg80211_michael_mic_failure(usbdev->net,
  1912. auth_req->bssid,
  1913. key_type, key_id, NULL,
  1914. GFP_KERNEL);
  1915. }
  1916. buflen -= le32_to_cpu(auth_req->length);
  1917. buf += le32_to_cpu(auth_req->length);
  1918. }
  1919. }
  1920. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  1921. struct ndis_80211_status_indication *indication,
  1922. int len)
  1923. {
  1924. struct ndis_80211_pmkid_cand_list *cand_list;
  1925. int list_len, expected_len, i;
  1926. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1927. sizeof(struct ndis_80211_pmkid_cand_list)) {
  1928. devinfo(usbdev, "pmkid candidate list indication: "
  1929. "too short message (%i)", len);
  1930. return;
  1931. }
  1932. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  1933. sizeof(struct ndis_80211_pmkid_candidate);
  1934. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  1935. offsetof(struct ndis_80211_status_indication, u);
  1936. if (len < expected_len) {
  1937. devinfo(usbdev, "pmkid candidate list indication: "
  1938. "list larger than buffer (%i < %i)",
  1939. len, expected_len);
  1940. return;
  1941. }
  1942. cand_list = &indication->u.cand_list;
  1943. devinfo(usbdev, "pmkid candidate list indication: "
  1944. "version %i, candidates %i",
  1945. le32_to_cpu(cand_list->version),
  1946. le32_to_cpu(cand_list->num_candidates));
  1947. if (le32_to_cpu(cand_list->version) != 1)
  1948. return;
  1949. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  1950. struct ndis_80211_pmkid_candidate *cand =
  1951. &cand_list->candidate_list[i];
  1952. devdbg(usbdev, "cand[%i]: flags: 0x%08x, bssid: %pM",
  1953. i, le32_to_cpu(cand->flags), cand->bssid);
  1954. #if 0
  1955. struct iw_pmkid_cand pcand;
  1956. union iwreq_data wrqu;
  1957. memset(&pcand, 0, sizeof(pcand));
  1958. if (le32_to_cpu(cand->flags) & 0x01)
  1959. pcand.flags |= IW_PMKID_CAND_PREAUTH;
  1960. pcand.index = i;
  1961. memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
  1962. memset(&wrqu, 0, sizeof(wrqu));
  1963. wrqu.data.length = sizeof(pcand);
  1964. wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
  1965. (u8 *)&pcand);
  1966. #endif
  1967. }
  1968. }
  1969. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  1970. struct rndis_indicate *msg, int buflen)
  1971. {
  1972. struct ndis_80211_status_indication *indication;
  1973. int len, offset;
  1974. offset = offsetof(struct rndis_indicate, status) +
  1975. le32_to_cpu(msg->offset);
  1976. len = le32_to_cpu(msg->length);
  1977. if (len < 8) {
  1978. devinfo(usbdev, "media specific indication, "
  1979. "ignore too short message (%i < 8)", len);
  1980. return;
  1981. }
  1982. if (offset + len > buflen) {
  1983. devinfo(usbdev, "media specific indication, "
  1984. "too large to fit to buffer (%i > %i)",
  1985. offset + len, buflen);
  1986. return;
  1987. }
  1988. indication = (void *)((u8 *)msg + offset);
  1989. switch (le32_to_cpu(indication->status_type)) {
  1990. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  1991. devinfo(usbdev, "radio state indication: %i",
  1992. le32_to_cpu(indication->u.radio_status));
  1993. return;
  1994. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  1995. devinfo(usbdev, "media stream mode indication: %i",
  1996. le32_to_cpu(indication->u.media_stream_mode));
  1997. return;
  1998. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  1999. rndis_wlan_auth_indication(usbdev, indication, len);
  2000. return;
  2001. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2002. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2003. return;
  2004. default:
  2005. devinfo(usbdev, "media specific indication: "
  2006. "unknown status type 0x%08x",
  2007. le32_to_cpu(indication->status_type));
  2008. }
  2009. }
  2010. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2011. {
  2012. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2013. struct rndis_indicate *msg = ind;
  2014. switch (msg->status) {
  2015. case RNDIS_STATUS_MEDIA_CONNECT:
  2016. if (priv->current_command_oid == OID_802_11_ADD_KEY) {
  2017. /* OID_802_11_ADD_KEY causes sometimes extra
  2018. * "media connect" indications which confuses driver
  2019. * and userspace to think that device is
  2020. * roaming/reassociating when it isn't.
  2021. */
  2022. devdbg(usbdev, "ignored OID_802_11_ADD_KEY triggered "
  2023. "'media connect'");
  2024. return;
  2025. }
  2026. usbnet_pause_rx(usbdev);
  2027. devinfo(usbdev, "media connect");
  2028. /* queue work to avoid recursive calls into rndis_command */
  2029. set_bit(WORK_LINK_UP, &priv->work_pending);
  2030. queue_work(priv->workqueue, &priv->work);
  2031. break;
  2032. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2033. devinfo(usbdev, "media disconnect");
  2034. /* queue work to avoid recursive calls into rndis_command */
  2035. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2036. queue_work(priv->workqueue, &priv->work);
  2037. break;
  2038. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2039. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2040. break;
  2041. default:
  2042. devinfo(usbdev, "indication: 0x%08x",
  2043. le32_to_cpu(msg->status));
  2044. break;
  2045. }
  2046. }
  2047. static int rndis_wlan_get_caps(struct usbnet *usbdev)
  2048. {
  2049. struct {
  2050. __le32 num_items;
  2051. __le32 items[8];
  2052. } networks_supported;
  2053. int len, retval, i, n;
  2054. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2055. /* determine supported modes */
  2056. len = sizeof(networks_supported);
  2057. retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
  2058. &networks_supported, &len);
  2059. if (retval >= 0) {
  2060. n = le32_to_cpu(networks_supported.num_items);
  2061. if (n > 8)
  2062. n = 8;
  2063. for (i = 0; i < n; i++) {
  2064. switch (le32_to_cpu(networks_supported.items[i])) {
  2065. case NDIS_80211_TYPE_FREQ_HOP:
  2066. case NDIS_80211_TYPE_DIRECT_SEQ:
  2067. priv->caps |= CAP_MODE_80211B;
  2068. break;
  2069. case NDIS_80211_TYPE_OFDM_A:
  2070. priv->caps |= CAP_MODE_80211A;
  2071. break;
  2072. case NDIS_80211_TYPE_OFDM_G:
  2073. priv->caps |= CAP_MODE_80211G;
  2074. break;
  2075. }
  2076. }
  2077. }
  2078. return retval;
  2079. }
  2080. #define DEVICE_POLLER_JIFFIES (HZ)
  2081. static void rndis_device_poller(struct work_struct *work)
  2082. {
  2083. struct rndis_wlan_private *priv =
  2084. container_of(work, struct rndis_wlan_private,
  2085. dev_poller_work.work);
  2086. struct usbnet *usbdev = priv->usbdev;
  2087. __le32 rssi, tmp;
  2088. int len, ret, j;
  2089. int update_jiffies = DEVICE_POLLER_JIFFIES;
  2090. void *buf;
  2091. /* Only check/do workaround when connected. Calling is_associated()
  2092. * also polls device with rndis_command() and catches for media link
  2093. * indications.
  2094. */
  2095. if (!is_associated(usbdev))
  2096. goto end;
  2097. len = sizeof(rssi);
  2098. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  2099. if (ret == 0)
  2100. priv->last_qual = level_to_qual(le32_to_cpu(rssi));
  2101. devdbg(usbdev, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d",
  2102. ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
  2103. /* Workaround transfer stalls on poor quality links.
  2104. * TODO: find right way to fix these stalls (as stalls do not happen
  2105. * with ndiswrapper/windows driver). */
  2106. if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
  2107. /* Decrease stats worker interval to catch stalls.
  2108. * faster. Faster than 400-500ms causes packet loss,
  2109. * Slower doesn't catch stalls fast enough.
  2110. */
  2111. j = msecs_to_jiffies(priv->param_workaround_interval);
  2112. if (j > DEVICE_POLLER_JIFFIES)
  2113. j = DEVICE_POLLER_JIFFIES;
  2114. else if (j <= 0)
  2115. j = 1;
  2116. update_jiffies = j;
  2117. /* Send scan OID. Use of both OIDs is required to get device
  2118. * working.
  2119. */
  2120. tmp = cpu_to_le32(1);
  2121. rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  2122. sizeof(tmp));
  2123. len = CONTROL_BUFFER_SIZE;
  2124. buf = kmalloc(len, GFP_KERNEL);
  2125. if (!buf)
  2126. goto end;
  2127. rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  2128. kfree(buf);
  2129. }
  2130. end:
  2131. if (update_jiffies >= HZ)
  2132. update_jiffies = round_jiffies_relative(update_jiffies);
  2133. else {
  2134. j = round_jiffies_relative(update_jiffies);
  2135. if (abs(j - update_jiffies) <= 10)
  2136. update_jiffies = j;
  2137. }
  2138. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2139. update_jiffies);
  2140. }
  2141. /*
  2142. * driver/device initialization
  2143. */
  2144. static int bcm4320a_early_init(struct usbnet *usbdev)
  2145. {
  2146. /* bcm4320a doesn't handle configuration parameters well. Try
  2147. * set any and you get partially zeroed mac and broken device.
  2148. */
  2149. return 0;
  2150. }
  2151. static int bcm4320b_early_init(struct usbnet *usbdev)
  2152. {
  2153. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2154. char buf[8];
  2155. /* Early initialization settings, setting these won't have effect
  2156. * if called after generic_rndis_bind().
  2157. */
  2158. priv->param_country[0] = modparam_country[0];
  2159. priv->param_country[1] = modparam_country[1];
  2160. priv->param_country[2] = 0;
  2161. priv->param_frameburst = modparam_frameburst;
  2162. priv->param_afterburner = modparam_afterburner;
  2163. priv->param_power_save = modparam_power_save;
  2164. priv->param_power_output = modparam_power_output;
  2165. priv->param_roamtrigger = modparam_roamtrigger;
  2166. priv->param_roamdelta = modparam_roamdelta;
  2167. priv->param_country[0] = toupper(priv->param_country[0]);
  2168. priv->param_country[1] = toupper(priv->param_country[1]);
  2169. /* doesn't support EU as country code, use FI instead */
  2170. if (!strcmp(priv->param_country, "EU"))
  2171. strcpy(priv->param_country, "FI");
  2172. if (priv->param_power_save < 0)
  2173. priv->param_power_save = 0;
  2174. else if (priv->param_power_save > 2)
  2175. priv->param_power_save = 2;
  2176. if (priv->param_power_output < 0)
  2177. priv->param_power_output = 0;
  2178. else if (priv->param_power_output > 3)
  2179. priv->param_power_output = 3;
  2180. if (priv->param_roamtrigger < -80)
  2181. priv->param_roamtrigger = -80;
  2182. else if (priv->param_roamtrigger > -60)
  2183. priv->param_roamtrigger = -60;
  2184. if (priv->param_roamdelta < 0)
  2185. priv->param_roamdelta = 0;
  2186. else if (priv->param_roamdelta > 2)
  2187. priv->param_roamdelta = 2;
  2188. if (modparam_workaround_interval < 0)
  2189. priv->param_workaround_interval = 500;
  2190. else
  2191. priv->param_workaround_interval = modparam_workaround_interval;
  2192. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2193. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2194. priv->param_frameburst ? "1" : "0");
  2195. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2196. priv->param_afterburner ? "1" : "0");
  2197. sprintf(buf, "%d", priv->param_power_save);
  2198. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2199. sprintf(buf, "%d", priv->param_power_output);
  2200. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2201. sprintf(buf, "%d", priv->param_roamtrigger);
  2202. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2203. sprintf(buf, "%d", priv->param_roamdelta);
  2204. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2205. return 0;
  2206. }
  2207. /* same as rndis_netdev_ops but with local multicast handler */
  2208. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2209. .ndo_open = usbnet_open,
  2210. .ndo_stop = usbnet_stop,
  2211. .ndo_start_xmit = usbnet_start_xmit,
  2212. .ndo_tx_timeout = usbnet_tx_timeout,
  2213. .ndo_set_mac_address = eth_mac_addr,
  2214. .ndo_validate_addr = eth_validate_addr,
  2215. .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
  2216. };
  2217. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2218. {
  2219. struct wiphy *wiphy;
  2220. struct rndis_wlan_private *priv;
  2221. int retval, len;
  2222. __le32 tmp;
  2223. /* allocate wiphy and rndis private data
  2224. * NOTE: We only support a single virtual interface, so wiphy
  2225. * and wireless_dev are somewhat synonymous for this device.
  2226. */
  2227. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2228. if (!wiphy)
  2229. return -ENOMEM;
  2230. priv = wiphy_priv(wiphy);
  2231. usbdev->net->ieee80211_ptr = &priv->wdev;
  2232. priv->wdev.wiphy = wiphy;
  2233. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2234. /* These have to be initialized before calling generic_rndis_bind().
  2235. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2236. */
  2237. usbdev->driver_priv = priv;
  2238. priv->usbdev = usbdev;
  2239. mutex_init(&priv->command_lock);
  2240. /* because rndis_command() sleeps we need to use workqueue */
  2241. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2242. INIT_WORK(&priv->work, rndis_wlan_worker);
  2243. INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
  2244. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2245. /* try bind rndis_host */
  2246. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2247. if (retval < 0)
  2248. goto fail;
  2249. /* generic_rndis_bind set packet filter to multicast_all+
  2250. * promisc mode which doesn't work well for our devices (device
  2251. * picks up rssi to closest station instead of to access point).
  2252. *
  2253. * rndis_host wants to avoid all OID as much as possible
  2254. * so do promisc/multicast handling in rndis_wlan.
  2255. */
  2256. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2257. tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  2258. retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
  2259. sizeof(tmp));
  2260. len = sizeof(tmp);
  2261. retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
  2262. &len);
  2263. priv->multicast_size = le32_to_cpu(tmp);
  2264. if (retval < 0 || priv->multicast_size < 0)
  2265. priv->multicast_size = 0;
  2266. if (priv->multicast_size > 0)
  2267. usbdev->net->flags |= IFF_MULTICAST;
  2268. else
  2269. usbdev->net->flags &= ~IFF_MULTICAST;
  2270. /* fill-out wiphy structure and register w/ cfg80211 */
  2271. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2272. wiphy->privid = rndis_wiphy_privid;
  2273. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2274. | BIT(NL80211_IFTYPE_ADHOC);
  2275. wiphy->max_scan_ssids = 1;
  2276. /* TODO: fill-out band/encr information based on priv->caps */
  2277. rndis_wlan_get_caps(usbdev);
  2278. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2279. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2280. priv->band.channels = priv->channels;
  2281. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2282. priv->band.bitrates = priv->rates;
  2283. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2284. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2285. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2286. memcpy(priv->cipher_suites, rndis_cipher_suites,
  2287. sizeof(rndis_cipher_suites));
  2288. wiphy->cipher_suites = priv->cipher_suites;
  2289. wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
  2290. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2291. if (wiphy_register(wiphy)) {
  2292. retval = -ENODEV;
  2293. goto fail;
  2294. }
  2295. set_default_iw_params(usbdev);
  2296. /* set default rts/frag */
  2297. rndis_set_wiphy_params(wiphy,
  2298. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2299. /* turn radio on */
  2300. priv->radio_on = true;
  2301. disassociate(usbdev, true);
  2302. netif_carrier_off(usbdev->net);
  2303. return 0;
  2304. fail:
  2305. cancel_delayed_work_sync(&priv->dev_poller_work);
  2306. cancel_delayed_work_sync(&priv->scan_work);
  2307. cancel_work_sync(&priv->work);
  2308. flush_workqueue(priv->workqueue);
  2309. destroy_workqueue(priv->workqueue);
  2310. wiphy_free(wiphy);
  2311. return retval;
  2312. }
  2313. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2314. {
  2315. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2316. /* turn radio off */
  2317. disassociate(usbdev, false);
  2318. cancel_delayed_work_sync(&priv->dev_poller_work);
  2319. cancel_delayed_work_sync(&priv->scan_work);
  2320. cancel_work_sync(&priv->work);
  2321. flush_workqueue(priv->workqueue);
  2322. destroy_workqueue(priv->workqueue);
  2323. if (priv && priv->wpa_ie_len)
  2324. kfree(priv->wpa_ie);
  2325. rndis_unbind(usbdev, intf);
  2326. wiphy_unregister(priv->wdev.wiphy);
  2327. wiphy_free(priv->wdev.wiphy);
  2328. }
  2329. static int rndis_wlan_reset(struct usbnet *usbdev)
  2330. {
  2331. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2332. int retval;
  2333. devdbg(usbdev, "rndis_wlan_reset");
  2334. retval = rndis_reset(usbdev);
  2335. if (retval)
  2336. devwarn(usbdev, "rndis_reset() failed: %d", retval);
  2337. /* rndis_reset cleared multicast list, so restore here.
  2338. (set_multicast_list() also turns on current packet filter) */
  2339. set_multicast_list(usbdev);
  2340. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2341. round_jiffies_relative(DEVICE_POLLER_JIFFIES));
  2342. return deauthenticate(usbdev);
  2343. }
  2344. static int rndis_wlan_stop(struct usbnet *usbdev)
  2345. {
  2346. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2347. int retval;
  2348. __le32 filter;
  2349. devdbg(usbdev, "rndis_wlan_stop");
  2350. retval = disassociate(usbdev, false);
  2351. priv->work_pending = 0;
  2352. cancel_delayed_work_sync(&priv->dev_poller_work);
  2353. cancel_delayed_work_sync(&priv->scan_work);
  2354. cancel_work_sync(&priv->work);
  2355. flush_workqueue(priv->workqueue);
  2356. if (priv->scan_request) {
  2357. cfg80211_scan_done(priv->scan_request, true);
  2358. priv->scan_request = NULL;
  2359. }
  2360. /* Set current packet filter zero to block receiving data packets from
  2361. device. */
  2362. filter = 0;
  2363. rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2364. sizeof(filter));
  2365. return retval;
  2366. }
  2367. static const struct driver_info bcm4320b_info = {
  2368. .description = "Wireless RNDIS device, BCM4320b based",
  2369. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2370. FLAG_AVOID_UNLINK_URBS,
  2371. .bind = rndis_wlan_bind,
  2372. .unbind = rndis_wlan_unbind,
  2373. .status = rndis_status,
  2374. .rx_fixup = rndis_rx_fixup,
  2375. .tx_fixup = rndis_tx_fixup,
  2376. .reset = rndis_wlan_reset,
  2377. .stop = rndis_wlan_stop,
  2378. .early_init = bcm4320b_early_init,
  2379. .indication = rndis_wlan_indication,
  2380. };
  2381. static const struct driver_info bcm4320a_info = {
  2382. .description = "Wireless RNDIS device, BCM4320a based",
  2383. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2384. FLAG_AVOID_UNLINK_URBS,
  2385. .bind = rndis_wlan_bind,
  2386. .unbind = rndis_wlan_unbind,
  2387. .status = rndis_status,
  2388. .rx_fixup = rndis_rx_fixup,
  2389. .tx_fixup = rndis_tx_fixup,
  2390. .reset = rndis_wlan_reset,
  2391. .stop = rndis_wlan_stop,
  2392. .early_init = bcm4320a_early_init,
  2393. .indication = rndis_wlan_indication,
  2394. };
  2395. static const struct driver_info rndis_wlan_info = {
  2396. .description = "Wireless RNDIS device",
  2397. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2398. FLAG_AVOID_UNLINK_URBS,
  2399. .bind = rndis_wlan_bind,
  2400. .unbind = rndis_wlan_unbind,
  2401. .status = rndis_status,
  2402. .rx_fixup = rndis_rx_fixup,
  2403. .tx_fixup = rndis_tx_fixup,
  2404. .reset = rndis_wlan_reset,
  2405. .stop = rndis_wlan_stop,
  2406. .early_init = bcm4320a_early_init,
  2407. .indication = rndis_wlan_indication,
  2408. };
  2409. /*-------------------------------------------------------------------------*/
  2410. static const struct usb_device_id products [] = {
  2411. #define RNDIS_MASTER_INTERFACE \
  2412. .bInterfaceClass = USB_CLASS_COMM, \
  2413. .bInterfaceSubClass = 2 /* ACM */, \
  2414. .bInterfaceProtocol = 0x0ff
  2415. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2416. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  2417. */
  2418. {
  2419. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2420. | USB_DEVICE_ID_MATCH_DEVICE,
  2421. .idVendor = 0x0411,
  2422. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  2423. RNDIS_MASTER_INTERFACE,
  2424. .driver_info = (unsigned long) &bcm4320b_info,
  2425. }, {
  2426. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2427. | USB_DEVICE_ID_MATCH_DEVICE,
  2428. .idVendor = 0x0baf,
  2429. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  2430. RNDIS_MASTER_INTERFACE,
  2431. .driver_info = (unsigned long) &bcm4320b_info,
  2432. }, {
  2433. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2434. | USB_DEVICE_ID_MATCH_DEVICE,
  2435. .idVendor = 0x050d,
  2436. .idProduct = 0x011b, /* Belkin F5D7051 */
  2437. RNDIS_MASTER_INTERFACE,
  2438. .driver_info = (unsigned long) &bcm4320b_info,
  2439. }, {
  2440. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2441. | USB_DEVICE_ID_MATCH_DEVICE,
  2442. .idVendor = 0x1799, /* Belkin has two vendor ids */
  2443. .idProduct = 0x011b, /* Belkin F5D7051 */
  2444. RNDIS_MASTER_INTERFACE,
  2445. .driver_info = (unsigned long) &bcm4320b_info,
  2446. }, {
  2447. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2448. | USB_DEVICE_ID_MATCH_DEVICE,
  2449. .idVendor = 0x13b1,
  2450. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  2451. RNDIS_MASTER_INTERFACE,
  2452. .driver_info = (unsigned long) &bcm4320b_info,
  2453. }, {
  2454. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2455. | USB_DEVICE_ID_MATCH_DEVICE,
  2456. .idVendor = 0x13b1,
  2457. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  2458. RNDIS_MASTER_INTERFACE,
  2459. .driver_info = (unsigned long) &bcm4320b_info,
  2460. }, {
  2461. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2462. | USB_DEVICE_ID_MATCH_DEVICE,
  2463. .idVendor = 0x0b05,
  2464. .idProduct = 0x1717, /* Asus WL169gE */
  2465. RNDIS_MASTER_INTERFACE,
  2466. .driver_info = (unsigned long) &bcm4320b_info,
  2467. }, {
  2468. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2469. | USB_DEVICE_ID_MATCH_DEVICE,
  2470. .idVendor = 0x0a5c,
  2471. .idProduct = 0xd11b, /* Eminent EM4045 */
  2472. RNDIS_MASTER_INTERFACE,
  2473. .driver_info = (unsigned long) &bcm4320b_info,
  2474. }, {
  2475. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2476. | USB_DEVICE_ID_MATCH_DEVICE,
  2477. .idVendor = 0x1690,
  2478. .idProduct = 0x0715, /* BT Voyager 1055 */
  2479. RNDIS_MASTER_INTERFACE,
  2480. .driver_info = (unsigned long) &bcm4320b_info,
  2481. },
  2482. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  2483. * parameters available, hardware probably contain older firmware version with
  2484. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  2485. */
  2486. {
  2487. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2488. | USB_DEVICE_ID_MATCH_DEVICE,
  2489. .idVendor = 0x13b1,
  2490. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  2491. RNDIS_MASTER_INTERFACE,
  2492. .driver_info = (unsigned long) &bcm4320a_info,
  2493. }, {
  2494. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2495. | USB_DEVICE_ID_MATCH_DEVICE,
  2496. .idVendor = 0x0baf,
  2497. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  2498. RNDIS_MASTER_INTERFACE,
  2499. .driver_info = (unsigned long) &bcm4320a_info,
  2500. }, {
  2501. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2502. | USB_DEVICE_ID_MATCH_DEVICE,
  2503. .idVendor = 0x0411,
  2504. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  2505. RNDIS_MASTER_INTERFACE,
  2506. .driver_info = (unsigned long) &bcm4320a_info,
  2507. },
  2508. /* Generic Wireless RNDIS devices that we don't have exact
  2509. * idVendor/idProduct/chip yet.
  2510. */
  2511. {
  2512. /* RNDIS is MSFT's un-official variant of CDC ACM */
  2513. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  2514. .driver_info = (unsigned long) &rndis_wlan_info,
  2515. }, {
  2516. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  2517. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  2518. .driver_info = (unsigned long) &rndis_wlan_info,
  2519. },
  2520. { }, // END
  2521. };
  2522. MODULE_DEVICE_TABLE(usb, products);
  2523. static struct usb_driver rndis_wlan_driver = {
  2524. .name = "rndis_wlan",
  2525. .id_table = products,
  2526. .probe = usbnet_probe,
  2527. .disconnect = usbnet_disconnect,
  2528. .suspend = usbnet_suspend,
  2529. .resume = usbnet_resume,
  2530. };
  2531. static int __init rndis_wlan_init(void)
  2532. {
  2533. return usb_register(&rndis_wlan_driver);
  2534. }
  2535. module_init(rndis_wlan_init);
  2536. static void __exit rndis_wlan_exit(void)
  2537. {
  2538. usb_deregister(&rndis_wlan_driver);
  2539. }
  2540. module_exit(rndis_wlan_exit);
  2541. MODULE_AUTHOR("Bjorge Dijkstra");
  2542. MODULE_AUTHOR("Jussi Kivilinna");
  2543. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  2544. MODULE_LICENSE("GPL");