ieee80211.c 138 KB

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