ieee80211.c 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <net/mac80211.h>
  11. #include <net/ieee80211_radiotap.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/wireless.h>
  21. #include <linux/rtnetlink.h>
  22. #include <net/iw_handler.h>
  23. #include <linux/compiler.h>
  24. #include <linux/bitmap.h>
  25. #include <net/cfg80211.h>
  26. #include "ieee80211_common.h"
  27. #include "ieee80211_i.h"
  28. #include "ieee80211_rate.h"
  29. #include "wep.h"
  30. #include "wpa.h"
  31. #include "tkip.h"
  32. #include "wme.h"
  33. #include "aes_ccm.h"
  34. #include "ieee80211_led.h"
  35. #include "ieee80211_cfg.h"
  36. #include "debugfs.h"
  37. #include "debugfs_netdev.h"
  38. #include "debugfs_key.h"
  39. /* privid for wiphys to determine whether they belong to us or not */
  40. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  41. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  42. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  43. static const unsigned char rfc1042_header[] =
  44. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  45. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  46. static const unsigned char bridge_tunnel_header[] =
  47. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  48. /* No encapsulation header if EtherType < 0x600 (=length) */
  49. static const unsigned char eapol_header[] =
  50. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
  51. static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
  52. struct ieee80211_hdr *hdr)
  53. {
  54. /* Set the sequence number for this frame. */
  55. hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
  56. /* Increase the sequence number. */
  57. sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
  58. }
  59. struct ieee80211_key_conf *
  60. ieee80211_key_data2conf(struct ieee80211_local *local,
  61. const struct ieee80211_key *data)
  62. {
  63. struct ieee80211_key_conf *conf;
  64. conf = kmalloc(sizeof(*conf) + data->keylen, GFP_ATOMIC);
  65. if (!conf)
  66. return NULL;
  67. conf->hw_key_idx = data->hw_key_idx;
  68. conf->alg = data->alg;
  69. conf->keylen = data->keylen;
  70. conf->flags = 0;
  71. if (data->force_sw_encrypt)
  72. conf->flags |= IEEE80211_KEY_FORCE_SW_ENCRYPT;
  73. conf->keyidx = data->keyidx;
  74. if (data->default_tx_key)
  75. conf->flags |= IEEE80211_KEY_DEFAULT_TX_KEY;
  76. if (local->default_wep_only)
  77. conf->flags |= IEEE80211_KEY_DEFAULT_WEP_ONLY;
  78. memcpy(conf->key, data->key, data->keylen);
  79. return conf;
  80. }
  81. struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
  82. int idx, size_t key_len, gfp_t flags)
  83. {
  84. struct ieee80211_key *key;
  85. key = kzalloc(sizeof(struct ieee80211_key) + key_len, flags);
  86. if (!key)
  87. return NULL;
  88. kref_init(&key->kref);
  89. return key;
  90. }
  91. static void ieee80211_key_release(struct kref *kref)
  92. {
  93. struct ieee80211_key *key;
  94. key = container_of(kref, struct ieee80211_key, kref);
  95. if (key->alg == ALG_CCMP)
  96. ieee80211_aes_key_free(key->u.ccmp.tfm);
  97. ieee80211_debugfs_key_remove(key);
  98. kfree(key);
  99. }
  100. void ieee80211_key_free(struct ieee80211_key *key)
  101. {
  102. if (key)
  103. kref_put(&key->kref, ieee80211_key_release);
  104. }
  105. static int rate_list_match(const int *rate_list, int rate)
  106. {
  107. int i;
  108. if (!rate_list)
  109. return 0;
  110. for (i = 0; rate_list[i] >= 0; i++)
  111. if (rate_list[i] == rate)
  112. return 1;
  113. return 0;
  114. }
  115. void ieee80211_prepare_rates(struct ieee80211_local *local,
  116. struct ieee80211_hw_mode *mode)
  117. {
  118. int i;
  119. for (i = 0; i < mode->num_rates; i++) {
  120. struct ieee80211_rate *rate = &mode->rates[i];
  121. rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
  122. IEEE80211_RATE_BASIC);
  123. if (local->supp_rates[mode->mode]) {
  124. if (!rate_list_match(local->supp_rates[mode->mode],
  125. rate->rate))
  126. continue;
  127. }
  128. rate->flags |= IEEE80211_RATE_SUPPORTED;
  129. /* Use configured basic rate set if it is available. If not,
  130. * use defaults that are sane for most cases. */
  131. if (local->basic_rates[mode->mode]) {
  132. if (rate_list_match(local->basic_rates[mode->mode],
  133. rate->rate))
  134. rate->flags |= IEEE80211_RATE_BASIC;
  135. } else switch (mode->mode) {
  136. case MODE_IEEE80211A:
  137. if (rate->rate == 60 || rate->rate == 120 ||
  138. rate->rate == 240)
  139. rate->flags |= IEEE80211_RATE_BASIC;
  140. break;
  141. case MODE_IEEE80211B:
  142. if (rate->rate == 10 || rate->rate == 20)
  143. rate->flags |= IEEE80211_RATE_BASIC;
  144. break;
  145. case MODE_ATHEROS_TURBO:
  146. if (rate->rate == 120 || rate->rate == 240 ||
  147. rate->rate == 480)
  148. rate->flags |= IEEE80211_RATE_BASIC;
  149. break;
  150. case MODE_IEEE80211G:
  151. if (rate->rate == 10 || rate->rate == 20 ||
  152. rate->rate == 55 || rate->rate == 110)
  153. rate->flags |= IEEE80211_RATE_BASIC;
  154. break;
  155. }
  156. /* Set ERP and MANDATORY flags based on phymode */
  157. switch (mode->mode) {
  158. case MODE_IEEE80211A:
  159. if (rate->rate == 60 || rate->rate == 120 ||
  160. rate->rate == 240)
  161. rate->flags |= IEEE80211_RATE_MANDATORY;
  162. break;
  163. case MODE_IEEE80211B:
  164. if (rate->rate == 10)
  165. rate->flags |= IEEE80211_RATE_MANDATORY;
  166. break;
  167. case MODE_ATHEROS_TURBO:
  168. break;
  169. case MODE_IEEE80211G:
  170. if (rate->rate == 10 || rate->rate == 20 ||
  171. rate->rate == 55 || rate->rate == 110 ||
  172. rate->rate == 60 || rate->rate == 120 ||
  173. rate->rate == 240)
  174. rate->flags |= IEEE80211_RATE_MANDATORY;
  175. break;
  176. }
  177. if (ieee80211_is_erp_rate(mode->mode, rate->rate))
  178. rate->flags |= IEEE80211_RATE_ERP;
  179. }
  180. }
  181. static void ieee80211_key_threshold_notify(struct net_device *dev,
  182. struct ieee80211_key *key,
  183. struct sta_info *sta)
  184. {
  185. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  186. struct sk_buff *skb;
  187. struct ieee80211_msg_key_notification *msg;
  188. /* if no one will get it anyway, don't even allocate it.
  189. * unlikely because this is only relevant for APs
  190. * where the device must be open... */
  191. if (unlikely(!local->apdev))
  192. return;
  193. skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
  194. sizeof(struct ieee80211_msg_key_notification));
  195. if (!skb)
  196. return;
  197. skb_reserve(skb, sizeof(struct ieee80211_frame_info));
  198. msg = (struct ieee80211_msg_key_notification *)
  199. skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
  200. msg->tx_rx_count = key->tx_rx_count;
  201. memcpy(msg->ifname, dev->name, IFNAMSIZ);
  202. if (sta)
  203. memcpy(msg->addr, sta->addr, ETH_ALEN);
  204. else
  205. memset(msg->addr, 0xff, ETH_ALEN);
  206. key->tx_rx_count = 0;
  207. ieee80211_rx_mgmt(local, skb, NULL,
  208. ieee80211_msg_key_threshold_notification);
  209. }
  210. static u8 * ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
  211. {
  212. u16 fc;
  213. if (len < 24)
  214. return NULL;
  215. fc = le16_to_cpu(hdr->frame_control);
  216. switch (fc & IEEE80211_FCTL_FTYPE) {
  217. case IEEE80211_FTYPE_DATA:
  218. switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  219. case IEEE80211_FCTL_TODS:
  220. return hdr->addr1;
  221. case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  222. return NULL;
  223. case IEEE80211_FCTL_FROMDS:
  224. return hdr->addr2;
  225. case 0:
  226. return hdr->addr3;
  227. }
  228. break;
  229. case IEEE80211_FTYPE_MGMT:
  230. return hdr->addr3;
  231. case IEEE80211_FTYPE_CTL:
  232. if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
  233. return hdr->addr1;
  234. else
  235. return NULL;
  236. }
  237. return NULL;
  238. }
  239. int ieee80211_get_hdrlen(u16 fc)
  240. {
  241. int hdrlen = 24;
  242. switch (fc & IEEE80211_FCTL_FTYPE) {
  243. case IEEE80211_FTYPE_DATA:
  244. if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
  245. hdrlen = 30; /* Addr4 */
  246. /*
  247. * The QoS Control field is two bytes and its presence is
  248. * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
  249. * hdrlen if that bit is set.
  250. * This works by masking out the bit and shifting it to
  251. * bit position 1 so the result has the value 0 or 2.
  252. */
  253. hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
  254. >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
  255. break;
  256. case IEEE80211_FTYPE_CTL:
  257. /*
  258. * ACK and CTS are 10 bytes, all others 16. To see how
  259. * to get this condition consider
  260. * subtype mask: 0b0000000011110000 (0x00F0)
  261. * ACK subtype: 0b0000000011010000 (0x00D0)
  262. * CTS subtype: 0b0000000011000000 (0x00C0)
  263. * bits that matter: ^^^ (0x00E0)
  264. * value of those: 0b0000000011000000 (0x00C0)
  265. */
  266. if ((fc & 0xE0) == 0xC0)
  267. hdrlen = 10;
  268. else
  269. hdrlen = 16;
  270. break;
  271. }
  272. return hdrlen;
  273. }
  274. EXPORT_SYMBOL(ieee80211_get_hdrlen);
  275. int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  276. {
  277. const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
  278. int hdrlen;
  279. if (unlikely(skb->len < 10))
  280. return 0;
  281. hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
  282. if (unlikely(hdrlen > skb->len))
  283. return 0;
  284. return hdrlen;
  285. }
  286. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  287. static int ieee80211_get_radiotap_len(struct sk_buff *skb)
  288. {
  289. struct ieee80211_radiotap_header *hdr =
  290. (struct ieee80211_radiotap_header *) skb->data;
  291. return le16_to_cpu(hdr->it_len);
  292. }
  293. #ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
  294. static void ieee80211_dump_frame(const char *ifname, const char *title,
  295. const struct sk_buff *skb)
  296. {
  297. const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  298. u16 fc;
  299. int hdrlen;
  300. printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
  301. if (skb->len < 4) {
  302. printk("\n");
  303. return;
  304. }
  305. fc = le16_to_cpu(hdr->frame_control);
  306. hdrlen = ieee80211_get_hdrlen(fc);
  307. if (hdrlen > skb->len)
  308. hdrlen = skb->len;
  309. if (hdrlen >= 4)
  310. printk(" FC=0x%04x DUR=0x%04x",
  311. fc, le16_to_cpu(hdr->duration_id));
  312. if (hdrlen >= 10)
  313. printk(" A1=" MAC_FMT, MAC_ARG(hdr->addr1));
  314. if (hdrlen >= 16)
  315. printk(" A2=" MAC_FMT, MAC_ARG(hdr->addr2));
  316. if (hdrlen >= 24)
  317. printk(" A3=" MAC_FMT, MAC_ARG(hdr->addr3));
  318. if (hdrlen >= 30)
  319. printk(" A4=" MAC_FMT, MAC_ARG(hdr->addr4));
  320. printk("\n");
  321. }
  322. #else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
  323. static inline void ieee80211_dump_frame(const char *ifname, const char *title,
  324. struct sk_buff *skb)
  325. {
  326. }
  327. #endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
  328. static int ieee80211_is_eapol(const struct sk_buff *skb)
  329. {
  330. const struct ieee80211_hdr *hdr;
  331. u16 fc;
  332. int hdrlen;
  333. if (unlikely(skb->len < 10))
  334. return 0;
  335. hdr = (const struct ieee80211_hdr *) skb->data;
  336. fc = le16_to_cpu(hdr->frame_control);
  337. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  338. return 0;
  339. hdrlen = ieee80211_get_hdrlen(fc);
  340. if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
  341. memcmp(skb->data + hdrlen, eapol_header,
  342. sizeof(eapol_header)) == 0))
  343. return 1;
  344. return 0;
  345. }
  346. static ieee80211_txrx_result
  347. ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
  348. {
  349. struct rate_control_extra extra;
  350. memset(&extra, 0, sizeof(extra));
  351. extra.mode = tx->u.tx.mode;
  352. extra.mgmt_data = tx->sdata &&
  353. tx->sdata->type == IEEE80211_IF_TYPE_MGMT;
  354. extra.ethertype = tx->ethertype;
  355. tx->u.tx.rate = rate_control_get_rate(tx->local, tx->dev, tx->skb,
  356. &extra);
  357. if (unlikely(extra.probe != NULL)) {
  358. tx->u.tx.control->flags |= IEEE80211_TXCTL_RATE_CTRL_PROBE;
  359. tx->u.tx.probe_last_frag = 1;
  360. tx->u.tx.control->alt_retry_rate = tx->u.tx.rate->val;
  361. tx->u.tx.rate = extra.probe;
  362. } else {
  363. tx->u.tx.control->alt_retry_rate = -1;
  364. }
  365. if (!tx->u.tx.rate)
  366. return TXRX_DROP;
  367. if (tx->u.tx.mode->mode == MODE_IEEE80211G &&
  368. tx->local->cts_protect_erp_frames && tx->fragmented &&
  369. extra.nonerp) {
  370. tx->u.tx.last_frag_rate = tx->u.tx.rate;
  371. tx->u.tx.probe_last_frag = extra.probe ? 1 : 0;
  372. tx->u.tx.rate = extra.nonerp;
  373. tx->u.tx.control->rate = extra.nonerp;
  374. tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  375. } else {
  376. tx->u.tx.last_frag_rate = tx->u.tx.rate;
  377. tx->u.tx.control->rate = tx->u.tx.rate;
  378. }
  379. tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
  380. if ((tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
  381. tx->local->short_preamble &&
  382. (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
  383. tx->u.tx.short_preamble = 1;
  384. tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
  385. }
  386. return TXRX_CONTINUE;
  387. }
  388. static ieee80211_txrx_result
  389. ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
  390. {
  391. if (tx->sta)
  392. tx->u.tx.control->key_idx = tx->sta->key_idx_compression;
  393. else
  394. tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
  395. if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
  396. tx->key = NULL;
  397. else if (tx->sta && tx->sta->key)
  398. tx->key = tx->sta->key;
  399. else if (tx->sdata->default_key)
  400. tx->key = tx->sdata->default_key;
  401. else if (tx->sdata->drop_unencrypted &&
  402. !(tx->sdata->eapol && ieee80211_is_eapol(tx->skb))) {
  403. I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
  404. return TXRX_DROP;
  405. } else
  406. tx->key = NULL;
  407. if (tx->key) {
  408. tx->key->tx_rx_count++;
  409. if (unlikely(tx->local->key_tx_rx_threshold &&
  410. tx->key->tx_rx_count >
  411. tx->local->key_tx_rx_threshold)) {
  412. ieee80211_key_threshold_notify(tx->dev, tx->key,
  413. tx->sta);
  414. }
  415. }
  416. return TXRX_CONTINUE;
  417. }
  418. static ieee80211_txrx_result
  419. ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
  420. {
  421. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  422. size_t hdrlen, per_fragm, num_fragm, payload_len, left;
  423. struct sk_buff **frags, *first, *frag;
  424. int i;
  425. u16 seq;
  426. u8 *pos;
  427. int frag_threshold = tx->local->fragmentation_threshold;
  428. if (!tx->fragmented)
  429. return TXRX_CONTINUE;
  430. first = tx->skb;
  431. hdrlen = ieee80211_get_hdrlen(tx->fc);
  432. payload_len = first->len - hdrlen;
  433. per_fragm = frag_threshold - hdrlen - FCS_LEN;
  434. num_fragm = (payload_len + per_fragm - 1) / per_fragm;
  435. frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
  436. if (!frags)
  437. goto fail;
  438. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  439. seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
  440. pos = first->data + hdrlen + per_fragm;
  441. left = payload_len - per_fragm;
  442. for (i = 0; i < num_fragm - 1; i++) {
  443. struct ieee80211_hdr *fhdr;
  444. size_t copylen;
  445. if (left <= 0)
  446. goto fail;
  447. /* reserve enough extra head and tail room for possible
  448. * encryption */
  449. frag = frags[i] =
  450. dev_alloc_skb(tx->local->hw.extra_tx_headroom +
  451. frag_threshold +
  452. IEEE80211_ENCRYPT_HEADROOM +
  453. IEEE80211_ENCRYPT_TAILROOM);
  454. if (!frag)
  455. goto fail;
  456. /* Make sure that all fragments use the same priority so
  457. * that they end up using the same TX queue */
  458. frag->priority = first->priority;
  459. skb_reserve(frag, tx->local->hw.extra_tx_headroom +
  460. IEEE80211_ENCRYPT_HEADROOM);
  461. fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
  462. memcpy(fhdr, first->data, hdrlen);
  463. if (i == num_fragm - 2)
  464. fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
  465. fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
  466. copylen = left > per_fragm ? per_fragm : left;
  467. memcpy(skb_put(frag, copylen), pos, copylen);
  468. pos += copylen;
  469. left -= copylen;
  470. }
  471. skb_trim(first, hdrlen + per_fragm);
  472. tx->u.tx.num_extra_frag = num_fragm - 1;
  473. tx->u.tx.extra_frag = frags;
  474. return TXRX_CONTINUE;
  475. fail:
  476. printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
  477. if (frags) {
  478. for (i = 0; i < num_fragm - 1; i++)
  479. if (frags[i])
  480. dev_kfree_skb(frags[i]);
  481. kfree(frags);
  482. }
  483. I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
  484. return TXRX_DROP;
  485. }
  486. static int wep_encrypt_skb(struct ieee80211_txrx_data *tx, struct sk_buff *skb)
  487. {
  488. if (tx->key->force_sw_encrypt) {
  489. if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
  490. return -1;
  491. } else {
  492. tx->u.tx.control->key_idx = tx->key->hw_key_idx;
  493. if (tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
  494. if (ieee80211_wep_add_iv(tx->local, skb, tx->key) ==
  495. NULL)
  496. return -1;
  497. }
  498. }
  499. return 0;
  500. }
  501. void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
  502. {
  503. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  504. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  505. if (tx->u.tx.extra_frag) {
  506. struct ieee80211_hdr *fhdr;
  507. int i;
  508. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  509. fhdr = (struct ieee80211_hdr *)
  510. tx->u.tx.extra_frag[i]->data;
  511. fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  512. }
  513. }
  514. }
  515. static ieee80211_txrx_result
  516. ieee80211_tx_h_wep_encrypt(struct ieee80211_txrx_data *tx)
  517. {
  518. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  519. u16 fc;
  520. fc = le16_to_cpu(hdr->frame_control);
  521. if (!tx->key || tx->key->alg != ALG_WEP ||
  522. ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  523. ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  524. (fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
  525. return TXRX_CONTINUE;
  526. tx->u.tx.control->iv_len = WEP_IV_LEN;
  527. tx->u.tx.control->icv_len = WEP_ICV_LEN;
  528. ieee80211_tx_set_iswep(tx);
  529. if (wep_encrypt_skb(tx, tx->skb) < 0) {
  530. I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
  531. return TXRX_DROP;
  532. }
  533. if (tx->u.tx.extra_frag) {
  534. int i;
  535. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  536. if (wep_encrypt_skb(tx, tx->u.tx.extra_frag[i]) < 0) {
  537. I802_DEBUG_INC(tx->local->
  538. tx_handlers_drop_wep);
  539. return TXRX_DROP;
  540. }
  541. }
  542. }
  543. return TXRX_CONTINUE;
  544. }
  545. static int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
  546. int rate, int erp, int short_preamble)
  547. {
  548. int dur;
  549. /* calculate duration (in microseconds, rounded up to next higher
  550. * integer if it includes a fractional microsecond) to send frame of
  551. * len bytes (does not include FCS) at the given rate. Duration will
  552. * also include SIFS.
  553. *
  554. * rate is in 100 kbps, so divident is multiplied by 10 in the
  555. * DIV_ROUND_UP() operations.
  556. */
  557. if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
  558. local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
  559. /*
  560. * OFDM:
  561. *
  562. * N_DBPS = DATARATE x 4
  563. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  564. * (16 = SIGNAL time, 6 = tail bits)
  565. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  566. *
  567. * T_SYM = 4 usec
  568. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  569. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  570. * signal ext = 6 usec
  571. */
  572. /* FIX: Atheros Turbo may have different (shorter) duration? */
  573. dur = 16; /* SIFS + signal ext */
  574. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  575. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  576. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  577. 4 * rate); /* T_SYM x N_SYM */
  578. } else {
  579. /*
  580. * 802.11b or 802.11g with 802.11b compatibility:
  581. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  582. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  583. *
  584. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  585. * aSIFSTime = 10 usec
  586. * aPreambleLength = 144 usec or 72 usec with short preamble
  587. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  588. */
  589. dur = 10; /* aSIFSTime = 10 usec */
  590. dur += short_preamble ? (72 + 24) : (144 + 48);
  591. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  592. }
  593. return dur;
  594. }
  595. /* Exported duration function for driver use */
  596. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  597. size_t frame_len, int rate)
  598. {
  599. struct ieee80211_local *local = hw_to_local(hw);
  600. u16 dur;
  601. int erp;
  602. erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
  603. dur = ieee80211_frame_duration(local, frame_len, rate,
  604. erp, local->short_preamble);
  605. return cpu_to_le16(dur);
  606. }
  607. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  608. static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
  609. int next_frag_len)
  610. {
  611. int rate, mrate, erp, dur, i;
  612. struct ieee80211_rate *txrate = tx->u.tx.rate;
  613. struct ieee80211_local *local = tx->local;
  614. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  615. erp = txrate->flags & IEEE80211_RATE_ERP;
  616. /*
  617. * data and mgmt (except PS Poll):
  618. * - during CFP: 32768
  619. * - during contention period:
  620. * if addr1 is group address: 0
  621. * if more fragments = 0 and addr1 is individual address: time to
  622. * transmit one ACK plus SIFS
  623. * if more fragments = 1 and addr1 is individual address: time to
  624. * transmit next fragment plus 2 x ACK plus 3 x SIFS
  625. *
  626. * IEEE 802.11, 9.6:
  627. * - control response frame (CTS or ACK) shall be transmitted using the
  628. * same rate as the immediately previous frame in the frame exchange
  629. * sequence, if this rate belongs to the PHY mandatory rates, or else
  630. * at the highest possible rate belonging to the PHY rates in the
  631. * BSSBasicRateSet
  632. */
  633. if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
  634. /* TODO: These control frames are not currently sent by
  635. * 80211.o, but should they be implemented, this function
  636. * needs to be updated to support duration field calculation.
  637. *
  638. * RTS: time needed to transmit pending data/mgmt frame plus
  639. * one CTS frame plus one ACK frame plus 3 x SIFS
  640. * CTS: duration of immediately previous RTS minus time
  641. * required to transmit CTS and its SIFS
  642. * ACK: 0 if immediately previous directed data/mgmt had
  643. * more=0, with more=1 duration in ACK frame is duration
  644. * from previous frame minus time needed to transmit ACK
  645. * and its SIFS
  646. * PS Poll: BIT(15) | BIT(14) | aid
  647. */
  648. return 0;
  649. }
  650. /* data/mgmt */
  651. if (0 /* FIX: data/mgmt during CFP */)
  652. return 32768;
  653. if (group_addr) /* Group address as the destination - no ACK */
  654. return 0;
  655. /* Individual destination address:
  656. * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
  657. * CTS and ACK frames shall be transmitted using the highest rate in
  658. * basic rate set that is less than or equal to the rate of the
  659. * immediately previous frame and that is using the same modulation
  660. * (CCK or OFDM). If no basic rate set matches with these requirements,
  661. * the highest mandatory rate of the PHY that is less than or equal to
  662. * the rate of the previous frame is used.
  663. * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
  664. */
  665. rate = -1;
  666. mrate = 10; /* use 1 Mbps if everything fails */
  667. for (i = 0; i < mode->num_rates; i++) {
  668. struct ieee80211_rate *r = &mode->rates[i];
  669. if (r->rate > txrate->rate)
  670. break;
  671. if (IEEE80211_RATE_MODULATION(txrate->flags) !=
  672. IEEE80211_RATE_MODULATION(r->flags))
  673. continue;
  674. if (r->flags & IEEE80211_RATE_BASIC)
  675. rate = r->rate;
  676. else if (r->flags & IEEE80211_RATE_MANDATORY)
  677. mrate = r->rate;
  678. }
  679. if (rate == -1) {
  680. /* No matching basic rate found; use highest suitable mandatory
  681. * PHY rate */
  682. rate = mrate;
  683. }
  684. /* Time needed to transmit ACK
  685. * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
  686. * to closest integer */
  687. dur = ieee80211_frame_duration(local, 10, rate, erp,
  688. local->short_preamble);
  689. if (next_frag_len) {
  690. /* Frame is fragmented: duration increases with time needed to
  691. * transmit next fragment plus ACK and 2 x SIFS. */
  692. dur *= 2; /* ACK + SIFS */
  693. /* next fragment */
  694. dur += ieee80211_frame_duration(local, next_frag_len,
  695. txrate->rate, erp,
  696. local->short_preamble);
  697. }
  698. return dur;
  699. }
  700. static ieee80211_txrx_result
  701. ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
  702. {
  703. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  704. u16 dur;
  705. struct ieee80211_tx_control *control = tx->u.tx.control;
  706. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  707. if (!is_multicast_ether_addr(hdr->addr1)) {
  708. if (tx->skb->len + FCS_LEN > tx->local->rts_threshold &&
  709. tx->local->rts_threshold < IEEE80211_MAX_RTS_THRESHOLD) {
  710. control->flags |= IEEE80211_TXCTL_USE_RTS_CTS;
  711. control->retry_limit =
  712. tx->local->long_retry_limit;
  713. } else {
  714. control->retry_limit =
  715. tx->local->short_retry_limit;
  716. }
  717. } else {
  718. control->retry_limit = 1;
  719. }
  720. if (tx->fragmented) {
  721. /* Do not use multiple retry rates when sending fragmented
  722. * frames.
  723. * TODO: The last fragment could still use multiple retry
  724. * rates. */
  725. control->alt_retry_rate = -1;
  726. }
  727. /* Use CTS protection for unicast frames sent using extended rates if
  728. * there are associated non-ERP stations and RTS/CTS is not configured
  729. * for the frame. */
  730. if (mode->mode == MODE_IEEE80211G &&
  731. (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
  732. tx->u.tx.unicast &&
  733. tx->local->cts_protect_erp_frames &&
  734. !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
  735. control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
  736. /* Setup duration field for the first fragment of the frame. Duration
  737. * for remaining fragments will be updated when they are being sent
  738. * to low-level driver in ieee80211_tx(). */
  739. dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
  740. tx->fragmented ? tx->u.tx.extra_frag[0]->len :
  741. 0);
  742. hdr->duration_id = cpu_to_le16(dur);
  743. if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
  744. (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
  745. struct ieee80211_rate *rate;
  746. /* Do not use multiple retry rates when using RTS/CTS */
  747. control->alt_retry_rate = -1;
  748. /* Use min(data rate, max base rate) as CTS/RTS rate */
  749. rate = tx->u.tx.rate;
  750. while (rate > mode->rates &&
  751. !(rate->flags & IEEE80211_RATE_BASIC))
  752. rate--;
  753. control->rts_cts_rate = rate->val;
  754. control->rts_rate = rate;
  755. }
  756. if (tx->sta) {
  757. tx->sta->tx_packets++;
  758. tx->sta->tx_fragments++;
  759. tx->sta->tx_bytes += tx->skb->len;
  760. if (tx->u.tx.extra_frag) {
  761. int i;
  762. tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
  763. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  764. tx->sta->tx_bytes +=
  765. tx->u.tx.extra_frag[i]->len;
  766. }
  767. }
  768. }
  769. return TXRX_CONTINUE;
  770. }
  771. static ieee80211_txrx_result
  772. ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
  773. {
  774. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  775. struct sk_buff *skb = tx->skb;
  776. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  777. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  778. u32 sta_flags;
  779. if (unlikely(tx->local->sta_scanning != 0) &&
  780. ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  781. (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
  782. return TXRX_DROP;
  783. if (tx->u.tx.ps_buffered)
  784. return TXRX_CONTINUE;
  785. sta_flags = tx->sta ? tx->sta->flags : 0;
  786. if (likely(tx->u.tx.unicast)) {
  787. if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
  788. tx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
  789. (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
  790. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  791. printk(KERN_DEBUG "%s: dropped data frame to not "
  792. "associated station " MAC_FMT "\n",
  793. tx->dev->name, MAC_ARG(hdr->addr1));
  794. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  795. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  796. return TXRX_DROP;
  797. }
  798. } else {
  799. if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  800. tx->local->num_sta == 0 &&
  801. !tx->local->allow_broadcast_always &&
  802. tx->sdata->type != IEEE80211_IF_TYPE_IBSS)) {
  803. /*
  804. * No associated STAs - no need to send multicast
  805. * frames.
  806. */
  807. return TXRX_DROP;
  808. }
  809. return TXRX_CONTINUE;
  810. }
  811. if (unlikely(!tx->u.tx.mgmt_interface && tx->sdata->ieee802_1x &&
  812. !(sta_flags & WLAN_STA_AUTHORIZED))) {
  813. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  814. printk(KERN_DEBUG "%s: dropped frame to " MAC_FMT
  815. " (unauthorized port)\n", tx->dev->name,
  816. MAC_ARG(hdr->addr1));
  817. #endif
  818. I802_DEBUG_INC(tx->local->tx_handlers_drop_unauth_port);
  819. return TXRX_DROP;
  820. }
  821. return TXRX_CONTINUE;
  822. }
  823. static ieee80211_txrx_result
  824. ieee80211_tx_h_sequence(struct ieee80211_txrx_data *tx)
  825. {
  826. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  827. if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
  828. ieee80211_include_sequence(tx->sdata, hdr);
  829. return TXRX_CONTINUE;
  830. }
  831. /* This function is called whenever the AP is about to exceed the maximum limit
  832. * of buffered frames for power saving STAs. This situation should not really
  833. * happen often during normal operation, so dropping the oldest buffered packet
  834. * from each queue should be OK to make some room for new frames. */
  835. static void purge_old_ps_buffers(struct ieee80211_local *local)
  836. {
  837. int total = 0, purged = 0;
  838. struct sk_buff *skb;
  839. struct ieee80211_sub_if_data *sdata;
  840. struct sta_info *sta;
  841. read_lock(&local->sub_if_lock);
  842. list_for_each_entry(sdata, &local->sub_if_list, list) {
  843. struct ieee80211_if_ap *ap;
  844. if (sdata->dev == local->mdev ||
  845. sdata->type != IEEE80211_IF_TYPE_AP)
  846. continue;
  847. ap = &sdata->u.ap;
  848. skb = skb_dequeue(&ap->ps_bc_buf);
  849. if (skb) {
  850. purged++;
  851. dev_kfree_skb(skb);
  852. }
  853. total += skb_queue_len(&ap->ps_bc_buf);
  854. }
  855. read_unlock(&local->sub_if_lock);
  856. spin_lock_bh(&local->sta_lock);
  857. list_for_each_entry(sta, &local->sta_list, list) {
  858. skb = skb_dequeue(&sta->ps_tx_buf);
  859. if (skb) {
  860. purged++;
  861. dev_kfree_skb(skb);
  862. }
  863. total += skb_queue_len(&sta->ps_tx_buf);
  864. }
  865. spin_unlock_bh(&local->sta_lock);
  866. local->total_ps_buffered = total;
  867. printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
  868. local->mdev->name, purged);
  869. }
  870. static inline ieee80211_txrx_result
  871. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
  872. {
  873. /* broadcast/multicast frame */
  874. /* If any of the associated stations is in power save mode,
  875. * the frame is buffered to be sent after DTIM beacon frame */
  876. if ((tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) &&
  877. tx->sdata->type != IEEE80211_IF_TYPE_WDS &&
  878. tx->sdata->bss && atomic_read(&tx->sdata->bss->num_sta_ps) &&
  879. !(tx->fc & IEEE80211_FCTL_ORDER)) {
  880. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  881. purge_old_ps_buffers(tx->local);
  882. if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
  883. AP_MAX_BC_BUFFER) {
  884. if (net_ratelimit()) {
  885. printk(KERN_DEBUG "%s: BC TX buffer full - "
  886. "dropping the oldest frame\n",
  887. tx->dev->name);
  888. }
  889. dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
  890. } else
  891. tx->local->total_ps_buffered++;
  892. skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
  893. return TXRX_QUEUED;
  894. }
  895. return TXRX_CONTINUE;
  896. }
  897. static inline ieee80211_txrx_result
  898. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
  899. {
  900. struct sta_info *sta = tx->sta;
  901. if (unlikely(!sta ||
  902. ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
  903. (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
  904. return TXRX_CONTINUE;
  905. if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
  906. struct ieee80211_tx_packet_data *pkt_data;
  907. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  908. printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS buffer (entries "
  909. "before %d)\n",
  910. MAC_ARG(sta->addr), sta->aid,
  911. skb_queue_len(&sta->ps_tx_buf));
  912. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  913. sta->flags |= WLAN_STA_TIM;
  914. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  915. purge_old_ps_buffers(tx->local);
  916. if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
  917. struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
  918. if (net_ratelimit()) {
  919. printk(KERN_DEBUG "%s: STA " MAC_FMT " TX "
  920. "buffer full - dropping oldest frame\n",
  921. tx->dev->name, MAC_ARG(sta->addr));
  922. }
  923. dev_kfree_skb(old);
  924. } else
  925. tx->local->total_ps_buffered++;
  926. /* Queue frame to be sent after STA sends an PS Poll frame */
  927. if (skb_queue_empty(&sta->ps_tx_buf)) {
  928. if (tx->local->ops->set_tim)
  929. tx->local->ops->set_tim(local_to_hw(tx->local),
  930. sta->aid, 1);
  931. if (tx->sdata->bss)
  932. bss_tim_set(tx->local, tx->sdata->bss, sta->aid);
  933. }
  934. pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
  935. pkt_data->jiffies = jiffies;
  936. skb_queue_tail(&sta->ps_tx_buf, tx->skb);
  937. return TXRX_QUEUED;
  938. }
  939. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  940. else if (unlikely(sta->flags & WLAN_STA_PS)) {
  941. printk(KERN_DEBUG "%s: STA " MAC_FMT " in PS mode, but pspoll "
  942. "set -> send frame\n", tx->dev->name,
  943. MAC_ARG(sta->addr));
  944. }
  945. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  946. sta->pspoll = 0;
  947. return TXRX_CONTINUE;
  948. }
  949. static ieee80211_txrx_result
  950. ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
  951. {
  952. if (unlikely(tx->u.tx.ps_buffered))
  953. return TXRX_CONTINUE;
  954. if (tx->u.tx.unicast)
  955. return ieee80211_tx_h_unicast_ps_buf(tx);
  956. else
  957. return ieee80211_tx_h_multicast_ps_buf(tx);
  958. }
  959. static void inline
  960. __ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  961. struct sk_buff *skb,
  962. struct net_device *dev,
  963. struct ieee80211_tx_control *control)
  964. {
  965. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  966. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  967. int hdrlen;
  968. memset(tx, 0, sizeof(*tx));
  969. tx->skb = skb;
  970. tx->dev = dev; /* use original interface */
  971. tx->local = local;
  972. tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  973. tx->sta = sta_info_get(local, hdr->addr1);
  974. tx->fc = le16_to_cpu(hdr->frame_control);
  975. control->power_level = local->hw.conf.power_level;
  976. tx->u.tx.control = control;
  977. tx->u.tx.unicast = !is_multicast_ether_addr(hdr->addr1);
  978. if (is_multicast_ether_addr(hdr->addr1))
  979. control->flags |= IEEE80211_TXCTL_NO_ACK;
  980. else
  981. control->flags &= ~IEEE80211_TXCTL_NO_ACK;
  982. tx->fragmented = local->fragmentation_threshold <
  983. IEEE80211_MAX_FRAG_THRESHOLD && tx->u.tx.unicast &&
  984. skb->len + FCS_LEN > local->fragmentation_threshold &&
  985. (!local->ops->set_frag_threshold);
  986. if (!tx->sta)
  987. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  988. else if (tx->sta->clear_dst_mask) {
  989. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  990. tx->sta->clear_dst_mask = 0;
  991. }
  992. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  993. if (local->sta_antenna_sel != STA_ANTENNA_SEL_AUTO && tx->sta)
  994. control->antenna_sel_tx = tx->sta->antenna_sel_tx;
  995. hdrlen = ieee80211_get_hdrlen(tx->fc);
  996. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  997. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  998. tx->ethertype = (pos[0] << 8) | pos[1];
  999. }
  1000. control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
  1001. }
  1002. static int inline is_ieee80211_device(struct net_device *dev,
  1003. struct net_device *master)
  1004. {
  1005. return (wdev_priv(dev->ieee80211_ptr) ==
  1006. wdev_priv(master->ieee80211_ptr));
  1007. }
  1008. /* Device in tx->dev has a reference added; use dev_put(tx->dev) when
  1009. * finished with it. */
  1010. static int inline ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  1011. struct sk_buff *skb,
  1012. struct net_device *mdev,
  1013. struct ieee80211_tx_control *control)
  1014. {
  1015. struct ieee80211_tx_packet_data *pkt_data;
  1016. struct net_device *dev;
  1017. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1018. dev = dev_get_by_index(pkt_data->ifindex);
  1019. if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
  1020. dev_put(dev);
  1021. dev = NULL;
  1022. }
  1023. if (unlikely(!dev))
  1024. return -ENODEV;
  1025. __ieee80211_tx_prepare(tx, skb, dev, control);
  1026. return 0;
  1027. }
  1028. static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
  1029. int queue)
  1030. {
  1031. return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  1032. }
  1033. static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
  1034. int queue)
  1035. {
  1036. return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
  1037. }
  1038. #define IEEE80211_TX_OK 0
  1039. #define IEEE80211_TX_AGAIN 1
  1040. #define IEEE80211_TX_FRAG_AGAIN 2
  1041. static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
  1042. struct ieee80211_txrx_data *tx)
  1043. {
  1044. struct ieee80211_tx_control *control = tx->u.tx.control;
  1045. int ret, i;
  1046. if (!ieee80211_qdisc_installed(local->mdev) &&
  1047. __ieee80211_queue_stopped(local, 0)) {
  1048. netif_stop_queue(local->mdev);
  1049. return IEEE80211_TX_AGAIN;
  1050. }
  1051. if (skb) {
  1052. ieee80211_dump_frame(local->mdev->name, "TX to low-level driver", skb);
  1053. ret = local->ops->tx(local_to_hw(local), skb, control);
  1054. if (ret)
  1055. return IEEE80211_TX_AGAIN;
  1056. local->mdev->trans_start = jiffies;
  1057. ieee80211_led_tx(local, 1);
  1058. }
  1059. if (tx->u.tx.extra_frag) {
  1060. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  1061. IEEE80211_TXCTL_USE_CTS_PROTECT |
  1062. IEEE80211_TXCTL_CLEAR_DST_MASK |
  1063. IEEE80211_TXCTL_FIRST_FRAGMENT);
  1064. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  1065. if (!tx->u.tx.extra_frag[i])
  1066. continue;
  1067. if (__ieee80211_queue_stopped(local, control->queue))
  1068. return IEEE80211_TX_FRAG_AGAIN;
  1069. if (i == tx->u.tx.num_extra_frag) {
  1070. control->tx_rate = tx->u.tx.last_frag_hwrate;
  1071. control->rate = tx->u.tx.last_frag_rate;
  1072. if (tx->u.tx.probe_last_frag)
  1073. control->flags |=
  1074. IEEE80211_TXCTL_RATE_CTRL_PROBE;
  1075. else
  1076. control->flags &=
  1077. ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  1078. }
  1079. ieee80211_dump_frame(local->mdev->name,
  1080. "TX to low-level driver",
  1081. tx->u.tx.extra_frag[i]);
  1082. ret = local->ops->tx(local_to_hw(local),
  1083. tx->u.tx.extra_frag[i],
  1084. control);
  1085. if (ret)
  1086. return IEEE80211_TX_FRAG_AGAIN;
  1087. local->mdev->trans_start = jiffies;
  1088. ieee80211_led_tx(local, 1);
  1089. tx->u.tx.extra_frag[i] = NULL;
  1090. }
  1091. kfree(tx->u.tx.extra_frag);
  1092. tx->u.tx.extra_frag = NULL;
  1093. }
  1094. return IEEE80211_TX_OK;
  1095. }
  1096. static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
  1097. struct ieee80211_tx_control *control, int mgmt)
  1098. {
  1099. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1100. struct sta_info *sta;
  1101. ieee80211_tx_handler *handler;
  1102. struct ieee80211_txrx_data tx;
  1103. ieee80211_txrx_result res = TXRX_DROP;
  1104. int ret, i;
  1105. WARN_ON(__ieee80211_queue_pending(local, control->queue));
  1106. if (unlikely(skb->len < 10)) {
  1107. dev_kfree_skb(skb);
  1108. return 0;
  1109. }
  1110. __ieee80211_tx_prepare(&tx, skb, dev, control);
  1111. sta = tx.sta;
  1112. tx.u.tx.mgmt_interface = mgmt;
  1113. tx.u.tx.mode = local->hw.conf.mode;
  1114. for (handler = local->tx_handlers; *handler != NULL; handler++) {
  1115. res = (*handler)(&tx);
  1116. if (res != TXRX_CONTINUE)
  1117. break;
  1118. }
  1119. skb = tx.skb; /* handlers are allowed to change skb */
  1120. if (sta)
  1121. sta_info_put(sta);
  1122. if (unlikely(res == TXRX_DROP)) {
  1123. I802_DEBUG_INC(local->tx_handlers_drop);
  1124. goto drop;
  1125. }
  1126. if (unlikely(res == TXRX_QUEUED)) {
  1127. I802_DEBUG_INC(local->tx_handlers_queued);
  1128. return 0;
  1129. }
  1130. if (tx.u.tx.extra_frag) {
  1131. for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
  1132. int next_len, dur;
  1133. struct ieee80211_hdr *hdr =
  1134. (struct ieee80211_hdr *)
  1135. tx.u.tx.extra_frag[i]->data;
  1136. if (i + 1 < tx.u.tx.num_extra_frag) {
  1137. next_len = tx.u.tx.extra_frag[i + 1]->len;
  1138. } else {
  1139. next_len = 0;
  1140. tx.u.tx.rate = tx.u.tx.last_frag_rate;
  1141. tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
  1142. }
  1143. dur = ieee80211_duration(&tx, 0, next_len);
  1144. hdr->duration_id = cpu_to_le16(dur);
  1145. }
  1146. }
  1147. retry:
  1148. ret = __ieee80211_tx(local, skb, &tx);
  1149. if (ret) {
  1150. struct ieee80211_tx_stored_packet *store =
  1151. &local->pending_packet[control->queue];
  1152. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1153. skb = NULL;
  1154. set_bit(IEEE80211_LINK_STATE_PENDING,
  1155. &local->state[control->queue]);
  1156. smp_mb();
  1157. /* When the driver gets out of buffers during sending of
  1158. * fragments and calls ieee80211_stop_queue, there is
  1159. * a small window between IEEE80211_LINK_STATE_XOFF and
  1160. * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
  1161. * gets available in that window (i.e. driver calls
  1162. * ieee80211_wake_queue), we would end up with ieee80211_tx
  1163. * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
  1164. * continuing transmitting here when that situation is
  1165. * possible to have happened. */
  1166. if (!__ieee80211_queue_stopped(local, control->queue)) {
  1167. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1168. &local->state[control->queue]);
  1169. goto retry;
  1170. }
  1171. memcpy(&store->control, control,
  1172. sizeof(struct ieee80211_tx_control));
  1173. store->skb = skb;
  1174. store->extra_frag = tx.u.tx.extra_frag;
  1175. store->num_extra_frag = tx.u.tx.num_extra_frag;
  1176. store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
  1177. store->last_frag_rate = tx.u.tx.last_frag_rate;
  1178. store->last_frag_rate_ctrl_probe = tx.u.tx.probe_last_frag;
  1179. }
  1180. return 0;
  1181. drop:
  1182. if (skb)
  1183. dev_kfree_skb(skb);
  1184. for (i = 0; i < tx.u.tx.num_extra_frag; i++)
  1185. if (tx.u.tx.extra_frag[i])
  1186. dev_kfree_skb(tx.u.tx.extra_frag[i]);
  1187. kfree(tx.u.tx.extra_frag);
  1188. return 0;
  1189. }
  1190. static void ieee80211_tx_pending(unsigned long data)
  1191. {
  1192. struct ieee80211_local *local = (struct ieee80211_local *)data;
  1193. struct net_device *dev = local->mdev;
  1194. struct ieee80211_tx_stored_packet *store;
  1195. struct ieee80211_txrx_data tx;
  1196. int i, ret, reschedule = 0;
  1197. netif_tx_lock_bh(dev);
  1198. for (i = 0; i < local->hw.queues; i++) {
  1199. if (__ieee80211_queue_stopped(local, i))
  1200. continue;
  1201. if (!__ieee80211_queue_pending(local, i)) {
  1202. reschedule = 1;
  1203. continue;
  1204. }
  1205. store = &local->pending_packet[i];
  1206. tx.u.tx.control = &store->control;
  1207. tx.u.tx.extra_frag = store->extra_frag;
  1208. tx.u.tx.num_extra_frag = store->num_extra_frag;
  1209. tx.u.tx.last_frag_hwrate = store->last_frag_hwrate;
  1210. tx.u.tx.last_frag_rate = store->last_frag_rate;
  1211. tx.u.tx.probe_last_frag = store->last_frag_rate_ctrl_probe;
  1212. ret = __ieee80211_tx(local, store->skb, &tx);
  1213. if (ret) {
  1214. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1215. store->skb = NULL;
  1216. } else {
  1217. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1218. &local->state[i]);
  1219. reschedule = 1;
  1220. }
  1221. }
  1222. netif_tx_unlock_bh(dev);
  1223. if (reschedule) {
  1224. if (!ieee80211_qdisc_installed(dev)) {
  1225. if (!__ieee80211_queue_stopped(local, 0))
  1226. netif_wake_queue(dev);
  1227. } else
  1228. netif_schedule(dev);
  1229. }
  1230. }
  1231. static void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  1232. {
  1233. int i, j;
  1234. struct ieee80211_tx_stored_packet *store;
  1235. for (i = 0; i < local->hw.queues; i++) {
  1236. if (!__ieee80211_queue_pending(local, i))
  1237. continue;
  1238. store = &local->pending_packet[i];
  1239. kfree_skb(store->skb);
  1240. for (j = 0; j < store->num_extra_frag; j++)
  1241. kfree_skb(store->extra_frag[j]);
  1242. kfree(store->extra_frag);
  1243. clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
  1244. }
  1245. }
  1246. static int ieee80211_master_start_xmit(struct sk_buff *skb,
  1247. struct net_device *dev)
  1248. {
  1249. struct ieee80211_tx_control control;
  1250. struct ieee80211_tx_packet_data *pkt_data;
  1251. struct net_device *odev = NULL;
  1252. struct ieee80211_sub_if_data *osdata;
  1253. int headroom;
  1254. int ret;
  1255. /*
  1256. * copy control out of the skb so other people can use skb->cb
  1257. */
  1258. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1259. memset(&control, 0, sizeof(struct ieee80211_tx_control));
  1260. if (pkt_data->ifindex)
  1261. odev = dev_get_by_index(pkt_data->ifindex);
  1262. if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
  1263. dev_put(odev);
  1264. odev = NULL;
  1265. }
  1266. if (unlikely(!odev)) {
  1267. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1268. printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
  1269. "originating device\n", dev->name);
  1270. #endif
  1271. dev_kfree_skb(skb);
  1272. return 0;
  1273. }
  1274. osdata = IEEE80211_DEV_TO_SUB_IF(odev);
  1275. headroom = osdata->local->hw.extra_tx_headroom +
  1276. IEEE80211_ENCRYPT_HEADROOM;
  1277. if (skb_headroom(skb) < headroom) {
  1278. if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
  1279. dev_kfree_skb(skb);
  1280. return 0;
  1281. }
  1282. }
  1283. control.ifindex = odev->ifindex;
  1284. control.type = osdata->type;
  1285. if (pkt_data->req_tx_status)
  1286. control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
  1287. if (pkt_data->do_not_encrypt)
  1288. control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  1289. if (pkt_data->requeue)
  1290. control.flags |= IEEE80211_TXCTL_REQUEUE;
  1291. control.queue = pkt_data->queue;
  1292. ret = ieee80211_tx(odev, skb, &control,
  1293. control.type == IEEE80211_IF_TYPE_MGMT);
  1294. dev_put(odev);
  1295. return ret;
  1296. }
  1297. /**
  1298. * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
  1299. * subinterfaces (wlan#, WDS, and VLAN interfaces)
  1300. * @skb: packet to be sent
  1301. * @dev: incoming interface
  1302. *
  1303. * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
  1304. * not be freed, and caller is responsible for either retrying later or freeing
  1305. * skb).
  1306. *
  1307. * This function takes in an Ethernet header and encapsulates it with suitable
  1308. * IEEE 802.11 header based on which interface the packet is coming in. The
  1309. * encapsulated packet will then be passed to master interface, wlan#.11, for
  1310. * transmission (through low-level driver).
  1311. */
  1312. static int ieee80211_subif_start_xmit(struct sk_buff *skb,
  1313. struct net_device *dev)
  1314. {
  1315. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1316. struct ieee80211_tx_packet_data *pkt_data;
  1317. struct ieee80211_sub_if_data *sdata;
  1318. int ret = 1, head_need;
  1319. u16 ethertype, hdrlen, fc;
  1320. struct ieee80211_hdr hdr;
  1321. const u8 *encaps_data;
  1322. int encaps_len, skip_header_bytes;
  1323. int nh_pos, h_pos, no_encrypt = 0;
  1324. struct sta_info *sta;
  1325. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1326. if (unlikely(skb->len < ETH_HLEN)) {
  1327. printk(KERN_DEBUG "%s: short skb (len=%d)\n",
  1328. dev->name, skb->len);
  1329. ret = 0;
  1330. goto fail;
  1331. }
  1332. nh_pos = skb_network_header(skb) - skb->data;
  1333. h_pos = skb_transport_header(skb) - skb->data;
  1334. /* convert Ethernet header to proper 802.11 header (based on
  1335. * operation mode) */
  1336. ethertype = (skb->data[12] << 8) | skb->data[13];
  1337. /* TODO: handling for 802.1x authorized/unauthorized port */
  1338. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  1339. if (likely(sdata->type == IEEE80211_IF_TYPE_AP ||
  1340. sdata->type == IEEE80211_IF_TYPE_VLAN)) {
  1341. fc |= IEEE80211_FCTL_FROMDS;
  1342. /* DA BSSID SA */
  1343. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1344. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1345. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1346. hdrlen = 24;
  1347. } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
  1348. fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
  1349. /* RA TA DA SA */
  1350. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  1351. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1352. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1353. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1354. hdrlen = 30;
  1355. } else if (sdata->type == IEEE80211_IF_TYPE_STA) {
  1356. fc |= IEEE80211_FCTL_TODS;
  1357. /* BSSID SA DA */
  1358. memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
  1359. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1360. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1361. hdrlen = 24;
  1362. } else if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
  1363. /* DA SA BSSID */
  1364. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1365. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1366. memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
  1367. hdrlen = 24;
  1368. } else {
  1369. ret = 0;
  1370. goto fail;
  1371. }
  1372. /* receiver is QoS enabled, use a QoS type frame */
  1373. sta = sta_info_get(local, hdr.addr1);
  1374. if (sta) {
  1375. if (sta->flags & WLAN_STA_WME) {
  1376. fc |= IEEE80211_STYPE_QOS_DATA;
  1377. hdrlen += 2;
  1378. }
  1379. sta_info_put(sta);
  1380. }
  1381. hdr.frame_control = cpu_to_le16(fc);
  1382. hdr.duration_id = 0;
  1383. hdr.seq_ctrl = 0;
  1384. skip_header_bytes = ETH_HLEN;
  1385. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1386. encaps_data = bridge_tunnel_header;
  1387. encaps_len = sizeof(bridge_tunnel_header);
  1388. skip_header_bytes -= 2;
  1389. } else if (ethertype >= 0x600) {
  1390. encaps_data = rfc1042_header;
  1391. encaps_len = sizeof(rfc1042_header);
  1392. skip_header_bytes -= 2;
  1393. } else {
  1394. encaps_data = NULL;
  1395. encaps_len = 0;
  1396. }
  1397. skb_pull(skb, skip_header_bytes);
  1398. nh_pos -= skip_header_bytes;
  1399. h_pos -= skip_header_bytes;
  1400. /* TODO: implement support for fragments so that there is no need to
  1401. * reallocate and copy payload; it might be enough to support one
  1402. * extra fragment that would be copied in the beginning of the frame
  1403. * data.. anyway, it would be nice to include this into skb structure
  1404. * somehow
  1405. *
  1406. * There are few options for this:
  1407. * use skb->cb as an extra space for 802.11 header
  1408. * allocate new buffer if not enough headroom
  1409. * make sure that there is enough headroom in every skb by increasing
  1410. * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
  1411. * alloc_skb() (net/core/skbuff.c)
  1412. */
  1413. head_need = hdrlen + encaps_len + local->hw.extra_tx_headroom;
  1414. head_need -= skb_headroom(skb);
  1415. /* We are going to modify skb data, so make a copy of it if happens to
  1416. * be cloned. This could happen, e.g., with Linux bridge code passing
  1417. * us broadcast frames. */
  1418. if (head_need > 0 || skb_cloned(skb)) {
  1419. #if 0
  1420. printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
  1421. "of headroom\n", dev->name, head_need);
  1422. #endif
  1423. if (skb_cloned(skb))
  1424. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1425. else
  1426. I802_DEBUG_INC(local->tx_expand_skb_head);
  1427. /* Since we have to reallocate the buffer, make sure that there
  1428. * is enough room for possible WEP IV/ICV and TKIP (8 bytes
  1429. * before payload and 12 after). */
  1430. if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
  1431. 12, GFP_ATOMIC)) {
  1432. printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
  1433. "\n", dev->name);
  1434. goto fail;
  1435. }
  1436. }
  1437. if (encaps_data) {
  1438. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1439. nh_pos += encaps_len;
  1440. h_pos += encaps_len;
  1441. }
  1442. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1443. nh_pos += hdrlen;
  1444. h_pos += hdrlen;
  1445. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1446. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1447. pkt_data->ifindex = sdata->dev->ifindex;
  1448. pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
  1449. pkt_data->do_not_encrypt = no_encrypt;
  1450. skb->dev = local->mdev;
  1451. sdata->stats.tx_packets++;
  1452. sdata->stats.tx_bytes += skb->len;
  1453. /* Update skb pointers to various headers since this modified frame
  1454. * is going to go through Linux networking code that may potentially
  1455. * need things like pointer to IP header. */
  1456. skb_set_mac_header(skb, 0);
  1457. skb_set_network_header(skb, nh_pos);
  1458. skb_set_transport_header(skb, h_pos);
  1459. dev->trans_start = jiffies;
  1460. dev_queue_xmit(skb);
  1461. return 0;
  1462. fail:
  1463. if (!ret)
  1464. dev_kfree_skb(skb);
  1465. return ret;
  1466. }
  1467. /*
  1468. * This is the transmit routine for the 802.11 type interfaces
  1469. * called by upper layers of the linux networking
  1470. * stack when it has a frame to transmit
  1471. */
  1472. static int
  1473. ieee80211_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1474. {
  1475. struct ieee80211_sub_if_data *sdata;
  1476. struct ieee80211_tx_packet_data *pkt_data;
  1477. struct ieee80211_hdr *hdr;
  1478. u16 fc;
  1479. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1480. if (skb->len < 10) {
  1481. dev_kfree_skb(skb);
  1482. return 0;
  1483. }
  1484. if (skb_headroom(skb) < sdata->local->hw.extra_tx_headroom) {
  1485. if (pskb_expand_head(skb,
  1486. sdata->local->hw.extra_tx_headroom, 0, GFP_ATOMIC)) {
  1487. dev_kfree_skb(skb);
  1488. return 0;
  1489. }
  1490. }
  1491. hdr = (struct ieee80211_hdr *) skb->data;
  1492. fc = le16_to_cpu(hdr->frame_control);
  1493. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  1494. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1495. pkt_data->ifindex = sdata->dev->ifindex;
  1496. pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
  1497. skb->priority = 20; /* use hardcoded priority for mgmt TX queue */
  1498. skb->dev = sdata->local->mdev;
  1499. /*
  1500. * We're using the protocol field of the the frame control header
  1501. * to request TX callback for hostapd. BIT(1) is checked.
  1502. */
  1503. if ((fc & BIT(1)) == BIT(1)) {
  1504. pkt_data->req_tx_status = 1;
  1505. fc &= ~BIT(1);
  1506. hdr->frame_control = cpu_to_le16(fc);
  1507. }
  1508. pkt_data->do_not_encrypt = !(fc & IEEE80211_FCTL_PROTECTED);
  1509. sdata->stats.tx_packets++;
  1510. sdata->stats.tx_bytes += skb->len;
  1511. dev_queue_xmit(skb);
  1512. return 0;
  1513. }
  1514. static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
  1515. struct ieee80211_if_ap *bss,
  1516. struct sk_buff *skb)
  1517. {
  1518. u8 *pos, *tim;
  1519. int aid0 = 0;
  1520. int i, have_bits = 0, n1, n2;
  1521. /* Generate bitmap for TIM only if there are any STAs in power save
  1522. * mode. */
  1523. spin_lock_bh(&local->sta_lock);
  1524. if (atomic_read(&bss->num_sta_ps) > 0)
  1525. /* in the hope that this is faster than
  1526. * checking byte-for-byte */
  1527. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1528. IEEE80211_MAX_AID+1);
  1529. if (bss->dtim_count == 0)
  1530. bss->dtim_count = bss->dtim_period - 1;
  1531. else
  1532. bss->dtim_count--;
  1533. tim = pos = (u8 *) skb_put(skb, 6);
  1534. *pos++ = WLAN_EID_TIM;
  1535. *pos++ = 4;
  1536. *pos++ = bss->dtim_count;
  1537. *pos++ = bss->dtim_period;
  1538. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1539. aid0 = 1;
  1540. if (have_bits) {
  1541. /* Find largest even number N1 so that bits numbered 1 through
  1542. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1543. * (N2 + 1) x 8 through 2007 are 0. */
  1544. n1 = 0;
  1545. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1546. if (bss->tim[i]) {
  1547. n1 = i & 0xfe;
  1548. break;
  1549. }
  1550. }
  1551. n2 = n1;
  1552. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1553. if (bss->tim[i]) {
  1554. n2 = i;
  1555. break;
  1556. }
  1557. }
  1558. /* Bitmap control */
  1559. *pos++ = n1 | aid0;
  1560. /* Part Virt Bitmap */
  1561. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1562. tim[1] = n2 - n1 + 4;
  1563. skb_put(skb, n2 - n1);
  1564. } else {
  1565. *pos++ = aid0; /* Bitmap control */
  1566. *pos++ = 0; /* Part Virt Bitmap */
  1567. }
  1568. spin_unlock_bh(&local->sta_lock);
  1569. }
  1570. struct sk_buff * ieee80211_beacon_get(struct ieee80211_hw *hw, int if_id,
  1571. struct ieee80211_tx_control *control)
  1572. {
  1573. struct ieee80211_local *local = hw_to_local(hw);
  1574. struct sk_buff *skb;
  1575. struct net_device *bdev;
  1576. struct ieee80211_sub_if_data *sdata = NULL;
  1577. struct ieee80211_if_ap *ap = NULL;
  1578. struct ieee80211_rate *rate;
  1579. struct rate_control_extra extra;
  1580. u8 *b_head, *b_tail;
  1581. int bh_len, bt_len;
  1582. bdev = dev_get_by_index(if_id);
  1583. if (bdev) {
  1584. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  1585. ap = &sdata->u.ap;
  1586. dev_put(bdev);
  1587. }
  1588. if (!ap || sdata->type != IEEE80211_IF_TYPE_AP ||
  1589. !ap->beacon_head) {
  1590. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1591. if (net_ratelimit())
  1592. printk(KERN_DEBUG "no beacon data avail for idx=%d "
  1593. "(%s)\n", if_id, bdev ? bdev->name : "N/A");
  1594. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1595. return NULL;
  1596. }
  1597. /* Assume we are generating the normal beacon locally */
  1598. b_head = ap->beacon_head;
  1599. b_tail = ap->beacon_tail;
  1600. bh_len = ap->beacon_head_len;
  1601. bt_len = ap->beacon_tail_len;
  1602. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  1603. bh_len + bt_len + 256 /* maximum TIM len */);
  1604. if (!skb)
  1605. return NULL;
  1606. skb_reserve(skb, local->hw.extra_tx_headroom);
  1607. memcpy(skb_put(skb, bh_len), b_head, bh_len);
  1608. ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
  1609. ieee80211_beacon_add_tim(local, ap, skb);
  1610. if (b_tail) {
  1611. memcpy(skb_put(skb, bt_len), b_tail, bt_len);
  1612. }
  1613. if (control) {
  1614. memset(&extra, 0, sizeof(extra));
  1615. extra.mode = local->oper_hw_mode;
  1616. rate = rate_control_get_rate(local, local->mdev, skb, &extra);
  1617. if (!rate) {
  1618. if (net_ratelimit()) {
  1619. printk(KERN_DEBUG "%s: ieee80211_beacon_get: no rate "
  1620. "found\n", local->mdev->name);
  1621. }
  1622. dev_kfree_skb(skb);
  1623. return NULL;
  1624. }
  1625. control->tx_rate = (local->short_preamble &&
  1626. (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
  1627. rate->val2 : rate->val;
  1628. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  1629. control->power_level = local->hw.conf.power_level;
  1630. control->flags |= IEEE80211_TXCTL_NO_ACK;
  1631. control->retry_limit = 1;
  1632. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  1633. }
  1634. ap->num_beacons++;
  1635. return skb;
  1636. }
  1637. EXPORT_SYMBOL(ieee80211_beacon_get);
  1638. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  1639. size_t frame_len,
  1640. const struct ieee80211_tx_control *frame_txctl)
  1641. {
  1642. struct ieee80211_local *local = hw_to_local(hw);
  1643. struct ieee80211_rate *rate;
  1644. int short_preamble = local->short_preamble;
  1645. int erp;
  1646. u16 dur;
  1647. rate = frame_txctl->rts_rate;
  1648. erp = !!(rate->flags & IEEE80211_RATE_ERP);
  1649. /* CTS duration */
  1650. dur = ieee80211_frame_duration(local, 10, rate->rate,
  1651. erp, short_preamble);
  1652. /* Data frame duration */
  1653. dur += ieee80211_frame_duration(local, frame_len, rate->rate,
  1654. erp, short_preamble);
  1655. /* ACK duration */
  1656. dur += ieee80211_frame_duration(local, 10, rate->rate,
  1657. erp, short_preamble);
  1658. return cpu_to_le16(dur);
  1659. }
  1660. EXPORT_SYMBOL(ieee80211_rts_duration);
  1661. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  1662. size_t frame_len,
  1663. const struct ieee80211_tx_control *frame_txctl)
  1664. {
  1665. struct ieee80211_local *local = hw_to_local(hw);
  1666. struct ieee80211_rate *rate;
  1667. int short_preamble = local->short_preamble;
  1668. int erp;
  1669. u16 dur;
  1670. rate = frame_txctl->rts_rate;
  1671. erp = !!(rate->flags & IEEE80211_RATE_ERP);
  1672. /* Data frame duration */
  1673. dur = ieee80211_frame_duration(local, frame_len, rate->rate,
  1674. erp, short_preamble);
  1675. if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
  1676. /* ACK duration */
  1677. dur += ieee80211_frame_duration(local, 10, rate->rate,
  1678. erp, short_preamble);
  1679. }
  1680. return cpu_to_le16(dur);
  1681. }
  1682. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  1683. void ieee80211_rts_get(struct ieee80211_hw *hw,
  1684. const void *frame, size_t frame_len,
  1685. const struct ieee80211_tx_control *frame_txctl,
  1686. struct ieee80211_rts *rts)
  1687. {
  1688. const struct ieee80211_hdr *hdr = frame;
  1689. u16 fctl;
  1690. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
  1691. rts->frame_control = cpu_to_le16(fctl);
  1692. rts->duration = ieee80211_rts_duration(hw, frame_len, frame_txctl);
  1693. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  1694. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  1695. }
  1696. EXPORT_SYMBOL(ieee80211_rts_get);
  1697. void ieee80211_ctstoself_get(struct ieee80211_hw *hw,
  1698. const void *frame, size_t frame_len,
  1699. const struct ieee80211_tx_control *frame_txctl,
  1700. struct ieee80211_cts *cts)
  1701. {
  1702. const struct ieee80211_hdr *hdr = frame;
  1703. u16 fctl;
  1704. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
  1705. cts->frame_control = cpu_to_le16(fctl);
  1706. cts->duration = ieee80211_ctstoself_duration(hw, frame_len, frame_txctl);
  1707. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  1708. }
  1709. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  1710. struct sk_buff *
  1711. ieee80211_get_buffered_bc(struct ieee80211_hw *hw, int if_id,
  1712. struct ieee80211_tx_control *control)
  1713. {
  1714. struct ieee80211_local *local = hw_to_local(hw);
  1715. struct sk_buff *skb;
  1716. struct sta_info *sta;
  1717. ieee80211_tx_handler *handler;
  1718. struct ieee80211_txrx_data tx;
  1719. ieee80211_txrx_result res = TXRX_DROP;
  1720. struct net_device *bdev;
  1721. struct ieee80211_sub_if_data *sdata;
  1722. struct ieee80211_if_ap *bss = NULL;
  1723. bdev = dev_get_by_index(if_id);
  1724. if (bdev) {
  1725. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  1726. bss = &sdata->u.ap;
  1727. dev_put(bdev);
  1728. }
  1729. if (!bss || sdata->type != IEEE80211_IF_TYPE_AP || !bss->beacon_head)
  1730. return NULL;
  1731. if (bss->dtim_count != 0)
  1732. return NULL; /* send buffered bc/mc only after DTIM beacon */
  1733. memset(control, 0, sizeof(*control));
  1734. while (1) {
  1735. skb = skb_dequeue(&bss->ps_bc_buf);
  1736. if (!skb)
  1737. return NULL;
  1738. local->total_ps_buffered--;
  1739. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  1740. struct ieee80211_hdr *hdr =
  1741. (struct ieee80211_hdr *) skb->data;
  1742. /* more buffered multicast/broadcast frames ==> set
  1743. * MoreData flag in IEEE 802.11 header to inform PS
  1744. * STAs */
  1745. hdr->frame_control |=
  1746. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1747. }
  1748. if (ieee80211_tx_prepare(&tx, skb, local->mdev, control) == 0)
  1749. break;
  1750. dev_kfree_skb_any(skb);
  1751. }
  1752. sta = tx.sta;
  1753. tx.u.tx.ps_buffered = 1;
  1754. for (handler = local->tx_handlers; *handler != NULL; handler++) {
  1755. res = (*handler)(&tx);
  1756. if (res == TXRX_DROP || res == TXRX_QUEUED)
  1757. break;
  1758. }
  1759. dev_put(tx.dev);
  1760. skb = tx.skb; /* handlers are allowed to change skb */
  1761. if (res == TXRX_DROP) {
  1762. I802_DEBUG_INC(local->tx_handlers_drop);
  1763. dev_kfree_skb(skb);
  1764. skb = NULL;
  1765. } else if (res == TXRX_QUEUED) {
  1766. I802_DEBUG_INC(local->tx_handlers_queued);
  1767. skb = NULL;
  1768. }
  1769. if (sta)
  1770. sta_info_put(sta);
  1771. return skb;
  1772. }
  1773. EXPORT_SYMBOL(ieee80211_get_buffered_bc);
  1774. static int __ieee80211_if_config(struct net_device *dev,
  1775. struct sk_buff *beacon,
  1776. struct ieee80211_tx_control *control)
  1777. {
  1778. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1779. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1780. struct ieee80211_if_conf conf;
  1781. static u8 scan_bssid[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1782. if (!local->ops->config_interface || !netif_running(dev))
  1783. return 0;
  1784. memset(&conf, 0, sizeof(conf));
  1785. conf.type = sdata->type;
  1786. if (sdata->type == IEEE80211_IF_TYPE_STA ||
  1787. sdata->type == IEEE80211_IF_TYPE_IBSS) {
  1788. if (local->sta_scanning &&
  1789. local->scan_dev == dev)
  1790. conf.bssid = scan_bssid;
  1791. else
  1792. conf.bssid = sdata->u.sta.bssid;
  1793. conf.ssid = sdata->u.sta.ssid;
  1794. conf.ssid_len = sdata->u.sta.ssid_len;
  1795. conf.generic_elem = sdata->u.sta.extra_ie;
  1796. conf.generic_elem_len = sdata->u.sta.extra_ie_len;
  1797. } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
  1798. conf.ssid = sdata->u.ap.ssid;
  1799. conf.ssid_len = sdata->u.ap.ssid_len;
  1800. conf.generic_elem = sdata->u.ap.generic_elem;
  1801. conf.generic_elem_len = sdata->u.ap.generic_elem_len;
  1802. conf.beacon = beacon;
  1803. conf.beacon_control = control;
  1804. }
  1805. return local->ops->config_interface(local_to_hw(local),
  1806. dev->ifindex, &conf);
  1807. }
  1808. int ieee80211_if_config(struct net_device *dev)
  1809. {
  1810. return __ieee80211_if_config(dev, NULL, NULL);
  1811. }
  1812. int ieee80211_if_config_beacon(struct net_device *dev)
  1813. {
  1814. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1815. struct ieee80211_tx_control control;
  1816. struct sk_buff *skb;
  1817. if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
  1818. return 0;
  1819. skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
  1820. if (!skb)
  1821. return -ENOMEM;
  1822. return __ieee80211_if_config(dev, skb, &control);
  1823. }
  1824. int ieee80211_hw_config(struct ieee80211_local *local)
  1825. {
  1826. struct ieee80211_hw_mode *mode;
  1827. struct ieee80211_channel *chan;
  1828. int ret = 0;
  1829. if (local->sta_scanning) {
  1830. chan = local->scan_channel;
  1831. mode = local->scan_hw_mode;
  1832. } else {
  1833. chan = local->oper_channel;
  1834. mode = local->oper_hw_mode;
  1835. }
  1836. local->hw.conf.channel = chan->chan;
  1837. local->hw.conf.channel_val = chan->val;
  1838. local->hw.conf.power_level = chan->power_level;
  1839. local->hw.conf.freq = chan->freq;
  1840. local->hw.conf.phymode = mode->mode;
  1841. local->hw.conf.antenna_max = chan->antenna_max;
  1842. local->hw.conf.chan = chan;
  1843. local->hw.conf.mode = mode;
  1844. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1845. printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
  1846. "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
  1847. local->hw.conf.phymode);
  1848. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1849. if (local->ops->config)
  1850. ret = local->ops->config(local_to_hw(local), &local->hw.conf);
  1851. return ret;
  1852. }
  1853. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  1854. {
  1855. /* FIX: what would be proper limits for MTU?
  1856. * This interface uses 802.3 frames. */
  1857. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
  1858. printk(KERN_WARNING "%s: invalid MTU %d\n",
  1859. dev->name, new_mtu);
  1860. return -EINVAL;
  1861. }
  1862. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1863. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  1864. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1865. dev->mtu = new_mtu;
  1866. return 0;
  1867. }
  1868. static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
  1869. {
  1870. /* FIX: what would be proper limits for MTU?
  1871. * This interface uses 802.11 frames. */
  1872. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
  1873. printk(KERN_WARNING "%s: invalid MTU %d\n",
  1874. dev->name, new_mtu);
  1875. return -EINVAL;
  1876. }
  1877. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1878. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  1879. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1880. dev->mtu = new_mtu;
  1881. return 0;
  1882. }
  1883. enum netif_tx_lock_class {
  1884. TX_LOCK_NORMAL,
  1885. TX_LOCK_MASTER,
  1886. };
  1887. static inline void netif_tx_lock_nested(struct net_device *dev, int subclass)
  1888. {
  1889. spin_lock_nested(&dev->_xmit_lock, subclass);
  1890. dev->xmit_lock_owner = smp_processor_id();
  1891. }
  1892. static void ieee80211_set_multicast_list(struct net_device *dev)
  1893. {
  1894. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1895. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1896. unsigned short flags;
  1897. netif_tx_lock_nested(local->mdev, TX_LOCK_MASTER);
  1898. if (((dev->flags & IFF_ALLMULTI) != 0) ^ (sdata->allmulti != 0)) {
  1899. if (sdata->allmulti) {
  1900. sdata->allmulti = 0;
  1901. local->iff_allmultis--;
  1902. } else {
  1903. sdata->allmulti = 1;
  1904. local->iff_allmultis++;
  1905. }
  1906. }
  1907. if (((dev->flags & IFF_PROMISC) != 0) ^ (sdata->promisc != 0)) {
  1908. if (sdata->promisc) {
  1909. sdata->promisc = 0;
  1910. local->iff_promiscs--;
  1911. } else {
  1912. sdata->promisc = 1;
  1913. local->iff_promiscs++;
  1914. }
  1915. }
  1916. if (dev->mc_count != sdata->mc_count) {
  1917. local->mc_count = local->mc_count - sdata->mc_count +
  1918. dev->mc_count;
  1919. sdata->mc_count = dev->mc_count;
  1920. }
  1921. if (local->ops->set_multicast_list) {
  1922. flags = local->mdev->flags;
  1923. if (local->iff_allmultis)
  1924. flags |= IFF_ALLMULTI;
  1925. if (local->iff_promiscs)
  1926. flags |= IFF_PROMISC;
  1927. read_lock(&local->sub_if_lock);
  1928. local->ops->set_multicast_list(local_to_hw(local), flags,
  1929. local->mc_count);
  1930. read_unlock(&local->sub_if_lock);
  1931. }
  1932. netif_tx_unlock(local->mdev);
  1933. }
  1934. struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
  1935. struct dev_mc_list *prev,
  1936. void **ptr)
  1937. {
  1938. struct ieee80211_local *local = hw_to_local(hw);
  1939. struct ieee80211_sub_if_data *sdata = *ptr;
  1940. struct dev_mc_list *mc;
  1941. if (!prev) {
  1942. WARN_ON(sdata);
  1943. sdata = NULL;
  1944. }
  1945. if (!prev || !prev->next) {
  1946. if (sdata)
  1947. sdata = list_entry(sdata->list.next,
  1948. struct ieee80211_sub_if_data, list);
  1949. else
  1950. sdata = list_entry(local->sub_if_list.next,
  1951. struct ieee80211_sub_if_data, list);
  1952. if (&sdata->list != &local->sub_if_list)
  1953. mc = sdata->dev->mc_list;
  1954. else
  1955. mc = NULL;
  1956. } else
  1957. mc = prev->next;
  1958. *ptr = sdata;
  1959. return mc;
  1960. }
  1961. EXPORT_SYMBOL(ieee80211_get_mc_list_item);
  1962. static struct net_device_stats *ieee80211_get_stats(struct net_device *dev)
  1963. {
  1964. struct ieee80211_sub_if_data *sdata;
  1965. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1966. return &(sdata->stats);
  1967. }
  1968. static void ieee80211_if_shutdown(struct net_device *dev)
  1969. {
  1970. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1971. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1972. ASSERT_RTNL();
  1973. switch (sdata->type) {
  1974. case IEEE80211_IF_TYPE_STA:
  1975. case IEEE80211_IF_TYPE_IBSS:
  1976. sdata->u.sta.state = IEEE80211_DISABLED;
  1977. del_timer_sync(&sdata->u.sta.timer);
  1978. skb_queue_purge(&sdata->u.sta.skb_queue);
  1979. if (!local->ops->hw_scan &&
  1980. local->scan_dev == sdata->dev) {
  1981. local->sta_scanning = 0;
  1982. cancel_delayed_work(&local->scan_work);
  1983. }
  1984. flush_workqueue(local->hw.workqueue);
  1985. break;
  1986. }
  1987. }
  1988. static inline int identical_mac_addr_allowed(int type1, int type2)
  1989. {
  1990. return (type1 == IEEE80211_IF_TYPE_MNTR ||
  1991. type2 == IEEE80211_IF_TYPE_MNTR ||
  1992. (type1 == IEEE80211_IF_TYPE_AP &&
  1993. type2 == IEEE80211_IF_TYPE_WDS) ||
  1994. (type1 == IEEE80211_IF_TYPE_WDS &&
  1995. (type2 == IEEE80211_IF_TYPE_WDS ||
  1996. type2 == IEEE80211_IF_TYPE_AP)) ||
  1997. (type1 == IEEE80211_IF_TYPE_AP &&
  1998. type2 == IEEE80211_IF_TYPE_VLAN) ||
  1999. (type1 == IEEE80211_IF_TYPE_VLAN &&
  2000. (type2 == IEEE80211_IF_TYPE_AP ||
  2001. type2 == IEEE80211_IF_TYPE_VLAN)));
  2002. }
  2003. static int ieee80211_master_open(struct net_device *dev)
  2004. {
  2005. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2006. struct ieee80211_sub_if_data *sdata;
  2007. int res = -EOPNOTSUPP;
  2008. read_lock(&local->sub_if_lock);
  2009. list_for_each_entry(sdata, &local->sub_if_list, list) {
  2010. if (sdata->dev != dev && netif_running(sdata->dev)) {
  2011. res = 0;
  2012. break;
  2013. }
  2014. }
  2015. read_unlock(&local->sub_if_lock);
  2016. return res;
  2017. }
  2018. static int ieee80211_master_stop(struct net_device *dev)
  2019. {
  2020. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2021. struct ieee80211_sub_if_data *sdata;
  2022. read_lock(&local->sub_if_lock);
  2023. list_for_each_entry(sdata, &local->sub_if_list, list)
  2024. if (sdata->dev != dev && netif_running(sdata->dev))
  2025. dev_close(sdata->dev);
  2026. read_unlock(&local->sub_if_lock);
  2027. return 0;
  2028. }
  2029. static int ieee80211_mgmt_open(struct net_device *dev)
  2030. {
  2031. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2032. if (!netif_running(local->mdev))
  2033. return -EOPNOTSUPP;
  2034. return 0;
  2035. }
  2036. static int ieee80211_mgmt_stop(struct net_device *dev)
  2037. {
  2038. return 0;
  2039. }
  2040. /* Check if running monitor interfaces should go to a "soft monitor" mode
  2041. * and switch them if necessary. */
  2042. static inline void ieee80211_start_soft_monitor(struct ieee80211_local *local)
  2043. {
  2044. struct ieee80211_if_init_conf conf;
  2045. if (local->open_count && local->open_count == local->monitors &&
  2046. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
  2047. local->ops->remove_interface) {
  2048. conf.if_id = -1;
  2049. conf.type = IEEE80211_IF_TYPE_MNTR;
  2050. conf.mac_addr = NULL;
  2051. local->ops->remove_interface(local_to_hw(local), &conf);
  2052. }
  2053. }
  2054. /* Check if running monitor interfaces should go to a "hard monitor" mode
  2055. * and switch them if necessary. */
  2056. static void ieee80211_start_hard_monitor(struct ieee80211_local *local)
  2057. {
  2058. struct ieee80211_if_init_conf conf;
  2059. if (local->open_count && local->open_count == local->monitors &&
  2060. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
  2061. local->ops->add_interface) {
  2062. conf.if_id = -1;
  2063. conf.type = IEEE80211_IF_TYPE_MNTR;
  2064. conf.mac_addr = NULL;
  2065. local->ops->add_interface(local_to_hw(local), &conf);
  2066. }
  2067. }
  2068. static int ieee80211_open(struct net_device *dev)
  2069. {
  2070. struct ieee80211_sub_if_data *sdata, *nsdata;
  2071. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2072. struct ieee80211_if_init_conf conf;
  2073. int res;
  2074. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  2075. read_lock(&local->sub_if_lock);
  2076. list_for_each_entry(nsdata, &local->sub_if_list, list) {
  2077. struct net_device *ndev = nsdata->dev;
  2078. if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
  2079. compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
  2080. !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
  2081. read_unlock(&local->sub_if_lock);
  2082. return -ENOTUNIQ;
  2083. }
  2084. }
  2085. read_unlock(&local->sub_if_lock);
  2086. if (sdata->type == IEEE80211_IF_TYPE_WDS &&
  2087. is_zero_ether_addr(sdata->u.wds.remote_addr))
  2088. return -ENOLINK;
  2089. if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
  2090. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  2091. /* run the interface in a "soft monitor" mode */
  2092. local->monitors++;
  2093. local->open_count++;
  2094. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  2095. return 0;
  2096. }
  2097. ieee80211_start_soft_monitor(local);
  2098. if (local->ops->add_interface) {
  2099. conf.if_id = dev->ifindex;
  2100. conf.type = sdata->type;
  2101. conf.mac_addr = dev->dev_addr;
  2102. res = local->ops->add_interface(local_to_hw(local), &conf);
  2103. if (res) {
  2104. if (sdata->type == IEEE80211_IF_TYPE_MNTR)
  2105. ieee80211_start_hard_monitor(local);
  2106. return res;
  2107. }
  2108. } else {
  2109. if (sdata->type != IEEE80211_IF_TYPE_STA)
  2110. return -EOPNOTSUPP;
  2111. if (local->open_count > 0)
  2112. return -ENOBUFS;
  2113. }
  2114. if (local->open_count == 0) {
  2115. res = 0;
  2116. tasklet_enable(&local->tx_pending_tasklet);
  2117. tasklet_enable(&local->tasklet);
  2118. if (local->ops->open)
  2119. res = local->ops->open(local_to_hw(local));
  2120. if (res == 0) {
  2121. res = dev_open(local->mdev);
  2122. if (res) {
  2123. if (local->ops->stop)
  2124. local->ops->stop(local_to_hw(local));
  2125. } else {
  2126. res = ieee80211_hw_config(local);
  2127. if (res && local->ops->stop)
  2128. local->ops->stop(local_to_hw(local));
  2129. else if (!res && local->apdev)
  2130. dev_open(local->apdev);
  2131. }
  2132. }
  2133. if (res) {
  2134. if (local->ops->remove_interface)
  2135. local->ops->remove_interface(local_to_hw(local),
  2136. &conf);
  2137. return res;
  2138. }
  2139. }
  2140. local->open_count++;
  2141. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  2142. local->monitors++;
  2143. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  2144. } else
  2145. ieee80211_if_config(dev);
  2146. if (sdata->type == IEEE80211_IF_TYPE_STA &&
  2147. !local->user_space_mlme)
  2148. netif_carrier_off(dev);
  2149. else
  2150. netif_carrier_on(dev);
  2151. netif_start_queue(dev);
  2152. return 0;
  2153. }
  2154. static int ieee80211_stop(struct net_device *dev)
  2155. {
  2156. struct ieee80211_sub_if_data *sdata;
  2157. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2158. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  2159. if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
  2160. local->open_count > 1 &&
  2161. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  2162. /* remove "soft monitor" interface */
  2163. local->open_count--;
  2164. local->monitors--;
  2165. if (!local->monitors)
  2166. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  2167. return 0;
  2168. }
  2169. netif_stop_queue(dev);
  2170. ieee80211_if_shutdown(dev);
  2171. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  2172. local->monitors--;
  2173. if (!local->monitors)
  2174. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  2175. }
  2176. local->open_count--;
  2177. if (local->open_count == 0) {
  2178. if (netif_running(local->mdev))
  2179. dev_close(local->mdev);
  2180. if (local->apdev)
  2181. dev_close(local->apdev);
  2182. if (local->ops->stop)
  2183. local->ops->stop(local_to_hw(local));
  2184. tasklet_disable(&local->tx_pending_tasklet);
  2185. tasklet_disable(&local->tasklet);
  2186. }
  2187. if (local->ops->remove_interface) {
  2188. struct ieee80211_if_init_conf conf;
  2189. conf.if_id = dev->ifindex;
  2190. conf.type = sdata->type;
  2191. conf.mac_addr = dev->dev_addr;
  2192. local->ops->remove_interface(local_to_hw(local), &conf);
  2193. }
  2194. ieee80211_start_hard_monitor(local);
  2195. return 0;
  2196. }
  2197. static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
  2198. {
  2199. memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
  2200. return ETH_ALEN;
  2201. }
  2202. static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
  2203. {
  2204. return compare_ether_addr(raddr, addr) == 0 ||
  2205. is_broadcast_ether_addr(raddr);
  2206. }
  2207. static ieee80211_txrx_result
  2208. ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
  2209. {
  2210. struct net_device *dev = rx->dev;
  2211. struct ieee80211_local *local = rx->local;
  2212. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  2213. u16 fc, hdrlen, ethertype;
  2214. u8 *payload;
  2215. u8 dst[ETH_ALEN];
  2216. u8 src[ETH_ALEN];
  2217. struct sk_buff *skb = rx->skb, *skb2;
  2218. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  2219. fc = rx->fc;
  2220. if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
  2221. return TXRX_CONTINUE;
  2222. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  2223. return TXRX_DROP;
  2224. hdrlen = ieee80211_get_hdrlen(fc);
  2225. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  2226. * header
  2227. * IEEE 802.11 address fields:
  2228. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  2229. * 0 0 DA SA BSSID n/a
  2230. * 0 1 DA BSSID SA n/a
  2231. * 1 0 BSSID SA DA n/a
  2232. * 1 1 RA TA DA SA
  2233. */
  2234. switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  2235. case IEEE80211_FCTL_TODS:
  2236. /* BSSID SA DA */
  2237. memcpy(dst, hdr->addr3, ETH_ALEN);
  2238. memcpy(src, hdr->addr2, ETH_ALEN);
  2239. if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
  2240. sdata->type != IEEE80211_IF_TYPE_VLAN)) {
  2241. printk(KERN_DEBUG "%s: dropped ToDS frame (BSSID="
  2242. MAC_FMT " SA=" MAC_FMT " DA=" MAC_FMT ")\n",
  2243. dev->name, MAC_ARG(hdr->addr1),
  2244. MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3));
  2245. return TXRX_DROP;
  2246. }
  2247. break;
  2248. case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  2249. /* RA TA DA SA */
  2250. memcpy(dst, hdr->addr3, ETH_ALEN);
  2251. memcpy(src, hdr->addr4, ETH_ALEN);
  2252. if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
  2253. printk(KERN_DEBUG "%s: dropped FromDS&ToDS frame (RA="
  2254. MAC_FMT " TA=" MAC_FMT " DA=" MAC_FMT " SA="
  2255. MAC_FMT ")\n",
  2256. rx->dev->name, MAC_ARG(hdr->addr1),
  2257. MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3),
  2258. MAC_ARG(hdr->addr4));
  2259. return TXRX_DROP;
  2260. }
  2261. break;
  2262. case IEEE80211_FCTL_FROMDS:
  2263. /* DA BSSID SA */
  2264. memcpy(dst, hdr->addr1, ETH_ALEN);
  2265. memcpy(src, hdr->addr3, ETH_ALEN);
  2266. if (sdata->type != IEEE80211_IF_TYPE_STA) {
  2267. return TXRX_DROP;
  2268. }
  2269. break;
  2270. case 0:
  2271. /* DA SA BSSID */
  2272. memcpy(dst, hdr->addr1, ETH_ALEN);
  2273. memcpy(src, hdr->addr2, ETH_ALEN);
  2274. if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
  2275. if (net_ratelimit()) {
  2276. printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
  2277. MAC_FMT " SA=" MAC_FMT " BSSID=" MAC_FMT
  2278. ")\n",
  2279. dev->name, MAC_ARG(hdr->addr1),
  2280. MAC_ARG(hdr->addr2),
  2281. MAC_ARG(hdr->addr3));
  2282. }
  2283. return TXRX_DROP;
  2284. }
  2285. break;
  2286. }
  2287. payload = skb->data + hdrlen;
  2288. if (unlikely(skb->len - hdrlen < 8)) {
  2289. if (net_ratelimit()) {
  2290. printk(KERN_DEBUG "%s: RX too short data frame "
  2291. "payload\n", dev->name);
  2292. }
  2293. return TXRX_DROP;
  2294. }
  2295. ethertype = (payload[6] << 8) | payload[7];
  2296. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  2297. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  2298. compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
  2299. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  2300. * replace EtherType */
  2301. skb_pull(skb, hdrlen + 6);
  2302. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  2303. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  2304. } else {
  2305. struct ethhdr *ehdr;
  2306. __be16 len;
  2307. skb_pull(skb, hdrlen);
  2308. len = htons(skb->len);
  2309. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  2310. memcpy(ehdr->h_dest, dst, ETH_ALEN);
  2311. memcpy(ehdr->h_source, src, ETH_ALEN);
  2312. ehdr->h_proto = len;
  2313. }
  2314. skb->dev = dev;
  2315. skb2 = NULL;
  2316. sdata->stats.rx_packets++;
  2317. sdata->stats.rx_bytes += skb->len;
  2318. if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
  2319. || sdata->type == IEEE80211_IF_TYPE_VLAN) && rx->u.rx.ra_match) {
  2320. if (is_multicast_ether_addr(skb->data)) {
  2321. /* send multicast frames both to higher layers in
  2322. * local net stack and back to the wireless media */
  2323. skb2 = skb_copy(skb, GFP_ATOMIC);
  2324. if (!skb2)
  2325. printk(KERN_DEBUG "%s: failed to clone "
  2326. "multicast frame\n", dev->name);
  2327. } else {
  2328. struct sta_info *dsta;
  2329. dsta = sta_info_get(local, skb->data);
  2330. if (dsta && !dsta->dev) {
  2331. printk(KERN_DEBUG "Station with null dev "
  2332. "structure!\n");
  2333. } else if (dsta && dsta->dev == dev) {
  2334. /* Destination station is associated to this
  2335. * AP, so send the frame directly to it and
  2336. * do not pass the frame to local net stack.
  2337. */
  2338. skb2 = skb;
  2339. skb = NULL;
  2340. }
  2341. if (dsta)
  2342. sta_info_put(dsta);
  2343. }
  2344. }
  2345. if (skb) {
  2346. /* deliver to local stack */
  2347. skb->protocol = eth_type_trans(skb, dev);
  2348. memset(skb->cb, 0, sizeof(skb->cb));
  2349. netif_rx(skb);
  2350. }
  2351. if (skb2) {
  2352. /* send to wireless media */
  2353. skb2->protocol = __constant_htons(ETH_P_802_3);
  2354. skb_set_network_header(skb2, 0);
  2355. skb_set_mac_header(skb2, 0);
  2356. dev_queue_xmit(skb2);
  2357. }
  2358. return TXRX_QUEUED;
  2359. }
  2360. static struct ieee80211_rate *
  2361. ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
  2362. {
  2363. struct ieee80211_hw_mode *mode;
  2364. int r;
  2365. list_for_each_entry(mode, &local->modes_list, list) {
  2366. if (mode->mode != phymode)
  2367. continue;
  2368. for (r = 0; r < mode->num_rates; r++) {
  2369. struct ieee80211_rate *rate = &mode->rates[r];
  2370. if (rate->val == hw_rate ||
  2371. (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
  2372. rate->val2 == hw_rate))
  2373. return rate;
  2374. }
  2375. }
  2376. return NULL;
  2377. }
  2378. static void
  2379. ieee80211_fill_frame_info(struct ieee80211_local *local,
  2380. struct ieee80211_frame_info *fi,
  2381. struct ieee80211_rx_status *status)
  2382. {
  2383. if (status) {
  2384. struct timespec ts;
  2385. struct ieee80211_rate *rate;
  2386. jiffies_to_timespec(jiffies, &ts);
  2387. fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
  2388. ts.tv_nsec / 1000);
  2389. fi->mactime = cpu_to_be64(status->mactime);
  2390. switch (status->phymode) {
  2391. case MODE_IEEE80211A:
  2392. fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
  2393. break;
  2394. case MODE_IEEE80211B:
  2395. fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
  2396. break;
  2397. case MODE_IEEE80211G:
  2398. fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
  2399. break;
  2400. case MODE_ATHEROS_TURBO:
  2401. fi->phytype =
  2402. htonl(ieee80211_phytype_dsss_dot11_turbo);
  2403. break;
  2404. default:
  2405. fi->phytype = htonl(0xAAAAAAAA);
  2406. break;
  2407. }
  2408. fi->channel = htonl(status->channel);
  2409. rate = ieee80211_get_rate(local, status->phymode,
  2410. status->rate);
  2411. if (rate) {
  2412. fi->datarate = htonl(rate->rate);
  2413. if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
  2414. if (status->rate == rate->val)
  2415. fi->preamble = htonl(2); /* long */
  2416. else if (status->rate == rate->val2)
  2417. fi->preamble = htonl(1); /* short */
  2418. } else
  2419. fi->preamble = htonl(0);
  2420. } else {
  2421. fi->datarate = htonl(0);
  2422. fi->preamble = htonl(0);
  2423. }
  2424. fi->antenna = htonl(status->antenna);
  2425. fi->priority = htonl(0xffffffff); /* no clue */
  2426. fi->ssi_type = htonl(ieee80211_ssi_raw);
  2427. fi->ssi_signal = htonl(status->ssi);
  2428. fi->ssi_noise = 0x00000000;
  2429. fi->encoding = 0;
  2430. } else {
  2431. /* clear everything because we really don't know.
  2432. * the msg_type field isn't present on monitor frames
  2433. * so we don't know whether it will be present or not,
  2434. * but it's ok to not clear it since it'll be assigned
  2435. * anyway */
  2436. memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
  2437. fi->ssi_type = htonl(ieee80211_ssi_none);
  2438. }
  2439. fi->version = htonl(IEEE80211_FI_VERSION);
  2440. fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
  2441. }
  2442. /* this routine is actually not just for this, but also
  2443. * for pushing fake 'management' frames into userspace.
  2444. * it shall be replaced by a netlink-based system. */
  2445. void
  2446. ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
  2447. struct ieee80211_rx_status *status, u32 msg_type)
  2448. {
  2449. struct ieee80211_frame_info *fi;
  2450. const size_t hlen = sizeof(struct ieee80211_frame_info);
  2451. struct ieee80211_sub_if_data *sdata;
  2452. skb->dev = local->apdev;
  2453. sdata = IEEE80211_DEV_TO_SUB_IF(local->apdev);
  2454. if (skb_headroom(skb) < hlen) {
  2455. I802_DEBUG_INC(local->rx_expand_skb_head);
  2456. if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
  2457. dev_kfree_skb(skb);
  2458. return;
  2459. }
  2460. }
  2461. fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
  2462. ieee80211_fill_frame_info(local, fi, status);
  2463. fi->msg_type = htonl(msg_type);
  2464. sdata->stats.rx_packets++;
  2465. sdata->stats.rx_bytes += skb->len;
  2466. skb_set_mac_header(skb, 0);
  2467. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2468. skb->pkt_type = PACKET_OTHERHOST;
  2469. skb->protocol = htons(ETH_P_802_2);
  2470. memset(skb->cb, 0, sizeof(skb->cb));
  2471. netif_rx(skb);
  2472. }
  2473. static void
  2474. ieee80211_rx_monitor(struct net_device *dev, struct sk_buff *skb,
  2475. struct ieee80211_rx_status *status)
  2476. {
  2477. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2478. struct ieee80211_sub_if_data *sdata;
  2479. struct ieee80211_rate *rate;
  2480. struct ieee80211_rtap_hdr {
  2481. struct ieee80211_radiotap_header hdr;
  2482. u8 flags;
  2483. u8 rate;
  2484. __le16 chan_freq;
  2485. __le16 chan_flags;
  2486. u8 antsignal;
  2487. } __attribute__ ((packed)) *rthdr;
  2488. skb->dev = dev;
  2489. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  2490. if (status->flag & RX_FLAG_RADIOTAP)
  2491. goto out;
  2492. if (skb_headroom(skb) < sizeof(*rthdr)) {
  2493. I802_DEBUG_INC(local->rx_expand_skb_head);
  2494. if (pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC)) {
  2495. dev_kfree_skb(skb);
  2496. return;
  2497. }
  2498. }
  2499. rthdr = (struct ieee80211_rtap_hdr *) skb_push(skb, sizeof(*rthdr));
  2500. memset(rthdr, 0, sizeof(*rthdr));
  2501. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  2502. rthdr->hdr.it_present =
  2503. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  2504. (1 << IEEE80211_RADIOTAP_RATE) |
  2505. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  2506. (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL));
  2507. rthdr->flags = local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS ?
  2508. IEEE80211_RADIOTAP_F_FCS : 0;
  2509. rate = ieee80211_get_rate(local, status->phymode, status->rate);
  2510. if (rate)
  2511. rthdr->rate = rate->rate / 5;
  2512. rthdr->chan_freq = cpu_to_le16(status->freq);
  2513. rthdr->chan_flags =
  2514. status->phymode == MODE_IEEE80211A ?
  2515. cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ) :
  2516. cpu_to_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ);
  2517. rthdr->antsignal = status->ssi;
  2518. out:
  2519. sdata->stats.rx_packets++;
  2520. sdata->stats.rx_bytes += skb->len;
  2521. skb_set_mac_header(skb, 0);
  2522. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2523. skb->pkt_type = PACKET_OTHERHOST;
  2524. skb->protocol = htons(ETH_P_802_2);
  2525. memset(skb->cb, 0, sizeof(skb->cb));
  2526. netif_rx(skb);
  2527. }
  2528. int ieee80211_radar_status(struct ieee80211_hw *hw, int channel,
  2529. int radar, int radar_type)
  2530. {
  2531. struct sk_buff *skb;
  2532. struct ieee80211_radar_info *msg;
  2533. struct ieee80211_local *local = hw_to_local(hw);
  2534. if (!local->apdev)
  2535. return 0;
  2536. skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
  2537. sizeof(struct ieee80211_radar_info));
  2538. if (!skb)
  2539. return -ENOMEM;
  2540. skb_reserve(skb, sizeof(struct ieee80211_frame_info));
  2541. msg = (struct ieee80211_radar_info *)
  2542. skb_put(skb, sizeof(struct ieee80211_radar_info));
  2543. msg->channel = channel;
  2544. msg->radar = radar;
  2545. msg->radar_type = radar_type;
  2546. ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_radar);
  2547. return 0;
  2548. }
  2549. EXPORT_SYMBOL(ieee80211_radar_status);
  2550. int ieee80211_set_aid_for_sta(struct ieee80211_hw *hw, u8 *peer_address,
  2551. u16 aid)
  2552. {
  2553. struct sk_buff *skb;
  2554. struct ieee80211_msg_set_aid_for_sta *msg;
  2555. struct ieee80211_local *local = hw_to_local(hw);
  2556. /* unlikely because if this event only happens for APs,
  2557. * which require an open ap device. */
  2558. if (unlikely(!local->apdev))
  2559. return 0;
  2560. skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
  2561. sizeof(struct ieee80211_msg_set_aid_for_sta));
  2562. if (!skb)
  2563. return -ENOMEM;
  2564. skb_reserve(skb, sizeof(struct ieee80211_frame_info));
  2565. msg = (struct ieee80211_msg_set_aid_for_sta *)
  2566. skb_put(skb, sizeof(struct ieee80211_msg_set_aid_for_sta));
  2567. memcpy(msg->sta_address, peer_address, ETH_ALEN);
  2568. msg->aid = aid;
  2569. ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_set_aid_for_sta);
  2570. return 0;
  2571. }
  2572. EXPORT_SYMBOL(ieee80211_set_aid_for_sta);
  2573. static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
  2574. {
  2575. struct ieee80211_sub_if_data *sdata;
  2576. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  2577. if (sdata->bss)
  2578. atomic_inc(&sdata->bss->num_sta_ps);
  2579. sta->flags |= WLAN_STA_PS;
  2580. sta->pspoll = 0;
  2581. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  2582. printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d enters power "
  2583. "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
  2584. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  2585. }
  2586. static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
  2587. {
  2588. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2589. struct sk_buff *skb;
  2590. int sent = 0;
  2591. struct ieee80211_sub_if_data *sdata;
  2592. struct ieee80211_tx_packet_data *pkt_data;
  2593. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  2594. if (sdata->bss)
  2595. atomic_dec(&sdata->bss->num_sta_ps);
  2596. sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
  2597. sta->pspoll = 0;
  2598. if (!skb_queue_empty(&sta->ps_tx_buf)) {
  2599. if (local->ops->set_tim)
  2600. local->ops->set_tim(local_to_hw(local), sta->aid, 0);
  2601. if (sdata->bss)
  2602. bss_tim_clear(local, sdata->bss, sta->aid);
  2603. }
  2604. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  2605. printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d exits power "
  2606. "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
  2607. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  2608. /* Send all buffered frames to the station */
  2609. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
  2610. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  2611. sent++;
  2612. pkt_data->requeue = 1;
  2613. dev_queue_xmit(skb);
  2614. }
  2615. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  2616. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  2617. local->total_ps_buffered--;
  2618. sent++;
  2619. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  2620. printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d send PS frame "
  2621. "since STA not sleeping anymore\n", dev->name,
  2622. MAC_ARG(sta->addr), sta->aid);
  2623. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  2624. pkt_data->requeue = 1;
  2625. dev_queue_xmit(skb);
  2626. }
  2627. return sent;
  2628. }
  2629. static ieee80211_txrx_result
  2630. ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
  2631. {
  2632. struct sk_buff *skb;
  2633. int no_pending_pkts;
  2634. if (likely(!rx->sta ||
  2635. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
  2636. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
  2637. !rx->u.rx.ra_match))
  2638. return TXRX_CONTINUE;
  2639. skb = skb_dequeue(&rx->sta->tx_filtered);
  2640. if (!skb) {
  2641. skb = skb_dequeue(&rx->sta->ps_tx_buf);
  2642. if (skb)
  2643. rx->local->total_ps_buffered--;
  2644. }
  2645. no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
  2646. skb_queue_empty(&rx->sta->ps_tx_buf);
  2647. if (skb) {
  2648. struct ieee80211_hdr *hdr =
  2649. (struct ieee80211_hdr *) skb->data;
  2650. /* tell TX path to send one frame even though the STA may
  2651. * still remain is PS mode after this frame exchange */
  2652. rx->sta->pspoll = 1;
  2653. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  2654. printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS Poll (entries "
  2655. "after %d)\n",
  2656. MAC_ARG(rx->sta->addr), rx->sta->aid,
  2657. skb_queue_len(&rx->sta->ps_tx_buf));
  2658. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  2659. /* Use MoreData flag to indicate whether there are more
  2660. * buffered frames for this STA */
  2661. if (no_pending_pkts) {
  2662. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  2663. rx->sta->flags &= ~WLAN_STA_TIM;
  2664. } else
  2665. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  2666. dev_queue_xmit(skb);
  2667. if (no_pending_pkts) {
  2668. if (rx->local->ops->set_tim)
  2669. rx->local->ops->set_tim(local_to_hw(rx->local),
  2670. rx->sta->aid, 0);
  2671. if (rx->sdata->bss)
  2672. bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
  2673. }
  2674. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  2675. } else if (!rx->u.rx.sent_ps_buffered) {
  2676. printk(KERN_DEBUG "%s: STA " MAC_FMT " sent PS Poll even "
  2677. "though there is no buffered frames for it\n",
  2678. rx->dev->name, MAC_ARG(rx->sta->addr));
  2679. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  2680. }
  2681. /* Free PS Poll skb here instead of returning TXRX_DROP that would
  2682. * count as an dropped frame. */
  2683. dev_kfree_skb(rx->skb);
  2684. return TXRX_QUEUED;
  2685. }
  2686. static inline struct ieee80211_fragment_entry *
  2687. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  2688. unsigned int frag, unsigned int seq, int rx_queue,
  2689. struct sk_buff **skb)
  2690. {
  2691. struct ieee80211_fragment_entry *entry;
  2692. int idx;
  2693. idx = sdata->fragment_next;
  2694. entry = &sdata->fragments[sdata->fragment_next++];
  2695. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  2696. sdata->fragment_next = 0;
  2697. if (!skb_queue_empty(&entry->skb_list)) {
  2698. #ifdef CONFIG_MAC80211_DEBUG
  2699. struct ieee80211_hdr *hdr =
  2700. (struct ieee80211_hdr *) entry->skb_list.next->data;
  2701. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  2702. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  2703. "addr1=" MAC_FMT " addr2=" MAC_FMT "\n",
  2704. sdata->dev->name, idx,
  2705. jiffies - entry->first_frag_time, entry->seq,
  2706. entry->last_frag, MAC_ARG(hdr->addr1),
  2707. MAC_ARG(hdr->addr2));
  2708. #endif /* CONFIG_MAC80211_DEBUG */
  2709. __skb_queue_purge(&entry->skb_list);
  2710. }
  2711. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  2712. *skb = NULL;
  2713. entry->first_frag_time = jiffies;
  2714. entry->seq = seq;
  2715. entry->rx_queue = rx_queue;
  2716. entry->last_frag = frag;
  2717. entry->ccmp = 0;
  2718. entry->extra_len = 0;
  2719. return entry;
  2720. }
  2721. static inline struct ieee80211_fragment_entry *
  2722. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  2723. u16 fc, unsigned int frag, unsigned int seq,
  2724. int rx_queue, struct ieee80211_hdr *hdr)
  2725. {
  2726. struct ieee80211_fragment_entry *entry;
  2727. int i, idx;
  2728. idx = sdata->fragment_next;
  2729. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  2730. struct ieee80211_hdr *f_hdr;
  2731. u16 f_fc;
  2732. idx--;
  2733. if (idx < 0)
  2734. idx = IEEE80211_FRAGMENT_MAX - 1;
  2735. entry = &sdata->fragments[idx];
  2736. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  2737. entry->rx_queue != rx_queue ||
  2738. entry->last_frag + 1 != frag)
  2739. continue;
  2740. f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
  2741. f_fc = le16_to_cpu(f_hdr->frame_control);
  2742. if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
  2743. compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
  2744. compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
  2745. continue;
  2746. if (entry->first_frag_time + 2 * HZ < jiffies) {
  2747. __skb_queue_purge(&entry->skb_list);
  2748. continue;
  2749. }
  2750. return entry;
  2751. }
  2752. return NULL;
  2753. }
  2754. static ieee80211_txrx_result
  2755. ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
  2756. {
  2757. struct ieee80211_hdr *hdr;
  2758. u16 sc;
  2759. unsigned int frag, seq;
  2760. struct ieee80211_fragment_entry *entry;
  2761. struct sk_buff *skb;
  2762. hdr = (struct ieee80211_hdr *) rx->skb->data;
  2763. sc = le16_to_cpu(hdr->seq_ctrl);
  2764. frag = sc & IEEE80211_SCTL_FRAG;
  2765. if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
  2766. (rx->skb)->len < 24 ||
  2767. is_multicast_ether_addr(hdr->addr1))) {
  2768. /* not fragmented */
  2769. goto out;
  2770. }
  2771. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  2772. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  2773. if (frag == 0) {
  2774. /* This is the first fragment of a new frame. */
  2775. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  2776. rx->u.rx.queue, &(rx->skb));
  2777. if (rx->key && rx->key->alg == ALG_CCMP &&
  2778. (rx->fc & IEEE80211_FCTL_PROTECTED)) {
  2779. /* Store CCMP PN so that we can verify that the next
  2780. * fragment has a sequential PN value. */
  2781. entry->ccmp = 1;
  2782. memcpy(entry->last_pn,
  2783. rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
  2784. CCMP_PN_LEN);
  2785. }
  2786. return TXRX_QUEUED;
  2787. }
  2788. /* This is a fragment for a frame that should already be pending in
  2789. * fragment cache. Add this fragment to the end of the pending entry.
  2790. */
  2791. entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
  2792. rx->u.rx.queue, hdr);
  2793. if (!entry) {
  2794. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  2795. return TXRX_DROP;
  2796. }
  2797. /* Verify that MPDUs within one MSDU have sequential PN values.
  2798. * (IEEE 802.11i, 8.3.3.4.5) */
  2799. if (entry->ccmp) {
  2800. int i;
  2801. u8 pn[CCMP_PN_LEN], *rpn;
  2802. if (!rx->key || rx->key->alg != ALG_CCMP)
  2803. return TXRX_DROP;
  2804. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  2805. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  2806. pn[i]++;
  2807. if (pn[i])
  2808. break;
  2809. }
  2810. rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
  2811. if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
  2812. printk(KERN_DEBUG "%s: defrag: CCMP PN not sequential"
  2813. " A2=" MAC_FMT " PN=%02x%02x%02x%02x%02x%02x "
  2814. "(expected %02x%02x%02x%02x%02x%02x)\n",
  2815. rx->dev->name, MAC_ARG(hdr->addr2),
  2816. rpn[0], rpn[1], rpn[2], rpn[3], rpn[4], rpn[5],
  2817. pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
  2818. return TXRX_DROP;
  2819. }
  2820. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  2821. }
  2822. skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
  2823. __skb_queue_tail(&entry->skb_list, rx->skb);
  2824. entry->last_frag = frag;
  2825. entry->extra_len += rx->skb->len;
  2826. if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
  2827. rx->skb = NULL;
  2828. return TXRX_QUEUED;
  2829. }
  2830. rx->skb = __skb_dequeue(&entry->skb_list);
  2831. if (skb_tailroom(rx->skb) < entry->extra_len) {
  2832. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  2833. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  2834. GFP_ATOMIC))) {
  2835. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  2836. __skb_queue_purge(&entry->skb_list);
  2837. return TXRX_DROP;
  2838. }
  2839. }
  2840. while ((skb = __skb_dequeue(&entry->skb_list))) {
  2841. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  2842. dev_kfree_skb(skb);
  2843. }
  2844. /* Complete frame has been reassembled - process it now */
  2845. rx->fragmented = 1;
  2846. out:
  2847. if (rx->sta)
  2848. rx->sta->rx_packets++;
  2849. if (is_multicast_ether_addr(hdr->addr1))
  2850. rx->local->dot11MulticastReceivedFrameCount++;
  2851. else
  2852. ieee80211_led_rx(rx->local);
  2853. return TXRX_CONTINUE;
  2854. }
  2855. static ieee80211_txrx_result
  2856. ieee80211_rx_h_monitor(struct ieee80211_txrx_data *rx)
  2857. {
  2858. if (rx->sdata->type == IEEE80211_IF_TYPE_MNTR) {
  2859. ieee80211_rx_monitor(rx->dev, rx->skb, rx->u.rx.status);
  2860. return TXRX_QUEUED;
  2861. }
  2862. if (rx->u.rx.status->flag & RX_FLAG_RADIOTAP)
  2863. skb_pull(rx->skb, ieee80211_get_radiotap_len(rx->skb));
  2864. return TXRX_CONTINUE;
  2865. }
  2866. static ieee80211_txrx_result
  2867. ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
  2868. {
  2869. struct ieee80211_hdr *hdr;
  2870. int always_sta_key;
  2871. hdr = (struct ieee80211_hdr *) rx->skb->data;
  2872. /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
  2873. if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
  2874. if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
  2875. rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
  2876. hdr->seq_ctrl)) {
  2877. if (rx->u.rx.ra_match) {
  2878. rx->local->dot11FrameDuplicateCount++;
  2879. rx->sta->num_duplicates++;
  2880. }
  2881. return TXRX_DROP;
  2882. } else
  2883. rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
  2884. }
  2885. if ((rx->local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) &&
  2886. rx->skb->len > FCS_LEN)
  2887. skb_trim(rx->skb, rx->skb->len - FCS_LEN);
  2888. if (unlikely(rx->skb->len < 16)) {
  2889. I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
  2890. return TXRX_DROP;
  2891. }
  2892. if (!rx->u.rx.ra_match)
  2893. rx->skb->pkt_type = PACKET_OTHERHOST;
  2894. else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
  2895. rx->skb->pkt_type = PACKET_HOST;
  2896. else if (is_multicast_ether_addr(hdr->addr1)) {
  2897. if (is_broadcast_ether_addr(hdr->addr1))
  2898. rx->skb->pkt_type = PACKET_BROADCAST;
  2899. else
  2900. rx->skb->pkt_type = PACKET_MULTICAST;
  2901. } else
  2902. rx->skb->pkt_type = PACKET_OTHERHOST;
  2903. /* Drop disallowed frame classes based on STA auth/assoc state;
  2904. * IEEE 802.11, Chap 5.5.
  2905. *
  2906. * 80211.o does filtering only based on association state, i.e., it
  2907. * drops Class 3 frames from not associated stations. hostapd sends
  2908. * deauth/disassoc frames when needed. In addition, hostapd is
  2909. * responsible for filtering on both auth and assoc states.
  2910. */
  2911. if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
  2912. ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
  2913. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
  2914. rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
  2915. (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
  2916. if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
  2917. !(rx->fc & IEEE80211_FCTL_TODS) &&
  2918. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
  2919. || !rx->u.rx.ra_match) {
  2920. /* Drop IBSS frames and frames for other hosts
  2921. * silently. */
  2922. return TXRX_DROP;
  2923. }
  2924. if (!rx->local->apdev)
  2925. return TXRX_DROP;
  2926. ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
  2927. ieee80211_msg_sta_not_assoc);
  2928. return TXRX_QUEUED;
  2929. }
  2930. if (rx->sdata->type == IEEE80211_IF_TYPE_STA)
  2931. always_sta_key = 0;
  2932. else
  2933. always_sta_key = 1;
  2934. if (rx->sta && rx->sta->key && always_sta_key) {
  2935. rx->key = rx->sta->key;
  2936. } else {
  2937. if (rx->sta && rx->sta->key)
  2938. rx->key = rx->sta->key;
  2939. else
  2940. rx->key = rx->sdata->default_key;
  2941. if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
  2942. rx->fc & IEEE80211_FCTL_PROTECTED) {
  2943. int keyidx = ieee80211_wep_get_keyidx(rx->skb);
  2944. if (keyidx >= 0 && keyidx < NUM_DEFAULT_KEYS &&
  2945. (!rx->sta || !rx->sta->key || keyidx > 0))
  2946. rx->key = rx->sdata->keys[keyidx];
  2947. if (!rx->key) {
  2948. if (!rx->u.rx.ra_match)
  2949. return TXRX_DROP;
  2950. printk(KERN_DEBUG "%s: RX WEP frame with "
  2951. "unknown keyidx %d (A1=" MAC_FMT " A2="
  2952. MAC_FMT " A3=" MAC_FMT ")\n",
  2953. rx->dev->name, keyidx,
  2954. MAC_ARG(hdr->addr1),
  2955. MAC_ARG(hdr->addr2),
  2956. MAC_ARG(hdr->addr3));
  2957. if (!rx->local->apdev)
  2958. return TXRX_DROP;
  2959. ieee80211_rx_mgmt(
  2960. rx->local, rx->skb, rx->u.rx.status,
  2961. ieee80211_msg_wep_frame_unknown_key);
  2962. return TXRX_QUEUED;
  2963. }
  2964. }
  2965. }
  2966. if (rx->fc & IEEE80211_FCTL_PROTECTED && rx->key && rx->u.rx.ra_match) {
  2967. rx->key->tx_rx_count++;
  2968. if (unlikely(rx->local->key_tx_rx_threshold &&
  2969. rx->key->tx_rx_count >
  2970. rx->local->key_tx_rx_threshold)) {
  2971. ieee80211_key_threshold_notify(rx->dev, rx->key,
  2972. rx->sta);
  2973. }
  2974. }
  2975. return TXRX_CONTINUE;
  2976. }
  2977. static ieee80211_txrx_result
  2978. ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
  2979. {
  2980. struct sta_info *sta = rx->sta;
  2981. struct net_device *dev = rx->dev;
  2982. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  2983. if (!sta)
  2984. return TXRX_CONTINUE;
  2985. /* Update last_rx only for IBSS packets which are for the current
  2986. * BSSID to avoid keeping the current IBSS network alive in cases where
  2987. * other STAs are using different BSSID. */
  2988. if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
  2989. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
  2990. if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
  2991. sta->last_rx = jiffies;
  2992. } else
  2993. if (!is_multicast_ether_addr(hdr->addr1) ||
  2994. rx->sdata->type == IEEE80211_IF_TYPE_STA) {
  2995. /* Update last_rx only for unicast frames in order to prevent
  2996. * the Probe Request frames (the only broadcast frames from a
  2997. * STA in infrastructure mode) from keeping a connection alive.
  2998. */
  2999. sta->last_rx = jiffies;
  3000. }
  3001. if (!rx->u.rx.ra_match)
  3002. return TXRX_CONTINUE;
  3003. sta->rx_fragments++;
  3004. sta->rx_bytes += rx->skb->len;
  3005. sta->last_rssi = (sta->last_rssi * 15 +
  3006. rx->u.rx.status->ssi) / 16;
  3007. sta->last_signal = (sta->last_signal * 15 +
  3008. rx->u.rx.status->signal) / 16;
  3009. sta->last_noise = (sta->last_noise * 15 +
  3010. rx->u.rx.status->noise) / 16;
  3011. if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
  3012. /* Change STA power saving mode only in the end of a frame
  3013. * exchange sequence */
  3014. if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
  3015. rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
  3016. else if (!(sta->flags & WLAN_STA_PS) &&
  3017. (rx->fc & IEEE80211_FCTL_PM))
  3018. ap_sta_ps_start(dev, sta);
  3019. }
  3020. /* Drop data::nullfunc frames silently, since they are used only to
  3021. * control station power saving mode. */
  3022. if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  3023. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
  3024. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  3025. /* Update counter and free packet here to avoid counting this
  3026. * as a dropped packed. */
  3027. sta->rx_packets++;
  3028. dev_kfree_skb(rx->skb);
  3029. return TXRX_QUEUED;
  3030. }
  3031. return TXRX_CONTINUE;
  3032. } /* ieee80211_rx_h_sta_process */
  3033. static ieee80211_txrx_result
  3034. ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
  3035. {
  3036. if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
  3037. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
  3038. !rx->key || rx->key->alg != ALG_WEP || !rx->u.rx.ra_match)
  3039. return TXRX_CONTINUE;
  3040. /* Check for weak IVs, if hwaccel did not remove IV from the frame */
  3041. if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) ||
  3042. rx->key->force_sw_encrypt) {
  3043. u8 *iv = ieee80211_wep_is_weak_iv(rx->skb, rx->key);
  3044. if (iv) {
  3045. rx->sta->wep_weak_iv_count++;
  3046. }
  3047. }
  3048. return TXRX_CONTINUE;
  3049. }
  3050. static ieee80211_txrx_result
  3051. ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
  3052. {
  3053. /* If the device handles decryption totally, skip this test */
  3054. if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
  3055. return TXRX_CONTINUE;
  3056. if ((rx->key && rx->key->alg != ALG_WEP) ||
  3057. !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
  3058. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  3059. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  3060. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
  3061. return TXRX_CONTINUE;
  3062. if (!rx->key) {
  3063. printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
  3064. rx->dev->name);
  3065. return TXRX_DROP;
  3066. }
  3067. if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED) ||
  3068. rx->key->force_sw_encrypt) {
  3069. if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
  3070. printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
  3071. "failed\n", rx->dev->name);
  3072. return TXRX_DROP;
  3073. }
  3074. } else if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
  3075. ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
  3076. /* remove ICV */
  3077. skb_trim(rx->skb, rx->skb->len - 4);
  3078. }
  3079. return TXRX_CONTINUE;
  3080. }
  3081. static ieee80211_txrx_result
  3082. ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
  3083. {
  3084. if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
  3085. rx->sdata->type != IEEE80211_IF_TYPE_STA && rx->u.rx.ra_match) {
  3086. /* Pass both encrypted and unencrypted EAPOL frames to user
  3087. * space for processing. */
  3088. if (!rx->local->apdev)
  3089. return TXRX_DROP;
  3090. ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
  3091. ieee80211_msg_normal);
  3092. return TXRX_QUEUED;
  3093. }
  3094. if (unlikely(rx->sdata->ieee802_1x &&
  3095. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  3096. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
  3097. (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
  3098. !ieee80211_is_eapol(rx->skb))) {
  3099. #ifdef CONFIG_MAC80211_DEBUG
  3100. struct ieee80211_hdr *hdr =
  3101. (struct ieee80211_hdr *) rx->skb->data;
  3102. printk(KERN_DEBUG "%s: dropped frame from " MAC_FMT
  3103. " (unauthorized port)\n", rx->dev->name,
  3104. MAC_ARG(hdr->addr2));
  3105. #endif /* CONFIG_MAC80211_DEBUG */
  3106. return TXRX_DROP;
  3107. }
  3108. return TXRX_CONTINUE;
  3109. }
  3110. static ieee80211_txrx_result
  3111. ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
  3112. {
  3113. /* If the device handles decryption totally, skip this test */
  3114. if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
  3115. return TXRX_CONTINUE;
  3116. /* Drop unencrypted frames if key is set. */
  3117. if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
  3118. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  3119. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
  3120. (rx->key || rx->sdata->drop_unencrypted) &&
  3121. (rx->sdata->eapol == 0 ||
  3122. !ieee80211_is_eapol(rx->skb)))) {
  3123. printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
  3124. "encryption\n", rx->dev->name);
  3125. return TXRX_DROP;
  3126. }
  3127. return TXRX_CONTINUE;
  3128. }
  3129. static ieee80211_txrx_result
  3130. ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
  3131. {
  3132. struct ieee80211_sub_if_data *sdata;
  3133. if (!rx->u.rx.ra_match)
  3134. return TXRX_DROP;
  3135. sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
  3136. if ((sdata->type == IEEE80211_IF_TYPE_STA ||
  3137. sdata->type == IEEE80211_IF_TYPE_IBSS) &&
  3138. !rx->local->user_space_mlme) {
  3139. ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
  3140. } else {
  3141. /* Management frames are sent to hostapd for processing */
  3142. if (!rx->local->apdev)
  3143. return TXRX_DROP;
  3144. ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
  3145. ieee80211_msg_normal);
  3146. }
  3147. return TXRX_QUEUED;
  3148. }
  3149. static ieee80211_txrx_result
  3150. ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
  3151. {
  3152. struct ieee80211_local *local = rx->local;
  3153. struct sk_buff *skb = rx->skb;
  3154. if (unlikely(local->sta_scanning != 0)) {
  3155. ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
  3156. return TXRX_QUEUED;
  3157. }
  3158. if (unlikely(rx->u.rx.in_scan)) {
  3159. /* scanning finished during invoking of handlers */
  3160. I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
  3161. return TXRX_DROP;
  3162. }
  3163. return TXRX_CONTINUE;
  3164. }
  3165. static void ieee80211_rx_michael_mic_report(struct net_device *dev,
  3166. struct ieee80211_hdr *hdr,
  3167. struct sta_info *sta,
  3168. struct ieee80211_txrx_data *rx)
  3169. {
  3170. int keyidx, hdrlen;
  3171. hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
  3172. if (rx->skb->len >= hdrlen + 4)
  3173. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  3174. else
  3175. keyidx = -1;
  3176. /* TODO: verify that this is not triggered by fragmented
  3177. * frames (hw does not verify MIC for them). */
  3178. printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
  3179. "failure from " MAC_FMT " to " MAC_FMT " keyidx=%d\n",
  3180. dev->name, MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr1), keyidx);
  3181. if (!sta) {
  3182. /* Some hardware versions seem to generate incorrect
  3183. * Michael MIC reports; ignore them to avoid triggering
  3184. * countermeasures. */
  3185. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  3186. "error for unknown address " MAC_FMT "\n",
  3187. dev->name, MAC_ARG(hdr->addr2));
  3188. goto ignore;
  3189. }
  3190. if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
  3191. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  3192. "error for a frame with no ISWEP flag (src "
  3193. MAC_FMT ")\n", dev->name, MAC_ARG(hdr->addr2));
  3194. goto ignore;
  3195. }
  3196. if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
  3197. rx->sdata->type == IEEE80211_IF_TYPE_AP) {
  3198. keyidx = ieee80211_wep_get_keyidx(rx->skb);
  3199. /* AP with Pairwise keys support should never receive Michael
  3200. * MIC errors for non-zero keyidx because these are reserved
  3201. * for group keys and only the AP is sending real multicast
  3202. * frames in BSS. */
  3203. if (keyidx) {
  3204. printk(KERN_DEBUG "%s: ignored Michael MIC error for "
  3205. "a frame with non-zero keyidx (%d) (src " MAC_FMT
  3206. ")\n", dev->name, keyidx, MAC_ARG(hdr->addr2));
  3207. goto ignore;
  3208. }
  3209. }
  3210. if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  3211. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  3212. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
  3213. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  3214. "error for a frame that cannot be encrypted "
  3215. "(fc=0x%04x) (src " MAC_FMT ")\n",
  3216. dev->name, rx->fc, MAC_ARG(hdr->addr2));
  3217. goto ignore;
  3218. }
  3219. do {
  3220. union iwreq_data wrqu;
  3221. char *buf = kmalloc(128, GFP_ATOMIC);
  3222. if (!buf)
  3223. break;
  3224. /* TODO: needed parameters: count, key type, TSC */
  3225. sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
  3226. "keyid=%d %scast addr=" MAC_FMT ")",
  3227. keyidx, hdr->addr1[0] & 0x01 ? "broad" : "uni",
  3228. MAC_ARG(hdr->addr2));
  3229. memset(&wrqu, 0, sizeof(wrqu));
  3230. wrqu.data.length = strlen(buf);
  3231. wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
  3232. kfree(buf);
  3233. } while (0);
  3234. /* TODO: consider verifying the MIC error report with software
  3235. * implementation if we get too many spurious reports from the
  3236. * hardware. */
  3237. if (!rx->local->apdev)
  3238. goto ignore;
  3239. ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
  3240. ieee80211_msg_michael_mic_failure);
  3241. return;
  3242. ignore:
  3243. dev_kfree_skb(rx->skb);
  3244. rx->skb = NULL;
  3245. }
  3246. static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
  3247. struct ieee80211_local *local,
  3248. ieee80211_rx_handler *handlers,
  3249. struct ieee80211_txrx_data *rx,
  3250. struct sta_info *sta)
  3251. {
  3252. ieee80211_rx_handler *handler;
  3253. ieee80211_txrx_result res = TXRX_DROP;
  3254. for (handler = handlers; *handler != NULL; handler++) {
  3255. res = (*handler)(rx);
  3256. if (res != TXRX_CONTINUE) {
  3257. if (res == TXRX_DROP) {
  3258. I802_DEBUG_INC(local->rx_handlers_drop);
  3259. if (sta)
  3260. sta->rx_dropped++;
  3261. }
  3262. if (res == TXRX_QUEUED)
  3263. I802_DEBUG_INC(local->rx_handlers_queued);
  3264. break;
  3265. }
  3266. }
  3267. if (res == TXRX_DROP) {
  3268. dev_kfree_skb(rx->skb);
  3269. }
  3270. return res;
  3271. }
  3272. static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
  3273. ieee80211_rx_handler *handlers,
  3274. struct ieee80211_txrx_data *rx,
  3275. struct sta_info *sta)
  3276. {
  3277. if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
  3278. TXRX_CONTINUE)
  3279. dev_kfree_skb(rx->skb);
  3280. }
  3281. /*
  3282. * This is the receive path handler. It is called by a low level driver when an
  3283. * 802.11 MPDU is received from the hardware.
  3284. */
  3285. void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
  3286. struct ieee80211_rx_status *status)
  3287. {
  3288. struct ieee80211_local *local = hw_to_local(hw);
  3289. struct ieee80211_sub_if_data *sdata;
  3290. struct sta_info *sta;
  3291. struct ieee80211_hdr *hdr;
  3292. struct ieee80211_txrx_data rx;
  3293. u16 type;
  3294. int multicast;
  3295. int radiotap_len = 0;
  3296. if (status->flag & RX_FLAG_RADIOTAP) {
  3297. radiotap_len = ieee80211_get_radiotap_len(skb);
  3298. skb_pull(skb, radiotap_len);
  3299. }
  3300. hdr = (struct ieee80211_hdr *) skb->data;
  3301. memset(&rx, 0, sizeof(rx));
  3302. rx.skb = skb;
  3303. rx.local = local;
  3304. rx.u.rx.status = status;
  3305. rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
  3306. type = rx.fc & IEEE80211_FCTL_FTYPE;
  3307. if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
  3308. local->dot11ReceivedFragmentCount++;
  3309. multicast = is_multicast_ether_addr(hdr->addr1);
  3310. if (skb->len >= 16)
  3311. sta = rx.sta = sta_info_get(local, hdr->addr2);
  3312. else
  3313. sta = rx.sta = NULL;
  3314. if (sta) {
  3315. rx.dev = sta->dev;
  3316. rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
  3317. }
  3318. if ((status->flag & RX_FLAG_MMIC_ERROR)) {
  3319. ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
  3320. goto end;
  3321. }
  3322. if (unlikely(local->sta_scanning))
  3323. rx.u.rx.in_scan = 1;
  3324. if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
  3325. sta) != TXRX_CONTINUE)
  3326. goto end;
  3327. skb = rx.skb;
  3328. skb_push(skb, radiotap_len);
  3329. if (sta && !sta->assoc_ap && !(sta->flags & WLAN_STA_WDS) &&
  3330. !local->iff_promiscs && !multicast) {
  3331. rx.u.rx.ra_match = 1;
  3332. ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
  3333. sta);
  3334. } else {
  3335. struct ieee80211_sub_if_data *prev = NULL;
  3336. struct sk_buff *skb_new;
  3337. u8 *bssid = ieee80211_get_bssid(hdr, skb->len - radiotap_len);
  3338. read_lock(&local->sub_if_lock);
  3339. list_for_each_entry(sdata, &local->sub_if_list, list) {
  3340. rx.u.rx.ra_match = 1;
  3341. switch (sdata->type) {
  3342. case IEEE80211_IF_TYPE_STA:
  3343. if (!bssid)
  3344. continue;
  3345. if (!ieee80211_bssid_match(bssid,
  3346. sdata->u.sta.bssid)) {
  3347. if (!rx.u.rx.in_scan)
  3348. continue;
  3349. rx.u.rx.ra_match = 0;
  3350. } else if (!multicast &&
  3351. compare_ether_addr(sdata->dev->dev_addr,
  3352. hdr->addr1) != 0) {
  3353. if (!sdata->promisc)
  3354. continue;
  3355. rx.u.rx.ra_match = 0;
  3356. }
  3357. break;
  3358. case IEEE80211_IF_TYPE_IBSS:
  3359. if (!bssid)
  3360. continue;
  3361. if (!ieee80211_bssid_match(bssid,
  3362. sdata->u.sta.bssid)) {
  3363. if (!rx.u.rx.in_scan)
  3364. continue;
  3365. rx.u.rx.ra_match = 0;
  3366. } else if (!multicast &&
  3367. compare_ether_addr(sdata->dev->dev_addr,
  3368. hdr->addr1) != 0) {
  3369. if (!sdata->promisc)
  3370. continue;
  3371. rx.u.rx.ra_match = 0;
  3372. } else if (!sta)
  3373. sta = rx.sta =
  3374. ieee80211_ibss_add_sta(sdata->dev,
  3375. skb, bssid,
  3376. hdr->addr2);
  3377. break;
  3378. case IEEE80211_IF_TYPE_AP:
  3379. if (!bssid) {
  3380. if (compare_ether_addr(sdata->dev->dev_addr,
  3381. hdr->addr1) != 0)
  3382. continue;
  3383. } else if (!ieee80211_bssid_match(bssid,
  3384. sdata->dev->dev_addr)) {
  3385. if (!rx.u.rx.in_scan)
  3386. continue;
  3387. rx.u.rx.ra_match = 0;
  3388. }
  3389. if (sdata->dev == local->mdev &&
  3390. !rx.u.rx.in_scan)
  3391. /* do not receive anything via
  3392. * master device when not scanning */
  3393. continue;
  3394. break;
  3395. case IEEE80211_IF_TYPE_WDS:
  3396. if (bssid ||
  3397. (rx.fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
  3398. continue;
  3399. if (compare_ether_addr(sdata->u.wds.remote_addr,
  3400. hdr->addr2) != 0)
  3401. continue;
  3402. break;
  3403. }
  3404. if (prev) {
  3405. skb_new = skb_copy(skb, GFP_ATOMIC);
  3406. if (!skb_new) {
  3407. if (net_ratelimit())
  3408. printk(KERN_DEBUG "%s: failed to copy "
  3409. "multicast frame for %s",
  3410. local->mdev->name, prev->dev->name);
  3411. continue;
  3412. }
  3413. rx.skb = skb_new;
  3414. rx.dev = prev->dev;
  3415. rx.sdata = prev;
  3416. ieee80211_invoke_rx_handlers(local,
  3417. local->rx_handlers,
  3418. &rx, sta);
  3419. }
  3420. prev = sdata;
  3421. }
  3422. if (prev) {
  3423. rx.skb = skb;
  3424. rx.dev = prev->dev;
  3425. rx.sdata = prev;
  3426. ieee80211_invoke_rx_handlers(local, local->rx_handlers,
  3427. &rx, sta);
  3428. } else
  3429. dev_kfree_skb(skb);
  3430. read_unlock(&local->sub_if_lock);
  3431. }
  3432. end:
  3433. if (sta)
  3434. sta_info_put(sta);
  3435. }
  3436. EXPORT_SYMBOL(__ieee80211_rx);
  3437. static ieee80211_txrx_result
  3438. ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
  3439. {
  3440. struct ieee80211_local *local = tx->local;
  3441. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  3442. struct sk_buff *skb = tx->skb;
  3443. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  3444. u32 load = 0, hdrtime;
  3445. /* TODO: this could be part of tx_status handling, so that the number
  3446. * of retries would be known; TX rate should in that case be stored
  3447. * somewhere with the packet */
  3448. /* Estimate total channel use caused by this frame */
  3449. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  3450. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  3451. if (mode->mode == MODE_IEEE80211A ||
  3452. mode->mode == MODE_ATHEROS_TURBO ||
  3453. mode->mode == MODE_ATHEROS_TURBOG ||
  3454. (mode->mode == MODE_IEEE80211G &&
  3455. tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
  3456. hdrtime = CHAN_UTIL_HDR_SHORT;
  3457. else
  3458. hdrtime = CHAN_UTIL_HDR_LONG;
  3459. load = hdrtime;
  3460. if (!is_multicast_ether_addr(hdr->addr1))
  3461. load += hdrtime;
  3462. if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  3463. load += 2 * hdrtime;
  3464. else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  3465. load += hdrtime;
  3466. load += skb->len * tx->u.tx.rate->rate_inv;
  3467. if (tx->u.tx.extra_frag) {
  3468. int i;
  3469. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  3470. load += 2 * hdrtime;
  3471. load += tx->u.tx.extra_frag[i]->len *
  3472. tx->u.tx.rate->rate;
  3473. }
  3474. }
  3475. /* Divide channel_use by 8 to avoid wrapping around the counter */
  3476. load >>= CHAN_UTIL_SHIFT;
  3477. local->channel_use_raw += load;
  3478. if (tx->sta)
  3479. tx->sta->channel_use_raw += load;
  3480. tx->sdata->channel_use_raw += load;
  3481. return TXRX_CONTINUE;
  3482. }
  3483. static ieee80211_txrx_result
  3484. ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
  3485. {
  3486. struct ieee80211_local *local = rx->local;
  3487. struct sk_buff *skb = rx->skb;
  3488. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  3489. u32 load = 0, hdrtime;
  3490. struct ieee80211_rate *rate;
  3491. struct ieee80211_hw_mode *mode = local->hw.conf.mode;
  3492. int i;
  3493. /* Estimate total channel use caused by this frame */
  3494. if (unlikely(mode->num_rates < 0))
  3495. return TXRX_CONTINUE;
  3496. rate = &mode->rates[0];
  3497. for (i = 0; i < mode->num_rates; i++) {
  3498. if (mode->rates[i].val == rx->u.rx.status->rate) {
  3499. rate = &mode->rates[i];
  3500. break;
  3501. }
  3502. }
  3503. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  3504. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  3505. if (mode->mode == MODE_IEEE80211A ||
  3506. mode->mode == MODE_ATHEROS_TURBO ||
  3507. mode->mode == MODE_ATHEROS_TURBOG ||
  3508. (mode->mode == MODE_IEEE80211G &&
  3509. rate->flags & IEEE80211_RATE_ERP))
  3510. hdrtime = CHAN_UTIL_HDR_SHORT;
  3511. else
  3512. hdrtime = CHAN_UTIL_HDR_LONG;
  3513. load = hdrtime;
  3514. if (!is_multicast_ether_addr(hdr->addr1))
  3515. load += hdrtime;
  3516. load += skb->len * rate->rate_inv;
  3517. /* Divide channel_use by 8 to avoid wrapping around the counter */
  3518. load >>= CHAN_UTIL_SHIFT;
  3519. local->channel_use_raw += load;
  3520. if (rx->sta)
  3521. rx->sta->channel_use_raw += load;
  3522. rx->u.rx.load = load;
  3523. return TXRX_CONTINUE;
  3524. }
  3525. static ieee80211_txrx_result
  3526. ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
  3527. {
  3528. rx->sdata->channel_use_raw += rx->u.rx.load;
  3529. return TXRX_CONTINUE;
  3530. }
  3531. static void ieee80211_stat_refresh(unsigned long data)
  3532. {
  3533. struct ieee80211_local *local = (struct ieee80211_local *) data;
  3534. struct sta_info *sta;
  3535. struct ieee80211_sub_if_data *sdata;
  3536. if (!local->stat_time)
  3537. return;
  3538. /* go through all stations */
  3539. spin_lock_bh(&local->sta_lock);
  3540. list_for_each_entry(sta, &local->sta_list, list) {
  3541. sta->channel_use = (sta->channel_use_raw / local->stat_time) /
  3542. CHAN_UTIL_PER_10MS;
  3543. sta->channel_use_raw = 0;
  3544. }
  3545. spin_unlock_bh(&local->sta_lock);
  3546. /* go through all subinterfaces */
  3547. read_lock(&local->sub_if_lock);
  3548. list_for_each_entry(sdata, &local->sub_if_list, list) {
  3549. sdata->channel_use = (sdata->channel_use_raw /
  3550. local->stat_time) / CHAN_UTIL_PER_10MS;
  3551. sdata->channel_use_raw = 0;
  3552. }
  3553. read_unlock(&local->sub_if_lock);
  3554. /* hardware interface */
  3555. local->channel_use = (local->channel_use_raw /
  3556. local->stat_time) / CHAN_UTIL_PER_10MS;
  3557. local->channel_use_raw = 0;
  3558. local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
  3559. add_timer(&local->stat_timer);
  3560. }
  3561. /* This is a version of the rx handler that can be called from hard irq
  3562. * context. Post the skb on the queue and schedule the tasklet */
  3563. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
  3564. struct ieee80211_rx_status *status)
  3565. {
  3566. struct ieee80211_local *local = hw_to_local(hw);
  3567. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  3568. skb->dev = local->mdev;
  3569. /* copy status into skb->cb for use by tasklet */
  3570. memcpy(skb->cb, status, sizeof(*status));
  3571. skb->pkt_type = IEEE80211_RX_MSG;
  3572. skb_queue_tail(&local->skb_queue, skb);
  3573. tasklet_schedule(&local->tasklet);
  3574. }
  3575. EXPORT_SYMBOL(ieee80211_rx_irqsafe);
  3576. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  3577. struct sk_buff *skb,
  3578. struct ieee80211_tx_status *status)
  3579. {
  3580. struct ieee80211_local *local = hw_to_local(hw);
  3581. struct ieee80211_tx_status *saved;
  3582. int tmp;
  3583. skb->dev = local->mdev;
  3584. saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
  3585. if (unlikely(!saved)) {
  3586. if (net_ratelimit())
  3587. printk(KERN_WARNING "%s: Not enough memory, "
  3588. "dropping tx status", skb->dev->name);
  3589. /* should be dev_kfree_skb_irq, but due to this function being
  3590. * named _irqsafe instead of just _irq we can't be sure that
  3591. * people won't call it from non-irq contexts */
  3592. dev_kfree_skb_any(skb);
  3593. return;
  3594. }
  3595. memcpy(saved, status, sizeof(struct ieee80211_tx_status));
  3596. /* copy pointer to saved status into skb->cb for use by tasklet */
  3597. memcpy(skb->cb, &saved, sizeof(saved));
  3598. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  3599. skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
  3600. &local->skb_queue : &local->skb_queue_unreliable, skb);
  3601. tmp = skb_queue_len(&local->skb_queue) +
  3602. skb_queue_len(&local->skb_queue_unreliable);
  3603. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  3604. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  3605. memcpy(&saved, skb->cb, sizeof(saved));
  3606. kfree(saved);
  3607. dev_kfree_skb_irq(skb);
  3608. tmp--;
  3609. I802_DEBUG_INC(local->tx_status_drop);
  3610. }
  3611. tasklet_schedule(&local->tasklet);
  3612. }
  3613. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  3614. static void ieee80211_tasklet_handler(unsigned long data)
  3615. {
  3616. struct ieee80211_local *local = (struct ieee80211_local *) data;
  3617. struct sk_buff *skb;
  3618. struct ieee80211_rx_status rx_status;
  3619. struct ieee80211_tx_status *tx_status;
  3620. while ((skb = skb_dequeue(&local->skb_queue)) ||
  3621. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  3622. switch (skb->pkt_type) {
  3623. case IEEE80211_RX_MSG:
  3624. /* status is in skb->cb */
  3625. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  3626. /* Clear skb->type in order to not confuse kernel
  3627. * netstack. */
  3628. skb->pkt_type = 0;
  3629. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  3630. break;
  3631. case IEEE80211_TX_STATUS_MSG:
  3632. /* get pointer to saved status out of skb->cb */
  3633. memcpy(&tx_status, skb->cb, sizeof(tx_status));
  3634. skb->pkt_type = 0;
  3635. ieee80211_tx_status(local_to_hw(local),
  3636. skb, tx_status);
  3637. kfree(tx_status);
  3638. break;
  3639. default: /* should never get here! */
  3640. printk(KERN_ERR "%s: Unknown message type (%d)\n",
  3641. local->mdev->name, skb->pkt_type);
  3642. dev_kfree_skb(skb);
  3643. break;
  3644. }
  3645. }
  3646. }
  3647. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  3648. * make a prepared TX frame (one that has been given to hw) to look like brand
  3649. * new IEEE 802.11 frame that is ready to go through TX processing again.
  3650. * Also, tx_packet_data in cb is restored from tx_control. */
  3651. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  3652. struct ieee80211_key *key,
  3653. struct sk_buff *skb,
  3654. struct ieee80211_tx_control *control)
  3655. {
  3656. int hdrlen, iv_len, mic_len;
  3657. struct ieee80211_tx_packet_data *pkt_data;
  3658. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  3659. pkt_data->ifindex = control->ifindex;
  3660. pkt_data->mgmt_iface = (control->type == IEEE80211_IF_TYPE_MGMT);
  3661. pkt_data->req_tx_status = !!(control->flags & IEEE80211_TXCTL_REQ_TX_STATUS);
  3662. pkt_data->do_not_encrypt = !!(control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT);
  3663. pkt_data->requeue = !!(control->flags & IEEE80211_TXCTL_REQUEUE);
  3664. pkt_data->queue = control->queue;
  3665. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  3666. if (!key)
  3667. goto no_key;
  3668. switch (key->alg) {
  3669. case ALG_WEP:
  3670. iv_len = WEP_IV_LEN;
  3671. mic_len = WEP_ICV_LEN;
  3672. break;
  3673. case ALG_TKIP:
  3674. iv_len = TKIP_IV_LEN;
  3675. mic_len = TKIP_ICV_LEN;
  3676. break;
  3677. case ALG_CCMP:
  3678. iv_len = CCMP_HDR_LEN;
  3679. mic_len = CCMP_MIC_LEN;
  3680. break;
  3681. default:
  3682. goto no_key;
  3683. }
  3684. if (skb->len >= mic_len && key->force_sw_encrypt)
  3685. skb_trim(skb, skb->len - mic_len);
  3686. if (skb->len >= iv_len && skb->len > hdrlen) {
  3687. memmove(skb->data + iv_len, skb->data, hdrlen);
  3688. skb_pull(skb, iv_len);
  3689. }
  3690. no_key:
  3691. {
  3692. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  3693. u16 fc = le16_to_cpu(hdr->frame_control);
  3694. if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
  3695. fc &= ~IEEE80211_STYPE_QOS_DATA;
  3696. hdr->frame_control = cpu_to_le16(fc);
  3697. memmove(skb->data + 2, skb->data, hdrlen - 2);
  3698. skb_pull(skb, 2);
  3699. }
  3700. }
  3701. }
  3702. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  3703. struct ieee80211_tx_status *status)
  3704. {
  3705. struct sk_buff *skb2;
  3706. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  3707. struct ieee80211_local *local = hw_to_local(hw);
  3708. u16 frag, type;
  3709. u32 msg_type;
  3710. if (!status) {
  3711. printk(KERN_ERR
  3712. "%s: ieee80211_tx_status called with NULL status\n",
  3713. local->mdev->name);
  3714. dev_kfree_skb(skb);
  3715. return;
  3716. }
  3717. if (status->excessive_retries) {
  3718. struct sta_info *sta;
  3719. sta = sta_info_get(local, hdr->addr1);
  3720. if (sta) {
  3721. if (sta->flags & WLAN_STA_PS) {
  3722. /* The STA is in power save mode, so assume
  3723. * that this TX packet failed because of that.
  3724. */
  3725. status->excessive_retries = 0;
  3726. status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
  3727. }
  3728. sta_info_put(sta);
  3729. }
  3730. }
  3731. if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
  3732. struct sta_info *sta;
  3733. sta = sta_info_get(local, hdr->addr1);
  3734. if (sta) {
  3735. sta->tx_filtered_count++;
  3736. /* Clear the TX filter mask for this STA when sending
  3737. * the next packet. If the STA went to power save mode,
  3738. * this will happen when it is waking up for the next
  3739. * time. */
  3740. sta->clear_dst_mask = 1;
  3741. /* TODO: Is the WLAN_STA_PS flag always set here or is
  3742. * the race between RX and TX status causing some
  3743. * packets to be filtered out before 80211.o gets an
  3744. * update for PS status? This seems to be the case, so
  3745. * no changes are likely to be needed. */
  3746. if (sta->flags & WLAN_STA_PS &&
  3747. skb_queue_len(&sta->tx_filtered) <
  3748. STA_MAX_TX_BUFFER) {
  3749. ieee80211_remove_tx_extra(local, sta->key,
  3750. skb,
  3751. &status->control);
  3752. skb_queue_tail(&sta->tx_filtered, skb);
  3753. } else if (!(sta->flags & WLAN_STA_PS) &&
  3754. !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
  3755. /* Software retry the packet once */
  3756. status->control.flags |= IEEE80211_TXCTL_REQUEUE;
  3757. ieee80211_remove_tx_extra(local, sta->key,
  3758. skb,
  3759. &status->control);
  3760. dev_queue_xmit(skb);
  3761. } else {
  3762. if (net_ratelimit()) {
  3763. printk(KERN_DEBUG "%s: dropped TX "
  3764. "filtered frame queue_len=%d "
  3765. "PS=%d @%lu\n",
  3766. local->mdev->name,
  3767. skb_queue_len(
  3768. &sta->tx_filtered),
  3769. !!(sta->flags & WLAN_STA_PS),
  3770. jiffies);
  3771. }
  3772. dev_kfree_skb(skb);
  3773. }
  3774. sta_info_put(sta);
  3775. return;
  3776. }
  3777. } else {
  3778. /* FIXME: STUPID to call this with both local and local->mdev */
  3779. rate_control_tx_status(local, local->mdev, skb, status);
  3780. }
  3781. ieee80211_led_tx(local, 0);
  3782. /* SNMP counters
  3783. * Fragments are passed to low-level drivers as separate skbs, so these
  3784. * are actually fragments, not frames. Update frame counters only for
  3785. * the first fragment of the frame. */
  3786. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  3787. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  3788. if (status->flags & IEEE80211_TX_STATUS_ACK) {
  3789. if (frag == 0) {
  3790. local->dot11TransmittedFrameCount++;
  3791. if (is_multicast_ether_addr(hdr->addr1))
  3792. local->dot11MulticastTransmittedFrameCount++;
  3793. if (status->retry_count > 0)
  3794. local->dot11RetryCount++;
  3795. if (status->retry_count > 1)
  3796. local->dot11MultipleRetryCount++;
  3797. }
  3798. /* This counter shall be incremented for an acknowledged MPDU
  3799. * with an individual address in the address 1 field or an MPDU
  3800. * with a multicast address in the address 1 field of type Data
  3801. * or Management. */
  3802. if (!is_multicast_ether_addr(hdr->addr1) ||
  3803. type == IEEE80211_FTYPE_DATA ||
  3804. type == IEEE80211_FTYPE_MGMT)
  3805. local->dot11TransmittedFragmentCount++;
  3806. } else {
  3807. if (frag == 0)
  3808. local->dot11FailedCount++;
  3809. }
  3810. if (!(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS)
  3811. || unlikely(!local->apdev)) {
  3812. dev_kfree_skb(skb);
  3813. return;
  3814. }
  3815. msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
  3816. ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
  3817. /* skb was the original skb used for TX. Clone it and give the clone
  3818. * to netif_rx(). Free original skb. */
  3819. skb2 = skb_copy(skb, GFP_ATOMIC);
  3820. if (!skb2) {
  3821. dev_kfree_skb(skb);
  3822. return;
  3823. }
  3824. dev_kfree_skb(skb);
  3825. skb = skb2;
  3826. /* Send frame to hostapd */
  3827. ieee80211_rx_mgmt(local, skb, NULL, msg_type);
  3828. }
  3829. EXPORT_SYMBOL(ieee80211_tx_status);
  3830. /* TODO: implement register/unregister functions for adding TX/RX handlers
  3831. * into ordered list */
  3832. /* rx_pre handlers don't have dev and sdata fields available in
  3833. * ieee80211_txrx_data */
  3834. static ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
  3835. {
  3836. ieee80211_rx_h_parse_qos,
  3837. ieee80211_rx_h_load_stats,
  3838. NULL
  3839. };
  3840. static ieee80211_rx_handler ieee80211_rx_handlers[] =
  3841. {
  3842. ieee80211_rx_h_if_stats,
  3843. ieee80211_rx_h_monitor,
  3844. ieee80211_rx_h_passive_scan,
  3845. ieee80211_rx_h_check,
  3846. ieee80211_rx_h_sta_process,
  3847. ieee80211_rx_h_ccmp_decrypt,
  3848. ieee80211_rx_h_tkip_decrypt,
  3849. ieee80211_rx_h_wep_weak_iv_detection,
  3850. ieee80211_rx_h_wep_decrypt,
  3851. ieee80211_rx_h_defragment,
  3852. ieee80211_rx_h_ps_poll,
  3853. ieee80211_rx_h_michael_mic_verify,
  3854. /* this must be after decryption - so header is counted in MPDU mic
  3855. * must be before pae and data, so QOS_DATA format frames
  3856. * are not passed to user space by these functions
  3857. */
  3858. ieee80211_rx_h_remove_qos_control,
  3859. ieee80211_rx_h_802_1x_pae,
  3860. ieee80211_rx_h_drop_unencrypted,
  3861. ieee80211_rx_h_data,
  3862. ieee80211_rx_h_mgmt,
  3863. NULL
  3864. };
  3865. static ieee80211_tx_handler ieee80211_tx_handlers[] =
  3866. {
  3867. ieee80211_tx_h_check_assoc,
  3868. ieee80211_tx_h_sequence,
  3869. ieee80211_tx_h_ps_buf,
  3870. ieee80211_tx_h_select_key,
  3871. ieee80211_tx_h_michael_mic_add,
  3872. ieee80211_tx_h_fragment,
  3873. ieee80211_tx_h_tkip_encrypt,
  3874. ieee80211_tx_h_ccmp_encrypt,
  3875. ieee80211_tx_h_wep_encrypt,
  3876. ieee80211_tx_h_rate_ctrl,
  3877. ieee80211_tx_h_misc,
  3878. ieee80211_tx_h_load_stats,
  3879. NULL
  3880. };
  3881. int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
  3882. {
  3883. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  3884. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  3885. struct sta_info *sta;
  3886. if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
  3887. return 0;
  3888. /* Create STA entry for the new peer */
  3889. sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
  3890. if (!sta)
  3891. return -ENOMEM;
  3892. sta_info_put(sta);
  3893. /* Remove STA entry for the old peer */
  3894. sta = sta_info_get(local, sdata->u.wds.remote_addr);
  3895. if (sta) {
  3896. sta_info_put(sta);
  3897. sta_info_free(sta, 0);
  3898. } else {
  3899. printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
  3900. "peer " MAC_FMT "\n",
  3901. dev->name, MAC_ARG(sdata->u.wds.remote_addr));
  3902. }
  3903. /* Update WDS link data */
  3904. memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
  3905. return 0;
  3906. }
  3907. /* Must not be called for mdev and apdev */
  3908. void ieee80211_if_setup(struct net_device *dev)
  3909. {
  3910. ether_setup(dev);
  3911. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  3912. dev->wireless_handlers = &ieee80211_iw_handler_def;
  3913. dev->set_multicast_list = ieee80211_set_multicast_list;
  3914. dev->change_mtu = ieee80211_change_mtu;
  3915. dev->get_stats = ieee80211_get_stats;
  3916. dev->open = ieee80211_open;
  3917. dev->stop = ieee80211_stop;
  3918. dev->uninit = ieee80211_if_reinit;
  3919. dev->destructor = ieee80211_if_free;
  3920. }
  3921. void ieee80211_if_mgmt_setup(struct net_device *dev)
  3922. {
  3923. ether_setup(dev);
  3924. dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
  3925. dev->change_mtu = ieee80211_change_mtu_apdev;
  3926. dev->get_stats = ieee80211_get_stats;
  3927. dev->open = ieee80211_mgmt_open;
  3928. dev->stop = ieee80211_mgmt_stop;
  3929. dev->type = ARPHRD_IEEE80211_PRISM;
  3930. dev->hard_header_parse = header_parse_80211;
  3931. dev->uninit = ieee80211_if_reinit;
  3932. dev->destructor = ieee80211_if_free;
  3933. }
  3934. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  3935. const char *name)
  3936. {
  3937. struct rate_control_ref *ref, *old;
  3938. ASSERT_RTNL();
  3939. if (local->open_count || netif_running(local->mdev) ||
  3940. (local->apdev && netif_running(local->apdev)))
  3941. return -EBUSY;
  3942. ref = rate_control_alloc(name, local);
  3943. if (!ref) {
  3944. printk(KERN_WARNING "%s: Failed to select rate control "
  3945. "algorithm\n", local->mdev->name);
  3946. return -ENOENT;
  3947. }
  3948. old = local->rate_ctrl;
  3949. local->rate_ctrl = ref;
  3950. if (old) {
  3951. rate_control_put(old);
  3952. sta_info_flush(local, NULL);
  3953. }
  3954. printk(KERN_DEBUG "%s: Selected rate control "
  3955. "algorithm '%s'\n", local->mdev->name,
  3956. ref->ops->name);
  3957. return 0;
  3958. }
  3959. static void rate_control_deinitialize(struct ieee80211_local *local)
  3960. {
  3961. struct rate_control_ref *ref;
  3962. ref = local->rate_ctrl;
  3963. local->rate_ctrl = NULL;
  3964. rate_control_put(ref);
  3965. }
  3966. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  3967. const struct ieee80211_ops *ops)
  3968. {
  3969. struct net_device *mdev;
  3970. struct ieee80211_local *local;
  3971. struct ieee80211_sub_if_data *sdata;
  3972. int priv_size;
  3973. struct wiphy *wiphy;
  3974. /* Ensure 32-byte alignment of our private data and hw private data.
  3975. * We use the wiphy priv data for both our ieee80211_local and for
  3976. * the driver's private data
  3977. *
  3978. * In memory it'll be like this:
  3979. *
  3980. * +-------------------------+
  3981. * | struct wiphy |
  3982. * +-------------------------+
  3983. * | struct ieee80211_local |
  3984. * +-------------------------+
  3985. * | driver's private data |
  3986. * +-------------------------+
  3987. *
  3988. */
  3989. priv_size = ((sizeof(struct ieee80211_local) +
  3990. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  3991. priv_data_len;
  3992. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  3993. if (!wiphy)
  3994. return NULL;
  3995. wiphy->privid = mac80211_wiphy_privid;
  3996. local = wiphy_priv(wiphy);
  3997. local->hw.wiphy = wiphy;
  3998. local->hw.priv = (char *)local +
  3999. ((sizeof(struct ieee80211_local) +
  4000. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  4001. local->ops = ops;
  4002. /* for now, mdev needs sub_if_data :/ */
  4003. mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
  4004. "wmaster%d", ether_setup);
  4005. if (!mdev) {
  4006. wiphy_free(wiphy);
  4007. return NULL;
  4008. }
  4009. sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
  4010. mdev->ieee80211_ptr = &sdata->wdev;
  4011. sdata->wdev.wiphy = wiphy;
  4012. local->hw.queues = 1; /* default */
  4013. local->mdev = mdev;
  4014. local->rx_pre_handlers = ieee80211_rx_pre_handlers;
  4015. local->rx_handlers = ieee80211_rx_handlers;
  4016. local->tx_handlers = ieee80211_tx_handlers;
  4017. local->bridge_packets = 1;
  4018. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  4019. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  4020. local->short_retry_limit = 7;
  4021. local->long_retry_limit = 4;
  4022. local->hw.conf.radio_enabled = 1;
  4023. local->rate_ctrl_num_up = RATE_CONTROL_NUM_UP;
  4024. local->rate_ctrl_num_down = RATE_CONTROL_NUM_DOWN;
  4025. local->enabled_modes = (unsigned int) -1;
  4026. INIT_LIST_HEAD(&local->modes_list);
  4027. rwlock_init(&local->sub_if_lock);
  4028. INIT_LIST_HEAD(&local->sub_if_list);
  4029. INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
  4030. init_timer(&local->stat_timer);
  4031. local->stat_timer.function = ieee80211_stat_refresh;
  4032. local->stat_timer.data = (unsigned long) local;
  4033. ieee80211_rx_bss_list_init(mdev);
  4034. sta_info_init(local);
  4035. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  4036. mdev->open = ieee80211_master_open;
  4037. mdev->stop = ieee80211_master_stop;
  4038. mdev->type = ARPHRD_IEEE80211;
  4039. mdev->hard_header_parse = header_parse_80211;
  4040. sdata->type = IEEE80211_IF_TYPE_AP;
  4041. sdata->dev = mdev;
  4042. sdata->local = local;
  4043. sdata->u.ap.force_unicast_rateidx = -1;
  4044. sdata->u.ap.max_ratectrl_rateidx = -1;
  4045. ieee80211_if_sdata_init(sdata);
  4046. list_add_tail(&sdata->list, &local->sub_if_list);
  4047. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  4048. (unsigned long)local);
  4049. tasklet_disable(&local->tx_pending_tasklet);
  4050. tasklet_init(&local->tasklet,
  4051. ieee80211_tasklet_handler,
  4052. (unsigned long) local);
  4053. tasklet_disable(&local->tasklet);
  4054. skb_queue_head_init(&local->skb_queue);
  4055. skb_queue_head_init(&local->skb_queue_unreliable);
  4056. return local_to_hw(local);
  4057. }
  4058. EXPORT_SYMBOL(ieee80211_alloc_hw);
  4059. int ieee80211_register_hw(struct ieee80211_hw *hw)
  4060. {
  4061. struct ieee80211_local *local = hw_to_local(hw);
  4062. const char *name;
  4063. int result;
  4064. result = wiphy_register(local->hw.wiphy);
  4065. if (result < 0)
  4066. return result;
  4067. name = wiphy_dev(local->hw.wiphy)->driver->name;
  4068. local->hw.workqueue = create_singlethread_workqueue(name);
  4069. if (!local->hw.workqueue) {
  4070. result = -ENOMEM;
  4071. goto fail_workqueue;
  4072. }
  4073. debugfs_hw_add(local);
  4074. local->hw.conf.beacon_int = 1000;
  4075. local->wstats_flags |= local->hw.max_rssi ?
  4076. IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
  4077. local->wstats_flags |= local->hw.max_signal ?
  4078. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  4079. local->wstats_flags |= local->hw.max_noise ?
  4080. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  4081. if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
  4082. local->wstats_flags |= IW_QUAL_DBM;
  4083. result = sta_info_start(local);
  4084. if (result < 0)
  4085. goto fail_sta_info;
  4086. rtnl_lock();
  4087. result = dev_alloc_name(local->mdev, local->mdev->name);
  4088. if (result < 0)
  4089. goto fail_dev;
  4090. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  4091. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  4092. result = register_netdevice(local->mdev);
  4093. if (result < 0)
  4094. goto fail_dev;
  4095. ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  4096. result = ieee80211_init_rate_ctrl_alg(local, NULL);
  4097. if (result < 0) {
  4098. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  4099. "algorithm\n", local->mdev->name);
  4100. goto fail_rate;
  4101. }
  4102. result = ieee80211_wep_init(local);
  4103. if (result < 0) {
  4104. printk(KERN_DEBUG "%s: Failed to initialize wep\n",
  4105. local->mdev->name);
  4106. goto fail_wep;
  4107. }
  4108. ieee80211_install_qdisc(local->mdev);
  4109. /* add one default STA interface */
  4110. result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
  4111. IEEE80211_IF_TYPE_STA);
  4112. if (result)
  4113. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  4114. local->mdev->name);
  4115. local->reg_state = IEEE80211_DEV_REGISTERED;
  4116. rtnl_unlock();
  4117. ieee80211_led_init(local);
  4118. return 0;
  4119. fail_wep:
  4120. rate_control_deinitialize(local);
  4121. fail_rate:
  4122. ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  4123. unregister_netdevice(local->mdev);
  4124. fail_dev:
  4125. rtnl_unlock();
  4126. sta_info_stop(local);
  4127. fail_sta_info:
  4128. debugfs_hw_del(local);
  4129. destroy_workqueue(local->hw.workqueue);
  4130. fail_workqueue:
  4131. wiphy_unregister(local->hw.wiphy);
  4132. return result;
  4133. }
  4134. EXPORT_SYMBOL(ieee80211_register_hw);
  4135. int ieee80211_register_hwmode(struct ieee80211_hw *hw,
  4136. struct ieee80211_hw_mode *mode)
  4137. {
  4138. struct ieee80211_local *local = hw_to_local(hw);
  4139. struct ieee80211_rate *rate;
  4140. int i;
  4141. INIT_LIST_HEAD(&mode->list);
  4142. list_add_tail(&mode->list, &local->modes_list);
  4143. local->hw_modes |= (1 << mode->mode);
  4144. for (i = 0; i < mode->num_rates; i++) {
  4145. rate = &(mode->rates[i]);
  4146. rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
  4147. }
  4148. ieee80211_prepare_rates(local, mode);
  4149. if (!local->oper_hw_mode) {
  4150. /* Default to this mode */
  4151. local->hw.conf.phymode = mode->mode;
  4152. local->oper_hw_mode = local->scan_hw_mode = mode;
  4153. local->oper_channel = local->scan_channel = &mode->channels[0];
  4154. local->hw.conf.mode = local->oper_hw_mode;
  4155. local->hw.conf.chan = local->oper_channel;
  4156. }
  4157. if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
  4158. ieee80211_init_client(local->mdev);
  4159. return 0;
  4160. }
  4161. EXPORT_SYMBOL(ieee80211_register_hwmode);
  4162. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  4163. {
  4164. struct ieee80211_local *local = hw_to_local(hw);
  4165. struct ieee80211_sub_if_data *sdata, *tmp;
  4166. struct list_head tmp_list;
  4167. int i;
  4168. tasklet_kill(&local->tx_pending_tasklet);
  4169. tasklet_kill(&local->tasklet);
  4170. rtnl_lock();
  4171. BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
  4172. local->reg_state = IEEE80211_DEV_UNREGISTERED;
  4173. if (local->apdev)
  4174. ieee80211_if_del_mgmt(local);
  4175. write_lock_bh(&local->sub_if_lock);
  4176. list_replace_init(&local->sub_if_list, &tmp_list);
  4177. write_unlock_bh(&local->sub_if_lock);
  4178. list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
  4179. __ieee80211_if_del(local, sdata);
  4180. rtnl_unlock();
  4181. if (local->stat_time)
  4182. del_timer_sync(&local->stat_timer);
  4183. ieee80211_rx_bss_list_deinit(local->mdev);
  4184. ieee80211_clear_tx_pending(local);
  4185. sta_info_stop(local);
  4186. rate_control_deinitialize(local);
  4187. debugfs_hw_del(local);
  4188. for (i = 0; i < NUM_IEEE80211_MODES; i++) {
  4189. kfree(local->supp_rates[i]);
  4190. kfree(local->basic_rates[i]);
  4191. }
  4192. if (skb_queue_len(&local->skb_queue)
  4193. || skb_queue_len(&local->skb_queue_unreliable))
  4194. printk(KERN_WARNING "%s: skb_queue not empty\n",
  4195. local->mdev->name);
  4196. skb_queue_purge(&local->skb_queue);
  4197. skb_queue_purge(&local->skb_queue_unreliable);
  4198. destroy_workqueue(local->hw.workqueue);
  4199. wiphy_unregister(local->hw.wiphy);
  4200. ieee80211_wep_free(local);
  4201. ieee80211_led_exit(local);
  4202. }
  4203. EXPORT_SYMBOL(ieee80211_unregister_hw);
  4204. void ieee80211_free_hw(struct ieee80211_hw *hw)
  4205. {
  4206. struct ieee80211_local *local = hw_to_local(hw);
  4207. ieee80211_if_free(local->mdev);
  4208. wiphy_free(local->hw.wiphy);
  4209. }
  4210. EXPORT_SYMBOL(ieee80211_free_hw);
  4211. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  4212. {
  4213. struct ieee80211_local *local = hw_to_local(hw);
  4214. if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
  4215. &local->state[queue])) {
  4216. if (test_bit(IEEE80211_LINK_STATE_PENDING,
  4217. &local->state[queue]))
  4218. tasklet_schedule(&local->tx_pending_tasklet);
  4219. else
  4220. if (!ieee80211_qdisc_installed(local->mdev)) {
  4221. if (queue == 0)
  4222. netif_wake_queue(local->mdev);
  4223. } else
  4224. __netif_schedule(local->mdev);
  4225. }
  4226. }
  4227. EXPORT_SYMBOL(ieee80211_wake_queue);
  4228. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  4229. {
  4230. struct ieee80211_local *local = hw_to_local(hw);
  4231. if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
  4232. netif_stop_queue(local->mdev);
  4233. set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  4234. }
  4235. EXPORT_SYMBOL(ieee80211_stop_queue);
  4236. void ieee80211_start_queues(struct ieee80211_hw *hw)
  4237. {
  4238. struct ieee80211_local *local = hw_to_local(hw);
  4239. int i;
  4240. for (i = 0; i < local->hw.queues; i++)
  4241. clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
  4242. if (!ieee80211_qdisc_installed(local->mdev))
  4243. netif_start_queue(local->mdev);
  4244. }
  4245. EXPORT_SYMBOL(ieee80211_start_queues);
  4246. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  4247. {
  4248. int i;
  4249. for (i = 0; i < hw->queues; i++)
  4250. ieee80211_stop_queue(hw, i);
  4251. }
  4252. EXPORT_SYMBOL(ieee80211_stop_queues);
  4253. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  4254. {
  4255. int i;
  4256. for (i = 0; i < hw->queues; i++)
  4257. ieee80211_wake_queue(hw, i);
  4258. }
  4259. EXPORT_SYMBOL(ieee80211_wake_queues);
  4260. struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
  4261. {
  4262. struct ieee80211_sub_if_data *sdata;
  4263. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  4264. return &sdata->stats;
  4265. }
  4266. static int __init ieee80211_init(void)
  4267. {
  4268. struct sk_buff *skb;
  4269. int ret;
  4270. BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
  4271. ret = ieee80211_wme_register();
  4272. if (ret) {
  4273. printk(KERN_DEBUG "ieee80211_init: failed to "
  4274. "initialize WME (err=%d)\n", ret);
  4275. return ret;
  4276. }
  4277. ieee80211_debugfs_netdev_init();
  4278. return 0;
  4279. }
  4280. static void __exit ieee80211_exit(void)
  4281. {
  4282. ieee80211_wme_unregister();
  4283. ieee80211_debugfs_netdev_exit();
  4284. }
  4285. module_init(ieee80211_init);
  4286. module_exit(ieee80211_exit);
  4287. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  4288. MODULE_LICENSE("GPL");