rndis_wlan.c 100 KB

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