rndis_wlan.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764
  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/ieee80211.h>
  38. #include <linux/if_arp.h>
  39. #include <linux/ctype.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/slab.h>
  42. #include <net/cfg80211.h>
  43. #include <linux/usb/usbnet.h>
  44. #include <linux/usb/rndis_host.h>
  45. /* NOTE: All these are settings for Broadcom chipset */
  46. static char modparam_country[4] = "EU";
  47. module_param_string(country, modparam_country, 4, 0444);
  48. MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
  49. static int modparam_frameburst = 1;
  50. module_param_named(frameburst, modparam_frameburst, int, 0444);
  51. MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
  52. static int modparam_afterburner = 0;
  53. module_param_named(afterburner, modparam_afterburner, int, 0444);
  54. MODULE_PARM_DESC(afterburner,
  55. "enable afterburner aka '125 High Speed Mode' (default: off)");
  56. static int modparam_power_save = 0;
  57. module_param_named(power_save, modparam_power_save, int, 0444);
  58. MODULE_PARM_DESC(power_save,
  59. "set power save mode: 0=off, 1=on, 2=fast (default: off)");
  60. static int modparam_power_output = 3;
  61. module_param_named(power_output, modparam_power_output, int, 0444);
  62. MODULE_PARM_DESC(power_output,
  63. "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
  64. static int modparam_roamtrigger = -70;
  65. module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
  66. MODULE_PARM_DESC(roamtrigger,
  67. "set roaming dBm trigger: -80=optimize for distance, "
  68. "-60=bandwidth (default: -70)");
  69. static int modparam_roamdelta = 1;
  70. module_param_named(roamdelta, modparam_roamdelta, int, 0444);
  71. MODULE_PARM_DESC(roamdelta,
  72. "set roaming tendency: 0=aggressive, 1=moderate, "
  73. "2=conservative (default: moderate)");
  74. static int modparam_workaround_interval;
  75. module_param_named(workaround_interval, modparam_workaround_interval,
  76. int, 0444);
  77. MODULE_PARM_DESC(workaround_interval,
  78. "set stall workaround interval in msecs (0=disabled) (default: 0)");
  79. /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
  80. #define WL_NOISE -96 /* typical noise level in dBm */
  81. #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
  82. /* Assume that Broadcom 4320 (only chipset at time of writing known to be
  83. * based on wireless rndis) has default txpower of 13dBm.
  84. * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
  85. * 100% : 20 mW ~ 13dBm
  86. * 75% : 15 mW ~ 12dBm
  87. * 50% : 10 mW ~ 10dBm
  88. * 25% : 5 mW ~ 7dBm
  89. */
  90. #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
  91. #define BCM4320_DEFAULT_TXPOWER_DBM_75 12
  92. #define BCM4320_DEFAULT_TXPOWER_DBM_50 10
  93. #define BCM4320_DEFAULT_TXPOWER_DBM_25 7
  94. /* Known device types */
  95. #define RNDIS_UNKNOWN 0
  96. #define RNDIS_BCM4320A 1
  97. #define RNDIS_BCM4320B 2
  98. /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
  99. * slightly modified for datatype endianess, etc
  100. */
  101. #define NDIS_802_11_LENGTH_SSID 32
  102. #define NDIS_802_11_LENGTH_RATES 8
  103. #define NDIS_802_11_LENGTH_RATES_EX 16
  104. enum ndis_80211_net_type {
  105. NDIS_80211_TYPE_FREQ_HOP,
  106. NDIS_80211_TYPE_DIRECT_SEQ,
  107. NDIS_80211_TYPE_OFDM_A,
  108. NDIS_80211_TYPE_OFDM_G
  109. };
  110. enum ndis_80211_net_infra {
  111. NDIS_80211_INFRA_ADHOC,
  112. NDIS_80211_INFRA_INFRA,
  113. NDIS_80211_INFRA_AUTO_UNKNOWN
  114. };
  115. enum ndis_80211_auth_mode {
  116. NDIS_80211_AUTH_OPEN,
  117. NDIS_80211_AUTH_SHARED,
  118. NDIS_80211_AUTH_AUTO_SWITCH,
  119. NDIS_80211_AUTH_WPA,
  120. NDIS_80211_AUTH_WPA_PSK,
  121. NDIS_80211_AUTH_WPA_NONE,
  122. NDIS_80211_AUTH_WPA2,
  123. NDIS_80211_AUTH_WPA2_PSK
  124. };
  125. enum ndis_80211_encr_status {
  126. NDIS_80211_ENCR_WEP_ENABLED,
  127. NDIS_80211_ENCR_DISABLED,
  128. NDIS_80211_ENCR_WEP_KEY_ABSENT,
  129. NDIS_80211_ENCR_NOT_SUPPORTED,
  130. NDIS_80211_ENCR_TKIP_ENABLED,
  131. NDIS_80211_ENCR_TKIP_KEY_ABSENT,
  132. NDIS_80211_ENCR_CCMP_ENABLED,
  133. NDIS_80211_ENCR_CCMP_KEY_ABSENT
  134. };
  135. enum ndis_80211_priv_filter {
  136. NDIS_80211_PRIV_ACCEPT_ALL,
  137. NDIS_80211_PRIV_8021X_WEP
  138. };
  139. enum ndis_80211_status_type {
  140. NDIS_80211_STATUSTYPE_AUTHENTICATION,
  141. NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
  142. NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
  143. NDIS_80211_STATUSTYPE_RADIOSTATE,
  144. };
  145. enum ndis_80211_media_stream_mode {
  146. NDIS_80211_MEDIA_STREAM_OFF,
  147. NDIS_80211_MEDIA_STREAM_ON
  148. };
  149. enum ndis_80211_radio_status {
  150. NDIS_80211_RADIO_STATUS_ON,
  151. NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
  152. NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
  153. };
  154. enum ndis_80211_addkey_bits {
  155. NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
  156. NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
  157. NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
  158. NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  159. };
  160. enum ndis_80211_addwep_bits {
  161. NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
  162. NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  163. };
  164. enum ndis_80211_power_mode {
  165. NDIS_80211_POWER_MODE_CAM,
  166. NDIS_80211_POWER_MODE_MAX_PSP,
  167. NDIS_80211_POWER_MODE_FAST_PSP,
  168. };
  169. enum ndis_80211_pmkid_cand_list_flag_bits {
  170. NDIS_80211_PMKID_CAND_PREAUTH = cpu_to_le32(1 << 0)
  171. };
  172. struct ndis_80211_auth_request {
  173. __le32 length;
  174. u8 bssid[6];
  175. u8 padding[2];
  176. __le32 flags;
  177. } __packed;
  178. struct ndis_80211_pmkid_candidate {
  179. u8 bssid[6];
  180. u8 padding[2];
  181. __le32 flags;
  182. } __packed;
  183. struct ndis_80211_pmkid_cand_list {
  184. __le32 version;
  185. __le32 num_candidates;
  186. struct ndis_80211_pmkid_candidate candidate_list[0];
  187. } __packed;
  188. struct ndis_80211_status_indication {
  189. __le32 status_type;
  190. union {
  191. __le32 media_stream_mode;
  192. __le32 radio_status;
  193. struct ndis_80211_auth_request auth_request[0];
  194. struct ndis_80211_pmkid_cand_list cand_list;
  195. } u;
  196. } __packed;
  197. struct ndis_80211_ssid {
  198. __le32 length;
  199. u8 essid[NDIS_802_11_LENGTH_SSID];
  200. } __packed;
  201. struct ndis_80211_conf_freq_hop {
  202. __le32 length;
  203. __le32 hop_pattern;
  204. __le32 hop_set;
  205. __le32 dwell_time;
  206. } __packed;
  207. struct ndis_80211_conf {
  208. __le32 length;
  209. __le32 beacon_period;
  210. __le32 atim_window;
  211. __le32 ds_config;
  212. struct ndis_80211_conf_freq_hop fh_config;
  213. } __packed;
  214. struct ndis_80211_bssid_ex {
  215. __le32 length;
  216. u8 mac[6];
  217. u8 padding[2];
  218. struct ndis_80211_ssid ssid;
  219. __le32 privacy;
  220. __le32 rssi;
  221. __le32 net_type;
  222. struct ndis_80211_conf config;
  223. __le32 net_infra;
  224. u8 rates[NDIS_802_11_LENGTH_RATES_EX];
  225. __le32 ie_length;
  226. u8 ies[0];
  227. } __packed;
  228. struct ndis_80211_bssid_list_ex {
  229. __le32 num_items;
  230. struct ndis_80211_bssid_ex bssid[0];
  231. } __packed;
  232. struct ndis_80211_fixed_ies {
  233. u8 timestamp[8];
  234. __le16 beacon_interval;
  235. __le16 capabilities;
  236. } __packed;
  237. struct ndis_80211_wep_key {
  238. __le32 size;
  239. __le32 index;
  240. __le32 length;
  241. u8 material[32];
  242. } __packed;
  243. struct ndis_80211_key {
  244. __le32 size;
  245. __le32 index;
  246. __le32 length;
  247. u8 bssid[6];
  248. u8 padding[6];
  249. u8 rsc[8];
  250. u8 material[32];
  251. } __packed;
  252. struct ndis_80211_remove_key {
  253. __le32 size;
  254. __le32 index;
  255. u8 bssid[6];
  256. u8 padding[2];
  257. } __packed;
  258. struct ndis_config_param {
  259. __le32 name_offs;
  260. __le32 name_length;
  261. __le32 type;
  262. __le32 value_offs;
  263. __le32 value_length;
  264. } __packed;
  265. struct ndis_80211_assoc_info {
  266. __le32 length;
  267. __le16 req_ies;
  268. struct req_ie {
  269. __le16 capa;
  270. __le16 listen_interval;
  271. u8 cur_ap_address[6];
  272. } req_ie;
  273. __le32 req_ie_length;
  274. __le32 offset_req_ies;
  275. __le16 resp_ies;
  276. struct resp_ie {
  277. __le16 capa;
  278. __le16 status_code;
  279. __le16 assoc_id;
  280. } resp_ie;
  281. __le32 resp_ie_length;
  282. __le32 offset_resp_ies;
  283. } __packed;
  284. struct ndis_80211_auth_encr_pair {
  285. __le32 auth_mode;
  286. __le32 encr_mode;
  287. } __packed;
  288. struct ndis_80211_capability {
  289. __le32 length;
  290. __le32 version;
  291. __le32 num_pmkids;
  292. __le32 num_auth_encr_pair;
  293. struct ndis_80211_auth_encr_pair auth_encr_pair[0];
  294. } __packed;
  295. struct ndis_80211_bssid_info {
  296. u8 bssid[6];
  297. u8 pmkid[16];
  298. } __packed;
  299. struct ndis_80211_pmkid {
  300. __le32 length;
  301. __le32 bssid_info_count;
  302. struct ndis_80211_bssid_info bssid_info[0];
  303. } __packed;
  304. /*
  305. * private data
  306. */
  307. #define CAP_MODE_80211A 1
  308. #define CAP_MODE_80211B 2
  309. #define CAP_MODE_80211G 4
  310. #define CAP_MODE_MASK 7
  311. #define WORK_LINK_UP (1<<0)
  312. #define WORK_LINK_DOWN (1<<1)
  313. #define WORK_SET_MULTICAST_LIST (1<<2)
  314. #define RNDIS_WLAN_ALG_NONE 0
  315. #define RNDIS_WLAN_ALG_WEP (1<<0)
  316. #define RNDIS_WLAN_ALG_TKIP (1<<1)
  317. #define RNDIS_WLAN_ALG_CCMP (1<<2)
  318. #define RNDIS_WLAN_NUM_KEYS 4
  319. #define RNDIS_WLAN_KEY_MGMT_NONE 0
  320. #define RNDIS_WLAN_KEY_MGMT_802_1X (1<<0)
  321. #define RNDIS_WLAN_KEY_MGMT_PSK (1<<1)
  322. #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
  323. static const struct ieee80211_channel rndis_channels[] = {
  324. { .center_freq = 2412 },
  325. { .center_freq = 2417 },
  326. { .center_freq = 2422 },
  327. { .center_freq = 2427 },
  328. { .center_freq = 2432 },
  329. { .center_freq = 2437 },
  330. { .center_freq = 2442 },
  331. { .center_freq = 2447 },
  332. { .center_freq = 2452 },
  333. { .center_freq = 2457 },
  334. { .center_freq = 2462 },
  335. { .center_freq = 2467 },
  336. { .center_freq = 2472 },
  337. { .center_freq = 2484 },
  338. };
  339. static const struct ieee80211_rate rndis_rates[] = {
  340. { .bitrate = 10 },
  341. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  342. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  343. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  344. { .bitrate = 60 },
  345. { .bitrate = 90 },
  346. { .bitrate = 120 },
  347. { .bitrate = 180 },
  348. { .bitrate = 240 },
  349. { .bitrate = 360 },
  350. { .bitrate = 480 },
  351. { .bitrate = 540 }
  352. };
  353. static const u32 rndis_cipher_suites[] = {
  354. WLAN_CIPHER_SUITE_WEP40,
  355. WLAN_CIPHER_SUITE_WEP104,
  356. WLAN_CIPHER_SUITE_TKIP,
  357. WLAN_CIPHER_SUITE_CCMP,
  358. };
  359. struct rndis_wlan_encr_key {
  360. int len;
  361. u32 cipher;
  362. u8 material[32];
  363. u8 bssid[ETH_ALEN];
  364. bool pairwise;
  365. bool tx_key;
  366. };
  367. /* RNDIS device private data */
  368. struct rndis_wlan_private {
  369. struct usbnet *usbdev;
  370. struct wireless_dev wdev;
  371. struct cfg80211_scan_request *scan_request;
  372. struct workqueue_struct *workqueue;
  373. struct delayed_work dev_poller_work;
  374. struct delayed_work scan_work;
  375. struct work_struct work;
  376. struct mutex command_lock;
  377. unsigned long work_pending;
  378. int last_qual;
  379. s32 cqm_rssi_thold;
  380. u32 cqm_rssi_hyst;
  381. int last_cqm_event_rssi;
  382. struct ieee80211_supported_band band;
  383. struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
  384. struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
  385. u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
  386. int device_type;
  387. int caps;
  388. int multicast_size;
  389. /* module parameters */
  390. char param_country[4];
  391. int param_frameburst;
  392. int param_afterburner;
  393. int param_power_save;
  394. int param_power_output;
  395. int param_roamtrigger;
  396. int param_roamdelta;
  397. u32 param_workaround_interval;
  398. /* hardware state */
  399. bool radio_on;
  400. int power_mode;
  401. int infra_mode;
  402. bool connected;
  403. u8 bssid[ETH_ALEN];
  404. u32 current_command_oid;
  405. /* encryption stuff */
  406. u8 encr_tx_key_index;
  407. struct rndis_wlan_encr_key encr_keys[RNDIS_WLAN_NUM_KEYS];
  408. int wpa_version;
  409. u8 command_buffer[COMMAND_BUFFER_SIZE];
  410. };
  411. /*
  412. * cfg80211 ops
  413. */
  414. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  415. struct net_device *dev,
  416. enum nl80211_iftype type, u32 *flags,
  417. struct vif_params *params);
  418. static int rndis_scan(struct wiphy *wiphy,
  419. struct cfg80211_scan_request *request);
  420. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
  421. static int rndis_set_tx_power(struct wiphy *wiphy,
  422. enum nl80211_tx_power_setting type,
  423. int mbm);
  424. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
  425. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  426. struct cfg80211_connect_params *sme);
  427. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  428. u16 reason_code);
  429. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  430. struct cfg80211_ibss_params *params);
  431. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
  432. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  433. u8 key_index, bool pairwise, const u8 *mac_addr,
  434. struct key_params *params);
  435. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  436. u8 key_index, bool pairwise, const u8 *mac_addr);
  437. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  438. u8 key_index, bool unicast, bool multicast);
  439. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  440. u8 *mac, struct station_info *sinfo);
  441. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  442. int idx, u8 *mac, struct station_info *sinfo);
  443. static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  444. struct cfg80211_pmksa *pmksa);
  445. static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  446. struct cfg80211_pmksa *pmksa);
  447. static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
  448. static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
  449. bool enabled, int timeout);
  450. static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
  451. struct net_device *dev,
  452. s32 rssi_thold, u32 rssi_hyst);
  453. static const 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. .add_key = rndis_add_key,
  464. .del_key = rndis_del_key,
  465. .set_default_key = rndis_set_default_key,
  466. .get_station = rndis_get_station,
  467. .dump_station = rndis_dump_station,
  468. .set_pmksa = rndis_set_pmksa,
  469. .del_pmksa = rndis_del_pmksa,
  470. .flush_pmksa = rndis_flush_pmksa,
  471. .set_power_mgmt = rndis_set_power_mgmt,
  472. .set_cqm_rssi_config = rndis_set_cqm_rssi_config,
  473. };
  474. static void *rndis_wiphy_privid = &rndis_wiphy_privid;
  475. static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
  476. {
  477. return (struct rndis_wlan_private *)dev->driver_priv;
  478. }
  479. static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
  480. {
  481. switch (priv->param_power_output) {
  482. default:
  483. case 3:
  484. return BCM4320_DEFAULT_TXPOWER_DBM_100;
  485. case 2:
  486. return BCM4320_DEFAULT_TXPOWER_DBM_75;
  487. case 1:
  488. return BCM4320_DEFAULT_TXPOWER_DBM_50;
  489. case 0:
  490. return BCM4320_DEFAULT_TXPOWER_DBM_25;
  491. }
  492. }
  493. static bool is_wpa_key(struct rndis_wlan_private *priv, u8 idx)
  494. {
  495. int cipher = priv->encr_keys[idx].cipher;
  496. return (cipher == WLAN_CIPHER_SUITE_CCMP ||
  497. cipher == WLAN_CIPHER_SUITE_TKIP);
  498. }
  499. static int rndis_cipher_to_alg(u32 cipher)
  500. {
  501. switch (cipher) {
  502. default:
  503. return RNDIS_WLAN_ALG_NONE;
  504. case WLAN_CIPHER_SUITE_WEP40:
  505. case WLAN_CIPHER_SUITE_WEP104:
  506. return RNDIS_WLAN_ALG_WEP;
  507. case WLAN_CIPHER_SUITE_TKIP:
  508. return RNDIS_WLAN_ALG_TKIP;
  509. case WLAN_CIPHER_SUITE_CCMP:
  510. return RNDIS_WLAN_ALG_CCMP;
  511. }
  512. }
  513. static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
  514. {
  515. switch (akm_suite) {
  516. default:
  517. return RNDIS_WLAN_KEY_MGMT_NONE;
  518. case WLAN_AKM_SUITE_8021X:
  519. return RNDIS_WLAN_KEY_MGMT_802_1X;
  520. case WLAN_AKM_SUITE_PSK:
  521. return RNDIS_WLAN_KEY_MGMT_PSK;
  522. }
  523. }
  524. #ifdef DEBUG
  525. static const char *oid_to_string(u32 oid)
  526. {
  527. switch (oid) {
  528. #define OID_STR(oid) case oid: return(#oid)
  529. /* from rndis_host.h */
  530. OID_STR(RNDIS_OID_802_3_PERMANENT_ADDRESS);
  531. OID_STR(RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE);
  532. OID_STR(RNDIS_OID_GEN_CURRENT_PACKET_FILTER);
  533. OID_STR(RNDIS_OID_GEN_PHYSICAL_MEDIUM);
  534. /* from rndis_wlan.c */
  535. OID_STR(RNDIS_OID_GEN_LINK_SPEED);
  536. OID_STR(RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER);
  537. OID_STR(RNDIS_OID_GEN_XMIT_OK);
  538. OID_STR(RNDIS_OID_GEN_RCV_OK);
  539. OID_STR(RNDIS_OID_GEN_XMIT_ERROR);
  540. OID_STR(RNDIS_OID_GEN_RCV_ERROR);
  541. OID_STR(RNDIS_OID_GEN_RCV_NO_BUFFER);
  542. OID_STR(RNDIS_OID_802_3_CURRENT_ADDRESS);
  543. OID_STR(RNDIS_OID_802_3_MULTICAST_LIST);
  544. OID_STR(RNDIS_OID_802_3_MAXIMUM_LIST_SIZE);
  545. OID_STR(RNDIS_OID_802_11_BSSID);
  546. OID_STR(RNDIS_OID_802_11_SSID);
  547. OID_STR(RNDIS_OID_802_11_INFRASTRUCTURE_MODE);
  548. OID_STR(RNDIS_OID_802_11_ADD_WEP);
  549. OID_STR(RNDIS_OID_802_11_REMOVE_WEP);
  550. OID_STR(RNDIS_OID_802_11_DISASSOCIATE);
  551. OID_STR(RNDIS_OID_802_11_AUTHENTICATION_MODE);
  552. OID_STR(RNDIS_OID_802_11_PRIVACY_FILTER);
  553. OID_STR(RNDIS_OID_802_11_BSSID_LIST_SCAN);
  554. OID_STR(RNDIS_OID_802_11_ENCRYPTION_STATUS);
  555. OID_STR(RNDIS_OID_802_11_ADD_KEY);
  556. OID_STR(RNDIS_OID_802_11_REMOVE_KEY);
  557. OID_STR(RNDIS_OID_802_11_ASSOCIATION_INFORMATION);
  558. OID_STR(RNDIS_OID_802_11_CAPABILITY);
  559. OID_STR(RNDIS_OID_802_11_PMKID);
  560. OID_STR(RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED);
  561. OID_STR(RNDIS_OID_802_11_NETWORK_TYPE_IN_USE);
  562. OID_STR(RNDIS_OID_802_11_TX_POWER_LEVEL);
  563. OID_STR(RNDIS_OID_802_11_RSSI);
  564. OID_STR(RNDIS_OID_802_11_RSSI_TRIGGER);
  565. OID_STR(RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD);
  566. OID_STR(RNDIS_OID_802_11_RTS_THRESHOLD);
  567. OID_STR(RNDIS_OID_802_11_SUPPORTED_RATES);
  568. OID_STR(RNDIS_OID_802_11_CONFIGURATION);
  569. OID_STR(RNDIS_OID_802_11_POWER_MODE);
  570. OID_STR(RNDIS_OID_802_11_BSSID_LIST);
  571. #undef OID_STR
  572. }
  573. return "?";
  574. }
  575. #else
  576. static const char *oid_to_string(u32 oid)
  577. {
  578. return "?";
  579. }
  580. #endif
  581. /* translate error code */
  582. static int rndis_error_status(__le32 rndis_status)
  583. {
  584. int ret = -EINVAL;
  585. switch (le32_to_cpu(rndis_status)) {
  586. case RNDIS_STATUS_SUCCESS:
  587. ret = 0;
  588. break;
  589. case RNDIS_STATUS_FAILURE:
  590. case RNDIS_STATUS_INVALID_DATA:
  591. ret = -EINVAL;
  592. break;
  593. case RNDIS_STATUS_NOT_SUPPORTED:
  594. ret = -EOPNOTSUPP;
  595. break;
  596. case RNDIS_STATUS_ADAPTER_NOT_READY:
  597. case RNDIS_STATUS_ADAPTER_NOT_OPEN:
  598. ret = -EBUSY;
  599. break;
  600. }
  601. return ret;
  602. }
  603. static int rndis_query_oid(struct usbnet *dev, u32 oid, void *data, int *len)
  604. {
  605. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  606. union {
  607. void *buf;
  608. struct rndis_msg_hdr *header;
  609. struct rndis_query *get;
  610. struct rndis_query_c *get_c;
  611. } u;
  612. int ret, buflen;
  613. int resplen, respoffs, copylen;
  614. buflen = *len + sizeof(*u.get);
  615. if (buflen < CONTROL_BUFFER_SIZE)
  616. buflen = CONTROL_BUFFER_SIZE;
  617. if (buflen > COMMAND_BUFFER_SIZE) {
  618. u.buf = kmalloc(buflen, GFP_KERNEL);
  619. if (!u.buf)
  620. return -ENOMEM;
  621. } else {
  622. u.buf = priv->command_buffer;
  623. }
  624. mutex_lock(&priv->command_lock);
  625. memset(u.get, 0, sizeof *u.get);
  626. u.get->msg_type = cpu_to_le32(RNDIS_MSG_QUERY);
  627. u.get->msg_len = cpu_to_le32(sizeof *u.get);
  628. u.get->oid = cpu_to_le32(oid);
  629. priv->current_command_oid = oid;
  630. ret = rndis_command(dev, u.header, buflen);
  631. priv->current_command_oid = 0;
  632. if (ret < 0)
  633. netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
  634. __func__, oid_to_string(oid), ret,
  635. le32_to_cpu(u.get_c->status));
  636. if (ret == 0) {
  637. resplen = le32_to_cpu(u.get_c->len);
  638. respoffs = le32_to_cpu(u.get_c->offset) + 8;
  639. if (respoffs > buflen) {
  640. /* Device returned data offset outside buffer, error. */
  641. netdev_dbg(dev->net, "%s(%s): received invalid "
  642. "data offset: %d > %d\n", __func__,
  643. oid_to_string(oid), respoffs, buflen);
  644. ret = -EINVAL;
  645. goto exit_unlock;
  646. }
  647. if ((resplen + respoffs) > buflen) {
  648. /* Device would have returned more data if buffer would
  649. * have been big enough. Copy just the bits that we got.
  650. */
  651. copylen = buflen - respoffs;
  652. } else {
  653. copylen = resplen;
  654. }
  655. if (copylen > *len)
  656. copylen = *len;
  657. memcpy(data, u.buf + respoffs, copylen);
  658. *len = resplen;
  659. ret = rndis_error_status(u.get_c->status);
  660. if (ret < 0)
  661. netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
  662. __func__, oid_to_string(oid),
  663. le32_to_cpu(u.get_c->status), ret);
  664. }
  665. exit_unlock:
  666. mutex_unlock(&priv->command_lock);
  667. if (u.buf != priv->command_buffer)
  668. kfree(u.buf);
  669. return ret;
  670. }
  671. static int rndis_set_oid(struct usbnet *dev, u32 oid, const void *data,
  672. int len)
  673. {
  674. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  675. union {
  676. void *buf;
  677. struct rndis_msg_hdr *header;
  678. struct rndis_set *set;
  679. struct rndis_set_c *set_c;
  680. } u;
  681. int ret, buflen;
  682. buflen = len + sizeof(*u.set);
  683. if (buflen < CONTROL_BUFFER_SIZE)
  684. buflen = CONTROL_BUFFER_SIZE;
  685. if (buflen > COMMAND_BUFFER_SIZE) {
  686. u.buf = kmalloc(buflen, GFP_KERNEL);
  687. if (!u.buf)
  688. return -ENOMEM;
  689. } else {
  690. u.buf = priv->command_buffer;
  691. }
  692. mutex_lock(&priv->command_lock);
  693. memset(u.set, 0, sizeof *u.set);
  694. u.set->msg_type = cpu_to_le32(RNDIS_MSG_SET);
  695. u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
  696. u.set->oid = cpu_to_le32(oid);
  697. u.set->len = cpu_to_le32(len);
  698. u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
  699. u.set->handle = cpu_to_le32(0);
  700. memcpy(u.buf + sizeof(*u.set), data, len);
  701. priv->current_command_oid = oid;
  702. ret = rndis_command(dev, u.header, buflen);
  703. priv->current_command_oid = 0;
  704. if (ret < 0)
  705. netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
  706. __func__, oid_to_string(oid), ret,
  707. le32_to_cpu(u.set_c->status));
  708. if (ret == 0) {
  709. ret = rndis_error_status(u.set_c->status);
  710. if (ret < 0)
  711. netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
  712. __func__, oid_to_string(oid),
  713. le32_to_cpu(u.set_c->status), ret);
  714. }
  715. mutex_unlock(&priv->command_lock);
  716. if (u.buf != priv->command_buffer)
  717. kfree(u.buf);
  718. return ret;
  719. }
  720. static int rndis_reset(struct usbnet *usbdev)
  721. {
  722. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  723. struct rndis_reset *reset;
  724. int ret;
  725. mutex_lock(&priv->command_lock);
  726. reset = (void *)priv->command_buffer;
  727. memset(reset, 0, sizeof(*reset));
  728. reset->msg_type = cpu_to_le32(RNDIS_MSG_RESET);
  729. reset->msg_len = cpu_to_le32(sizeof(*reset));
  730. priv->current_command_oid = 0;
  731. ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
  732. mutex_unlock(&priv->command_lock);
  733. if (ret < 0)
  734. return ret;
  735. return 0;
  736. }
  737. /*
  738. * Specs say that we can only set config parameters only soon after device
  739. * initialization.
  740. * value_type: 0 = u32, 2 = unicode string
  741. */
  742. static int rndis_set_config_parameter(struct usbnet *dev, char *param,
  743. int value_type, void *value)
  744. {
  745. struct ndis_config_param *infobuf;
  746. int value_len, info_len, param_len, ret, i;
  747. __le16 *unibuf;
  748. __le32 *dst_value;
  749. if (value_type == 0)
  750. value_len = sizeof(__le32);
  751. else if (value_type == 2)
  752. value_len = strlen(value) * sizeof(__le16);
  753. else
  754. return -EINVAL;
  755. param_len = strlen(param) * sizeof(__le16);
  756. info_len = sizeof(*infobuf) + param_len + value_len;
  757. #ifdef DEBUG
  758. info_len += 12;
  759. #endif
  760. infobuf = kmalloc(info_len, GFP_KERNEL);
  761. if (!infobuf)
  762. return -ENOMEM;
  763. #ifdef DEBUG
  764. info_len -= 12;
  765. /* extra 12 bytes are for padding (debug output) */
  766. memset(infobuf, 0xCC, info_len + 12);
  767. #endif
  768. if (value_type == 2)
  769. netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
  770. param, (u8 *)value);
  771. else
  772. netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
  773. param, *(u32 *)value);
  774. infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
  775. infobuf->name_length = cpu_to_le32(param_len);
  776. infobuf->type = cpu_to_le32(value_type);
  777. infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
  778. infobuf->value_length = cpu_to_le32(value_len);
  779. /* simple string to unicode string conversion */
  780. unibuf = (void *)infobuf + sizeof(*infobuf);
  781. for (i = 0; i < param_len / sizeof(__le16); i++)
  782. unibuf[i] = cpu_to_le16(param[i]);
  783. if (value_type == 2) {
  784. unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
  785. for (i = 0; i < value_len / sizeof(__le16); i++)
  786. unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
  787. } else {
  788. dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
  789. *dst_value = cpu_to_le32(*(u32 *)value);
  790. }
  791. #ifdef DEBUG
  792. netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
  793. for (i = 0; i < info_len; i += 12) {
  794. u32 *tmp = (u32 *)((u8 *)infobuf + i);
  795. netdev_dbg(dev->net, "%08X:%08X:%08X\n",
  796. cpu_to_be32(tmp[0]),
  797. cpu_to_be32(tmp[1]),
  798. cpu_to_be32(tmp[2]));
  799. }
  800. #endif
  801. ret = rndis_set_oid(dev, RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER,
  802. infobuf, info_len);
  803. if (ret != 0)
  804. netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
  805. ret);
  806. kfree(infobuf);
  807. return ret;
  808. }
  809. static int rndis_set_config_parameter_str(struct usbnet *dev,
  810. char *param, char *value)
  811. {
  812. return rndis_set_config_parameter(dev, param, 2, value);
  813. }
  814. /*
  815. * data conversion functions
  816. */
  817. static int level_to_qual(int level)
  818. {
  819. int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
  820. return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
  821. }
  822. /*
  823. * common functions
  824. */
  825. static int set_infra_mode(struct usbnet *usbdev, int mode);
  826. static void restore_keys(struct usbnet *usbdev);
  827. static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
  828. bool *matched);
  829. static int rndis_start_bssid_list_scan(struct usbnet *usbdev)
  830. {
  831. __le32 tmp;
  832. /* Note: RNDIS_OID_802_11_BSSID_LIST_SCAN clears internal BSS list. */
  833. tmp = cpu_to_le32(1);
  834. return rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST_SCAN, &tmp,
  835. sizeof(tmp));
  836. }
  837. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  838. {
  839. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  840. int ret;
  841. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_SSID,
  842. ssid, sizeof(*ssid));
  843. if (ret < 0) {
  844. netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
  845. return ret;
  846. }
  847. if (ret == 0) {
  848. priv->radio_on = true;
  849. netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
  850. }
  851. return ret;
  852. }
  853. static int set_bssid(struct usbnet *usbdev, const u8 *bssid)
  854. {
  855. int ret;
  856. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID,
  857. bssid, ETH_ALEN);
  858. if (ret < 0) {
  859. netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
  860. bssid, ret);
  861. return ret;
  862. }
  863. return ret;
  864. }
  865. static int clear_bssid(struct usbnet *usbdev)
  866. {
  867. static const u8 broadcast_mac[ETH_ALEN] = {
  868. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  869. };
  870. return set_bssid(usbdev, broadcast_mac);
  871. }
  872. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  873. {
  874. int ret, len;
  875. len = ETH_ALEN;
  876. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID,
  877. bssid, &len);
  878. if (ret != 0)
  879. memset(bssid, 0, ETH_ALEN);
  880. return ret;
  881. }
  882. static int get_association_info(struct usbnet *usbdev,
  883. struct ndis_80211_assoc_info *info, int len)
  884. {
  885. return rndis_query_oid(usbdev,
  886. RNDIS_OID_802_11_ASSOCIATION_INFORMATION,
  887. info, &len);
  888. }
  889. static bool is_associated(struct usbnet *usbdev)
  890. {
  891. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  892. u8 bssid[ETH_ALEN];
  893. int ret;
  894. if (!priv->radio_on)
  895. return false;
  896. ret = get_bssid(usbdev, bssid);
  897. return (ret == 0 && !is_zero_ether_addr(bssid));
  898. }
  899. static int disassociate(struct usbnet *usbdev, bool reset_ssid)
  900. {
  901. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  902. struct ndis_80211_ssid ssid;
  903. int i, ret = 0;
  904. if (priv->radio_on) {
  905. ret = rndis_set_oid(usbdev,
  906. RNDIS_OID_802_11_DISASSOCIATE,
  907. NULL, 0);
  908. if (ret == 0) {
  909. priv->radio_on = false;
  910. netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
  911. __func__);
  912. if (reset_ssid)
  913. msleep(100);
  914. }
  915. }
  916. /* disassociate causes radio to be turned off; if reset_ssid
  917. * is given, set random ssid to enable radio */
  918. if (reset_ssid) {
  919. /* Set device to infrastructure mode so we don't get ad-hoc
  920. * 'media connect' indications with the random ssid.
  921. */
  922. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  923. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  924. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  925. ssid.essid[0] = 0x1;
  926. ssid.essid[1] = 0xff;
  927. for (i = 2; i < sizeof(ssid.essid); i++)
  928. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  929. ret = set_essid(usbdev, &ssid);
  930. }
  931. return ret;
  932. }
  933. static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
  934. enum nl80211_auth_type auth_type, int keymgmt)
  935. {
  936. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  937. __le32 tmp;
  938. int auth_mode, ret;
  939. netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
  940. __func__, wpa_version, auth_type, keymgmt);
  941. if (wpa_version & NL80211_WPA_VERSION_2) {
  942. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  943. auth_mode = NDIS_80211_AUTH_WPA2;
  944. else
  945. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  946. } else if (wpa_version & NL80211_WPA_VERSION_1) {
  947. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  948. auth_mode = NDIS_80211_AUTH_WPA;
  949. else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
  950. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  951. else
  952. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  953. } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  954. auth_mode = NDIS_80211_AUTH_SHARED;
  955. else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
  956. auth_mode = NDIS_80211_AUTH_OPEN;
  957. else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
  958. auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
  959. else
  960. return -ENOTSUPP;
  961. tmp = cpu_to_le32(auth_mode);
  962. ret = rndis_set_oid(usbdev,
  963. RNDIS_OID_802_11_AUTHENTICATION_MODE,
  964. &tmp, sizeof(tmp));
  965. if (ret != 0) {
  966. netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
  967. ret);
  968. return ret;
  969. }
  970. priv->wpa_version = wpa_version;
  971. return 0;
  972. }
  973. static int set_priv_filter(struct usbnet *usbdev)
  974. {
  975. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  976. __le32 tmp;
  977. netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
  978. __func__, priv->wpa_version);
  979. if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
  980. priv->wpa_version & NL80211_WPA_VERSION_1)
  981. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  982. else
  983. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  984. return rndis_set_oid(usbdev,
  985. RNDIS_OID_802_11_PRIVACY_FILTER, &tmp,
  986. sizeof(tmp));
  987. }
  988. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  989. {
  990. __le32 tmp;
  991. int encr_mode, ret;
  992. netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
  993. __func__, pairwise, groupwise);
  994. if (pairwise & RNDIS_WLAN_ALG_CCMP)
  995. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  996. else if (pairwise & RNDIS_WLAN_ALG_TKIP)
  997. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  998. else if (pairwise & RNDIS_WLAN_ALG_WEP)
  999. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  1000. else if (groupwise & RNDIS_WLAN_ALG_CCMP)
  1001. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  1002. else if (groupwise & RNDIS_WLAN_ALG_TKIP)
  1003. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  1004. else
  1005. encr_mode = NDIS_80211_ENCR_DISABLED;
  1006. tmp = cpu_to_le32(encr_mode);
  1007. ret = rndis_set_oid(usbdev,
  1008. RNDIS_OID_802_11_ENCRYPTION_STATUS, &tmp,
  1009. sizeof(tmp));
  1010. if (ret != 0) {
  1011. netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
  1012. ret);
  1013. return ret;
  1014. }
  1015. return 0;
  1016. }
  1017. static int set_infra_mode(struct usbnet *usbdev, int mode)
  1018. {
  1019. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1020. __le32 tmp;
  1021. int ret;
  1022. netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
  1023. __func__, priv->infra_mode);
  1024. tmp = cpu_to_le32(mode);
  1025. ret = rndis_set_oid(usbdev,
  1026. RNDIS_OID_802_11_INFRASTRUCTURE_MODE,
  1027. &tmp, sizeof(tmp));
  1028. if (ret != 0) {
  1029. netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
  1030. ret);
  1031. return ret;
  1032. }
  1033. /* NDIS drivers clear keys when infrastructure mode is
  1034. * changed. But Linux tools assume otherwise. So set the
  1035. * keys */
  1036. restore_keys(usbdev);
  1037. priv->infra_mode = mode;
  1038. return 0;
  1039. }
  1040. static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
  1041. {
  1042. __le32 tmp;
  1043. netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
  1044. if (rts_threshold < 0 || rts_threshold > 2347)
  1045. rts_threshold = 2347;
  1046. tmp = cpu_to_le32(rts_threshold);
  1047. return rndis_set_oid(usbdev,
  1048. RNDIS_OID_802_11_RTS_THRESHOLD,
  1049. &tmp, sizeof(tmp));
  1050. }
  1051. static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
  1052. {
  1053. __le32 tmp;
  1054. netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
  1055. if (frag_threshold < 256 || frag_threshold > 2346)
  1056. frag_threshold = 2346;
  1057. tmp = cpu_to_le32(frag_threshold);
  1058. return rndis_set_oid(usbdev,
  1059. RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD,
  1060. &tmp, sizeof(tmp));
  1061. }
  1062. static void set_default_iw_params(struct usbnet *usbdev)
  1063. {
  1064. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1065. set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
  1066. RNDIS_WLAN_KEY_MGMT_NONE);
  1067. set_priv_filter(usbdev);
  1068. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1069. }
  1070. static int deauthenticate(struct usbnet *usbdev)
  1071. {
  1072. int ret;
  1073. ret = disassociate(usbdev, true);
  1074. set_default_iw_params(usbdev);
  1075. return ret;
  1076. }
  1077. static int set_channel(struct usbnet *usbdev, int channel)
  1078. {
  1079. struct ndis_80211_conf config;
  1080. unsigned int dsconfig;
  1081. int len, ret;
  1082. netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
  1083. /* this OID is valid only when not associated */
  1084. if (is_associated(usbdev))
  1085. return 0;
  1086. dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
  1087. len = sizeof(config);
  1088. ret = rndis_query_oid(usbdev,
  1089. RNDIS_OID_802_11_CONFIGURATION,
  1090. &config, &len);
  1091. if (ret < 0) {
  1092. netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
  1093. __func__);
  1094. return ret;
  1095. }
  1096. config.ds_config = cpu_to_le32(dsconfig);
  1097. ret = rndis_set_oid(usbdev,
  1098. RNDIS_OID_802_11_CONFIGURATION,
  1099. &config, sizeof(config));
  1100. netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
  1101. return ret;
  1102. }
  1103. static struct ieee80211_channel *get_current_channel(struct usbnet *usbdev,
  1104. u32 *beacon_period)
  1105. {
  1106. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1107. struct ieee80211_channel *channel;
  1108. struct ndis_80211_conf config;
  1109. int len, ret;
  1110. /* Get channel and beacon interval */
  1111. len = sizeof(config);
  1112. ret = rndis_query_oid(usbdev,
  1113. RNDIS_OID_802_11_CONFIGURATION,
  1114. &config, &len);
  1115. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_CONFIGURATION -> %d\n",
  1116. __func__, ret);
  1117. if (ret < 0)
  1118. return NULL;
  1119. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1120. KHZ_TO_MHZ(le32_to_cpu(config.ds_config)));
  1121. if (!channel)
  1122. return NULL;
  1123. if (beacon_period)
  1124. *beacon_period = le32_to_cpu(config.beacon_period);
  1125. return channel;
  1126. }
  1127. /* index must be 0 - N, as per NDIS */
  1128. static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1129. u8 index)
  1130. {
  1131. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1132. struct ndis_80211_wep_key ndis_key;
  1133. u32 cipher;
  1134. int ret;
  1135. netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
  1136. __func__, index, key_len);
  1137. if (index >= RNDIS_WLAN_NUM_KEYS)
  1138. return -EINVAL;
  1139. if (key_len == 5)
  1140. cipher = WLAN_CIPHER_SUITE_WEP40;
  1141. else if (key_len == 13)
  1142. cipher = WLAN_CIPHER_SUITE_WEP104;
  1143. else
  1144. return -EINVAL;
  1145. memset(&ndis_key, 0, sizeof(ndis_key));
  1146. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1147. ndis_key.length = cpu_to_le32(key_len);
  1148. ndis_key.index = cpu_to_le32(index);
  1149. memcpy(&ndis_key.material, key, key_len);
  1150. if (index == priv->encr_tx_key_index) {
  1151. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1152. ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
  1153. RNDIS_WLAN_ALG_NONE);
  1154. if (ret)
  1155. netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
  1156. ret);
  1157. }
  1158. ret = rndis_set_oid(usbdev,
  1159. RNDIS_OID_802_11_ADD_WEP, &ndis_key,
  1160. sizeof(ndis_key));
  1161. if (ret != 0) {
  1162. netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
  1163. index + 1, ret);
  1164. return ret;
  1165. }
  1166. priv->encr_keys[index].len = key_len;
  1167. priv->encr_keys[index].cipher = cipher;
  1168. memcpy(&priv->encr_keys[index].material, key, key_len);
  1169. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1170. return 0;
  1171. }
  1172. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1173. u8 index, const u8 *addr, const u8 *rx_seq,
  1174. int seq_len, u32 cipher, __le32 flags)
  1175. {
  1176. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1177. struct ndis_80211_key ndis_key;
  1178. bool is_addr_ok;
  1179. int ret;
  1180. if (index >= RNDIS_WLAN_NUM_KEYS) {
  1181. netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
  1182. __func__, index);
  1183. return -EINVAL;
  1184. }
  1185. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1186. netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
  1187. __func__, key_len);
  1188. return -EINVAL;
  1189. }
  1190. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
  1191. if (!rx_seq || seq_len <= 0) {
  1192. netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
  1193. __func__);
  1194. return -EINVAL;
  1195. }
  1196. if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
  1197. netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
  1198. return -EINVAL;
  1199. }
  1200. }
  1201. is_addr_ok = addr && !is_zero_ether_addr(addr) &&
  1202. !is_broadcast_ether_addr(addr);
  1203. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1204. netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
  1205. __func__, addr);
  1206. return -EINVAL;
  1207. }
  1208. netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
  1209. __func__, index,
  1210. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1211. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1212. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1213. memset(&ndis_key, 0, sizeof(ndis_key));
  1214. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1215. sizeof(ndis_key.material) + key_len);
  1216. ndis_key.length = cpu_to_le32(key_len);
  1217. ndis_key.index = cpu_to_le32(index) | flags;
  1218. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1219. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1220. * different order than NDIS spec, so swap the order here. */
  1221. memcpy(ndis_key.material, key, 16);
  1222. memcpy(ndis_key.material + 16, key + 24, 8);
  1223. memcpy(ndis_key.material + 24, key + 16, 8);
  1224. } else
  1225. memcpy(ndis_key.material, key, key_len);
  1226. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1227. memcpy(ndis_key.rsc, rx_seq, seq_len);
  1228. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1229. /* pairwise key */
  1230. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1231. } else {
  1232. /* group key */
  1233. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1234. memset(ndis_key.bssid, 0xff, ETH_ALEN);
  1235. else
  1236. get_bssid(usbdev, ndis_key.bssid);
  1237. }
  1238. ret = rndis_set_oid(usbdev,
  1239. RNDIS_OID_802_11_ADD_KEY, &ndis_key,
  1240. le32_to_cpu(ndis_key.size));
  1241. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_ADD_KEY -> %08X\n",
  1242. __func__, ret);
  1243. if (ret != 0)
  1244. return ret;
  1245. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1246. priv->encr_keys[index].len = key_len;
  1247. priv->encr_keys[index].cipher = cipher;
  1248. memcpy(&priv->encr_keys[index].material, key, key_len);
  1249. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1250. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1251. else
  1252. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1253. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1254. priv->encr_tx_key_index = index;
  1255. return 0;
  1256. }
  1257. static int restore_key(struct usbnet *usbdev, u8 key_idx)
  1258. {
  1259. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1260. struct rndis_wlan_encr_key key;
  1261. if (is_wpa_key(priv, key_idx))
  1262. return 0;
  1263. key = priv->encr_keys[key_idx];
  1264. netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
  1265. if (key.len == 0)
  1266. return 0;
  1267. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1268. }
  1269. static void restore_keys(struct usbnet *usbdev)
  1270. {
  1271. int i;
  1272. for (i = 0; i < 4; i++)
  1273. restore_key(usbdev, i);
  1274. }
  1275. static void clear_key(struct rndis_wlan_private *priv, u8 idx)
  1276. {
  1277. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1278. }
  1279. /* remove_key is for both wep and wpa */
  1280. static int remove_key(struct usbnet *usbdev, u8 index, const u8 *bssid)
  1281. {
  1282. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1283. struct ndis_80211_remove_key remove_key;
  1284. __le32 keyindex;
  1285. bool is_wpa;
  1286. int ret;
  1287. if (index >= RNDIS_WLAN_NUM_KEYS)
  1288. return -ENOENT;
  1289. if (priv->encr_keys[index].len == 0)
  1290. return 0;
  1291. is_wpa = is_wpa_key(priv, index);
  1292. netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
  1293. __func__, index, is_wpa ? "wpa" : "wep",
  1294. priv->encr_keys[index].len);
  1295. clear_key(priv, index);
  1296. if (is_wpa) {
  1297. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1298. remove_key.index = cpu_to_le32(index);
  1299. if (bssid) {
  1300. /* pairwise key */
  1301. if (!is_broadcast_ether_addr(bssid))
  1302. remove_key.index |=
  1303. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1304. memcpy(remove_key.bssid, bssid,
  1305. sizeof(remove_key.bssid));
  1306. } else
  1307. memset(remove_key.bssid, 0xff,
  1308. sizeof(remove_key.bssid));
  1309. ret = rndis_set_oid(usbdev,
  1310. RNDIS_OID_802_11_REMOVE_KEY,
  1311. &remove_key, sizeof(remove_key));
  1312. if (ret != 0)
  1313. return ret;
  1314. } else {
  1315. keyindex = cpu_to_le32(index);
  1316. ret = rndis_set_oid(usbdev,
  1317. RNDIS_OID_802_11_REMOVE_WEP,
  1318. &keyindex, sizeof(keyindex));
  1319. if (ret != 0) {
  1320. netdev_warn(usbdev->net,
  1321. "removing encryption key %d failed (%08X)\n",
  1322. index, ret);
  1323. return ret;
  1324. }
  1325. }
  1326. /* if it is transmit key, disable encryption */
  1327. if (index == priv->encr_tx_key_index)
  1328. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1329. return 0;
  1330. }
  1331. static void set_multicast_list(struct usbnet *usbdev)
  1332. {
  1333. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1334. struct netdev_hw_addr *ha;
  1335. __le32 filter, basefilter;
  1336. int ret;
  1337. char *mc_addrs = NULL;
  1338. int mc_count;
  1339. basefilter = filter = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED |
  1340. RNDIS_PACKET_TYPE_BROADCAST);
  1341. if (usbdev->net->flags & IFF_PROMISC) {
  1342. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_PROMISCUOUS |
  1343. RNDIS_PACKET_TYPE_ALL_LOCAL);
  1344. } else if (usbdev->net->flags & IFF_ALLMULTI) {
  1345. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1346. }
  1347. if (filter != basefilter)
  1348. goto set_filter;
  1349. /*
  1350. * mc_list should be accessed holding the lock, so copy addresses to
  1351. * local buffer first.
  1352. */
  1353. netif_addr_lock_bh(usbdev->net);
  1354. mc_count = netdev_mc_count(usbdev->net);
  1355. if (mc_count > priv->multicast_size) {
  1356. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1357. } else if (mc_count) {
  1358. int i = 0;
  1359. mc_addrs = kmalloc(mc_count * ETH_ALEN, GFP_ATOMIC);
  1360. if (!mc_addrs) {
  1361. netdev_warn(usbdev->net,
  1362. "couldn't alloc %d bytes of memory\n",
  1363. mc_count * ETH_ALEN);
  1364. netif_addr_unlock_bh(usbdev->net);
  1365. return;
  1366. }
  1367. netdev_for_each_mc_addr(ha, usbdev->net)
  1368. memcpy(mc_addrs + i++ * ETH_ALEN,
  1369. ha->addr, ETH_ALEN);
  1370. }
  1371. netif_addr_unlock_bh(usbdev->net);
  1372. if (filter != basefilter)
  1373. goto set_filter;
  1374. if (mc_count) {
  1375. ret = rndis_set_oid(usbdev,
  1376. RNDIS_OID_802_3_MULTICAST_LIST,
  1377. mc_addrs, mc_count * ETH_ALEN);
  1378. kfree(mc_addrs);
  1379. if (ret == 0)
  1380. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_MULTICAST);
  1381. else
  1382. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1383. netdev_dbg(usbdev->net, "RNDIS_OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
  1384. mc_count, priv->multicast_size, ret);
  1385. }
  1386. set_filter:
  1387. ret = rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1388. sizeof(filter));
  1389. if (ret < 0) {
  1390. netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
  1391. le32_to_cpu(filter));
  1392. }
  1393. netdev_dbg(usbdev->net, "RNDIS_OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
  1394. le32_to_cpu(filter), ret);
  1395. }
  1396. #ifdef DEBUG
  1397. static void debug_print_pmkids(struct usbnet *usbdev,
  1398. struct ndis_80211_pmkid *pmkids,
  1399. const char *func_str)
  1400. {
  1401. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1402. int i, len, count, max_pmkids, entry_len;
  1403. max_pmkids = priv->wdev.wiphy->max_num_pmkids;
  1404. len = le32_to_cpu(pmkids->length);
  1405. count = le32_to_cpu(pmkids->bssid_info_count);
  1406. entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
  1407. netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
  1408. "%d)\n", func_str, count, len, entry_len);
  1409. if (count > max_pmkids)
  1410. count = max_pmkids;
  1411. for (i = 0; i < count; i++) {
  1412. u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
  1413. netdev_dbg(usbdev->net, "%s(): bssid: %pM, "
  1414. "pmkid: %08X:%08X:%08X:%08X\n",
  1415. func_str, pmkids->bssid_info[i].bssid,
  1416. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  1417. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  1418. }
  1419. }
  1420. #else
  1421. static void debug_print_pmkids(struct usbnet *usbdev,
  1422. struct ndis_80211_pmkid *pmkids,
  1423. const char *func_str)
  1424. {
  1425. return;
  1426. }
  1427. #endif
  1428. static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
  1429. {
  1430. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1431. struct ndis_80211_pmkid *pmkids;
  1432. int len, ret, max_pmkids;
  1433. max_pmkids = priv->wdev.wiphy->max_num_pmkids;
  1434. len = sizeof(*pmkids) + max_pmkids * sizeof(pmkids->bssid_info[0]);
  1435. pmkids = kzalloc(len, GFP_KERNEL);
  1436. if (!pmkids)
  1437. return ERR_PTR(-ENOMEM);
  1438. pmkids->length = cpu_to_le32(len);
  1439. pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
  1440. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_PMKID,
  1441. pmkids, &len);
  1442. if (ret < 0) {
  1443. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d)"
  1444. " -> %d\n", __func__, len, max_pmkids, ret);
  1445. kfree(pmkids);
  1446. return ERR_PTR(ret);
  1447. }
  1448. if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
  1449. pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
  1450. debug_print_pmkids(usbdev, pmkids, __func__);
  1451. return pmkids;
  1452. }
  1453. static int set_device_pmkids(struct usbnet *usbdev,
  1454. struct ndis_80211_pmkid *pmkids)
  1455. {
  1456. int ret, len, num_pmkids;
  1457. num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
  1458. len = sizeof(*pmkids) + num_pmkids * sizeof(pmkids->bssid_info[0]);
  1459. pmkids->length = cpu_to_le32(len);
  1460. debug_print_pmkids(usbdev, pmkids, __func__);
  1461. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID, pmkids,
  1462. le32_to_cpu(pmkids->length));
  1463. if (ret < 0) {
  1464. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d) -> %d"
  1465. "\n", __func__, len, num_pmkids, ret);
  1466. }
  1467. kfree(pmkids);
  1468. return ret;
  1469. }
  1470. static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
  1471. struct ndis_80211_pmkid *pmkids,
  1472. struct cfg80211_pmksa *pmksa,
  1473. int max_pmkids)
  1474. {
  1475. int i, newlen, err;
  1476. unsigned int count;
  1477. count = le32_to_cpu(pmkids->bssid_info_count);
  1478. if (count > max_pmkids)
  1479. count = max_pmkids;
  1480. for (i = 0; i < count; i++)
  1481. if (ether_addr_equal(pmkids->bssid_info[i].bssid,
  1482. pmksa->bssid))
  1483. break;
  1484. /* pmkid not found */
  1485. if (i == count) {
  1486. netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
  1487. __func__, pmksa->bssid);
  1488. err = -ENOENT;
  1489. goto error;
  1490. }
  1491. for (; i + 1 < count; i++)
  1492. pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
  1493. count--;
  1494. newlen = sizeof(*pmkids) + count * sizeof(pmkids->bssid_info[0]);
  1495. pmkids->length = cpu_to_le32(newlen);
  1496. pmkids->bssid_info_count = cpu_to_le32(count);
  1497. return pmkids;
  1498. error:
  1499. kfree(pmkids);
  1500. return ERR_PTR(err);
  1501. }
  1502. static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
  1503. struct ndis_80211_pmkid *pmkids,
  1504. struct cfg80211_pmksa *pmksa,
  1505. int max_pmkids)
  1506. {
  1507. int i, err, newlen;
  1508. unsigned int count;
  1509. count = le32_to_cpu(pmkids->bssid_info_count);
  1510. if (count > max_pmkids)
  1511. count = max_pmkids;
  1512. /* update with new pmkid */
  1513. for (i = 0; i < count; i++) {
  1514. if (!ether_addr_equal(pmkids->bssid_info[i].bssid,
  1515. pmksa->bssid))
  1516. continue;
  1517. memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
  1518. WLAN_PMKID_LEN);
  1519. return pmkids;
  1520. }
  1521. /* out of space, return error */
  1522. if (i == max_pmkids) {
  1523. netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
  1524. err = -ENOSPC;
  1525. goto error;
  1526. }
  1527. /* add new pmkid */
  1528. newlen = sizeof(*pmkids) + (count + 1) * sizeof(pmkids->bssid_info[0]);
  1529. pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
  1530. if (!pmkids) {
  1531. err = -ENOMEM;
  1532. goto error;
  1533. }
  1534. pmkids->length = cpu_to_le32(newlen);
  1535. pmkids->bssid_info_count = cpu_to_le32(count + 1);
  1536. memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
  1537. memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
  1538. return pmkids;
  1539. error:
  1540. kfree(pmkids);
  1541. return ERR_PTR(err);
  1542. }
  1543. /*
  1544. * cfg80211 ops
  1545. */
  1546. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1547. struct net_device *dev,
  1548. enum nl80211_iftype type, u32 *flags,
  1549. struct vif_params *params)
  1550. {
  1551. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1552. struct usbnet *usbdev = priv->usbdev;
  1553. int mode;
  1554. switch (type) {
  1555. case NL80211_IFTYPE_ADHOC:
  1556. mode = NDIS_80211_INFRA_ADHOC;
  1557. break;
  1558. case NL80211_IFTYPE_STATION:
  1559. mode = NDIS_80211_INFRA_INFRA;
  1560. break;
  1561. default:
  1562. return -EINVAL;
  1563. }
  1564. priv->wdev.iftype = type;
  1565. return set_infra_mode(usbdev, mode);
  1566. }
  1567. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1568. {
  1569. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1570. struct usbnet *usbdev = priv->usbdev;
  1571. int err;
  1572. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1573. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1574. if (err < 0)
  1575. return err;
  1576. }
  1577. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1578. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1579. if (err < 0)
  1580. return err;
  1581. }
  1582. return 0;
  1583. }
  1584. static int rndis_set_tx_power(struct wiphy *wiphy,
  1585. enum nl80211_tx_power_setting type,
  1586. int mbm)
  1587. {
  1588. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1589. struct usbnet *usbdev = priv->usbdev;
  1590. netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
  1591. __func__, type, mbm);
  1592. if (mbm < 0 || (mbm % 100))
  1593. return -ENOTSUPP;
  1594. /* Device doesn't support changing txpower after initialization, only
  1595. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1596. * currently used.
  1597. */
  1598. if (type == NL80211_TX_POWER_AUTOMATIC ||
  1599. MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
  1600. if (!priv->radio_on)
  1601. disassociate(usbdev, true); /* turn on radio */
  1602. return 0;
  1603. }
  1604. return -ENOTSUPP;
  1605. }
  1606. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
  1607. {
  1608. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1609. struct usbnet *usbdev = priv->usbdev;
  1610. *dbm = get_bcm4320_power_dbm(priv);
  1611. netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
  1612. return 0;
  1613. }
  1614. #define SCAN_DELAY_JIFFIES (6 * HZ)
  1615. static int rndis_scan(struct wiphy *wiphy,
  1616. struct cfg80211_scan_request *request)
  1617. {
  1618. struct net_device *dev = request->wdev->netdev;
  1619. struct usbnet *usbdev = netdev_priv(dev);
  1620. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1621. int ret;
  1622. int delay = SCAN_DELAY_JIFFIES;
  1623. netdev_dbg(usbdev->net, "cfg80211.scan\n");
  1624. /* Get current bssid list from device before new scan, as new scan
  1625. * clears internal bssid list.
  1626. */
  1627. rndis_check_bssid_list(usbdev, NULL, NULL);
  1628. if (!request)
  1629. return -EINVAL;
  1630. if (priv->scan_request && priv->scan_request != request)
  1631. return -EBUSY;
  1632. priv->scan_request = request;
  1633. ret = rndis_start_bssid_list_scan(usbdev);
  1634. if (ret == 0) {
  1635. if (priv->device_type == RNDIS_BCM4320A)
  1636. delay = HZ;
  1637. /* Wait before retrieving scan results from device */
  1638. queue_delayed_work(priv->workqueue, &priv->scan_work, delay);
  1639. }
  1640. return ret;
  1641. }
  1642. static bool rndis_bss_info_update(struct usbnet *usbdev,
  1643. struct ndis_80211_bssid_ex *bssid)
  1644. {
  1645. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1646. struct ieee80211_channel *channel;
  1647. struct cfg80211_bss *bss;
  1648. s32 signal;
  1649. u64 timestamp;
  1650. u16 capability;
  1651. u16 beacon_interval;
  1652. struct ndis_80211_fixed_ies *fixed;
  1653. int ie_len, bssid_len;
  1654. u8 *ie;
  1655. netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM], len: %d\n",
  1656. bssid->ssid.essid, bssid->mac, le32_to_cpu(bssid->length));
  1657. /* parse bssid structure */
  1658. bssid_len = le32_to_cpu(bssid->length);
  1659. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1660. sizeof(struct ndis_80211_fixed_ies))
  1661. return NULL;
  1662. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1663. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1664. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1665. (int)le32_to_cpu(bssid->ie_length));
  1666. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1667. if (ie_len < 0)
  1668. return NULL;
  1669. /* extract data for cfg80211_inform_bss */
  1670. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1671. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1672. if (!channel)
  1673. return NULL;
  1674. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1675. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1676. capability = le16_to_cpu(fixed->capabilities);
  1677. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1678. bss = cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1679. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1680. GFP_KERNEL);
  1681. cfg80211_put_bss(bss);
  1682. return (bss != NULL);
  1683. }
  1684. static struct ndis_80211_bssid_ex *next_bssid_list_item(
  1685. struct ndis_80211_bssid_ex *bssid,
  1686. int *bssid_len, void *buf, int len)
  1687. {
  1688. void *buf_end, *bssid_end;
  1689. buf_end = (char *)buf + len;
  1690. bssid_end = (char *)bssid + *bssid_len;
  1691. if ((int)(buf_end - bssid_end) < sizeof(bssid->length)) {
  1692. *bssid_len = 0;
  1693. return NULL;
  1694. } else {
  1695. bssid = (void *)((char *)bssid + *bssid_len);
  1696. *bssid_len = le32_to_cpu(bssid->length);
  1697. return bssid;
  1698. }
  1699. }
  1700. static bool check_bssid_list_item(struct ndis_80211_bssid_ex *bssid,
  1701. int bssid_len, void *buf, int len)
  1702. {
  1703. void *buf_end, *bssid_end;
  1704. if (!bssid || bssid_len <= 0 || bssid_len > len)
  1705. return false;
  1706. buf_end = (char *)buf + len;
  1707. bssid_end = (char *)bssid + bssid_len;
  1708. return (int)(buf_end - bssid_end) >= 0 && (int)(bssid_end - buf) >= 0;
  1709. }
  1710. static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
  1711. bool *matched)
  1712. {
  1713. void *buf = NULL;
  1714. struct ndis_80211_bssid_list_ex *bssid_list;
  1715. struct ndis_80211_bssid_ex *bssid;
  1716. int ret = -EINVAL, len, count, bssid_len, real_count, new_len;
  1717. netdev_dbg(usbdev->net, "%s()\n", __func__);
  1718. len = CONTROL_BUFFER_SIZE;
  1719. resize_buf:
  1720. buf = kzalloc(len, GFP_KERNEL);
  1721. if (!buf) {
  1722. ret = -ENOMEM;
  1723. goto out;
  1724. }
  1725. /* BSSID-list might have got bigger last time we checked, keep
  1726. * resizing until it won't get any bigger.
  1727. */
  1728. new_len = len;
  1729. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST,
  1730. buf, &new_len);
  1731. if (ret != 0 || new_len < sizeof(struct ndis_80211_bssid_list_ex))
  1732. goto out;
  1733. if (new_len > len) {
  1734. len = new_len;
  1735. kfree(buf);
  1736. goto resize_buf;
  1737. }
  1738. len = new_len;
  1739. bssid_list = buf;
  1740. count = le32_to_cpu(bssid_list->num_items);
  1741. real_count = 0;
  1742. netdev_dbg(usbdev->net, "%s(): buflen: %d\n", __func__, len);
  1743. bssid_len = 0;
  1744. bssid = next_bssid_list_item(bssid_list->bssid, &bssid_len, buf, len);
  1745. /* Device returns incorrect 'num_items'. Workaround by ignoring the
  1746. * received 'num_items' and walking through full bssid buffer instead.
  1747. */
  1748. while (check_bssid_list_item(bssid, bssid_len, buf, len)) {
  1749. if (rndis_bss_info_update(usbdev, bssid) && match_bssid &&
  1750. matched) {
  1751. if (ether_addr_equal(bssid->mac, match_bssid))
  1752. *matched = true;
  1753. }
  1754. real_count++;
  1755. bssid = next_bssid_list_item(bssid, &bssid_len, buf, len);
  1756. }
  1757. netdev_dbg(usbdev->net, "%s(): num_items from device: %d, really found:"
  1758. " %d\n", __func__, count, real_count);
  1759. out:
  1760. kfree(buf);
  1761. return ret;
  1762. }
  1763. static void rndis_get_scan_results(struct work_struct *work)
  1764. {
  1765. struct rndis_wlan_private *priv =
  1766. container_of(work, struct rndis_wlan_private, scan_work.work);
  1767. struct usbnet *usbdev = priv->usbdev;
  1768. int ret;
  1769. netdev_dbg(usbdev->net, "get_scan_results\n");
  1770. if (!priv->scan_request)
  1771. return;
  1772. ret = rndis_check_bssid_list(usbdev, NULL, NULL);
  1773. cfg80211_scan_done(priv->scan_request, ret < 0);
  1774. priv->scan_request = NULL;
  1775. }
  1776. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  1777. struct cfg80211_connect_params *sme)
  1778. {
  1779. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1780. struct usbnet *usbdev = priv->usbdev;
  1781. struct ieee80211_channel *channel = sme->channel;
  1782. struct ndis_80211_ssid ssid;
  1783. int pairwise = RNDIS_WLAN_ALG_NONE;
  1784. int groupwise = RNDIS_WLAN_ALG_NONE;
  1785. int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
  1786. int length, i, ret, chan = -1;
  1787. if (channel)
  1788. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1789. groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
  1790. for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
  1791. pairwise |=
  1792. rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
  1793. if (sme->crypto.n_ciphers_pairwise > 0 &&
  1794. pairwise == RNDIS_WLAN_ALG_NONE) {
  1795. netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
  1796. return -ENOTSUPP;
  1797. }
  1798. for (i = 0; i < sme->crypto.n_akm_suites; i++)
  1799. keymgmt |=
  1800. rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
  1801. if (sme->crypto.n_akm_suites > 0 &&
  1802. keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
  1803. netdev_err(usbdev->net, "Invalid keymgmt\n");
  1804. return -ENOTSUPP;
  1805. }
  1806. netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
  1807. sme->ssid, sme->bssid, chan,
  1808. sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
  1809. groupwise, pairwise, keymgmt);
  1810. if (is_associated(usbdev))
  1811. disassociate(usbdev, false);
  1812. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1813. if (ret < 0) {
  1814. netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
  1815. ret);
  1816. goto err_turn_radio_on;
  1817. }
  1818. ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
  1819. keymgmt);
  1820. if (ret < 0) {
  1821. netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
  1822. ret);
  1823. goto err_turn_radio_on;
  1824. }
  1825. set_priv_filter(usbdev);
  1826. ret = set_encr_mode(usbdev, pairwise, groupwise);
  1827. if (ret < 0) {
  1828. netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
  1829. ret);
  1830. goto err_turn_radio_on;
  1831. }
  1832. if (channel) {
  1833. ret = set_channel(usbdev, chan);
  1834. if (ret < 0) {
  1835. netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
  1836. ret);
  1837. goto err_turn_radio_on;
  1838. }
  1839. }
  1840. if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
  1841. priv->encr_tx_key_index = sme->key_idx;
  1842. ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
  1843. if (ret < 0) {
  1844. netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
  1845. ret, sme->key_len, sme->key_idx);
  1846. goto err_turn_radio_on;
  1847. }
  1848. }
  1849. if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
  1850. !is_broadcast_ether_addr(sme->bssid)) {
  1851. ret = set_bssid(usbdev, sme->bssid);
  1852. if (ret < 0) {
  1853. netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
  1854. ret);
  1855. goto err_turn_radio_on;
  1856. }
  1857. } else
  1858. clear_bssid(usbdev);
  1859. length = sme->ssid_len;
  1860. if (length > NDIS_802_11_LENGTH_SSID)
  1861. length = NDIS_802_11_LENGTH_SSID;
  1862. memset(&ssid, 0, sizeof(ssid));
  1863. ssid.length = cpu_to_le32(length);
  1864. memcpy(ssid.essid, sme->ssid, length);
  1865. /* Pause and purge rx queue, so we don't pass packets before
  1866. * 'media connect'-indication.
  1867. */
  1868. usbnet_pause_rx(usbdev);
  1869. usbnet_purge_paused_rxq(usbdev);
  1870. ret = set_essid(usbdev, &ssid);
  1871. if (ret < 0)
  1872. netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
  1873. return ret;
  1874. err_turn_radio_on:
  1875. disassociate(usbdev, true);
  1876. return ret;
  1877. }
  1878. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1879. u16 reason_code)
  1880. {
  1881. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1882. struct usbnet *usbdev = priv->usbdev;
  1883. netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
  1884. priv->connected = false;
  1885. memset(priv->bssid, 0, ETH_ALEN);
  1886. return deauthenticate(usbdev);
  1887. }
  1888. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1889. struct cfg80211_ibss_params *params)
  1890. {
  1891. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1892. struct usbnet *usbdev = priv->usbdev;
  1893. struct ieee80211_channel *channel = params->channel;
  1894. struct ndis_80211_ssid ssid;
  1895. enum nl80211_auth_type auth_type;
  1896. int ret, alg, length, chan = -1;
  1897. if (channel)
  1898. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1899. /* TODO: How to handle ad-hoc encryption?
  1900. * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
  1901. * pre-shared for encryption (open/shared/wpa), is key set before
  1902. * join_ibss? Which auth_type to use (not in params)? What about WPA?
  1903. */
  1904. if (params->privacy) {
  1905. auth_type = NL80211_AUTHTYPE_SHARED_KEY;
  1906. alg = RNDIS_WLAN_ALG_WEP;
  1907. } else {
  1908. auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  1909. alg = RNDIS_WLAN_ALG_NONE;
  1910. }
  1911. netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
  1912. params->ssid, params->bssid, chan, params->privacy);
  1913. if (is_associated(usbdev))
  1914. disassociate(usbdev, false);
  1915. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
  1916. if (ret < 0) {
  1917. netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
  1918. ret);
  1919. goto err_turn_radio_on;
  1920. }
  1921. ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
  1922. if (ret < 0) {
  1923. netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
  1924. ret);
  1925. goto err_turn_radio_on;
  1926. }
  1927. set_priv_filter(usbdev);
  1928. ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
  1929. if (ret < 0) {
  1930. netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
  1931. ret);
  1932. goto err_turn_radio_on;
  1933. }
  1934. if (channel) {
  1935. ret = set_channel(usbdev, chan);
  1936. if (ret < 0) {
  1937. netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
  1938. ret);
  1939. goto err_turn_radio_on;
  1940. }
  1941. }
  1942. if (params->bssid && !is_zero_ether_addr(params->bssid) &&
  1943. !is_broadcast_ether_addr(params->bssid)) {
  1944. ret = set_bssid(usbdev, params->bssid);
  1945. if (ret < 0) {
  1946. netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
  1947. ret);
  1948. goto err_turn_radio_on;
  1949. }
  1950. } else
  1951. clear_bssid(usbdev);
  1952. length = params->ssid_len;
  1953. if (length > NDIS_802_11_LENGTH_SSID)
  1954. length = NDIS_802_11_LENGTH_SSID;
  1955. memset(&ssid, 0, sizeof(ssid));
  1956. ssid.length = cpu_to_le32(length);
  1957. memcpy(ssid.essid, params->ssid, length);
  1958. /* Don't need to pause rx queue for ad-hoc. */
  1959. usbnet_purge_paused_rxq(usbdev);
  1960. usbnet_resume_rx(usbdev);
  1961. ret = set_essid(usbdev, &ssid);
  1962. if (ret < 0)
  1963. netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
  1964. ret);
  1965. return ret;
  1966. err_turn_radio_on:
  1967. disassociate(usbdev, true);
  1968. return ret;
  1969. }
  1970. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1971. {
  1972. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1973. struct usbnet *usbdev = priv->usbdev;
  1974. netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
  1975. priv->connected = false;
  1976. memset(priv->bssid, 0, ETH_ALEN);
  1977. return deauthenticate(usbdev);
  1978. }
  1979. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  1980. u8 key_index, bool pairwise, const u8 *mac_addr,
  1981. struct key_params *params)
  1982. {
  1983. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1984. struct usbnet *usbdev = priv->usbdev;
  1985. __le32 flags;
  1986. netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
  1987. __func__, key_index, mac_addr, params->cipher);
  1988. switch (params->cipher) {
  1989. case WLAN_CIPHER_SUITE_WEP40:
  1990. case WLAN_CIPHER_SUITE_WEP104:
  1991. return add_wep_key(usbdev, params->key, params->key_len,
  1992. key_index);
  1993. case WLAN_CIPHER_SUITE_TKIP:
  1994. case WLAN_CIPHER_SUITE_CCMP:
  1995. flags = 0;
  1996. if (params->seq && params->seq_len > 0)
  1997. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  1998. if (mac_addr)
  1999. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
  2000. NDIS_80211_ADDKEY_TRANSMIT_KEY;
  2001. return add_wpa_key(usbdev, params->key, params->key_len,
  2002. key_index, mac_addr, params->seq,
  2003. params->seq_len, params->cipher, flags);
  2004. default:
  2005. netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
  2006. __func__, params->cipher);
  2007. return -ENOTSUPP;
  2008. }
  2009. }
  2010. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  2011. u8 key_index, bool pairwise, const u8 *mac_addr)
  2012. {
  2013. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2014. struct usbnet *usbdev = priv->usbdev;
  2015. netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
  2016. return remove_key(usbdev, key_index, mac_addr);
  2017. }
  2018. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  2019. u8 key_index, bool unicast, bool multicast)
  2020. {
  2021. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2022. struct usbnet *usbdev = priv->usbdev;
  2023. struct rndis_wlan_encr_key key;
  2024. netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
  2025. if (key_index >= RNDIS_WLAN_NUM_KEYS)
  2026. return -ENOENT;
  2027. priv->encr_tx_key_index = key_index;
  2028. if (is_wpa_key(priv, key_index))
  2029. return 0;
  2030. key = priv->encr_keys[key_index];
  2031. return add_wep_key(usbdev, key.material, key.len, key_index);
  2032. }
  2033. static void rndis_fill_station_info(struct usbnet *usbdev,
  2034. struct station_info *sinfo)
  2035. {
  2036. __le32 linkspeed, rssi;
  2037. int ret, len;
  2038. memset(sinfo, 0, sizeof(*sinfo));
  2039. len = sizeof(linkspeed);
  2040. ret = rndis_query_oid(usbdev, RNDIS_OID_GEN_LINK_SPEED, &linkspeed, &len);
  2041. if (ret == 0) {
  2042. sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
  2043. sinfo->filled |= STATION_INFO_TX_BITRATE;
  2044. }
  2045. len = sizeof(rssi);
  2046. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
  2047. &rssi, &len);
  2048. if (ret == 0) {
  2049. sinfo->signal = level_to_qual(le32_to_cpu(rssi));
  2050. sinfo->filled |= STATION_INFO_SIGNAL;
  2051. }
  2052. }
  2053. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  2054. u8 *mac, struct station_info *sinfo)
  2055. {
  2056. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2057. struct usbnet *usbdev = priv->usbdev;
  2058. if (!ether_addr_equal(priv->bssid, mac))
  2059. return -ENOENT;
  2060. rndis_fill_station_info(usbdev, sinfo);
  2061. return 0;
  2062. }
  2063. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  2064. int idx, u8 *mac, struct station_info *sinfo)
  2065. {
  2066. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2067. struct usbnet *usbdev = priv->usbdev;
  2068. if (idx != 0)
  2069. return -ENOENT;
  2070. memcpy(mac, priv->bssid, ETH_ALEN);
  2071. rndis_fill_station_info(usbdev, sinfo);
  2072. return 0;
  2073. }
  2074. static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  2075. struct cfg80211_pmksa *pmksa)
  2076. {
  2077. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2078. struct usbnet *usbdev = priv->usbdev;
  2079. struct ndis_80211_pmkid *pmkids;
  2080. u32 *tmp = (u32 *)pmksa->pmkid;
  2081. netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
  2082. pmksa->bssid,
  2083. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  2084. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  2085. pmkids = get_device_pmkids(usbdev);
  2086. if (IS_ERR(pmkids)) {
  2087. /* couldn't read PMKID cache from device */
  2088. return PTR_ERR(pmkids);
  2089. }
  2090. pmkids = update_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
  2091. if (IS_ERR(pmkids)) {
  2092. /* not found, list full, etc */
  2093. return PTR_ERR(pmkids);
  2094. }
  2095. return set_device_pmkids(usbdev, pmkids);
  2096. }
  2097. static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  2098. struct cfg80211_pmksa *pmksa)
  2099. {
  2100. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2101. struct usbnet *usbdev = priv->usbdev;
  2102. struct ndis_80211_pmkid *pmkids;
  2103. u32 *tmp = (u32 *)pmksa->pmkid;
  2104. netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
  2105. pmksa->bssid,
  2106. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  2107. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  2108. pmkids = get_device_pmkids(usbdev);
  2109. if (IS_ERR(pmkids)) {
  2110. /* Couldn't read PMKID cache from device */
  2111. return PTR_ERR(pmkids);
  2112. }
  2113. pmkids = remove_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
  2114. if (IS_ERR(pmkids)) {
  2115. /* not found, etc */
  2116. return PTR_ERR(pmkids);
  2117. }
  2118. return set_device_pmkids(usbdev, pmkids);
  2119. }
  2120. static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
  2121. {
  2122. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2123. struct usbnet *usbdev = priv->usbdev;
  2124. struct ndis_80211_pmkid pmkid;
  2125. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2126. memset(&pmkid, 0, sizeof(pmkid));
  2127. pmkid.length = cpu_to_le32(sizeof(pmkid));
  2128. pmkid.bssid_info_count = cpu_to_le32(0);
  2129. return rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID,
  2130. &pmkid, sizeof(pmkid));
  2131. }
  2132. static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
  2133. bool enabled, int timeout)
  2134. {
  2135. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2136. struct usbnet *usbdev = priv->usbdev;
  2137. int power_mode;
  2138. __le32 mode;
  2139. int ret;
  2140. if (priv->device_type != RNDIS_BCM4320B)
  2141. return -ENOTSUPP;
  2142. netdev_dbg(usbdev->net, "%s(): %s, %d\n", __func__,
  2143. enabled ? "enabled" : "disabled",
  2144. timeout);
  2145. if (enabled)
  2146. power_mode = NDIS_80211_POWER_MODE_FAST_PSP;
  2147. else
  2148. power_mode = NDIS_80211_POWER_MODE_CAM;
  2149. if (power_mode == priv->power_mode)
  2150. return 0;
  2151. priv->power_mode = power_mode;
  2152. mode = cpu_to_le32(power_mode);
  2153. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_POWER_MODE,
  2154. &mode, sizeof(mode));
  2155. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_POWER_MODE -> %d\n",
  2156. __func__, ret);
  2157. return ret;
  2158. }
  2159. static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
  2160. struct net_device *dev,
  2161. s32 rssi_thold, u32 rssi_hyst)
  2162. {
  2163. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  2164. priv->cqm_rssi_thold = rssi_thold;
  2165. priv->cqm_rssi_hyst = rssi_hyst;
  2166. priv->last_cqm_event_rssi = 0;
  2167. return 0;
  2168. }
  2169. static void rndis_wlan_craft_connected_bss(struct usbnet *usbdev, u8 *bssid,
  2170. struct ndis_80211_assoc_info *info)
  2171. {
  2172. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2173. struct ieee80211_channel *channel;
  2174. struct ndis_80211_ssid ssid;
  2175. struct cfg80211_bss *bss;
  2176. s32 signal;
  2177. u64 timestamp;
  2178. u16 capability;
  2179. u32 beacon_period = 0;
  2180. __le32 rssi;
  2181. u8 ie_buf[34];
  2182. int len, ret, ie_len;
  2183. /* Get signal quality, in case of error use rssi=0 and ignore error. */
  2184. len = sizeof(rssi);
  2185. rssi = 0;
  2186. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
  2187. &rssi, &len);
  2188. signal = level_to_qual(le32_to_cpu(rssi));
  2189. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_RSSI -> %d, "
  2190. "rssi:%d, qual: %d\n", __func__, ret, le32_to_cpu(rssi),
  2191. level_to_qual(le32_to_cpu(rssi)));
  2192. /* Get AP capabilities */
  2193. if (info) {
  2194. capability = le16_to_cpu(info->resp_ie.capa);
  2195. } else {
  2196. /* Set atleast ESS/IBSS capability */
  2197. capability = (priv->infra_mode == NDIS_80211_INFRA_INFRA) ?
  2198. WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS;
  2199. }
  2200. /* Get channel and beacon interval */
  2201. channel = get_current_channel(usbdev, &beacon_period);
  2202. if (!channel) {
  2203. netdev_warn(usbdev->net, "%s(): could not get channel.\n",
  2204. __func__);
  2205. return;
  2206. }
  2207. /* Get SSID, in case of error, use zero length SSID and ignore error. */
  2208. len = sizeof(ssid);
  2209. memset(&ssid, 0, sizeof(ssid));
  2210. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_SSID,
  2211. &ssid, &len);
  2212. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_SSID -> %d, len: %d, ssid: "
  2213. "'%.32s'\n", __func__, ret,
  2214. le32_to_cpu(ssid.length), ssid.essid);
  2215. if (le32_to_cpu(ssid.length) > 32)
  2216. ssid.length = cpu_to_le32(32);
  2217. ie_buf[0] = WLAN_EID_SSID;
  2218. ie_buf[1] = le32_to_cpu(ssid.length);
  2219. memcpy(&ie_buf[2], ssid.essid, le32_to_cpu(ssid.length));
  2220. ie_len = le32_to_cpu(ssid.length) + 2;
  2221. /* no tsf */
  2222. timestamp = 0;
  2223. netdev_dbg(usbdev->net, "%s(): channel:%d(freq), bssid:[%pM], tsf:%d, "
  2224. "capa:%x, beacon int:%d, resp_ie(len:%d, essid:'%.32s'), "
  2225. "signal:%d\n", __func__, (channel ? channel->center_freq : -1),
  2226. bssid, (u32)timestamp, capability, beacon_period, ie_len,
  2227. ssid.essid, signal);
  2228. bss = cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid,
  2229. timestamp, capability, beacon_period, ie_buf, ie_len,
  2230. signal, GFP_KERNEL);
  2231. cfg80211_put_bss(bss);
  2232. }
  2233. /*
  2234. * workers, indication handlers, device poller
  2235. */
  2236. static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
  2237. {
  2238. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2239. struct ndis_80211_assoc_info *info = NULL;
  2240. u8 bssid[ETH_ALEN];
  2241. unsigned int resp_ie_len, req_ie_len;
  2242. unsigned int offset;
  2243. u8 *req_ie, *resp_ie;
  2244. int ret;
  2245. bool roamed = false;
  2246. bool match_bss;
  2247. if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
  2248. /* received media connect indication while connected, either
  2249. * device reassociated with same AP or roamed to new. */
  2250. roamed = true;
  2251. }
  2252. req_ie_len = 0;
  2253. resp_ie_len = 0;
  2254. req_ie = NULL;
  2255. resp_ie = NULL;
  2256. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  2257. info = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
  2258. if (!info) {
  2259. /* No memory? Try resume work later */
  2260. set_bit(WORK_LINK_UP, &priv->work_pending);
  2261. queue_work(priv->workqueue, &priv->work);
  2262. return;
  2263. }
  2264. /* Get association info IEs from device. */
  2265. ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
  2266. if (!ret) {
  2267. req_ie_len = le32_to_cpu(info->req_ie_length);
  2268. if (req_ie_len > CONTROL_BUFFER_SIZE)
  2269. req_ie_len = CONTROL_BUFFER_SIZE;
  2270. if (req_ie_len != 0) {
  2271. offset = le32_to_cpu(info->offset_req_ies);
  2272. if (offset > CONTROL_BUFFER_SIZE)
  2273. offset = CONTROL_BUFFER_SIZE;
  2274. req_ie = (u8 *)info + offset;
  2275. if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
  2276. req_ie_len =
  2277. CONTROL_BUFFER_SIZE - offset;
  2278. }
  2279. resp_ie_len = le32_to_cpu(info->resp_ie_length);
  2280. if (resp_ie_len > CONTROL_BUFFER_SIZE)
  2281. resp_ie_len = CONTROL_BUFFER_SIZE;
  2282. if (resp_ie_len != 0) {
  2283. offset = le32_to_cpu(info->offset_resp_ies);
  2284. if (offset > CONTROL_BUFFER_SIZE)
  2285. offset = CONTROL_BUFFER_SIZE;
  2286. resp_ie = (u8 *)info + offset;
  2287. if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
  2288. resp_ie_len =
  2289. CONTROL_BUFFER_SIZE - offset;
  2290. }
  2291. } else {
  2292. /* Since rndis_wlan_craft_connected_bss() might use info
  2293. * later and expects info to contain valid data if
  2294. * non-null, free info and set NULL here.
  2295. */
  2296. kfree(info);
  2297. info = NULL;
  2298. }
  2299. } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
  2300. return;
  2301. ret = get_bssid(usbdev, bssid);
  2302. if (ret < 0)
  2303. memset(bssid, 0, sizeof(bssid));
  2304. netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
  2305. bssid, roamed ? " roamed" : "");
  2306. /* Internal bss list in device should contain at least the currently
  2307. * connected bss and we can get it to cfg80211 with
  2308. * rndis_check_bssid_list().
  2309. *
  2310. * NDIS spec says: "If the device is associated, but the associated
  2311. * BSSID is not in its BSSID scan list, then the driver must add an
  2312. * entry for the BSSID at the end of the data that it returns in
  2313. * response to query of RNDIS_OID_802_11_BSSID_LIST."
  2314. *
  2315. * NOTE: Seems to be true for BCM4320b variant, but not BCM4320a.
  2316. */
  2317. match_bss = false;
  2318. rndis_check_bssid_list(usbdev, bssid, &match_bss);
  2319. if (!is_zero_ether_addr(bssid) && !match_bss) {
  2320. /* Couldn't get bss from device, we need to manually craft bss
  2321. * for cfg80211.
  2322. */
  2323. rndis_wlan_craft_connected_bss(usbdev, bssid, info);
  2324. }
  2325. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  2326. if (!roamed)
  2327. cfg80211_connect_result(usbdev->net, bssid, req_ie,
  2328. req_ie_len, resp_ie,
  2329. resp_ie_len, 0, GFP_KERNEL);
  2330. else
  2331. cfg80211_roamed(usbdev->net,
  2332. get_current_channel(usbdev, NULL),
  2333. bssid, req_ie, req_ie_len,
  2334. resp_ie, resp_ie_len, GFP_KERNEL);
  2335. } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  2336. cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
  2337. if (info != NULL)
  2338. kfree(info);
  2339. priv->connected = true;
  2340. memcpy(priv->bssid, bssid, ETH_ALEN);
  2341. usbnet_resume_rx(usbdev);
  2342. netif_carrier_on(usbdev->net);
  2343. }
  2344. static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
  2345. {
  2346. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2347. if (priv->connected) {
  2348. priv->connected = false;
  2349. memset(priv->bssid, 0, ETH_ALEN);
  2350. deauthenticate(usbdev);
  2351. cfg80211_disconnected(usbdev->net, 0, NULL, 0, GFP_KERNEL);
  2352. }
  2353. netif_carrier_off(usbdev->net);
  2354. }
  2355. static void rndis_wlan_worker(struct work_struct *work)
  2356. {
  2357. struct rndis_wlan_private *priv =
  2358. container_of(work, struct rndis_wlan_private, work);
  2359. struct usbnet *usbdev = priv->usbdev;
  2360. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
  2361. rndis_wlan_do_link_up_work(usbdev);
  2362. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
  2363. rndis_wlan_do_link_down_work(usbdev);
  2364. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  2365. set_multicast_list(usbdev);
  2366. }
  2367. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  2368. {
  2369. struct usbnet *usbdev = netdev_priv(dev);
  2370. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2371. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  2372. return;
  2373. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  2374. queue_work(priv->workqueue, &priv->work);
  2375. }
  2376. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  2377. struct ndis_80211_status_indication *indication,
  2378. int len)
  2379. {
  2380. u8 *buf;
  2381. const char *type;
  2382. int flags, buflen, key_id;
  2383. bool pairwise_error, group_error;
  2384. struct ndis_80211_auth_request *auth_req;
  2385. enum nl80211_key_type key_type;
  2386. /* must have at least one array entry */
  2387. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2388. sizeof(struct ndis_80211_auth_request)) {
  2389. netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
  2390. len);
  2391. return;
  2392. }
  2393. buf = (void *)&indication->u.auth_request[0];
  2394. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  2395. while (buflen >= sizeof(*auth_req)) {
  2396. auth_req = (void *)buf;
  2397. type = "unknown";
  2398. flags = le32_to_cpu(auth_req->flags);
  2399. pairwise_error = false;
  2400. group_error = false;
  2401. if (flags & 0x1)
  2402. type = "reauth request";
  2403. if (flags & 0x2)
  2404. type = "key update request";
  2405. if (flags & 0x6) {
  2406. pairwise_error = true;
  2407. type = "pairwise_error";
  2408. }
  2409. if (flags & 0xe) {
  2410. group_error = true;
  2411. type = "group_error";
  2412. }
  2413. netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
  2414. type, le32_to_cpu(auth_req->flags));
  2415. if (pairwise_error) {
  2416. key_type = NL80211_KEYTYPE_PAIRWISE;
  2417. key_id = -1;
  2418. cfg80211_michael_mic_failure(usbdev->net,
  2419. auth_req->bssid,
  2420. key_type, key_id, NULL,
  2421. GFP_KERNEL);
  2422. }
  2423. if (group_error) {
  2424. key_type = NL80211_KEYTYPE_GROUP;
  2425. key_id = -1;
  2426. cfg80211_michael_mic_failure(usbdev->net,
  2427. auth_req->bssid,
  2428. key_type, key_id, NULL,
  2429. GFP_KERNEL);
  2430. }
  2431. buflen -= le32_to_cpu(auth_req->length);
  2432. buf += le32_to_cpu(auth_req->length);
  2433. }
  2434. }
  2435. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  2436. struct ndis_80211_status_indication *indication,
  2437. int len)
  2438. {
  2439. struct ndis_80211_pmkid_cand_list *cand_list;
  2440. int list_len, expected_len, i;
  2441. if (len < offsetof(struct ndis_80211_status_indication, u) +
  2442. sizeof(struct ndis_80211_pmkid_cand_list)) {
  2443. netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
  2444. len);
  2445. return;
  2446. }
  2447. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  2448. sizeof(struct ndis_80211_pmkid_candidate);
  2449. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  2450. offsetof(struct ndis_80211_status_indication, u);
  2451. if (len < expected_len) {
  2452. netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
  2453. len, expected_len);
  2454. return;
  2455. }
  2456. cand_list = &indication->u.cand_list;
  2457. netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
  2458. le32_to_cpu(cand_list->version),
  2459. le32_to_cpu(cand_list->num_candidates));
  2460. if (le32_to_cpu(cand_list->version) != 1)
  2461. return;
  2462. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  2463. struct ndis_80211_pmkid_candidate *cand =
  2464. &cand_list->candidate_list[i];
  2465. bool preauth = !!(cand->flags & NDIS_80211_PMKID_CAND_PREAUTH);
  2466. netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, preauth: %d, bssid: %pM\n",
  2467. i, le32_to_cpu(cand->flags), preauth, cand->bssid);
  2468. cfg80211_pmksa_candidate_notify(usbdev->net, i, cand->bssid,
  2469. preauth, GFP_ATOMIC);
  2470. }
  2471. }
  2472. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  2473. struct rndis_indicate *msg, int buflen)
  2474. {
  2475. struct ndis_80211_status_indication *indication;
  2476. unsigned int len, offset;
  2477. offset = offsetof(struct rndis_indicate, status) +
  2478. le32_to_cpu(msg->offset);
  2479. len = le32_to_cpu(msg->length);
  2480. if (len < 8) {
  2481. netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
  2482. len);
  2483. return;
  2484. }
  2485. if (len > buflen || offset > buflen || offset + len > buflen) {
  2486. netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
  2487. offset + len, buflen);
  2488. return;
  2489. }
  2490. indication = (void *)((u8 *)msg + offset);
  2491. switch (le32_to_cpu(indication->status_type)) {
  2492. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  2493. netdev_info(usbdev->net, "radio state indication: %i\n",
  2494. le32_to_cpu(indication->u.radio_status));
  2495. return;
  2496. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  2497. netdev_info(usbdev->net, "media stream mode indication: %i\n",
  2498. le32_to_cpu(indication->u.media_stream_mode));
  2499. return;
  2500. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  2501. rndis_wlan_auth_indication(usbdev, indication, len);
  2502. return;
  2503. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2504. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2505. return;
  2506. default:
  2507. netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
  2508. le32_to_cpu(indication->status_type));
  2509. }
  2510. }
  2511. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2512. {
  2513. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2514. struct rndis_indicate *msg = ind;
  2515. switch (le32_to_cpu(msg->status)) {
  2516. case RNDIS_STATUS_MEDIA_CONNECT:
  2517. if (priv->current_command_oid == RNDIS_OID_802_11_ADD_KEY) {
  2518. /* RNDIS_OID_802_11_ADD_KEY causes sometimes extra
  2519. * "media connect" indications which confuses driver
  2520. * and userspace to think that device is
  2521. * roaming/reassociating when it isn't.
  2522. */
  2523. netdev_dbg(usbdev->net, "ignored RNDIS_OID_802_11_ADD_KEY triggered 'media connect'\n");
  2524. return;
  2525. }
  2526. usbnet_pause_rx(usbdev);
  2527. netdev_info(usbdev->net, "media connect\n");
  2528. /* queue work to avoid recursive calls into rndis_command */
  2529. set_bit(WORK_LINK_UP, &priv->work_pending);
  2530. queue_work(priv->workqueue, &priv->work);
  2531. break;
  2532. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2533. netdev_info(usbdev->net, "media disconnect\n");
  2534. /* queue work to avoid recursive calls into rndis_command */
  2535. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2536. queue_work(priv->workqueue, &priv->work);
  2537. break;
  2538. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2539. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2540. break;
  2541. default:
  2542. netdev_info(usbdev->net, "indication: 0x%08x\n",
  2543. le32_to_cpu(msg->status));
  2544. break;
  2545. }
  2546. }
  2547. static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
  2548. {
  2549. struct {
  2550. __le32 num_items;
  2551. __le32 items[8];
  2552. } networks_supported;
  2553. struct ndis_80211_capability *caps;
  2554. u8 caps_buf[sizeof(*caps) + sizeof(caps->auth_encr_pair) * 16];
  2555. int len, retval, i, n;
  2556. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2557. /* determine supported modes */
  2558. len = sizeof(networks_supported);
  2559. retval = rndis_query_oid(usbdev,
  2560. RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED,
  2561. &networks_supported, &len);
  2562. if (retval >= 0) {
  2563. n = le32_to_cpu(networks_supported.num_items);
  2564. if (n > 8)
  2565. n = 8;
  2566. for (i = 0; i < n; i++) {
  2567. switch (le32_to_cpu(networks_supported.items[i])) {
  2568. case NDIS_80211_TYPE_FREQ_HOP:
  2569. case NDIS_80211_TYPE_DIRECT_SEQ:
  2570. priv->caps |= CAP_MODE_80211B;
  2571. break;
  2572. case NDIS_80211_TYPE_OFDM_A:
  2573. priv->caps |= CAP_MODE_80211A;
  2574. break;
  2575. case NDIS_80211_TYPE_OFDM_G:
  2576. priv->caps |= CAP_MODE_80211G;
  2577. break;
  2578. }
  2579. }
  2580. }
  2581. /* get device 802.11 capabilities, number of PMKIDs */
  2582. caps = (struct ndis_80211_capability *)caps_buf;
  2583. len = sizeof(caps_buf);
  2584. retval = rndis_query_oid(usbdev,
  2585. RNDIS_OID_802_11_CAPABILITY,
  2586. caps, &len);
  2587. if (retval >= 0) {
  2588. netdev_dbg(usbdev->net, "RNDIS_OID_802_11_CAPABILITY -> len %d, "
  2589. "ver %d, pmkids %d, auth-encr-pairs %d\n",
  2590. le32_to_cpu(caps->length),
  2591. le32_to_cpu(caps->version),
  2592. le32_to_cpu(caps->num_pmkids),
  2593. le32_to_cpu(caps->num_auth_encr_pair));
  2594. wiphy->max_num_pmkids = le32_to_cpu(caps->num_pmkids);
  2595. } else
  2596. wiphy->max_num_pmkids = 0;
  2597. return retval;
  2598. }
  2599. static void rndis_do_cqm(struct usbnet *usbdev, s32 rssi)
  2600. {
  2601. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2602. enum nl80211_cqm_rssi_threshold_event event;
  2603. int thold, hyst, last_event;
  2604. if (priv->cqm_rssi_thold >= 0 || rssi >= 0)
  2605. return;
  2606. if (priv->infra_mode != NDIS_80211_INFRA_INFRA)
  2607. return;
  2608. last_event = priv->last_cqm_event_rssi;
  2609. thold = priv->cqm_rssi_thold;
  2610. hyst = priv->cqm_rssi_hyst;
  2611. if (rssi < thold && (last_event == 0 || rssi < last_event - hyst))
  2612. event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
  2613. else if (rssi > thold && (last_event == 0 || rssi > last_event + hyst))
  2614. event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
  2615. else
  2616. return;
  2617. priv->last_cqm_event_rssi = rssi;
  2618. cfg80211_cqm_rssi_notify(usbdev->net, event, GFP_KERNEL);
  2619. }
  2620. #define DEVICE_POLLER_JIFFIES (HZ)
  2621. static void rndis_device_poller(struct work_struct *work)
  2622. {
  2623. struct rndis_wlan_private *priv =
  2624. container_of(work, struct rndis_wlan_private,
  2625. dev_poller_work.work);
  2626. struct usbnet *usbdev = priv->usbdev;
  2627. __le32 rssi, tmp;
  2628. int len, ret, j;
  2629. int update_jiffies = DEVICE_POLLER_JIFFIES;
  2630. void *buf;
  2631. /* Only check/do workaround when connected. Calling is_associated()
  2632. * also polls device with rndis_command() and catches for media link
  2633. * indications.
  2634. */
  2635. if (!is_associated(usbdev)) {
  2636. /* Workaround bad scanning in BCM4320a devices with active
  2637. * background scanning when not associated.
  2638. */
  2639. if (priv->device_type == RNDIS_BCM4320A && priv->radio_on &&
  2640. !priv->scan_request) {
  2641. /* Get previous scan results */
  2642. rndis_check_bssid_list(usbdev, NULL, NULL);
  2643. /* Initiate new scan */
  2644. rndis_start_bssid_list_scan(usbdev);
  2645. }
  2646. goto end;
  2647. }
  2648. len = sizeof(rssi);
  2649. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
  2650. &rssi, &len);
  2651. if (ret == 0) {
  2652. priv->last_qual = level_to_qual(le32_to_cpu(rssi));
  2653. rndis_do_cqm(usbdev, le32_to_cpu(rssi));
  2654. }
  2655. netdev_dbg(usbdev->net, "dev-poller: RNDIS_OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
  2656. ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
  2657. /* Workaround transfer stalls on poor quality links.
  2658. * TODO: find right way to fix these stalls (as stalls do not happen
  2659. * with ndiswrapper/windows driver). */
  2660. if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
  2661. /* Decrease stats worker interval to catch stalls.
  2662. * faster. Faster than 400-500ms causes packet loss,
  2663. * Slower doesn't catch stalls fast enough.
  2664. */
  2665. j = msecs_to_jiffies(priv->param_workaround_interval);
  2666. if (j > DEVICE_POLLER_JIFFIES)
  2667. j = DEVICE_POLLER_JIFFIES;
  2668. else if (j <= 0)
  2669. j = 1;
  2670. update_jiffies = j;
  2671. /* Send scan OID. Use of both OIDs is required to get device
  2672. * working.
  2673. */
  2674. tmp = cpu_to_le32(1);
  2675. rndis_set_oid(usbdev,
  2676. RNDIS_OID_802_11_BSSID_LIST_SCAN,
  2677. &tmp, sizeof(tmp));
  2678. len = CONTROL_BUFFER_SIZE;
  2679. buf = kmalloc(len, GFP_KERNEL);
  2680. if (!buf)
  2681. goto end;
  2682. rndis_query_oid(usbdev,
  2683. RNDIS_OID_802_11_BSSID_LIST,
  2684. buf, &len);
  2685. kfree(buf);
  2686. }
  2687. end:
  2688. if (update_jiffies >= HZ)
  2689. update_jiffies = round_jiffies_relative(update_jiffies);
  2690. else {
  2691. j = round_jiffies_relative(update_jiffies);
  2692. if (abs(j - update_jiffies) <= 10)
  2693. update_jiffies = j;
  2694. }
  2695. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2696. update_jiffies);
  2697. }
  2698. /*
  2699. * driver/device initialization
  2700. */
  2701. static void rndis_copy_module_params(struct usbnet *usbdev, int device_type)
  2702. {
  2703. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2704. priv->device_type = device_type;
  2705. priv->param_country[0] = modparam_country[0];
  2706. priv->param_country[1] = modparam_country[1];
  2707. priv->param_country[2] = 0;
  2708. priv->param_frameburst = modparam_frameburst;
  2709. priv->param_afterburner = modparam_afterburner;
  2710. priv->param_power_save = modparam_power_save;
  2711. priv->param_power_output = modparam_power_output;
  2712. priv->param_roamtrigger = modparam_roamtrigger;
  2713. priv->param_roamdelta = modparam_roamdelta;
  2714. priv->param_country[0] = toupper(priv->param_country[0]);
  2715. priv->param_country[1] = toupper(priv->param_country[1]);
  2716. /* doesn't support EU as country code, use FI instead */
  2717. if (!strcmp(priv->param_country, "EU"))
  2718. strcpy(priv->param_country, "FI");
  2719. if (priv->param_power_save < 0)
  2720. priv->param_power_save = 0;
  2721. else if (priv->param_power_save > 2)
  2722. priv->param_power_save = 2;
  2723. if (priv->param_power_output < 0)
  2724. priv->param_power_output = 0;
  2725. else if (priv->param_power_output > 3)
  2726. priv->param_power_output = 3;
  2727. if (priv->param_roamtrigger < -80)
  2728. priv->param_roamtrigger = -80;
  2729. else if (priv->param_roamtrigger > -60)
  2730. priv->param_roamtrigger = -60;
  2731. if (priv->param_roamdelta < 0)
  2732. priv->param_roamdelta = 0;
  2733. else if (priv->param_roamdelta > 2)
  2734. priv->param_roamdelta = 2;
  2735. if (modparam_workaround_interval < 0)
  2736. priv->param_workaround_interval = 500;
  2737. else
  2738. priv->param_workaround_interval = modparam_workaround_interval;
  2739. }
  2740. static int unknown_early_init(struct usbnet *usbdev)
  2741. {
  2742. /* copy module parameters for unknown so that iwconfig reports txpower
  2743. * and workaround parameter is copied to private structure correctly.
  2744. */
  2745. rndis_copy_module_params(usbdev, RNDIS_UNKNOWN);
  2746. /* This is unknown device, so do not try set configuration parameters.
  2747. */
  2748. return 0;
  2749. }
  2750. static int bcm4320a_early_init(struct usbnet *usbdev)
  2751. {
  2752. /* copy module parameters for bcm4320a so that iwconfig reports txpower
  2753. * and workaround parameter is copied to private structure correctly.
  2754. */
  2755. rndis_copy_module_params(usbdev, RNDIS_BCM4320A);
  2756. /* bcm4320a doesn't handle configuration parameters well. Try
  2757. * set any and you get partially zeroed mac and broken device.
  2758. */
  2759. return 0;
  2760. }
  2761. static int bcm4320b_early_init(struct usbnet *usbdev)
  2762. {
  2763. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2764. char buf[8];
  2765. rndis_copy_module_params(usbdev, RNDIS_BCM4320B);
  2766. /* Early initialization settings, setting these won't have effect
  2767. * if called after generic_rndis_bind().
  2768. */
  2769. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2770. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2771. priv->param_frameburst ? "1" : "0");
  2772. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2773. priv->param_afterburner ? "1" : "0");
  2774. sprintf(buf, "%d", priv->param_power_save);
  2775. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2776. sprintf(buf, "%d", priv->param_power_output);
  2777. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2778. sprintf(buf, "%d", priv->param_roamtrigger);
  2779. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2780. sprintf(buf, "%d", priv->param_roamdelta);
  2781. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2782. return 0;
  2783. }
  2784. /* same as rndis_netdev_ops but with local multicast handler */
  2785. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2786. .ndo_open = usbnet_open,
  2787. .ndo_stop = usbnet_stop,
  2788. .ndo_start_xmit = usbnet_start_xmit,
  2789. .ndo_tx_timeout = usbnet_tx_timeout,
  2790. .ndo_set_mac_address = eth_mac_addr,
  2791. .ndo_validate_addr = eth_validate_addr,
  2792. .ndo_set_rx_mode = rndis_wlan_set_multicast_list,
  2793. };
  2794. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2795. {
  2796. struct wiphy *wiphy;
  2797. struct rndis_wlan_private *priv;
  2798. int retval, len;
  2799. __le32 tmp;
  2800. /* allocate wiphy and rndis private data
  2801. * NOTE: We only support a single virtual interface, so wiphy
  2802. * and wireless_dev are somewhat synonymous for this device.
  2803. */
  2804. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2805. if (!wiphy)
  2806. return -ENOMEM;
  2807. priv = wiphy_priv(wiphy);
  2808. usbdev->net->ieee80211_ptr = &priv->wdev;
  2809. priv->wdev.wiphy = wiphy;
  2810. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2811. /* These have to be initialized before calling generic_rndis_bind().
  2812. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2813. */
  2814. usbdev->driver_priv = priv;
  2815. priv->usbdev = usbdev;
  2816. mutex_init(&priv->command_lock);
  2817. /* because rndis_command() sleeps we need to use workqueue */
  2818. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2819. INIT_WORK(&priv->work, rndis_wlan_worker);
  2820. INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
  2821. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2822. /* try bind rndis_host */
  2823. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2824. if (retval < 0)
  2825. goto fail;
  2826. /* generic_rndis_bind set packet filter to multicast_all+
  2827. * promisc mode which doesn't work well for our devices (device
  2828. * picks up rssi to closest station instead of to access point).
  2829. *
  2830. * rndis_host wants to avoid all OID as much as possible
  2831. * so do promisc/multicast handling in rndis_wlan.
  2832. */
  2833. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2834. tmp = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST);
  2835. retval = rndis_set_oid(usbdev,
  2836. RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
  2837. &tmp, sizeof(tmp));
  2838. len = sizeof(tmp);
  2839. retval = rndis_query_oid(usbdev,
  2840. RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
  2841. &tmp, &len);
  2842. priv->multicast_size = le32_to_cpu(tmp);
  2843. if (retval < 0 || priv->multicast_size < 0)
  2844. priv->multicast_size = 0;
  2845. if (priv->multicast_size > 0)
  2846. usbdev->net->flags |= IFF_MULTICAST;
  2847. else
  2848. usbdev->net->flags &= ~IFF_MULTICAST;
  2849. /* fill-out wiphy structure and register w/ cfg80211 */
  2850. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2851. wiphy->privid = rndis_wiphy_privid;
  2852. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2853. | BIT(NL80211_IFTYPE_ADHOC);
  2854. wiphy->max_scan_ssids = 1;
  2855. /* TODO: fill-out band/encr information based on priv->caps */
  2856. rndis_wlan_get_caps(usbdev, wiphy);
  2857. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2858. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2859. priv->band.channels = priv->channels;
  2860. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2861. priv->band.bitrates = priv->rates;
  2862. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2863. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2864. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2865. memcpy(priv->cipher_suites, rndis_cipher_suites,
  2866. sizeof(rndis_cipher_suites));
  2867. wiphy->cipher_suites = priv->cipher_suites;
  2868. wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
  2869. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2870. if (wiphy_register(wiphy)) {
  2871. retval = -ENODEV;
  2872. goto fail;
  2873. }
  2874. set_default_iw_params(usbdev);
  2875. priv->power_mode = -1;
  2876. /* set default rts/frag */
  2877. rndis_set_wiphy_params(wiphy,
  2878. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2879. /* turn radio off on init */
  2880. priv->radio_on = false;
  2881. disassociate(usbdev, false);
  2882. netif_carrier_off(usbdev->net);
  2883. return 0;
  2884. fail:
  2885. cancel_delayed_work_sync(&priv->dev_poller_work);
  2886. cancel_delayed_work_sync(&priv->scan_work);
  2887. cancel_work_sync(&priv->work);
  2888. flush_workqueue(priv->workqueue);
  2889. destroy_workqueue(priv->workqueue);
  2890. wiphy_free(wiphy);
  2891. return retval;
  2892. }
  2893. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2894. {
  2895. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2896. /* turn radio off */
  2897. disassociate(usbdev, false);
  2898. cancel_delayed_work_sync(&priv->dev_poller_work);
  2899. cancel_delayed_work_sync(&priv->scan_work);
  2900. cancel_work_sync(&priv->work);
  2901. flush_workqueue(priv->workqueue);
  2902. destroy_workqueue(priv->workqueue);
  2903. rndis_unbind(usbdev, intf);
  2904. wiphy_unregister(priv->wdev.wiphy);
  2905. wiphy_free(priv->wdev.wiphy);
  2906. }
  2907. static int rndis_wlan_reset(struct usbnet *usbdev)
  2908. {
  2909. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2910. int retval;
  2911. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2912. retval = rndis_reset(usbdev);
  2913. if (retval)
  2914. netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
  2915. /* rndis_reset cleared multicast list, so restore here.
  2916. (set_multicast_list() also turns on current packet filter) */
  2917. set_multicast_list(usbdev);
  2918. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2919. round_jiffies_relative(DEVICE_POLLER_JIFFIES));
  2920. return deauthenticate(usbdev);
  2921. }
  2922. static int rndis_wlan_stop(struct usbnet *usbdev)
  2923. {
  2924. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2925. int retval;
  2926. __le32 filter;
  2927. netdev_dbg(usbdev->net, "%s()\n", __func__);
  2928. retval = disassociate(usbdev, false);
  2929. priv->work_pending = 0;
  2930. cancel_delayed_work_sync(&priv->dev_poller_work);
  2931. cancel_delayed_work_sync(&priv->scan_work);
  2932. cancel_work_sync(&priv->work);
  2933. flush_workqueue(priv->workqueue);
  2934. if (priv->scan_request) {
  2935. cfg80211_scan_done(priv->scan_request, true);
  2936. priv->scan_request = NULL;
  2937. }
  2938. /* Set current packet filter zero to block receiving data packets from
  2939. device. */
  2940. filter = 0;
  2941. rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2942. sizeof(filter));
  2943. return retval;
  2944. }
  2945. static const struct driver_info bcm4320b_info = {
  2946. .description = "Wireless RNDIS device, BCM4320b based",
  2947. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2948. FLAG_AVOID_UNLINK_URBS,
  2949. .bind = rndis_wlan_bind,
  2950. .unbind = rndis_wlan_unbind,
  2951. .status = rndis_status,
  2952. .rx_fixup = rndis_rx_fixup,
  2953. .tx_fixup = rndis_tx_fixup,
  2954. .reset = rndis_wlan_reset,
  2955. .stop = rndis_wlan_stop,
  2956. .early_init = bcm4320b_early_init,
  2957. .indication = rndis_wlan_indication,
  2958. };
  2959. static const struct driver_info bcm4320a_info = {
  2960. .description = "Wireless RNDIS device, BCM4320a based",
  2961. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2962. FLAG_AVOID_UNLINK_URBS,
  2963. .bind = rndis_wlan_bind,
  2964. .unbind = rndis_wlan_unbind,
  2965. .status = rndis_status,
  2966. .rx_fixup = rndis_rx_fixup,
  2967. .tx_fixup = rndis_tx_fixup,
  2968. .reset = rndis_wlan_reset,
  2969. .stop = rndis_wlan_stop,
  2970. .early_init = bcm4320a_early_init,
  2971. .indication = rndis_wlan_indication,
  2972. };
  2973. static const struct driver_info rndis_wlan_info = {
  2974. .description = "Wireless RNDIS device",
  2975. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2976. FLAG_AVOID_UNLINK_URBS,
  2977. .bind = rndis_wlan_bind,
  2978. .unbind = rndis_wlan_unbind,
  2979. .status = rndis_status,
  2980. .rx_fixup = rndis_rx_fixup,
  2981. .tx_fixup = rndis_tx_fixup,
  2982. .reset = rndis_wlan_reset,
  2983. .stop = rndis_wlan_stop,
  2984. .early_init = unknown_early_init,
  2985. .indication = rndis_wlan_indication,
  2986. };
  2987. /*-------------------------------------------------------------------------*/
  2988. static const struct usb_device_id products [] = {
  2989. #define RNDIS_MASTER_INTERFACE \
  2990. .bInterfaceClass = USB_CLASS_COMM, \
  2991. .bInterfaceSubClass = 2 /* ACM */, \
  2992. .bInterfaceProtocol = 0x0ff
  2993. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2994. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  2995. */
  2996. {
  2997. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2998. | USB_DEVICE_ID_MATCH_DEVICE,
  2999. .idVendor = 0x0411,
  3000. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  3001. RNDIS_MASTER_INTERFACE,
  3002. .driver_info = (unsigned long) &bcm4320b_info,
  3003. }, {
  3004. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3005. | USB_DEVICE_ID_MATCH_DEVICE,
  3006. .idVendor = 0x0baf,
  3007. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  3008. RNDIS_MASTER_INTERFACE,
  3009. .driver_info = (unsigned long) &bcm4320b_info,
  3010. }, {
  3011. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3012. | USB_DEVICE_ID_MATCH_DEVICE,
  3013. .idVendor = 0x050d,
  3014. .idProduct = 0x011b, /* Belkin F5D7051 */
  3015. RNDIS_MASTER_INTERFACE,
  3016. .driver_info = (unsigned long) &bcm4320b_info,
  3017. }, {
  3018. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3019. | USB_DEVICE_ID_MATCH_DEVICE,
  3020. .idVendor = 0x1799, /* Belkin has two vendor ids */
  3021. .idProduct = 0x011b, /* Belkin F5D7051 */
  3022. RNDIS_MASTER_INTERFACE,
  3023. .driver_info = (unsigned long) &bcm4320b_info,
  3024. }, {
  3025. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3026. | USB_DEVICE_ID_MATCH_DEVICE,
  3027. .idVendor = 0x13b1,
  3028. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  3029. RNDIS_MASTER_INTERFACE,
  3030. .driver_info = (unsigned long) &bcm4320b_info,
  3031. }, {
  3032. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3033. | USB_DEVICE_ID_MATCH_DEVICE,
  3034. .idVendor = 0x13b1,
  3035. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  3036. RNDIS_MASTER_INTERFACE,
  3037. .driver_info = (unsigned long) &bcm4320b_info,
  3038. }, {
  3039. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3040. | USB_DEVICE_ID_MATCH_DEVICE,
  3041. .idVendor = 0x0b05,
  3042. .idProduct = 0x1717, /* Asus WL169gE */
  3043. RNDIS_MASTER_INTERFACE,
  3044. .driver_info = (unsigned long) &bcm4320b_info,
  3045. }, {
  3046. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3047. | USB_DEVICE_ID_MATCH_DEVICE,
  3048. .idVendor = 0x0a5c,
  3049. .idProduct = 0xd11b, /* Eminent EM4045 */
  3050. RNDIS_MASTER_INTERFACE,
  3051. .driver_info = (unsigned long) &bcm4320b_info,
  3052. }, {
  3053. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3054. | USB_DEVICE_ID_MATCH_DEVICE,
  3055. .idVendor = 0x1690,
  3056. .idProduct = 0x0715, /* BT Voyager 1055 */
  3057. RNDIS_MASTER_INTERFACE,
  3058. .driver_info = (unsigned long) &bcm4320b_info,
  3059. },
  3060. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  3061. * parameters available, hardware probably contain older firmware version with
  3062. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  3063. */
  3064. {
  3065. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3066. | USB_DEVICE_ID_MATCH_DEVICE,
  3067. .idVendor = 0x13b1,
  3068. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  3069. RNDIS_MASTER_INTERFACE,
  3070. .driver_info = (unsigned long) &bcm4320a_info,
  3071. }, {
  3072. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3073. | USB_DEVICE_ID_MATCH_DEVICE,
  3074. .idVendor = 0x0baf,
  3075. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  3076. RNDIS_MASTER_INTERFACE,
  3077. .driver_info = (unsigned long) &bcm4320a_info,
  3078. }, {
  3079. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3080. | USB_DEVICE_ID_MATCH_DEVICE,
  3081. .idVendor = 0x0411,
  3082. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  3083. RNDIS_MASTER_INTERFACE,
  3084. .driver_info = (unsigned long) &bcm4320a_info,
  3085. },
  3086. /* Generic Wireless RNDIS devices that we don't have exact
  3087. * idVendor/idProduct/chip yet.
  3088. */
  3089. {
  3090. /* RNDIS is MSFT's un-official variant of CDC ACM */
  3091. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  3092. .driver_info = (unsigned long) &rndis_wlan_info,
  3093. }, {
  3094. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  3095. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  3096. .driver_info = (unsigned long) &rndis_wlan_info,
  3097. },
  3098. { }, // END
  3099. };
  3100. MODULE_DEVICE_TABLE(usb, products);
  3101. static struct usb_driver rndis_wlan_driver = {
  3102. .name = "rndis_wlan",
  3103. .id_table = products,
  3104. .probe = usbnet_probe,
  3105. .disconnect = usbnet_disconnect,
  3106. .suspend = usbnet_suspend,
  3107. .resume = usbnet_resume,
  3108. .disable_hub_initiated_lpm = 1,
  3109. };
  3110. module_usb_driver(rndis_wlan_driver);
  3111. MODULE_AUTHOR("Bjorge Dijkstra");
  3112. MODULE_AUTHOR("Jussi Kivilinna");
  3113. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  3114. MODULE_LICENSE("GPL");