rndis_wlan.c 100 KB

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