ieee80211.c 145 KB

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