ieee80211.c 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273
  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->local->cts_protect_erp_frames && 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 &&
  743. tx->local->cts_protect_erp_frames &&
  744. !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
  745. control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
  746. /* Setup duration field for the first fragment of the frame. Duration
  747. * for remaining fragments will be updated when they are being sent
  748. * to low-level driver in ieee80211_tx(). */
  749. dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
  750. tx->fragmented ? tx->u.tx.extra_frag[0]->len :
  751. 0);
  752. hdr->duration_id = cpu_to_le16(dur);
  753. if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
  754. (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
  755. struct ieee80211_rate *rate;
  756. /* Do not use multiple retry rates when using RTS/CTS */
  757. control->alt_retry_rate = -1;
  758. /* Use min(data rate, max base rate) as CTS/RTS rate */
  759. rate = tx->u.tx.rate;
  760. while (rate > mode->rates &&
  761. !(rate->flags & IEEE80211_RATE_BASIC))
  762. rate--;
  763. control->rts_cts_rate = rate->val;
  764. control->rts_rate = rate;
  765. }
  766. if (tx->sta) {
  767. tx->sta->tx_packets++;
  768. tx->sta->tx_fragments++;
  769. tx->sta->tx_bytes += tx->skb->len;
  770. if (tx->u.tx.extra_frag) {
  771. int i;
  772. tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
  773. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  774. tx->sta->tx_bytes +=
  775. tx->u.tx.extra_frag[i]->len;
  776. }
  777. }
  778. }
  779. return TXRX_CONTINUE;
  780. }
  781. static ieee80211_txrx_result
  782. ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
  783. {
  784. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  785. struct sk_buff *skb = tx->skb;
  786. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  787. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  788. u32 sta_flags;
  789. if (unlikely(tx->local->sta_scanning != 0) &&
  790. ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  791. (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
  792. return TXRX_DROP;
  793. if (tx->u.tx.ps_buffered)
  794. return TXRX_CONTINUE;
  795. sta_flags = tx->sta ? tx->sta->flags : 0;
  796. if (likely(tx->u.tx.unicast)) {
  797. if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
  798. tx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
  799. (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
  800. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  801. printk(KERN_DEBUG "%s: dropped data frame to not "
  802. "associated station " MAC_FMT "\n",
  803. tx->dev->name, MAC_ARG(hdr->addr1));
  804. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  805. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  806. return TXRX_DROP;
  807. }
  808. } else {
  809. if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  810. tx->local->num_sta == 0 &&
  811. !tx->local->allow_broadcast_always &&
  812. tx->sdata->type != IEEE80211_IF_TYPE_IBSS)) {
  813. /*
  814. * No associated STAs - no need to send multicast
  815. * frames.
  816. */
  817. return TXRX_DROP;
  818. }
  819. return TXRX_CONTINUE;
  820. }
  821. if (unlikely(!tx->u.tx.mgmt_interface && tx->sdata->ieee802_1x &&
  822. !(sta_flags & WLAN_STA_AUTHORIZED))) {
  823. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  824. printk(KERN_DEBUG "%s: dropped frame to " MAC_FMT
  825. " (unauthorized port)\n", tx->dev->name,
  826. MAC_ARG(hdr->addr1));
  827. #endif
  828. I802_DEBUG_INC(tx->local->tx_handlers_drop_unauth_port);
  829. return TXRX_DROP;
  830. }
  831. return TXRX_CONTINUE;
  832. }
  833. static ieee80211_txrx_result
  834. ieee80211_tx_h_sequence(struct ieee80211_txrx_data *tx)
  835. {
  836. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  837. if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
  838. ieee80211_include_sequence(tx->sdata, hdr);
  839. return TXRX_CONTINUE;
  840. }
  841. /* This function is called whenever the AP is about to exceed the maximum limit
  842. * of buffered frames for power saving STAs. This situation should not really
  843. * happen often during normal operation, so dropping the oldest buffered packet
  844. * from each queue should be OK to make some room for new frames. */
  845. static void purge_old_ps_buffers(struct ieee80211_local *local)
  846. {
  847. int total = 0, purged = 0;
  848. struct sk_buff *skb;
  849. struct ieee80211_sub_if_data *sdata;
  850. struct sta_info *sta;
  851. read_lock(&local->sub_if_lock);
  852. list_for_each_entry(sdata, &local->sub_if_list, list) {
  853. struct ieee80211_if_ap *ap;
  854. if (sdata->dev == local->mdev ||
  855. sdata->type != IEEE80211_IF_TYPE_AP)
  856. continue;
  857. ap = &sdata->u.ap;
  858. skb = skb_dequeue(&ap->ps_bc_buf);
  859. if (skb) {
  860. purged++;
  861. dev_kfree_skb(skb);
  862. }
  863. total += skb_queue_len(&ap->ps_bc_buf);
  864. }
  865. read_unlock(&local->sub_if_lock);
  866. spin_lock_bh(&local->sta_lock);
  867. list_for_each_entry(sta, &local->sta_list, list) {
  868. skb = skb_dequeue(&sta->ps_tx_buf);
  869. if (skb) {
  870. purged++;
  871. dev_kfree_skb(skb);
  872. }
  873. total += skb_queue_len(&sta->ps_tx_buf);
  874. }
  875. spin_unlock_bh(&local->sta_lock);
  876. local->total_ps_buffered = total;
  877. printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
  878. local->mdev->name, purged);
  879. }
  880. static inline ieee80211_txrx_result
  881. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
  882. {
  883. /* broadcast/multicast frame */
  884. /* If any of the associated stations is in power save mode,
  885. * the frame is buffered to be sent after DTIM beacon frame */
  886. if ((tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) &&
  887. tx->sdata->type != IEEE80211_IF_TYPE_WDS &&
  888. tx->sdata->bss && atomic_read(&tx->sdata->bss->num_sta_ps) &&
  889. !(tx->fc & IEEE80211_FCTL_ORDER)) {
  890. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  891. purge_old_ps_buffers(tx->local);
  892. if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
  893. AP_MAX_BC_BUFFER) {
  894. if (net_ratelimit()) {
  895. printk(KERN_DEBUG "%s: BC TX buffer full - "
  896. "dropping the oldest frame\n",
  897. tx->dev->name);
  898. }
  899. dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
  900. } else
  901. tx->local->total_ps_buffered++;
  902. skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
  903. return TXRX_QUEUED;
  904. }
  905. return TXRX_CONTINUE;
  906. }
  907. static inline ieee80211_txrx_result
  908. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
  909. {
  910. struct sta_info *sta = tx->sta;
  911. if (unlikely(!sta ||
  912. ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
  913. (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
  914. return TXRX_CONTINUE;
  915. if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
  916. struct ieee80211_tx_packet_data *pkt_data;
  917. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  918. printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS buffer (entries "
  919. "before %d)\n",
  920. MAC_ARG(sta->addr), sta->aid,
  921. skb_queue_len(&sta->ps_tx_buf));
  922. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  923. sta->flags |= WLAN_STA_TIM;
  924. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  925. purge_old_ps_buffers(tx->local);
  926. if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
  927. struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
  928. if (net_ratelimit()) {
  929. printk(KERN_DEBUG "%s: STA " MAC_FMT " TX "
  930. "buffer full - dropping oldest frame\n",
  931. tx->dev->name, MAC_ARG(sta->addr));
  932. }
  933. dev_kfree_skb(old);
  934. } else
  935. tx->local->total_ps_buffered++;
  936. /* Queue frame to be sent after STA sends an PS Poll frame */
  937. if (skb_queue_empty(&sta->ps_tx_buf)) {
  938. if (tx->local->ops->set_tim)
  939. tx->local->ops->set_tim(local_to_hw(tx->local),
  940. sta->aid, 1);
  941. if (tx->sdata->bss)
  942. bss_tim_set(tx->local, tx->sdata->bss, sta->aid);
  943. }
  944. pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
  945. pkt_data->jiffies = jiffies;
  946. skb_queue_tail(&sta->ps_tx_buf, tx->skb);
  947. return TXRX_QUEUED;
  948. }
  949. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  950. else if (unlikely(sta->flags & WLAN_STA_PS)) {
  951. printk(KERN_DEBUG "%s: STA " MAC_FMT " in PS mode, but pspoll "
  952. "set -> send frame\n", tx->dev->name,
  953. MAC_ARG(sta->addr));
  954. }
  955. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  956. sta->pspoll = 0;
  957. return TXRX_CONTINUE;
  958. }
  959. static ieee80211_txrx_result
  960. ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
  961. {
  962. if (unlikely(tx->u.tx.ps_buffered))
  963. return TXRX_CONTINUE;
  964. if (tx->u.tx.unicast)
  965. return ieee80211_tx_h_unicast_ps_buf(tx);
  966. else
  967. return ieee80211_tx_h_multicast_ps_buf(tx);
  968. }
  969. /*
  970. * deal with packet injection down monitor interface
  971. * with Radiotap Header -- only called for monitor mode interface
  972. */
  973. static ieee80211_txrx_result
  974. __ieee80211_parse_tx_radiotap(
  975. struct ieee80211_txrx_data *tx,
  976. struct sk_buff *skb, struct ieee80211_tx_control *control)
  977. {
  978. /*
  979. * this is the moment to interpret and discard the radiotap header that
  980. * must be at the start of the packet injected in Monitor mode
  981. *
  982. * Need to take some care with endian-ness since radiotap
  983. * args are little-endian
  984. */
  985. struct ieee80211_radiotap_iterator iterator;
  986. struct ieee80211_radiotap_header *rthdr =
  987. (struct ieee80211_radiotap_header *) skb->data;
  988. struct ieee80211_hw_mode *mode = tx->local->hw.conf.mode;
  989. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
  990. /*
  991. * default control situation for all injected packets
  992. * FIXME: this does not suit all usage cases, expand to allow control
  993. */
  994. control->retry_limit = 1; /* no retry */
  995. control->key_idx = -1; /* no encryption key */
  996. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  997. IEEE80211_TXCTL_USE_CTS_PROTECT);
  998. control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT |
  999. IEEE80211_TXCTL_NO_ACK;
  1000. control->antenna_sel_tx = 0; /* default to default antenna */
  1001. /*
  1002. * for every radiotap entry that is present
  1003. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  1004. * entries present, or -EINVAL on error)
  1005. */
  1006. while (!ret) {
  1007. int i, target_rate;
  1008. ret = ieee80211_radiotap_iterator_next(&iterator);
  1009. if (ret)
  1010. continue;
  1011. /* see if this argument is something we can use */
  1012. switch (iterator.this_arg_index) {
  1013. /*
  1014. * You must take care when dereferencing iterator.this_arg
  1015. * for multibyte types... the pointer is not aligned. Use
  1016. * get_unaligned((type *)iterator.this_arg) to dereference
  1017. * iterator.this_arg for type "type" safely on all arches.
  1018. */
  1019. case IEEE80211_RADIOTAP_RATE:
  1020. /*
  1021. * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
  1022. * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
  1023. */
  1024. target_rate = (*iterator.this_arg) * 5;
  1025. for (i = 0; i < mode->num_rates; i++) {
  1026. struct ieee80211_rate *r = &mode->rates[i];
  1027. if (r->rate > target_rate)
  1028. continue;
  1029. control->rate = r;
  1030. if (r->flags & IEEE80211_RATE_PREAMBLE2)
  1031. control->tx_rate = r->val2;
  1032. else
  1033. control->tx_rate = r->val;
  1034. /* end on exact match */
  1035. if (r->rate == target_rate)
  1036. i = mode->num_rates;
  1037. }
  1038. break;
  1039. case IEEE80211_RADIOTAP_ANTENNA:
  1040. /*
  1041. * radiotap uses 0 for 1st ant, mac80211 is 1 for
  1042. * 1st ant
  1043. */
  1044. control->antenna_sel_tx = (*iterator.this_arg) + 1;
  1045. break;
  1046. case IEEE80211_RADIOTAP_DBM_TX_POWER:
  1047. control->power_level = *iterator.this_arg;
  1048. break;
  1049. case IEEE80211_RADIOTAP_FLAGS:
  1050. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  1051. /*
  1052. * this indicates that the skb we have been
  1053. * handed has the 32-bit FCS CRC at the end...
  1054. * we should react to that by snipping it off
  1055. * because it will be recomputed and added
  1056. * on transmission
  1057. */
  1058. if (skb->len < (iterator.max_length + FCS_LEN))
  1059. return TXRX_DROP;
  1060. skb_trim(skb, skb->len - FCS_LEN);
  1061. }
  1062. break;
  1063. default:
  1064. break;
  1065. }
  1066. }
  1067. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  1068. return TXRX_DROP;
  1069. /*
  1070. * remove the radiotap header
  1071. * iterator->max_length was sanity-checked against
  1072. * skb->len by iterator init
  1073. */
  1074. skb_pull(skb, iterator.max_length);
  1075. return TXRX_CONTINUE;
  1076. }
  1077. static ieee80211_txrx_result inline
  1078. __ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  1079. struct sk_buff *skb,
  1080. struct net_device *dev,
  1081. struct ieee80211_tx_control *control)
  1082. {
  1083. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1084. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1085. struct ieee80211_sub_if_data *sdata;
  1086. ieee80211_txrx_result res = TXRX_CONTINUE;
  1087. int hdrlen;
  1088. memset(tx, 0, sizeof(*tx));
  1089. tx->skb = skb;
  1090. tx->dev = dev; /* use original interface */
  1091. tx->local = local;
  1092. tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1093. tx->sta = sta_info_get(local, hdr->addr1);
  1094. tx->fc = le16_to_cpu(hdr->frame_control);
  1095. /*
  1096. * set defaults for things that can be set by
  1097. * injected radiotap headers
  1098. */
  1099. control->power_level = local->hw.conf.power_level;
  1100. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  1101. if (local->sta_antenna_sel != STA_ANTENNA_SEL_AUTO && tx->sta)
  1102. control->antenna_sel_tx = tx->sta->antenna_sel_tx;
  1103. /* process and remove the injection radiotap header */
  1104. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1105. if (unlikely(sdata->type == IEEE80211_IF_TYPE_MNTR)) {
  1106. if (__ieee80211_parse_tx_radiotap(tx, skb, control) ==
  1107. TXRX_DROP) {
  1108. return TXRX_DROP;
  1109. }
  1110. /*
  1111. * we removed the radiotap header after this point,
  1112. * we filled control with what we could use
  1113. * set to the actual ieee header now
  1114. */
  1115. hdr = (struct ieee80211_hdr *) skb->data;
  1116. res = TXRX_QUEUED; /* indication it was monitor packet */
  1117. }
  1118. tx->u.tx.control = control;
  1119. tx->u.tx.unicast = !is_multicast_ether_addr(hdr->addr1);
  1120. if (is_multicast_ether_addr(hdr->addr1))
  1121. control->flags |= IEEE80211_TXCTL_NO_ACK;
  1122. else
  1123. control->flags &= ~IEEE80211_TXCTL_NO_ACK;
  1124. tx->fragmented = local->fragmentation_threshold <
  1125. IEEE80211_MAX_FRAG_THRESHOLD && tx->u.tx.unicast &&
  1126. skb->len + FCS_LEN > local->fragmentation_threshold &&
  1127. (!local->ops->set_frag_threshold);
  1128. if (!tx->sta)
  1129. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  1130. else if (tx->sta->clear_dst_mask) {
  1131. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  1132. tx->sta->clear_dst_mask = 0;
  1133. }
  1134. hdrlen = ieee80211_get_hdrlen(tx->fc);
  1135. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  1136. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  1137. tx->ethertype = (pos[0] << 8) | pos[1];
  1138. }
  1139. control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
  1140. return res;
  1141. }
  1142. static int inline is_ieee80211_device(struct net_device *dev,
  1143. struct net_device *master)
  1144. {
  1145. return (wdev_priv(dev->ieee80211_ptr) ==
  1146. wdev_priv(master->ieee80211_ptr));
  1147. }
  1148. /* Device in tx->dev has a reference added; use dev_put(tx->dev) when
  1149. * finished with it. */
  1150. static int inline ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  1151. struct sk_buff *skb,
  1152. struct net_device *mdev,
  1153. struct ieee80211_tx_control *control)
  1154. {
  1155. struct ieee80211_tx_packet_data *pkt_data;
  1156. struct net_device *dev;
  1157. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1158. dev = dev_get_by_index(pkt_data->ifindex);
  1159. if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
  1160. dev_put(dev);
  1161. dev = NULL;
  1162. }
  1163. if (unlikely(!dev))
  1164. return -ENODEV;
  1165. __ieee80211_tx_prepare(tx, skb, dev, control);
  1166. return 0;
  1167. }
  1168. static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
  1169. int queue)
  1170. {
  1171. return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  1172. }
  1173. static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
  1174. int queue)
  1175. {
  1176. return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
  1177. }
  1178. #define IEEE80211_TX_OK 0
  1179. #define IEEE80211_TX_AGAIN 1
  1180. #define IEEE80211_TX_FRAG_AGAIN 2
  1181. static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
  1182. struct ieee80211_txrx_data *tx)
  1183. {
  1184. struct ieee80211_tx_control *control = tx->u.tx.control;
  1185. int ret, i;
  1186. if (!ieee80211_qdisc_installed(local->mdev) &&
  1187. __ieee80211_queue_stopped(local, 0)) {
  1188. netif_stop_queue(local->mdev);
  1189. return IEEE80211_TX_AGAIN;
  1190. }
  1191. if (skb) {
  1192. ieee80211_dump_frame(local->mdev->name, "TX to low-level driver", skb);
  1193. ret = local->ops->tx(local_to_hw(local), skb, control);
  1194. if (ret)
  1195. return IEEE80211_TX_AGAIN;
  1196. local->mdev->trans_start = jiffies;
  1197. ieee80211_led_tx(local, 1);
  1198. }
  1199. if (tx->u.tx.extra_frag) {
  1200. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  1201. IEEE80211_TXCTL_USE_CTS_PROTECT |
  1202. IEEE80211_TXCTL_CLEAR_DST_MASK |
  1203. IEEE80211_TXCTL_FIRST_FRAGMENT);
  1204. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  1205. if (!tx->u.tx.extra_frag[i])
  1206. continue;
  1207. if (__ieee80211_queue_stopped(local, control->queue))
  1208. return IEEE80211_TX_FRAG_AGAIN;
  1209. if (i == tx->u.tx.num_extra_frag) {
  1210. control->tx_rate = tx->u.tx.last_frag_hwrate;
  1211. control->rate = tx->u.tx.last_frag_rate;
  1212. if (tx->u.tx.probe_last_frag)
  1213. control->flags |=
  1214. IEEE80211_TXCTL_RATE_CTRL_PROBE;
  1215. else
  1216. control->flags &=
  1217. ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  1218. }
  1219. ieee80211_dump_frame(local->mdev->name,
  1220. "TX to low-level driver",
  1221. tx->u.tx.extra_frag[i]);
  1222. ret = local->ops->tx(local_to_hw(local),
  1223. tx->u.tx.extra_frag[i],
  1224. control);
  1225. if (ret)
  1226. return IEEE80211_TX_FRAG_AGAIN;
  1227. local->mdev->trans_start = jiffies;
  1228. ieee80211_led_tx(local, 1);
  1229. tx->u.tx.extra_frag[i] = NULL;
  1230. }
  1231. kfree(tx->u.tx.extra_frag);
  1232. tx->u.tx.extra_frag = NULL;
  1233. }
  1234. return IEEE80211_TX_OK;
  1235. }
  1236. static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
  1237. struct ieee80211_tx_control *control, int mgmt)
  1238. {
  1239. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1240. struct sta_info *sta;
  1241. ieee80211_tx_handler *handler;
  1242. struct ieee80211_txrx_data tx;
  1243. ieee80211_txrx_result res = TXRX_DROP, res_prepare;
  1244. int ret, i;
  1245. WARN_ON(__ieee80211_queue_pending(local, control->queue));
  1246. if (unlikely(skb->len < 10)) {
  1247. dev_kfree_skb(skb);
  1248. return 0;
  1249. }
  1250. res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
  1251. if (res_prepare == TXRX_DROP) {
  1252. dev_kfree_skb(skb);
  1253. return 0;
  1254. }
  1255. sta = tx.sta;
  1256. tx.u.tx.mgmt_interface = mgmt;
  1257. tx.u.tx.mode = local->hw.conf.mode;
  1258. if (res_prepare == TXRX_QUEUED) { /* if it was an injected packet */
  1259. res = TXRX_CONTINUE;
  1260. } else {
  1261. for (handler = local->tx_handlers; *handler != NULL;
  1262. handler++) {
  1263. res = (*handler)(&tx);
  1264. if (res != TXRX_CONTINUE)
  1265. break;
  1266. }
  1267. }
  1268. skb = tx.skb; /* handlers are allowed to change skb */
  1269. if (sta)
  1270. sta_info_put(sta);
  1271. if (unlikely(res == TXRX_DROP)) {
  1272. I802_DEBUG_INC(local->tx_handlers_drop);
  1273. goto drop;
  1274. }
  1275. if (unlikely(res == TXRX_QUEUED)) {
  1276. I802_DEBUG_INC(local->tx_handlers_queued);
  1277. return 0;
  1278. }
  1279. if (tx.u.tx.extra_frag) {
  1280. for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
  1281. int next_len, dur;
  1282. struct ieee80211_hdr *hdr =
  1283. (struct ieee80211_hdr *)
  1284. tx.u.tx.extra_frag[i]->data;
  1285. if (i + 1 < tx.u.tx.num_extra_frag) {
  1286. next_len = tx.u.tx.extra_frag[i + 1]->len;
  1287. } else {
  1288. next_len = 0;
  1289. tx.u.tx.rate = tx.u.tx.last_frag_rate;
  1290. tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
  1291. }
  1292. dur = ieee80211_duration(&tx, 0, next_len);
  1293. hdr->duration_id = cpu_to_le16(dur);
  1294. }
  1295. }
  1296. retry:
  1297. ret = __ieee80211_tx(local, skb, &tx);
  1298. if (ret) {
  1299. struct ieee80211_tx_stored_packet *store =
  1300. &local->pending_packet[control->queue];
  1301. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1302. skb = NULL;
  1303. set_bit(IEEE80211_LINK_STATE_PENDING,
  1304. &local->state[control->queue]);
  1305. smp_mb();
  1306. /* When the driver gets out of buffers during sending of
  1307. * fragments and calls ieee80211_stop_queue, there is
  1308. * a small window between IEEE80211_LINK_STATE_XOFF and
  1309. * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
  1310. * gets available in that window (i.e. driver calls
  1311. * ieee80211_wake_queue), we would end up with ieee80211_tx
  1312. * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
  1313. * continuing transmitting here when that situation is
  1314. * possible to have happened. */
  1315. if (!__ieee80211_queue_stopped(local, control->queue)) {
  1316. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1317. &local->state[control->queue]);
  1318. goto retry;
  1319. }
  1320. memcpy(&store->control, control,
  1321. sizeof(struct ieee80211_tx_control));
  1322. store->skb = skb;
  1323. store->extra_frag = tx.u.tx.extra_frag;
  1324. store->num_extra_frag = tx.u.tx.num_extra_frag;
  1325. store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
  1326. store->last_frag_rate = tx.u.tx.last_frag_rate;
  1327. store->last_frag_rate_ctrl_probe = tx.u.tx.probe_last_frag;
  1328. }
  1329. return 0;
  1330. drop:
  1331. if (skb)
  1332. dev_kfree_skb(skb);
  1333. for (i = 0; i < tx.u.tx.num_extra_frag; i++)
  1334. if (tx.u.tx.extra_frag[i])
  1335. dev_kfree_skb(tx.u.tx.extra_frag[i]);
  1336. kfree(tx.u.tx.extra_frag);
  1337. return 0;
  1338. }
  1339. static void ieee80211_tx_pending(unsigned long data)
  1340. {
  1341. struct ieee80211_local *local = (struct ieee80211_local *)data;
  1342. struct net_device *dev = local->mdev;
  1343. struct ieee80211_tx_stored_packet *store;
  1344. struct ieee80211_txrx_data tx;
  1345. int i, ret, reschedule = 0;
  1346. netif_tx_lock_bh(dev);
  1347. for (i = 0; i < local->hw.queues; i++) {
  1348. if (__ieee80211_queue_stopped(local, i))
  1349. continue;
  1350. if (!__ieee80211_queue_pending(local, i)) {
  1351. reschedule = 1;
  1352. continue;
  1353. }
  1354. store = &local->pending_packet[i];
  1355. tx.u.tx.control = &store->control;
  1356. tx.u.tx.extra_frag = store->extra_frag;
  1357. tx.u.tx.num_extra_frag = store->num_extra_frag;
  1358. tx.u.tx.last_frag_hwrate = store->last_frag_hwrate;
  1359. tx.u.tx.last_frag_rate = store->last_frag_rate;
  1360. tx.u.tx.probe_last_frag = store->last_frag_rate_ctrl_probe;
  1361. ret = __ieee80211_tx(local, store->skb, &tx);
  1362. if (ret) {
  1363. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1364. store->skb = NULL;
  1365. } else {
  1366. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1367. &local->state[i]);
  1368. reschedule = 1;
  1369. }
  1370. }
  1371. netif_tx_unlock_bh(dev);
  1372. if (reschedule) {
  1373. if (!ieee80211_qdisc_installed(dev)) {
  1374. if (!__ieee80211_queue_stopped(local, 0))
  1375. netif_wake_queue(dev);
  1376. } else
  1377. netif_schedule(dev);
  1378. }
  1379. }
  1380. static void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  1381. {
  1382. int i, j;
  1383. struct ieee80211_tx_stored_packet *store;
  1384. for (i = 0; i < local->hw.queues; i++) {
  1385. if (!__ieee80211_queue_pending(local, i))
  1386. continue;
  1387. store = &local->pending_packet[i];
  1388. kfree_skb(store->skb);
  1389. for (j = 0; j < store->num_extra_frag; j++)
  1390. kfree_skb(store->extra_frag[j]);
  1391. kfree(store->extra_frag);
  1392. clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
  1393. }
  1394. }
  1395. static int ieee80211_master_start_xmit(struct sk_buff *skb,
  1396. struct net_device *dev)
  1397. {
  1398. struct ieee80211_tx_control control;
  1399. struct ieee80211_tx_packet_data *pkt_data;
  1400. struct net_device *odev = NULL;
  1401. struct ieee80211_sub_if_data *osdata;
  1402. int headroom;
  1403. int ret;
  1404. /*
  1405. * copy control out of the skb so other people can use skb->cb
  1406. */
  1407. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1408. memset(&control, 0, sizeof(struct ieee80211_tx_control));
  1409. if (pkt_data->ifindex)
  1410. odev = dev_get_by_index(pkt_data->ifindex);
  1411. if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
  1412. dev_put(odev);
  1413. odev = NULL;
  1414. }
  1415. if (unlikely(!odev)) {
  1416. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1417. printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
  1418. "originating device\n", dev->name);
  1419. #endif
  1420. dev_kfree_skb(skb);
  1421. return 0;
  1422. }
  1423. osdata = IEEE80211_DEV_TO_SUB_IF(odev);
  1424. headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
  1425. if (skb_headroom(skb) < headroom) {
  1426. if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
  1427. dev_kfree_skb(skb);
  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. local->ops->add_interface) {
  2251. conf.if_id = -1;
  2252. conf.type = IEEE80211_IF_TYPE_MNTR;
  2253. conf.mac_addr = NULL;
  2254. local->ops->add_interface(local_to_hw(local), &conf);
  2255. }
  2256. }
  2257. static int ieee80211_open(struct net_device *dev)
  2258. {
  2259. struct ieee80211_sub_if_data *sdata, *nsdata;
  2260. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2261. struct ieee80211_if_init_conf conf;
  2262. int res;
  2263. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  2264. read_lock(&local->sub_if_lock);
  2265. list_for_each_entry(nsdata, &local->sub_if_list, list) {
  2266. struct net_device *ndev = nsdata->dev;
  2267. if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
  2268. compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
  2269. !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
  2270. read_unlock(&local->sub_if_lock);
  2271. return -ENOTUNIQ;
  2272. }
  2273. }
  2274. read_unlock(&local->sub_if_lock);
  2275. if (sdata->type == IEEE80211_IF_TYPE_WDS &&
  2276. is_zero_ether_addr(sdata->u.wds.remote_addr))
  2277. return -ENOLINK;
  2278. if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
  2279. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  2280. /* run the interface in a "soft monitor" mode */
  2281. local->monitors++;
  2282. local->open_count++;
  2283. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  2284. return 0;
  2285. }
  2286. ieee80211_start_soft_monitor(local);
  2287. if (local->ops->add_interface) {
  2288. conf.if_id = dev->ifindex;
  2289. conf.type = sdata->type;
  2290. conf.mac_addr = dev->dev_addr;
  2291. res = local->ops->add_interface(local_to_hw(local), &conf);
  2292. if (res) {
  2293. if (sdata->type == IEEE80211_IF_TYPE_MNTR)
  2294. ieee80211_start_hard_monitor(local);
  2295. return res;
  2296. }
  2297. } else {
  2298. if (sdata->type != IEEE80211_IF_TYPE_STA)
  2299. return -EOPNOTSUPP;
  2300. if (local->open_count > 0)
  2301. return -ENOBUFS;
  2302. }
  2303. if (local->open_count == 0) {
  2304. res = 0;
  2305. tasklet_enable(&local->tx_pending_tasklet);
  2306. tasklet_enable(&local->tasklet);
  2307. if (local->ops->open)
  2308. res = local->ops->open(local_to_hw(local));
  2309. if (res == 0) {
  2310. res = dev_open(local->mdev);
  2311. if (res) {
  2312. if (local->ops->stop)
  2313. local->ops->stop(local_to_hw(local));
  2314. } else {
  2315. res = ieee80211_hw_config(local);
  2316. if (res && local->ops->stop)
  2317. local->ops->stop(local_to_hw(local));
  2318. else if (!res && local->apdev)
  2319. dev_open(local->apdev);
  2320. }
  2321. }
  2322. if (res) {
  2323. if (local->ops->remove_interface)
  2324. local->ops->remove_interface(local_to_hw(local),
  2325. &conf);
  2326. return res;
  2327. }
  2328. }
  2329. local->open_count++;
  2330. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  2331. local->monitors++;
  2332. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  2333. } else
  2334. ieee80211_if_config(dev);
  2335. if (sdata->type == IEEE80211_IF_TYPE_STA &&
  2336. !local->user_space_mlme)
  2337. netif_carrier_off(dev);
  2338. else
  2339. netif_carrier_on(dev);
  2340. netif_start_queue(dev);
  2341. return 0;
  2342. }
  2343. static int ieee80211_stop(struct net_device *dev)
  2344. {
  2345. struct ieee80211_sub_if_data *sdata;
  2346. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2347. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  2348. if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
  2349. local->open_count > 1 &&
  2350. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  2351. /* remove "soft monitor" interface */
  2352. local->open_count--;
  2353. local->monitors--;
  2354. if (!local->monitors)
  2355. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  2356. return 0;
  2357. }
  2358. netif_stop_queue(dev);
  2359. ieee80211_if_shutdown(dev);
  2360. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  2361. local->monitors--;
  2362. if (!local->monitors)
  2363. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  2364. }
  2365. local->open_count--;
  2366. if (local->open_count == 0) {
  2367. if (netif_running(local->mdev))
  2368. dev_close(local->mdev);
  2369. if (local->apdev)
  2370. dev_close(local->apdev);
  2371. if (local->ops->stop)
  2372. local->ops->stop(local_to_hw(local));
  2373. tasklet_disable(&local->tx_pending_tasklet);
  2374. tasklet_disable(&local->tasklet);
  2375. }
  2376. if (local->ops->remove_interface) {
  2377. struct ieee80211_if_init_conf conf;
  2378. conf.if_id = dev->ifindex;
  2379. conf.type = sdata->type;
  2380. conf.mac_addr = dev->dev_addr;
  2381. local->ops->remove_interface(local_to_hw(local), &conf);
  2382. }
  2383. ieee80211_start_hard_monitor(local);
  2384. return 0;
  2385. }
  2386. static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
  2387. {
  2388. memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
  2389. return ETH_ALEN;
  2390. }
  2391. static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
  2392. {
  2393. return compare_ether_addr(raddr, addr) == 0 ||
  2394. is_broadcast_ether_addr(raddr);
  2395. }
  2396. static ieee80211_txrx_result
  2397. ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
  2398. {
  2399. struct net_device *dev = rx->dev;
  2400. struct ieee80211_local *local = rx->local;
  2401. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  2402. u16 fc, hdrlen, ethertype;
  2403. u8 *payload;
  2404. u8 dst[ETH_ALEN];
  2405. u8 src[ETH_ALEN];
  2406. struct sk_buff *skb = rx->skb, *skb2;
  2407. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  2408. fc = rx->fc;
  2409. if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
  2410. return TXRX_CONTINUE;
  2411. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  2412. return TXRX_DROP;
  2413. hdrlen = ieee80211_get_hdrlen(fc);
  2414. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  2415. * header
  2416. * IEEE 802.11 address fields:
  2417. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  2418. * 0 0 DA SA BSSID n/a
  2419. * 0 1 DA BSSID SA n/a
  2420. * 1 0 BSSID SA DA n/a
  2421. * 1 1 RA TA DA SA
  2422. */
  2423. switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  2424. case IEEE80211_FCTL_TODS:
  2425. /* BSSID SA DA */
  2426. memcpy(dst, hdr->addr3, ETH_ALEN);
  2427. memcpy(src, hdr->addr2, ETH_ALEN);
  2428. if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
  2429. sdata->type != IEEE80211_IF_TYPE_VLAN)) {
  2430. printk(KERN_DEBUG "%s: dropped ToDS frame (BSSID="
  2431. MAC_FMT " SA=" MAC_FMT " DA=" MAC_FMT ")\n",
  2432. dev->name, MAC_ARG(hdr->addr1),
  2433. MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3));
  2434. return TXRX_DROP;
  2435. }
  2436. break;
  2437. case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  2438. /* RA TA DA SA */
  2439. memcpy(dst, hdr->addr3, ETH_ALEN);
  2440. memcpy(src, hdr->addr4, ETH_ALEN);
  2441. if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
  2442. printk(KERN_DEBUG "%s: dropped FromDS&ToDS frame (RA="
  2443. MAC_FMT " TA=" MAC_FMT " DA=" MAC_FMT " SA="
  2444. MAC_FMT ")\n",
  2445. rx->dev->name, MAC_ARG(hdr->addr1),
  2446. MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3),
  2447. MAC_ARG(hdr->addr4));
  2448. return TXRX_DROP;
  2449. }
  2450. break;
  2451. case IEEE80211_FCTL_FROMDS:
  2452. /* DA BSSID SA */
  2453. memcpy(dst, hdr->addr1, ETH_ALEN);
  2454. memcpy(src, hdr->addr3, ETH_ALEN);
  2455. if (sdata->type != IEEE80211_IF_TYPE_STA) {
  2456. return TXRX_DROP;
  2457. }
  2458. break;
  2459. case 0:
  2460. /* DA SA BSSID */
  2461. memcpy(dst, hdr->addr1, ETH_ALEN);
  2462. memcpy(src, hdr->addr2, ETH_ALEN);
  2463. if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
  2464. if (net_ratelimit()) {
  2465. printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
  2466. MAC_FMT " SA=" MAC_FMT " BSSID=" MAC_FMT
  2467. ")\n",
  2468. dev->name, MAC_ARG(hdr->addr1),
  2469. MAC_ARG(hdr->addr2),
  2470. MAC_ARG(hdr->addr3));
  2471. }
  2472. return TXRX_DROP;
  2473. }
  2474. break;
  2475. }
  2476. payload = skb->data + hdrlen;
  2477. if (unlikely(skb->len - hdrlen < 8)) {
  2478. if (net_ratelimit()) {
  2479. printk(KERN_DEBUG "%s: RX too short data frame "
  2480. "payload\n", dev->name);
  2481. }
  2482. return TXRX_DROP;
  2483. }
  2484. ethertype = (payload[6] << 8) | payload[7];
  2485. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  2486. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  2487. compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
  2488. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  2489. * replace EtherType */
  2490. skb_pull(skb, hdrlen + 6);
  2491. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  2492. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  2493. } else {
  2494. struct ethhdr *ehdr;
  2495. __be16 len;
  2496. skb_pull(skb, hdrlen);
  2497. len = htons(skb->len);
  2498. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  2499. memcpy(ehdr->h_dest, dst, ETH_ALEN);
  2500. memcpy(ehdr->h_source, src, ETH_ALEN);
  2501. ehdr->h_proto = len;
  2502. }
  2503. skb->dev = dev;
  2504. skb2 = NULL;
  2505. sdata->stats.rx_packets++;
  2506. sdata->stats.rx_bytes += skb->len;
  2507. if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
  2508. || sdata->type == IEEE80211_IF_TYPE_VLAN) && rx->u.rx.ra_match) {
  2509. if (is_multicast_ether_addr(skb->data)) {
  2510. /* send multicast frames both to higher layers in
  2511. * local net stack and back to the wireless media */
  2512. skb2 = skb_copy(skb, GFP_ATOMIC);
  2513. if (!skb2)
  2514. printk(KERN_DEBUG "%s: failed to clone "
  2515. "multicast frame\n", dev->name);
  2516. } else {
  2517. struct sta_info *dsta;
  2518. dsta = sta_info_get(local, skb->data);
  2519. if (dsta && !dsta->dev) {
  2520. printk(KERN_DEBUG "Station with null dev "
  2521. "structure!\n");
  2522. } else if (dsta && dsta->dev == dev) {
  2523. /* Destination station is associated to this
  2524. * AP, so send the frame directly to it and
  2525. * do not pass the frame to local net stack.
  2526. */
  2527. skb2 = skb;
  2528. skb = NULL;
  2529. }
  2530. if (dsta)
  2531. sta_info_put(dsta);
  2532. }
  2533. }
  2534. if (skb) {
  2535. /* deliver to local stack */
  2536. skb->protocol = eth_type_trans(skb, dev);
  2537. memset(skb->cb, 0, sizeof(skb->cb));
  2538. netif_rx(skb);
  2539. }
  2540. if (skb2) {
  2541. /* send to wireless media */
  2542. skb2->protocol = __constant_htons(ETH_P_802_3);
  2543. skb_set_network_header(skb2, 0);
  2544. skb_set_mac_header(skb2, 0);
  2545. dev_queue_xmit(skb2);
  2546. }
  2547. return TXRX_QUEUED;
  2548. }
  2549. static struct ieee80211_rate *
  2550. ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
  2551. {
  2552. struct ieee80211_hw_mode *mode;
  2553. int r;
  2554. list_for_each_entry(mode, &local->modes_list, list) {
  2555. if (mode->mode != phymode)
  2556. continue;
  2557. for (r = 0; r < mode->num_rates; r++) {
  2558. struct ieee80211_rate *rate = &mode->rates[r];
  2559. if (rate->val == hw_rate ||
  2560. (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
  2561. rate->val2 == hw_rate))
  2562. return rate;
  2563. }
  2564. }
  2565. return NULL;
  2566. }
  2567. static void
  2568. ieee80211_fill_frame_info(struct ieee80211_local *local,
  2569. struct ieee80211_frame_info *fi,
  2570. struct ieee80211_rx_status *status)
  2571. {
  2572. if (status) {
  2573. struct timespec ts;
  2574. struct ieee80211_rate *rate;
  2575. jiffies_to_timespec(jiffies, &ts);
  2576. fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
  2577. ts.tv_nsec / 1000);
  2578. fi->mactime = cpu_to_be64(status->mactime);
  2579. switch (status->phymode) {
  2580. case MODE_IEEE80211A:
  2581. fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
  2582. break;
  2583. case MODE_IEEE80211B:
  2584. fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
  2585. break;
  2586. case MODE_IEEE80211G:
  2587. fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
  2588. break;
  2589. case MODE_ATHEROS_TURBO:
  2590. fi->phytype =
  2591. htonl(ieee80211_phytype_dsss_dot11_turbo);
  2592. break;
  2593. default:
  2594. fi->phytype = htonl(0xAAAAAAAA);
  2595. break;
  2596. }
  2597. fi->channel = htonl(status->channel);
  2598. rate = ieee80211_get_rate(local, status->phymode,
  2599. status->rate);
  2600. if (rate) {
  2601. fi->datarate = htonl(rate->rate);
  2602. if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
  2603. if (status->rate == rate->val)
  2604. fi->preamble = htonl(2); /* long */
  2605. else if (status->rate == rate->val2)
  2606. fi->preamble = htonl(1); /* short */
  2607. } else
  2608. fi->preamble = htonl(0);
  2609. } else {
  2610. fi->datarate = htonl(0);
  2611. fi->preamble = htonl(0);
  2612. }
  2613. fi->antenna = htonl(status->antenna);
  2614. fi->priority = htonl(0xffffffff); /* no clue */
  2615. fi->ssi_type = htonl(ieee80211_ssi_raw);
  2616. fi->ssi_signal = htonl(status->ssi);
  2617. fi->ssi_noise = 0x00000000;
  2618. fi->encoding = 0;
  2619. } else {
  2620. /* clear everything because we really don't know.
  2621. * the msg_type field isn't present on monitor frames
  2622. * so we don't know whether it will be present or not,
  2623. * but it's ok to not clear it since it'll be assigned
  2624. * anyway */
  2625. memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
  2626. fi->ssi_type = htonl(ieee80211_ssi_none);
  2627. }
  2628. fi->version = htonl(IEEE80211_FI_VERSION);
  2629. fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
  2630. }
  2631. /* this routine is actually not just for this, but also
  2632. * for pushing fake 'management' frames into userspace.
  2633. * it shall be replaced by a netlink-based system. */
  2634. void
  2635. ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
  2636. struct ieee80211_rx_status *status, u32 msg_type)
  2637. {
  2638. struct ieee80211_frame_info *fi;
  2639. const size_t hlen = sizeof(struct ieee80211_frame_info);
  2640. struct ieee80211_sub_if_data *sdata;
  2641. skb->dev = local->apdev;
  2642. sdata = IEEE80211_DEV_TO_SUB_IF(local->apdev);
  2643. if (skb_headroom(skb) < hlen) {
  2644. I802_DEBUG_INC(local->rx_expand_skb_head);
  2645. if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
  2646. dev_kfree_skb(skb);
  2647. return;
  2648. }
  2649. }
  2650. fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
  2651. ieee80211_fill_frame_info(local, fi, status);
  2652. fi->msg_type = htonl(msg_type);
  2653. sdata->stats.rx_packets++;
  2654. sdata->stats.rx_bytes += skb->len;
  2655. skb_set_mac_header(skb, 0);
  2656. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2657. skb->pkt_type = PACKET_OTHERHOST;
  2658. skb->protocol = htons(ETH_P_802_2);
  2659. memset(skb->cb, 0, sizeof(skb->cb));
  2660. netif_rx(skb);
  2661. }
  2662. static void
  2663. ieee80211_rx_monitor(struct net_device *dev, struct sk_buff *skb,
  2664. struct ieee80211_rx_status *status)
  2665. {
  2666. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2667. struct ieee80211_sub_if_data *sdata;
  2668. struct ieee80211_rate *rate;
  2669. struct ieee80211_rtap_hdr {
  2670. struct ieee80211_radiotap_header hdr;
  2671. u8 flags;
  2672. u8 rate;
  2673. __le16 chan_freq;
  2674. __le16 chan_flags;
  2675. u8 antsignal;
  2676. } __attribute__ ((packed)) *rthdr;
  2677. skb->dev = dev;
  2678. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  2679. if (status->flag & RX_FLAG_RADIOTAP)
  2680. goto out;
  2681. if (skb_headroom(skb) < sizeof(*rthdr)) {
  2682. I802_DEBUG_INC(local->rx_expand_skb_head);
  2683. if (pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC)) {
  2684. dev_kfree_skb(skb);
  2685. return;
  2686. }
  2687. }
  2688. rthdr = (struct ieee80211_rtap_hdr *) skb_push(skb, sizeof(*rthdr));
  2689. memset(rthdr, 0, sizeof(*rthdr));
  2690. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  2691. rthdr->hdr.it_present =
  2692. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  2693. (1 << IEEE80211_RADIOTAP_RATE) |
  2694. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  2695. (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL));
  2696. rthdr->flags = local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS ?
  2697. IEEE80211_RADIOTAP_F_FCS : 0;
  2698. rate = ieee80211_get_rate(local, status->phymode, status->rate);
  2699. if (rate)
  2700. rthdr->rate = rate->rate / 5;
  2701. rthdr->chan_freq = cpu_to_le16(status->freq);
  2702. rthdr->chan_flags =
  2703. status->phymode == MODE_IEEE80211A ?
  2704. cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ) :
  2705. cpu_to_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ);
  2706. rthdr->antsignal = status->ssi;
  2707. out:
  2708. sdata->stats.rx_packets++;
  2709. sdata->stats.rx_bytes += skb->len;
  2710. skb_set_mac_header(skb, 0);
  2711. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2712. skb->pkt_type = PACKET_OTHERHOST;
  2713. skb->protocol = htons(ETH_P_802_2);
  2714. memset(skb->cb, 0, sizeof(skb->cb));
  2715. netif_rx(skb);
  2716. }
  2717. int ieee80211_radar_status(struct ieee80211_hw *hw, int channel,
  2718. int radar, int radar_type)
  2719. {
  2720. struct sk_buff *skb;
  2721. struct ieee80211_radar_info *msg;
  2722. struct ieee80211_local *local = hw_to_local(hw);
  2723. if (!local->apdev)
  2724. return 0;
  2725. skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
  2726. sizeof(struct ieee80211_radar_info));
  2727. if (!skb)
  2728. return -ENOMEM;
  2729. skb_reserve(skb, sizeof(struct ieee80211_frame_info));
  2730. msg = (struct ieee80211_radar_info *)
  2731. skb_put(skb, sizeof(struct ieee80211_radar_info));
  2732. msg->channel = channel;
  2733. msg->radar = radar;
  2734. msg->radar_type = radar_type;
  2735. ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_radar);
  2736. return 0;
  2737. }
  2738. EXPORT_SYMBOL(ieee80211_radar_status);
  2739. static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
  2740. {
  2741. struct ieee80211_sub_if_data *sdata;
  2742. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  2743. if (sdata->bss)
  2744. atomic_inc(&sdata->bss->num_sta_ps);
  2745. sta->flags |= WLAN_STA_PS;
  2746. sta->pspoll = 0;
  2747. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  2748. printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d enters power "
  2749. "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
  2750. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  2751. }
  2752. static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
  2753. {
  2754. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  2755. struct sk_buff *skb;
  2756. int sent = 0;
  2757. struct ieee80211_sub_if_data *sdata;
  2758. struct ieee80211_tx_packet_data *pkt_data;
  2759. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  2760. if (sdata->bss)
  2761. atomic_dec(&sdata->bss->num_sta_ps);
  2762. sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
  2763. sta->pspoll = 0;
  2764. if (!skb_queue_empty(&sta->ps_tx_buf)) {
  2765. if (local->ops->set_tim)
  2766. local->ops->set_tim(local_to_hw(local), sta->aid, 0);
  2767. if (sdata->bss)
  2768. bss_tim_clear(local, sdata->bss, sta->aid);
  2769. }
  2770. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  2771. printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d exits power "
  2772. "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
  2773. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  2774. /* Send all buffered frames to the station */
  2775. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
  2776. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  2777. sent++;
  2778. pkt_data->requeue = 1;
  2779. dev_queue_xmit(skb);
  2780. }
  2781. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  2782. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  2783. local->total_ps_buffered--;
  2784. sent++;
  2785. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  2786. printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d send PS frame "
  2787. "since STA not sleeping anymore\n", dev->name,
  2788. MAC_ARG(sta->addr), sta->aid);
  2789. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  2790. pkt_data->requeue = 1;
  2791. dev_queue_xmit(skb);
  2792. }
  2793. return sent;
  2794. }
  2795. static ieee80211_txrx_result
  2796. ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
  2797. {
  2798. struct sk_buff *skb;
  2799. int no_pending_pkts;
  2800. if (likely(!rx->sta ||
  2801. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
  2802. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
  2803. !rx->u.rx.ra_match))
  2804. return TXRX_CONTINUE;
  2805. skb = skb_dequeue(&rx->sta->tx_filtered);
  2806. if (!skb) {
  2807. skb = skb_dequeue(&rx->sta->ps_tx_buf);
  2808. if (skb)
  2809. rx->local->total_ps_buffered--;
  2810. }
  2811. no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
  2812. skb_queue_empty(&rx->sta->ps_tx_buf);
  2813. if (skb) {
  2814. struct ieee80211_hdr *hdr =
  2815. (struct ieee80211_hdr *) skb->data;
  2816. /* tell TX path to send one frame even though the STA may
  2817. * still remain is PS mode after this frame exchange */
  2818. rx->sta->pspoll = 1;
  2819. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  2820. printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS Poll (entries "
  2821. "after %d)\n",
  2822. MAC_ARG(rx->sta->addr), rx->sta->aid,
  2823. skb_queue_len(&rx->sta->ps_tx_buf));
  2824. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  2825. /* Use MoreData flag to indicate whether there are more
  2826. * buffered frames for this STA */
  2827. if (no_pending_pkts) {
  2828. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  2829. rx->sta->flags &= ~WLAN_STA_TIM;
  2830. } else
  2831. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  2832. dev_queue_xmit(skb);
  2833. if (no_pending_pkts) {
  2834. if (rx->local->ops->set_tim)
  2835. rx->local->ops->set_tim(local_to_hw(rx->local),
  2836. rx->sta->aid, 0);
  2837. if (rx->sdata->bss)
  2838. bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
  2839. }
  2840. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  2841. } else if (!rx->u.rx.sent_ps_buffered) {
  2842. printk(KERN_DEBUG "%s: STA " MAC_FMT " sent PS Poll even "
  2843. "though there is no buffered frames for it\n",
  2844. rx->dev->name, MAC_ARG(rx->sta->addr));
  2845. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  2846. }
  2847. /* Free PS Poll skb here instead of returning TXRX_DROP that would
  2848. * count as an dropped frame. */
  2849. dev_kfree_skb(rx->skb);
  2850. return TXRX_QUEUED;
  2851. }
  2852. static inline struct ieee80211_fragment_entry *
  2853. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  2854. unsigned int frag, unsigned int seq, int rx_queue,
  2855. struct sk_buff **skb)
  2856. {
  2857. struct ieee80211_fragment_entry *entry;
  2858. int idx;
  2859. idx = sdata->fragment_next;
  2860. entry = &sdata->fragments[sdata->fragment_next++];
  2861. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  2862. sdata->fragment_next = 0;
  2863. if (!skb_queue_empty(&entry->skb_list)) {
  2864. #ifdef CONFIG_MAC80211_DEBUG
  2865. struct ieee80211_hdr *hdr =
  2866. (struct ieee80211_hdr *) entry->skb_list.next->data;
  2867. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  2868. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  2869. "addr1=" MAC_FMT " addr2=" MAC_FMT "\n",
  2870. sdata->dev->name, idx,
  2871. jiffies - entry->first_frag_time, entry->seq,
  2872. entry->last_frag, MAC_ARG(hdr->addr1),
  2873. MAC_ARG(hdr->addr2));
  2874. #endif /* CONFIG_MAC80211_DEBUG */
  2875. __skb_queue_purge(&entry->skb_list);
  2876. }
  2877. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  2878. *skb = NULL;
  2879. entry->first_frag_time = jiffies;
  2880. entry->seq = seq;
  2881. entry->rx_queue = rx_queue;
  2882. entry->last_frag = frag;
  2883. entry->ccmp = 0;
  2884. entry->extra_len = 0;
  2885. return entry;
  2886. }
  2887. static inline struct ieee80211_fragment_entry *
  2888. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  2889. u16 fc, unsigned int frag, unsigned int seq,
  2890. int rx_queue, struct ieee80211_hdr *hdr)
  2891. {
  2892. struct ieee80211_fragment_entry *entry;
  2893. int i, idx;
  2894. idx = sdata->fragment_next;
  2895. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  2896. struct ieee80211_hdr *f_hdr;
  2897. u16 f_fc;
  2898. idx--;
  2899. if (idx < 0)
  2900. idx = IEEE80211_FRAGMENT_MAX - 1;
  2901. entry = &sdata->fragments[idx];
  2902. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  2903. entry->rx_queue != rx_queue ||
  2904. entry->last_frag + 1 != frag)
  2905. continue;
  2906. f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
  2907. f_fc = le16_to_cpu(f_hdr->frame_control);
  2908. if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
  2909. compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
  2910. compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
  2911. continue;
  2912. if (entry->first_frag_time + 2 * HZ < jiffies) {
  2913. __skb_queue_purge(&entry->skb_list);
  2914. continue;
  2915. }
  2916. return entry;
  2917. }
  2918. return NULL;
  2919. }
  2920. static ieee80211_txrx_result
  2921. ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
  2922. {
  2923. struct ieee80211_hdr *hdr;
  2924. u16 sc;
  2925. unsigned int frag, seq;
  2926. struct ieee80211_fragment_entry *entry;
  2927. struct sk_buff *skb;
  2928. hdr = (struct ieee80211_hdr *) rx->skb->data;
  2929. sc = le16_to_cpu(hdr->seq_ctrl);
  2930. frag = sc & IEEE80211_SCTL_FRAG;
  2931. if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
  2932. (rx->skb)->len < 24 ||
  2933. is_multicast_ether_addr(hdr->addr1))) {
  2934. /* not fragmented */
  2935. goto out;
  2936. }
  2937. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  2938. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  2939. if (frag == 0) {
  2940. /* This is the first fragment of a new frame. */
  2941. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  2942. rx->u.rx.queue, &(rx->skb));
  2943. if (rx->key && rx->key->alg == ALG_CCMP &&
  2944. (rx->fc & IEEE80211_FCTL_PROTECTED)) {
  2945. /* Store CCMP PN so that we can verify that the next
  2946. * fragment has a sequential PN value. */
  2947. entry->ccmp = 1;
  2948. memcpy(entry->last_pn,
  2949. rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
  2950. CCMP_PN_LEN);
  2951. }
  2952. return TXRX_QUEUED;
  2953. }
  2954. /* This is a fragment for a frame that should already be pending in
  2955. * fragment cache. Add this fragment to the end of the pending entry.
  2956. */
  2957. entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
  2958. rx->u.rx.queue, hdr);
  2959. if (!entry) {
  2960. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  2961. return TXRX_DROP;
  2962. }
  2963. /* Verify that MPDUs within one MSDU have sequential PN values.
  2964. * (IEEE 802.11i, 8.3.3.4.5) */
  2965. if (entry->ccmp) {
  2966. int i;
  2967. u8 pn[CCMP_PN_LEN], *rpn;
  2968. if (!rx->key || rx->key->alg != ALG_CCMP)
  2969. return TXRX_DROP;
  2970. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  2971. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  2972. pn[i]++;
  2973. if (pn[i])
  2974. break;
  2975. }
  2976. rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
  2977. if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
  2978. printk(KERN_DEBUG "%s: defrag: CCMP PN not sequential"
  2979. " A2=" MAC_FMT " PN=%02x%02x%02x%02x%02x%02x "
  2980. "(expected %02x%02x%02x%02x%02x%02x)\n",
  2981. rx->dev->name, MAC_ARG(hdr->addr2),
  2982. rpn[0], rpn[1], rpn[2], rpn[3], rpn[4], rpn[5],
  2983. pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
  2984. return TXRX_DROP;
  2985. }
  2986. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  2987. }
  2988. skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
  2989. __skb_queue_tail(&entry->skb_list, rx->skb);
  2990. entry->last_frag = frag;
  2991. entry->extra_len += rx->skb->len;
  2992. if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
  2993. rx->skb = NULL;
  2994. return TXRX_QUEUED;
  2995. }
  2996. rx->skb = __skb_dequeue(&entry->skb_list);
  2997. if (skb_tailroom(rx->skb) < entry->extra_len) {
  2998. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  2999. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  3000. GFP_ATOMIC))) {
  3001. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  3002. __skb_queue_purge(&entry->skb_list);
  3003. return TXRX_DROP;
  3004. }
  3005. }
  3006. while ((skb = __skb_dequeue(&entry->skb_list))) {
  3007. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  3008. dev_kfree_skb(skb);
  3009. }
  3010. /* Complete frame has been reassembled - process it now */
  3011. rx->fragmented = 1;
  3012. out:
  3013. if (rx->sta)
  3014. rx->sta->rx_packets++;
  3015. if (is_multicast_ether_addr(hdr->addr1))
  3016. rx->local->dot11MulticastReceivedFrameCount++;
  3017. else
  3018. ieee80211_led_rx(rx->local);
  3019. return TXRX_CONTINUE;
  3020. }
  3021. static ieee80211_txrx_result
  3022. ieee80211_rx_h_monitor(struct ieee80211_txrx_data *rx)
  3023. {
  3024. if (rx->sdata->type == IEEE80211_IF_TYPE_MNTR) {
  3025. ieee80211_rx_monitor(rx->dev, rx->skb, rx->u.rx.status);
  3026. return TXRX_QUEUED;
  3027. }
  3028. if (rx->u.rx.status->flag & RX_FLAG_RADIOTAP)
  3029. skb_pull(rx->skb, ieee80211_get_radiotap_len(rx->skb));
  3030. return TXRX_CONTINUE;
  3031. }
  3032. static ieee80211_txrx_result
  3033. ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
  3034. {
  3035. struct ieee80211_hdr *hdr;
  3036. int always_sta_key;
  3037. hdr = (struct ieee80211_hdr *) rx->skb->data;
  3038. /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
  3039. if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
  3040. if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
  3041. rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
  3042. hdr->seq_ctrl)) {
  3043. if (rx->u.rx.ra_match) {
  3044. rx->local->dot11FrameDuplicateCount++;
  3045. rx->sta->num_duplicates++;
  3046. }
  3047. return TXRX_DROP;
  3048. } else
  3049. rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
  3050. }
  3051. if ((rx->local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) &&
  3052. rx->skb->len > FCS_LEN)
  3053. skb_trim(rx->skb, rx->skb->len - FCS_LEN);
  3054. if (unlikely(rx->skb->len < 16)) {
  3055. I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
  3056. return TXRX_DROP;
  3057. }
  3058. if (!rx->u.rx.ra_match)
  3059. rx->skb->pkt_type = PACKET_OTHERHOST;
  3060. else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
  3061. rx->skb->pkt_type = PACKET_HOST;
  3062. else if (is_multicast_ether_addr(hdr->addr1)) {
  3063. if (is_broadcast_ether_addr(hdr->addr1))
  3064. rx->skb->pkt_type = PACKET_BROADCAST;
  3065. else
  3066. rx->skb->pkt_type = PACKET_MULTICAST;
  3067. } else
  3068. rx->skb->pkt_type = PACKET_OTHERHOST;
  3069. /* Drop disallowed frame classes based on STA auth/assoc state;
  3070. * IEEE 802.11, Chap 5.5.
  3071. *
  3072. * 80211.o does filtering only based on association state, i.e., it
  3073. * drops Class 3 frames from not associated stations. hostapd sends
  3074. * deauth/disassoc frames when needed. In addition, hostapd is
  3075. * responsible for filtering on both auth and assoc states.
  3076. */
  3077. if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
  3078. ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
  3079. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
  3080. rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
  3081. (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
  3082. if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
  3083. !(rx->fc & IEEE80211_FCTL_TODS) &&
  3084. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
  3085. || !rx->u.rx.ra_match) {
  3086. /* Drop IBSS frames and frames for other hosts
  3087. * silently. */
  3088. return TXRX_DROP;
  3089. }
  3090. if (!rx->local->apdev)
  3091. return TXRX_DROP;
  3092. ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
  3093. ieee80211_msg_sta_not_assoc);
  3094. return TXRX_QUEUED;
  3095. }
  3096. if (rx->sdata->type == IEEE80211_IF_TYPE_STA)
  3097. always_sta_key = 0;
  3098. else
  3099. always_sta_key = 1;
  3100. if (rx->sta && rx->sta->key && always_sta_key) {
  3101. rx->key = rx->sta->key;
  3102. } else {
  3103. if (rx->sta && rx->sta->key)
  3104. rx->key = rx->sta->key;
  3105. else
  3106. rx->key = rx->sdata->default_key;
  3107. if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
  3108. rx->fc & IEEE80211_FCTL_PROTECTED) {
  3109. int keyidx = ieee80211_wep_get_keyidx(rx->skb);
  3110. if (keyidx >= 0 && keyidx < NUM_DEFAULT_KEYS &&
  3111. (!rx->sta || !rx->sta->key || keyidx > 0))
  3112. rx->key = rx->sdata->keys[keyidx];
  3113. if (!rx->key) {
  3114. if (!rx->u.rx.ra_match)
  3115. return TXRX_DROP;
  3116. printk(KERN_DEBUG "%s: RX WEP frame with "
  3117. "unknown keyidx %d (A1=" MAC_FMT " A2="
  3118. MAC_FMT " A3=" MAC_FMT ")\n",
  3119. rx->dev->name, keyidx,
  3120. MAC_ARG(hdr->addr1),
  3121. MAC_ARG(hdr->addr2),
  3122. MAC_ARG(hdr->addr3));
  3123. if (!rx->local->apdev)
  3124. return TXRX_DROP;
  3125. ieee80211_rx_mgmt(
  3126. rx->local, rx->skb, rx->u.rx.status,
  3127. ieee80211_msg_wep_frame_unknown_key);
  3128. return TXRX_QUEUED;
  3129. }
  3130. }
  3131. }
  3132. if (rx->fc & IEEE80211_FCTL_PROTECTED && rx->key && rx->u.rx.ra_match) {
  3133. rx->key->tx_rx_count++;
  3134. if (unlikely(rx->local->key_tx_rx_threshold &&
  3135. rx->key->tx_rx_count >
  3136. rx->local->key_tx_rx_threshold)) {
  3137. ieee80211_key_threshold_notify(rx->dev, rx->key,
  3138. rx->sta);
  3139. }
  3140. }
  3141. return TXRX_CONTINUE;
  3142. }
  3143. static ieee80211_txrx_result
  3144. ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
  3145. {
  3146. struct sta_info *sta = rx->sta;
  3147. struct net_device *dev = rx->dev;
  3148. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  3149. if (!sta)
  3150. return TXRX_CONTINUE;
  3151. /* Update last_rx only for IBSS packets which are for the current
  3152. * BSSID to avoid keeping the current IBSS network alive in cases where
  3153. * other STAs are using different BSSID. */
  3154. if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
  3155. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
  3156. if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
  3157. sta->last_rx = jiffies;
  3158. } else
  3159. if (!is_multicast_ether_addr(hdr->addr1) ||
  3160. rx->sdata->type == IEEE80211_IF_TYPE_STA) {
  3161. /* Update last_rx only for unicast frames in order to prevent
  3162. * the Probe Request frames (the only broadcast frames from a
  3163. * STA in infrastructure mode) from keeping a connection alive.
  3164. */
  3165. sta->last_rx = jiffies;
  3166. }
  3167. if (!rx->u.rx.ra_match)
  3168. return TXRX_CONTINUE;
  3169. sta->rx_fragments++;
  3170. sta->rx_bytes += rx->skb->len;
  3171. sta->last_rssi = (sta->last_rssi * 15 +
  3172. rx->u.rx.status->ssi) / 16;
  3173. sta->last_signal = (sta->last_signal * 15 +
  3174. rx->u.rx.status->signal) / 16;
  3175. sta->last_noise = (sta->last_noise * 15 +
  3176. rx->u.rx.status->noise) / 16;
  3177. if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
  3178. /* Change STA power saving mode only in the end of a frame
  3179. * exchange sequence */
  3180. if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
  3181. rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
  3182. else if (!(sta->flags & WLAN_STA_PS) &&
  3183. (rx->fc & IEEE80211_FCTL_PM))
  3184. ap_sta_ps_start(dev, sta);
  3185. }
  3186. /* Drop data::nullfunc frames silently, since they are used only to
  3187. * control station power saving mode. */
  3188. if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  3189. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
  3190. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  3191. /* Update counter and free packet here to avoid counting this
  3192. * as a dropped packed. */
  3193. sta->rx_packets++;
  3194. dev_kfree_skb(rx->skb);
  3195. return TXRX_QUEUED;
  3196. }
  3197. return TXRX_CONTINUE;
  3198. } /* ieee80211_rx_h_sta_process */
  3199. static ieee80211_txrx_result
  3200. ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
  3201. {
  3202. if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
  3203. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
  3204. !rx->key || rx->key->alg != ALG_WEP || !rx->u.rx.ra_match)
  3205. return TXRX_CONTINUE;
  3206. /* Check for weak IVs, if hwaccel did not remove IV from the frame */
  3207. if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) ||
  3208. rx->key->force_sw_encrypt) {
  3209. u8 *iv = ieee80211_wep_is_weak_iv(rx->skb, rx->key);
  3210. if (iv) {
  3211. rx->sta->wep_weak_iv_count++;
  3212. }
  3213. }
  3214. return TXRX_CONTINUE;
  3215. }
  3216. static ieee80211_txrx_result
  3217. ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
  3218. {
  3219. /* If the device handles decryption totally, skip this test */
  3220. if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
  3221. return TXRX_CONTINUE;
  3222. if ((rx->key && rx->key->alg != ALG_WEP) ||
  3223. !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
  3224. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  3225. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  3226. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
  3227. return TXRX_CONTINUE;
  3228. if (!rx->key) {
  3229. printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
  3230. rx->dev->name);
  3231. return TXRX_DROP;
  3232. }
  3233. if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED) ||
  3234. rx->key->force_sw_encrypt) {
  3235. if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
  3236. printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
  3237. "failed\n", rx->dev->name);
  3238. return TXRX_DROP;
  3239. }
  3240. } else if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
  3241. ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
  3242. /* remove ICV */
  3243. skb_trim(rx->skb, rx->skb->len - 4);
  3244. }
  3245. return TXRX_CONTINUE;
  3246. }
  3247. static ieee80211_txrx_result
  3248. ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
  3249. {
  3250. if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
  3251. rx->sdata->type != IEEE80211_IF_TYPE_STA && rx->u.rx.ra_match) {
  3252. /* Pass both encrypted and unencrypted EAPOL frames to user
  3253. * space for processing. */
  3254. if (!rx->local->apdev)
  3255. return TXRX_DROP;
  3256. ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
  3257. ieee80211_msg_normal);
  3258. return TXRX_QUEUED;
  3259. }
  3260. if (unlikely(rx->sdata->ieee802_1x &&
  3261. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  3262. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
  3263. (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
  3264. !ieee80211_is_eapol(rx->skb))) {
  3265. #ifdef CONFIG_MAC80211_DEBUG
  3266. struct ieee80211_hdr *hdr =
  3267. (struct ieee80211_hdr *) rx->skb->data;
  3268. printk(KERN_DEBUG "%s: dropped frame from " MAC_FMT
  3269. " (unauthorized port)\n", rx->dev->name,
  3270. MAC_ARG(hdr->addr2));
  3271. #endif /* CONFIG_MAC80211_DEBUG */
  3272. return TXRX_DROP;
  3273. }
  3274. return TXRX_CONTINUE;
  3275. }
  3276. static ieee80211_txrx_result
  3277. ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
  3278. {
  3279. /* If the device handles decryption totally, skip this test */
  3280. if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
  3281. return TXRX_CONTINUE;
  3282. /* Drop unencrypted frames if key is set. */
  3283. if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
  3284. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  3285. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
  3286. (rx->key || rx->sdata->drop_unencrypted) &&
  3287. (rx->sdata->eapol == 0 ||
  3288. !ieee80211_is_eapol(rx->skb)))) {
  3289. printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
  3290. "encryption\n", rx->dev->name);
  3291. return TXRX_DROP;
  3292. }
  3293. return TXRX_CONTINUE;
  3294. }
  3295. static ieee80211_txrx_result
  3296. ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
  3297. {
  3298. struct ieee80211_sub_if_data *sdata;
  3299. if (!rx->u.rx.ra_match)
  3300. return TXRX_DROP;
  3301. sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
  3302. if ((sdata->type == IEEE80211_IF_TYPE_STA ||
  3303. sdata->type == IEEE80211_IF_TYPE_IBSS) &&
  3304. !rx->local->user_space_mlme) {
  3305. ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
  3306. } else {
  3307. /* Management frames are sent to hostapd for processing */
  3308. if (!rx->local->apdev)
  3309. return TXRX_DROP;
  3310. ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
  3311. ieee80211_msg_normal);
  3312. }
  3313. return TXRX_QUEUED;
  3314. }
  3315. static ieee80211_txrx_result
  3316. ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
  3317. {
  3318. struct ieee80211_local *local = rx->local;
  3319. struct sk_buff *skb = rx->skb;
  3320. if (unlikely(local->sta_scanning != 0)) {
  3321. ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
  3322. return TXRX_QUEUED;
  3323. }
  3324. if (unlikely(rx->u.rx.in_scan)) {
  3325. /* scanning finished during invoking of handlers */
  3326. I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
  3327. return TXRX_DROP;
  3328. }
  3329. return TXRX_CONTINUE;
  3330. }
  3331. static void ieee80211_rx_michael_mic_report(struct net_device *dev,
  3332. struct ieee80211_hdr *hdr,
  3333. struct sta_info *sta,
  3334. struct ieee80211_txrx_data *rx)
  3335. {
  3336. int keyidx, hdrlen;
  3337. hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
  3338. if (rx->skb->len >= hdrlen + 4)
  3339. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  3340. else
  3341. keyidx = -1;
  3342. /* TODO: verify that this is not triggered by fragmented
  3343. * frames (hw does not verify MIC for them). */
  3344. printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
  3345. "failure from " MAC_FMT " to " MAC_FMT " keyidx=%d\n",
  3346. dev->name, MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr1), keyidx);
  3347. if (!sta) {
  3348. /* Some hardware versions seem to generate incorrect
  3349. * Michael MIC reports; ignore them to avoid triggering
  3350. * countermeasures. */
  3351. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  3352. "error for unknown address " MAC_FMT "\n",
  3353. dev->name, MAC_ARG(hdr->addr2));
  3354. goto ignore;
  3355. }
  3356. if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
  3357. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  3358. "error for a frame with no ISWEP flag (src "
  3359. MAC_FMT ")\n", dev->name, MAC_ARG(hdr->addr2));
  3360. goto ignore;
  3361. }
  3362. if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
  3363. rx->sdata->type == IEEE80211_IF_TYPE_AP) {
  3364. keyidx = ieee80211_wep_get_keyidx(rx->skb);
  3365. /* AP with Pairwise keys support should never receive Michael
  3366. * MIC errors for non-zero keyidx because these are reserved
  3367. * for group keys and only the AP is sending real multicast
  3368. * frames in BSS. */
  3369. if (keyidx) {
  3370. printk(KERN_DEBUG "%s: ignored Michael MIC error for "
  3371. "a frame with non-zero keyidx (%d) (src " MAC_FMT
  3372. ")\n", dev->name, keyidx, MAC_ARG(hdr->addr2));
  3373. goto ignore;
  3374. }
  3375. }
  3376. if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  3377. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  3378. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
  3379. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  3380. "error for a frame that cannot be encrypted "
  3381. "(fc=0x%04x) (src " MAC_FMT ")\n",
  3382. dev->name, rx->fc, MAC_ARG(hdr->addr2));
  3383. goto ignore;
  3384. }
  3385. do {
  3386. union iwreq_data wrqu;
  3387. char *buf = kmalloc(128, GFP_ATOMIC);
  3388. if (!buf)
  3389. break;
  3390. /* TODO: needed parameters: count, key type, TSC */
  3391. sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
  3392. "keyid=%d %scast addr=" MAC_FMT ")",
  3393. keyidx, hdr->addr1[0] & 0x01 ? "broad" : "uni",
  3394. MAC_ARG(hdr->addr2));
  3395. memset(&wrqu, 0, sizeof(wrqu));
  3396. wrqu.data.length = strlen(buf);
  3397. wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
  3398. kfree(buf);
  3399. } while (0);
  3400. /* TODO: consider verifying the MIC error report with software
  3401. * implementation if we get too many spurious reports from the
  3402. * hardware. */
  3403. if (!rx->local->apdev)
  3404. goto ignore;
  3405. ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
  3406. ieee80211_msg_michael_mic_failure);
  3407. return;
  3408. ignore:
  3409. dev_kfree_skb(rx->skb);
  3410. rx->skb = NULL;
  3411. }
  3412. static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
  3413. struct ieee80211_local *local,
  3414. ieee80211_rx_handler *handlers,
  3415. struct ieee80211_txrx_data *rx,
  3416. struct sta_info *sta)
  3417. {
  3418. ieee80211_rx_handler *handler;
  3419. ieee80211_txrx_result res = TXRX_DROP;
  3420. for (handler = handlers; *handler != NULL; handler++) {
  3421. res = (*handler)(rx);
  3422. if (res != TXRX_CONTINUE) {
  3423. if (res == TXRX_DROP) {
  3424. I802_DEBUG_INC(local->rx_handlers_drop);
  3425. if (sta)
  3426. sta->rx_dropped++;
  3427. }
  3428. if (res == TXRX_QUEUED)
  3429. I802_DEBUG_INC(local->rx_handlers_queued);
  3430. break;
  3431. }
  3432. }
  3433. if (res == TXRX_DROP) {
  3434. dev_kfree_skb(rx->skb);
  3435. }
  3436. return res;
  3437. }
  3438. static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
  3439. ieee80211_rx_handler *handlers,
  3440. struct ieee80211_txrx_data *rx,
  3441. struct sta_info *sta)
  3442. {
  3443. if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
  3444. TXRX_CONTINUE)
  3445. dev_kfree_skb(rx->skb);
  3446. }
  3447. /*
  3448. * This is the receive path handler. It is called by a low level driver when an
  3449. * 802.11 MPDU is received from the hardware.
  3450. */
  3451. void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
  3452. struct ieee80211_rx_status *status)
  3453. {
  3454. struct ieee80211_local *local = hw_to_local(hw);
  3455. struct ieee80211_sub_if_data *sdata;
  3456. struct sta_info *sta;
  3457. struct ieee80211_hdr *hdr;
  3458. struct ieee80211_txrx_data rx;
  3459. u16 type;
  3460. int multicast;
  3461. int radiotap_len = 0;
  3462. if (status->flag & RX_FLAG_RADIOTAP) {
  3463. radiotap_len = ieee80211_get_radiotap_len(skb);
  3464. skb_pull(skb, radiotap_len);
  3465. }
  3466. hdr = (struct ieee80211_hdr *) skb->data;
  3467. memset(&rx, 0, sizeof(rx));
  3468. rx.skb = skb;
  3469. rx.local = local;
  3470. rx.u.rx.status = status;
  3471. rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
  3472. type = rx.fc & IEEE80211_FCTL_FTYPE;
  3473. if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
  3474. local->dot11ReceivedFragmentCount++;
  3475. multicast = is_multicast_ether_addr(hdr->addr1);
  3476. if (skb->len >= 16)
  3477. sta = rx.sta = sta_info_get(local, hdr->addr2);
  3478. else
  3479. sta = rx.sta = NULL;
  3480. if (sta) {
  3481. rx.dev = sta->dev;
  3482. rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
  3483. }
  3484. if ((status->flag & RX_FLAG_MMIC_ERROR)) {
  3485. ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
  3486. goto end;
  3487. }
  3488. if (unlikely(local->sta_scanning))
  3489. rx.u.rx.in_scan = 1;
  3490. if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
  3491. sta) != TXRX_CONTINUE)
  3492. goto end;
  3493. skb = rx.skb;
  3494. skb_push(skb, radiotap_len);
  3495. if (sta && !sta->assoc_ap && !(sta->flags & WLAN_STA_WDS) &&
  3496. !local->iff_promiscs && !multicast) {
  3497. rx.u.rx.ra_match = 1;
  3498. ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
  3499. sta);
  3500. } else {
  3501. struct ieee80211_sub_if_data *prev = NULL;
  3502. struct sk_buff *skb_new;
  3503. u8 *bssid = ieee80211_get_bssid(hdr, skb->len - radiotap_len);
  3504. read_lock(&local->sub_if_lock);
  3505. list_for_each_entry(sdata, &local->sub_if_list, list) {
  3506. rx.u.rx.ra_match = 1;
  3507. switch (sdata->type) {
  3508. case IEEE80211_IF_TYPE_STA:
  3509. if (!bssid)
  3510. continue;
  3511. if (!ieee80211_bssid_match(bssid,
  3512. sdata->u.sta.bssid)) {
  3513. if (!rx.u.rx.in_scan)
  3514. continue;
  3515. rx.u.rx.ra_match = 0;
  3516. } else if (!multicast &&
  3517. compare_ether_addr(sdata->dev->dev_addr,
  3518. hdr->addr1) != 0) {
  3519. if (!sdata->promisc)
  3520. continue;
  3521. rx.u.rx.ra_match = 0;
  3522. }
  3523. break;
  3524. case IEEE80211_IF_TYPE_IBSS:
  3525. if (!bssid)
  3526. continue;
  3527. if (!ieee80211_bssid_match(bssid,
  3528. sdata->u.sta.bssid)) {
  3529. if (!rx.u.rx.in_scan)
  3530. continue;
  3531. rx.u.rx.ra_match = 0;
  3532. } else if (!multicast &&
  3533. compare_ether_addr(sdata->dev->dev_addr,
  3534. hdr->addr1) != 0) {
  3535. if (!sdata->promisc)
  3536. continue;
  3537. rx.u.rx.ra_match = 0;
  3538. } else if (!sta)
  3539. sta = rx.sta =
  3540. ieee80211_ibss_add_sta(sdata->dev,
  3541. skb, bssid,
  3542. hdr->addr2);
  3543. break;
  3544. case IEEE80211_IF_TYPE_AP:
  3545. if (!bssid) {
  3546. if (compare_ether_addr(sdata->dev->dev_addr,
  3547. hdr->addr1) != 0)
  3548. continue;
  3549. } else if (!ieee80211_bssid_match(bssid,
  3550. sdata->dev->dev_addr)) {
  3551. if (!rx.u.rx.in_scan)
  3552. continue;
  3553. rx.u.rx.ra_match = 0;
  3554. }
  3555. if (sdata->dev == local->mdev &&
  3556. !rx.u.rx.in_scan)
  3557. /* do not receive anything via
  3558. * master device when not scanning */
  3559. continue;
  3560. break;
  3561. case IEEE80211_IF_TYPE_WDS:
  3562. if (bssid ||
  3563. (rx.fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
  3564. continue;
  3565. if (compare_ether_addr(sdata->u.wds.remote_addr,
  3566. hdr->addr2) != 0)
  3567. continue;
  3568. break;
  3569. }
  3570. if (prev) {
  3571. skb_new = skb_copy(skb, GFP_ATOMIC);
  3572. if (!skb_new) {
  3573. if (net_ratelimit())
  3574. printk(KERN_DEBUG "%s: failed to copy "
  3575. "multicast frame for %s",
  3576. local->mdev->name, prev->dev->name);
  3577. continue;
  3578. }
  3579. rx.skb = skb_new;
  3580. rx.dev = prev->dev;
  3581. rx.sdata = prev;
  3582. ieee80211_invoke_rx_handlers(local,
  3583. local->rx_handlers,
  3584. &rx, sta);
  3585. }
  3586. prev = sdata;
  3587. }
  3588. if (prev) {
  3589. rx.skb = skb;
  3590. rx.dev = prev->dev;
  3591. rx.sdata = prev;
  3592. ieee80211_invoke_rx_handlers(local, local->rx_handlers,
  3593. &rx, sta);
  3594. } else
  3595. dev_kfree_skb(skb);
  3596. read_unlock(&local->sub_if_lock);
  3597. }
  3598. end:
  3599. if (sta)
  3600. sta_info_put(sta);
  3601. }
  3602. EXPORT_SYMBOL(__ieee80211_rx);
  3603. static ieee80211_txrx_result
  3604. ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
  3605. {
  3606. struct ieee80211_local *local = tx->local;
  3607. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  3608. struct sk_buff *skb = tx->skb;
  3609. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  3610. u32 load = 0, hdrtime;
  3611. /* TODO: this could be part of tx_status handling, so that the number
  3612. * of retries would be known; TX rate should in that case be stored
  3613. * somewhere with the packet */
  3614. /* Estimate total channel use caused by this frame */
  3615. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  3616. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  3617. if (mode->mode == MODE_IEEE80211A ||
  3618. mode->mode == MODE_ATHEROS_TURBO ||
  3619. mode->mode == MODE_ATHEROS_TURBOG ||
  3620. (mode->mode == MODE_IEEE80211G &&
  3621. tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
  3622. hdrtime = CHAN_UTIL_HDR_SHORT;
  3623. else
  3624. hdrtime = CHAN_UTIL_HDR_LONG;
  3625. load = hdrtime;
  3626. if (!is_multicast_ether_addr(hdr->addr1))
  3627. load += hdrtime;
  3628. if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  3629. load += 2 * hdrtime;
  3630. else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  3631. load += hdrtime;
  3632. load += skb->len * tx->u.tx.rate->rate_inv;
  3633. if (tx->u.tx.extra_frag) {
  3634. int i;
  3635. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  3636. load += 2 * hdrtime;
  3637. load += tx->u.tx.extra_frag[i]->len *
  3638. tx->u.tx.rate->rate;
  3639. }
  3640. }
  3641. /* Divide channel_use by 8 to avoid wrapping around the counter */
  3642. load >>= CHAN_UTIL_SHIFT;
  3643. local->channel_use_raw += load;
  3644. if (tx->sta)
  3645. tx->sta->channel_use_raw += load;
  3646. tx->sdata->channel_use_raw += load;
  3647. return TXRX_CONTINUE;
  3648. }
  3649. static ieee80211_txrx_result
  3650. ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
  3651. {
  3652. struct ieee80211_local *local = rx->local;
  3653. struct sk_buff *skb = rx->skb;
  3654. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  3655. u32 load = 0, hdrtime;
  3656. struct ieee80211_rate *rate;
  3657. struct ieee80211_hw_mode *mode = local->hw.conf.mode;
  3658. int i;
  3659. /* Estimate total channel use caused by this frame */
  3660. if (unlikely(mode->num_rates < 0))
  3661. return TXRX_CONTINUE;
  3662. rate = &mode->rates[0];
  3663. for (i = 0; i < mode->num_rates; i++) {
  3664. if (mode->rates[i].val == rx->u.rx.status->rate) {
  3665. rate = &mode->rates[i];
  3666. break;
  3667. }
  3668. }
  3669. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  3670. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  3671. if (mode->mode == MODE_IEEE80211A ||
  3672. mode->mode == MODE_ATHEROS_TURBO ||
  3673. mode->mode == MODE_ATHEROS_TURBOG ||
  3674. (mode->mode == MODE_IEEE80211G &&
  3675. rate->flags & IEEE80211_RATE_ERP))
  3676. hdrtime = CHAN_UTIL_HDR_SHORT;
  3677. else
  3678. hdrtime = CHAN_UTIL_HDR_LONG;
  3679. load = hdrtime;
  3680. if (!is_multicast_ether_addr(hdr->addr1))
  3681. load += hdrtime;
  3682. load += skb->len * rate->rate_inv;
  3683. /* Divide channel_use by 8 to avoid wrapping around the counter */
  3684. load >>= CHAN_UTIL_SHIFT;
  3685. local->channel_use_raw += load;
  3686. if (rx->sta)
  3687. rx->sta->channel_use_raw += load;
  3688. rx->u.rx.load = load;
  3689. return TXRX_CONTINUE;
  3690. }
  3691. static ieee80211_txrx_result
  3692. ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
  3693. {
  3694. rx->sdata->channel_use_raw += rx->u.rx.load;
  3695. return TXRX_CONTINUE;
  3696. }
  3697. static void ieee80211_stat_refresh(unsigned long data)
  3698. {
  3699. struct ieee80211_local *local = (struct ieee80211_local *) data;
  3700. struct sta_info *sta;
  3701. struct ieee80211_sub_if_data *sdata;
  3702. if (!local->stat_time)
  3703. return;
  3704. /* go through all stations */
  3705. spin_lock_bh(&local->sta_lock);
  3706. list_for_each_entry(sta, &local->sta_list, list) {
  3707. sta->channel_use = (sta->channel_use_raw / local->stat_time) /
  3708. CHAN_UTIL_PER_10MS;
  3709. sta->channel_use_raw = 0;
  3710. }
  3711. spin_unlock_bh(&local->sta_lock);
  3712. /* go through all subinterfaces */
  3713. read_lock(&local->sub_if_lock);
  3714. list_for_each_entry(sdata, &local->sub_if_list, list) {
  3715. sdata->channel_use = (sdata->channel_use_raw /
  3716. local->stat_time) / CHAN_UTIL_PER_10MS;
  3717. sdata->channel_use_raw = 0;
  3718. }
  3719. read_unlock(&local->sub_if_lock);
  3720. /* hardware interface */
  3721. local->channel_use = (local->channel_use_raw /
  3722. local->stat_time) / CHAN_UTIL_PER_10MS;
  3723. local->channel_use_raw = 0;
  3724. local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
  3725. add_timer(&local->stat_timer);
  3726. }
  3727. /* This is a version of the rx handler that can be called from hard irq
  3728. * context. Post the skb on the queue and schedule the tasklet */
  3729. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
  3730. struct ieee80211_rx_status *status)
  3731. {
  3732. struct ieee80211_local *local = hw_to_local(hw);
  3733. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  3734. skb->dev = local->mdev;
  3735. /* copy status into skb->cb for use by tasklet */
  3736. memcpy(skb->cb, status, sizeof(*status));
  3737. skb->pkt_type = IEEE80211_RX_MSG;
  3738. skb_queue_tail(&local->skb_queue, skb);
  3739. tasklet_schedule(&local->tasklet);
  3740. }
  3741. EXPORT_SYMBOL(ieee80211_rx_irqsafe);
  3742. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  3743. struct sk_buff *skb,
  3744. struct ieee80211_tx_status *status)
  3745. {
  3746. struct ieee80211_local *local = hw_to_local(hw);
  3747. struct ieee80211_tx_status *saved;
  3748. int tmp;
  3749. skb->dev = local->mdev;
  3750. saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
  3751. if (unlikely(!saved)) {
  3752. if (net_ratelimit())
  3753. printk(KERN_WARNING "%s: Not enough memory, "
  3754. "dropping tx status", skb->dev->name);
  3755. /* should be dev_kfree_skb_irq, but due to this function being
  3756. * named _irqsafe instead of just _irq we can't be sure that
  3757. * people won't call it from non-irq contexts */
  3758. dev_kfree_skb_any(skb);
  3759. return;
  3760. }
  3761. memcpy(saved, status, sizeof(struct ieee80211_tx_status));
  3762. /* copy pointer to saved status into skb->cb for use by tasklet */
  3763. memcpy(skb->cb, &saved, sizeof(saved));
  3764. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  3765. skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
  3766. &local->skb_queue : &local->skb_queue_unreliable, skb);
  3767. tmp = skb_queue_len(&local->skb_queue) +
  3768. skb_queue_len(&local->skb_queue_unreliable);
  3769. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  3770. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  3771. memcpy(&saved, skb->cb, sizeof(saved));
  3772. kfree(saved);
  3773. dev_kfree_skb_irq(skb);
  3774. tmp--;
  3775. I802_DEBUG_INC(local->tx_status_drop);
  3776. }
  3777. tasklet_schedule(&local->tasklet);
  3778. }
  3779. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  3780. static void ieee80211_tasklet_handler(unsigned long data)
  3781. {
  3782. struct ieee80211_local *local = (struct ieee80211_local *) data;
  3783. struct sk_buff *skb;
  3784. struct ieee80211_rx_status rx_status;
  3785. struct ieee80211_tx_status *tx_status;
  3786. while ((skb = skb_dequeue(&local->skb_queue)) ||
  3787. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  3788. switch (skb->pkt_type) {
  3789. case IEEE80211_RX_MSG:
  3790. /* status is in skb->cb */
  3791. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  3792. /* Clear skb->type in order to not confuse kernel
  3793. * netstack. */
  3794. skb->pkt_type = 0;
  3795. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  3796. break;
  3797. case IEEE80211_TX_STATUS_MSG:
  3798. /* get pointer to saved status out of skb->cb */
  3799. memcpy(&tx_status, skb->cb, sizeof(tx_status));
  3800. skb->pkt_type = 0;
  3801. ieee80211_tx_status(local_to_hw(local),
  3802. skb, tx_status);
  3803. kfree(tx_status);
  3804. break;
  3805. default: /* should never get here! */
  3806. printk(KERN_ERR "%s: Unknown message type (%d)\n",
  3807. local->mdev->name, skb->pkt_type);
  3808. dev_kfree_skb(skb);
  3809. break;
  3810. }
  3811. }
  3812. }
  3813. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  3814. * make a prepared TX frame (one that has been given to hw) to look like brand
  3815. * new IEEE 802.11 frame that is ready to go through TX processing again.
  3816. * Also, tx_packet_data in cb is restored from tx_control. */
  3817. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  3818. struct ieee80211_key *key,
  3819. struct sk_buff *skb,
  3820. struct ieee80211_tx_control *control)
  3821. {
  3822. int hdrlen, iv_len, mic_len;
  3823. struct ieee80211_tx_packet_data *pkt_data;
  3824. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  3825. pkt_data->ifindex = control->ifindex;
  3826. pkt_data->mgmt_iface = (control->type == IEEE80211_IF_TYPE_MGMT);
  3827. pkt_data->req_tx_status = !!(control->flags & IEEE80211_TXCTL_REQ_TX_STATUS);
  3828. pkt_data->do_not_encrypt = !!(control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT);
  3829. pkt_data->requeue = !!(control->flags & IEEE80211_TXCTL_REQUEUE);
  3830. pkt_data->queue = control->queue;
  3831. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  3832. if (!key)
  3833. goto no_key;
  3834. switch (key->alg) {
  3835. case ALG_WEP:
  3836. iv_len = WEP_IV_LEN;
  3837. mic_len = WEP_ICV_LEN;
  3838. break;
  3839. case ALG_TKIP:
  3840. iv_len = TKIP_IV_LEN;
  3841. mic_len = TKIP_ICV_LEN;
  3842. break;
  3843. case ALG_CCMP:
  3844. iv_len = CCMP_HDR_LEN;
  3845. mic_len = CCMP_MIC_LEN;
  3846. break;
  3847. default:
  3848. goto no_key;
  3849. }
  3850. if (skb->len >= mic_len && key->force_sw_encrypt)
  3851. skb_trim(skb, skb->len - mic_len);
  3852. if (skb->len >= iv_len && skb->len > hdrlen) {
  3853. memmove(skb->data + iv_len, skb->data, hdrlen);
  3854. skb_pull(skb, iv_len);
  3855. }
  3856. no_key:
  3857. {
  3858. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  3859. u16 fc = le16_to_cpu(hdr->frame_control);
  3860. if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
  3861. fc &= ~IEEE80211_STYPE_QOS_DATA;
  3862. hdr->frame_control = cpu_to_le16(fc);
  3863. memmove(skb->data + 2, skb->data, hdrlen - 2);
  3864. skb_pull(skb, 2);
  3865. }
  3866. }
  3867. }
  3868. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  3869. struct ieee80211_tx_status *status)
  3870. {
  3871. struct sk_buff *skb2;
  3872. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  3873. struct ieee80211_local *local = hw_to_local(hw);
  3874. u16 frag, type;
  3875. u32 msg_type;
  3876. struct ieee80211_tx_status_rtap_hdr *rthdr;
  3877. struct ieee80211_sub_if_data *sdata;
  3878. int monitors;
  3879. if (!status) {
  3880. printk(KERN_ERR
  3881. "%s: ieee80211_tx_status called with NULL status\n",
  3882. local->mdev->name);
  3883. dev_kfree_skb(skb);
  3884. return;
  3885. }
  3886. if (status->excessive_retries) {
  3887. struct sta_info *sta;
  3888. sta = sta_info_get(local, hdr->addr1);
  3889. if (sta) {
  3890. if (sta->flags & WLAN_STA_PS) {
  3891. /* The STA is in power save mode, so assume
  3892. * that this TX packet failed because of that.
  3893. */
  3894. status->excessive_retries = 0;
  3895. status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
  3896. }
  3897. sta_info_put(sta);
  3898. }
  3899. }
  3900. if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
  3901. struct sta_info *sta;
  3902. sta = sta_info_get(local, hdr->addr1);
  3903. if (sta) {
  3904. sta->tx_filtered_count++;
  3905. /* Clear the TX filter mask for this STA when sending
  3906. * the next packet. If the STA went to power save mode,
  3907. * this will happen when it is waking up for the next
  3908. * time. */
  3909. sta->clear_dst_mask = 1;
  3910. /* TODO: Is the WLAN_STA_PS flag always set here or is
  3911. * the race between RX and TX status causing some
  3912. * packets to be filtered out before 80211.o gets an
  3913. * update for PS status? This seems to be the case, so
  3914. * no changes are likely to be needed. */
  3915. if (sta->flags & WLAN_STA_PS &&
  3916. skb_queue_len(&sta->tx_filtered) <
  3917. STA_MAX_TX_BUFFER) {
  3918. ieee80211_remove_tx_extra(local, sta->key,
  3919. skb,
  3920. &status->control);
  3921. skb_queue_tail(&sta->tx_filtered, skb);
  3922. } else if (!(sta->flags & WLAN_STA_PS) &&
  3923. !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
  3924. /* Software retry the packet once */
  3925. status->control.flags |= IEEE80211_TXCTL_REQUEUE;
  3926. ieee80211_remove_tx_extra(local, sta->key,
  3927. skb,
  3928. &status->control);
  3929. dev_queue_xmit(skb);
  3930. } else {
  3931. if (net_ratelimit()) {
  3932. printk(KERN_DEBUG "%s: dropped TX "
  3933. "filtered frame queue_len=%d "
  3934. "PS=%d @%lu\n",
  3935. local->mdev->name,
  3936. skb_queue_len(
  3937. &sta->tx_filtered),
  3938. !!(sta->flags & WLAN_STA_PS),
  3939. jiffies);
  3940. }
  3941. dev_kfree_skb(skb);
  3942. }
  3943. sta_info_put(sta);
  3944. return;
  3945. }
  3946. } else {
  3947. /* FIXME: STUPID to call this with both local and local->mdev */
  3948. rate_control_tx_status(local, local->mdev, skb, status);
  3949. }
  3950. ieee80211_led_tx(local, 0);
  3951. /* SNMP counters
  3952. * Fragments are passed to low-level drivers as separate skbs, so these
  3953. * are actually fragments, not frames. Update frame counters only for
  3954. * the first fragment of the frame. */
  3955. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  3956. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  3957. if (status->flags & IEEE80211_TX_STATUS_ACK) {
  3958. if (frag == 0) {
  3959. local->dot11TransmittedFrameCount++;
  3960. if (is_multicast_ether_addr(hdr->addr1))
  3961. local->dot11MulticastTransmittedFrameCount++;
  3962. if (status->retry_count > 0)
  3963. local->dot11RetryCount++;
  3964. if (status->retry_count > 1)
  3965. local->dot11MultipleRetryCount++;
  3966. }
  3967. /* This counter shall be incremented for an acknowledged MPDU
  3968. * with an individual address in the address 1 field or an MPDU
  3969. * with a multicast address in the address 1 field of type Data
  3970. * or Management. */
  3971. if (!is_multicast_ether_addr(hdr->addr1) ||
  3972. type == IEEE80211_FTYPE_DATA ||
  3973. type == IEEE80211_FTYPE_MGMT)
  3974. local->dot11TransmittedFragmentCount++;
  3975. } else {
  3976. if (frag == 0)
  3977. local->dot11FailedCount++;
  3978. }
  3979. msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
  3980. ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
  3981. /* this was a transmitted frame, but now we want to reuse it */
  3982. skb_orphan(skb);
  3983. if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
  3984. local->apdev) {
  3985. if (local->monitors) {
  3986. skb2 = skb_clone(skb, GFP_ATOMIC);
  3987. } else {
  3988. skb2 = skb;
  3989. skb = NULL;
  3990. }
  3991. if (skb2)
  3992. /* Send frame to hostapd */
  3993. ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
  3994. if (!skb)
  3995. return;
  3996. }
  3997. if (!local->monitors) {
  3998. dev_kfree_skb(skb);
  3999. return;
  4000. }
  4001. /* send frame to monitor interfaces now */
  4002. if (skb_headroom(skb) < sizeof(*rthdr)) {
  4003. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  4004. dev_kfree_skb(skb);
  4005. return;
  4006. }
  4007. rthdr = (struct ieee80211_tx_status_rtap_hdr*)
  4008. skb_push(skb, sizeof(*rthdr));
  4009. memset(rthdr, 0, sizeof(*rthdr));
  4010. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  4011. rthdr->hdr.it_present =
  4012. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  4013. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  4014. if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
  4015. !is_multicast_ether_addr(hdr->addr1))
  4016. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  4017. if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
  4018. (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
  4019. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  4020. else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
  4021. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  4022. rthdr->data_retries = status->retry_count;
  4023. read_lock(&local->sub_if_lock);
  4024. monitors = local->monitors;
  4025. list_for_each_entry(sdata, &local->sub_if_list, list) {
  4026. /*
  4027. * Using the monitors counter is possibly racy, but
  4028. * if the value is wrong we simply either clone the skb
  4029. * once too much or forget sending it to one monitor iface
  4030. * The latter case isn't nice but fixing the race is much
  4031. * more complicated.
  4032. */
  4033. if (!monitors || !skb)
  4034. goto out;
  4035. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  4036. if (!netif_running(sdata->dev))
  4037. continue;
  4038. monitors--;
  4039. if (monitors)
  4040. skb2 = skb_clone(skb, GFP_KERNEL);
  4041. else
  4042. skb2 = NULL;
  4043. skb->dev = sdata->dev;
  4044. /* XXX: is this sufficient for BPF? */
  4045. skb_set_mac_header(skb, 0);
  4046. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4047. skb->pkt_type = PACKET_OTHERHOST;
  4048. skb->protocol = htons(ETH_P_802_2);
  4049. memset(skb->cb, 0, sizeof(skb->cb));
  4050. netif_rx(skb);
  4051. skb = skb2;
  4052. break;
  4053. }
  4054. }
  4055. out:
  4056. read_unlock(&local->sub_if_lock);
  4057. if (skb)
  4058. dev_kfree_skb(skb);
  4059. }
  4060. EXPORT_SYMBOL(ieee80211_tx_status);
  4061. /* TODO: implement register/unregister functions for adding TX/RX handlers
  4062. * into ordered list */
  4063. /* rx_pre handlers don't have dev and sdata fields available in
  4064. * ieee80211_txrx_data */
  4065. static ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
  4066. {
  4067. ieee80211_rx_h_parse_qos,
  4068. ieee80211_rx_h_load_stats,
  4069. NULL
  4070. };
  4071. static ieee80211_rx_handler ieee80211_rx_handlers[] =
  4072. {
  4073. ieee80211_rx_h_if_stats,
  4074. ieee80211_rx_h_monitor,
  4075. ieee80211_rx_h_passive_scan,
  4076. ieee80211_rx_h_check,
  4077. ieee80211_rx_h_sta_process,
  4078. ieee80211_rx_h_ccmp_decrypt,
  4079. ieee80211_rx_h_tkip_decrypt,
  4080. ieee80211_rx_h_wep_weak_iv_detection,
  4081. ieee80211_rx_h_wep_decrypt,
  4082. ieee80211_rx_h_defragment,
  4083. ieee80211_rx_h_ps_poll,
  4084. ieee80211_rx_h_michael_mic_verify,
  4085. /* this must be after decryption - so header is counted in MPDU mic
  4086. * must be before pae and data, so QOS_DATA format frames
  4087. * are not passed to user space by these functions
  4088. */
  4089. ieee80211_rx_h_remove_qos_control,
  4090. ieee80211_rx_h_802_1x_pae,
  4091. ieee80211_rx_h_drop_unencrypted,
  4092. ieee80211_rx_h_data,
  4093. ieee80211_rx_h_mgmt,
  4094. NULL
  4095. };
  4096. static ieee80211_tx_handler ieee80211_tx_handlers[] =
  4097. {
  4098. ieee80211_tx_h_check_assoc,
  4099. ieee80211_tx_h_sequence,
  4100. ieee80211_tx_h_ps_buf,
  4101. ieee80211_tx_h_select_key,
  4102. ieee80211_tx_h_michael_mic_add,
  4103. ieee80211_tx_h_fragment,
  4104. ieee80211_tx_h_tkip_encrypt,
  4105. ieee80211_tx_h_ccmp_encrypt,
  4106. ieee80211_tx_h_wep_encrypt,
  4107. ieee80211_tx_h_rate_ctrl,
  4108. ieee80211_tx_h_misc,
  4109. ieee80211_tx_h_load_stats,
  4110. NULL
  4111. };
  4112. int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
  4113. {
  4114. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  4115. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  4116. struct sta_info *sta;
  4117. if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
  4118. return 0;
  4119. /* Create STA entry for the new peer */
  4120. sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
  4121. if (!sta)
  4122. return -ENOMEM;
  4123. sta_info_put(sta);
  4124. /* Remove STA entry for the old peer */
  4125. sta = sta_info_get(local, sdata->u.wds.remote_addr);
  4126. if (sta) {
  4127. sta_info_put(sta);
  4128. sta_info_free(sta, 0);
  4129. } else {
  4130. printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
  4131. "peer " MAC_FMT "\n",
  4132. dev->name, MAC_ARG(sdata->u.wds.remote_addr));
  4133. }
  4134. /* Update WDS link data */
  4135. memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
  4136. return 0;
  4137. }
  4138. /* Must not be called for mdev and apdev */
  4139. void ieee80211_if_setup(struct net_device *dev)
  4140. {
  4141. ether_setup(dev);
  4142. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  4143. dev->wireless_handlers = &ieee80211_iw_handler_def;
  4144. dev->set_multicast_list = ieee80211_set_multicast_list;
  4145. dev->change_mtu = ieee80211_change_mtu;
  4146. dev->get_stats = ieee80211_get_stats;
  4147. dev->open = ieee80211_open;
  4148. dev->stop = ieee80211_stop;
  4149. dev->uninit = ieee80211_if_reinit;
  4150. dev->destructor = ieee80211_if_free;
  4151. }
  4152. void ieee80211_if_mgmt_setup(struct net_device *dev)
  4153. {
  4154. ether_setup(dev);
  4155. dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
  4156. dev->change_mtu = ieee80211_change_mtu_apdev;
  4157. dev->get_stats = ieee80211_get_stats;
  4158. dev->open = ieee80211_mgmt_open;
  4159. dev->stop = ieee80211_mgmt_stop;
  4160. dev->type = ARPHRD_IEEE80211_PRISM;
  4161. dev->hard_header_parse = header_parse_80211;
  4162. dev->uninit = ieee80211_if_reinit;
  4163. dev->destructor = ieee80211_if_free;
  4164. }
  4165. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  4166. const char *name)
  4167. {
  4168. struct rate_control_ref *ref, *old;
  4169. ASSERT_RTNL();
  4170. if (local->open_count || netif_running(local->mdev) ||
  4171. (local->apdev && netif_running(local->apdev)))
  4172. return -EBUSY;
  4173. ref = rate_control_alloc(name, local);
  4174. if (!ref) {
  4175. printk(KERN_WARNING "%s: Failed to select rate control "
  4176. "algorithm\n", local->mdev->name);
  4177. return -ENOENT;
  4178. }
  4179. old = local->rate_ctrl;
  4180. local->rate_ctrl = ref;
  4181. if (old) {
  4182. rate_control_put(old);
  4183. sta_info_flush(local, NULL);
  4184. }
  4185. printk(KERN_DEBUG "%s: Selected rate control "
  4186. "algorithm '%s'\n", local->mdev->name,
  4187. ref->ops->name);
  4188. return 0;
  4189. }
  4190. static void rate_control_deinitialize(struct ieee80211_local *local)
  4191. {
  4192. struct rate_control_ref *ref;
  4193. ref = local->rate_ctrl;
  4194. local->rate_ctrl = NULL;
  4195. rate_control_put(ref);
  4196. }
  4197. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  4198. const struct ieee80211_ops *ops)
  4199. {
  4200. struct net_device *mdev;
  4201. struct ieee80211_local *local;
  4202. struct ieee80211_sub_if_data *sdata;
  4203. int priv_size;
  4204. struct wiphy *wiphy;
  4205. /* Ensure 32-byte alignment of our private data and hw private data.
  4206. * We use the wiphy priv data for both our ieee80211_local and for
  4207. * the driver's private data
  4208. *
  4209. * In memory it'll be like this:
  4210. *
  4211. * +-------------------------+
  4212. * | struct wiphy |
  4213. * +-------------------------+
  4214. * | struct ieee80211_local |
  4215. * +-------------------------+
  4216. * | driver's private data |
  4217. * +-------------------------+
  4218. *
  4219. */
  4220. priv_size = ((sizeof(struct ieee80211_local) +
  4221. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  4222. priv_data_len;
  4223. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  4224. if (!wiphy)
  4225. return NULL;
  4226. wiphy->privid = mac80211_wiphy_privid;
  4227. local = wiphy_priv(wiphy);
  4228. local->hw.wiphy = wiphy;
  4229. local->hw.priv = (char *)local +
  4230. ((sizeof(struct ieee80211_local) +
  4231. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  4232. local->ops = ops;
  4233. /* for now, mdev needs sub_if_data :/ */
  4234. mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
  4235. "wmaster%d", ether_setup);
  4236. if (!mdev) {
  4237. wiphy_free(wiphy);
  4238. return NULL;
  4239. }
  4240. sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
  4241. mdev->ieee80211_ptr = &sdata->wdev;
  4242. sdata->wdev.wiphy = wiphy;
  4243. local->hw.queues = 1; /* default */
  4244. local->mdev = mdev;
  4245. local->rx_pre_handlers = ieee80211_rx_pre_handlers;
  4246. local->rx_handlers = ieee80211_rx_handlers;
  4247. local->tx_handlers = ieee80211_tx_handlers;
  4248. local->bridge_packets = 1;
  4249. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  4250. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  4251. local->short_retry_limit = 7;
  4252. local->long_retry_limit = 4;
  4253. local->hw.conf.radio_enabled = 1;
  4254. local->rate_ctrl_num_up = RATE_CONTROL_NUM_UP;
  4255. local->rate_ctrl_num_down = RATE_CONTROL_NUM_DOWN;
  4256. local->enabled_modes = (unsigned int) -1;
  4257. INIT_LIST_HEAD(&local->modes_list);
  4258. rwlock_init(&local->sub_if_lock);
  4259. INIT_LIST_HEAD(&local->sub_if_list);
  4260. INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
  4261. init_timer(&local->stat_timer);
  4262. local->stat_timer.function = ieee80211_stat_refresh;
  4263. local->stat_timer.data = (unsigned long) local;
  4264. ieee80211_rx_bss_list_init(mdev);
  4265. sta_info_init(local);
  4266. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  4267. mdev->open = ieee80211_master_open;
  4268. mdev->stop = ieee80211_master_stop;
  4269. mdev->type = ARPHRD_IEEE80211;
  4270. mdev->hard_header_parse = header_parse_80211;
  4271. sdata->type = IEEE80211_IF_TYPE_AP;
  4272. sdata->dev = mdev;
  4273. sdata->local = local;
  4274. sdata->u.ap.force_unicast_rateidx = -1;
  4275. sdata->u.ap.max_ratectrl_rateidx = -1;
  4276. ieee80211_if_sdata_init(sdata);
  4277. list_add_tail(&sdata->list, &local->sub_if_list);
  4278. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  4279. (unsigned long)local);
  4280. tasklet_disable(&local->tx_pending_tasklet);
  4281. tasklet_init(&local->tasklet,
  4282. ieee80211_tasklet_handler,
  4283. (unsigned long) local);
  4284. tasklet_disable(&local->tasklet);
  4285. skb_queue_head_init(&local->skb_queue);
  4286. skb_queue_head_init(&local->skb_queue_unreliable);
  4287. return local_to_hw(local);
  4288. }
  4289. EXPORT_SYMBOL(ieee80211_alloc_hw);
  4290. int ieee80211_register_hw(struct ieee80211_hw *hw)
  4291. {
  4292. struct ieee80211_local *local = hw_to_local(hw);
  4293. const char *name;
  4294. int result;
  4295. result = wiphy_register(local->hw.wiphy);
  4296. if (result < 0)
  4297. return result;
  4298. name = wiphy_dev(local->hw.wiphy)->driver->name;
  4299. local->hw.workqueue = create_singlethread_workqueue(name);
  4300. if (!local->hw.workqueue) {
  4301. result = -ENOMEM;
  4302. goto fail_workqueue;
  4303. }
  4304. /*
  4305. * The hardware needs headroom for sending the frame,
  4306. * and we need some headroom for passing the frame to monitor
  4307. * interfaces, but never both at the same time.
  4308. */
  4309. local->tx_headroom = max(local->hw.extra_tx_headroom,
  4310. sizeof(struct ieee80211_tx_status_rtap_hdr));
  4311. debugfs_hw_add(local);
  4312. local->hw.conf.beacon_int = 1000;
  4313. local->wstats_flags |= local->hw.max_rssi ?
  4314. IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
  4315. local->wstats_flags |= local->hw.max_signal ?
  4316. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  4317. local->wstats_flags |= local->hw.max_noise ?
  4318. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  4319. if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
  4320. local->wstats_flags |= IW_QUAL_DBM;
  4321. result = sta_info_start(local);
  4322. if (result < 0)
  4323. goto fail_sta_info;
  4324. rtnl_lock();
  4325. result = dev_alloc_name(local->mdev, local->mdev->name);
  4326. if (result < 0)
  4327. goto fail_dev;
  4328. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  4329. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  4330. result = register_netdevice(local->mdev);
  4331. if (result < 0)
  4332. goto fail_dev;
  4333. ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  4334. result = ieee80211_init_rate_ctrl_alg(local, NULL);
  4335. if (result < 0) {
  4336. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  4337. "algorithm\n", local->mdev->name);
  4338. goto fail_rate;
  4339. }
  4340. result = ieee80211_wep_init(local);
  4341. if (result < 0) {
  4342. printk(KERN_DEBUG "%s: Failed to initialize wep\n",
  4343. local->mdev->name);
  4344. goto fail_wep;
  4345. }
  4346. ieee80211_install_qdisc(local->mdev);
  4347. /* add one default STA interface */
  4348. result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
  4349. IEEE80211_IF_TYPE_STA);
  4350. if (result)
  4351. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  4352. local->mdev->name);
  4353. local->reg_state = IEEE80211_DEV_REGISTERED;
  4354. rtnl_unlock();
  4355. ieee80211_led_init(local);
  4356. return 0;
  4357. fail_wep:
  4358. rate_control_deinitialize(local);
  4359. fail_rate:
  4360. ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  4361. unregister_netdevice(local->mdev);
  4362. fail_dev:
  4363. rtnl_unlock();
  4364. sta_info_stop(local);
  4365. fail_sta_info:
  4366. debugfs_hw_del(local);
  4367. destroy_workqueue(local->hw.workqueue);
  4368. fail_workqueue:
  4369. wiphy_unregister(local->hw.wiphy);
  4370. return result;
  4371. }
  4372. EXPORT_SYMBOL(ieee80211_register_hw);
  4373. int ieee80211_register_hwmode(struct ieee80211_hw *hw,
  4374. struct ieee80211_hw_mode *mode)
  4375. {
  4376. struct ieee80211_local *local = hw_to_local(hw);
  4377. struct ieee80211_rate *rate;
  4378. int i;
  4379. INIT_LIST_HEAD(&mode->list);
  4380. list_add_tail(&mode->list, &local->modes_list);
  4381. local->hw_modes |= (1 << mode->mode);
  4382. for (i = 0; i < mode->num_rates; i++) {
  4383. rate = &(mode->rates[i]);
  4384. rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
  4385. }
  4386. ieee80211_prepare_rates(local, mode);
  4387. if (!local->oper_hw_mode) {
  4388. /* Default to this mode */
  4389. local->hw.conf.phymode = mode->mode;
  4390. local->oper_hw_mode = local->scan_hw_mode = mode;
  4391. local->oper_channel = local->scan_channel = &mode->channels[0];
  4392. local->hw.conf.mode = local->oper_hw_mode;
  4393. local->hw.conf.chan = local->oper_channel;
  4394. }
  4395. if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
  4396. ieee80211_init_client(local->mdev);
  4397. return 0;
  4398. }
  4399. EXPORT_SYMBOL(ieee80211_register_hwmode);
  4400. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  4401. {
  4402. struct ieee80211_local *local = hw_to_local(hw);
  4403. struct ieee80211_sub_if_data *sdata, *tmp;
  4404. struct list_head tmp_list;
  4405. int i;
  4406. tasklet_kill(&local->tx_pending_tasklet);
  4407. tasklet_kill(&local->tasklet);
  4408. rtnl_lock();
  4409. BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
  4410. local->reg_state = IEEE80211_DEV_UNREGISTERED;
  4411. if (local->apdev)
  4412. ieee80211_if_del_mgmt(local);
  4413. write_lock_bh(&local->sub_if_lock);
  4414. list_replace_init(&local->sub_if_list, &tmp_list);
  4415. write_unlock_bh(&local->sub_if_lock);
  4416. list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
  4417. __ieee80211_if_del(local, sdata);
  4418. rtnl_unlock();
  4419. if (local->stat_time)
  4420. del_timer_sync(&local->stat_timer);
  4421. ieee80211_rx_bss_list_deinit(local->mdev);
  4422. ieee80211_clear_tx_pending(local);
  4423. sta_info_stop(local);
  4424. rate_control_deinitialize(local);
  4425. debugfs_hw_del(local);
  4426. for (i = 0; i < NUM_IEEE80211_MODES; i++) {
  4427. kfree(local->supp_rates[i]);
  4428. kfree(local->basic_rates[i]);
  4429. }
  4430. if (skb_queue_len(&local->skb_queue)
  4431. || skb_queue_len(&local->skb_queue_unreliable))
  4432. printk(KERN_WARNING "%s: skb_queue not empty\n",
  4433. local->mdev->name);
  4434. skb_queue_purge(&local->skb_queue);
  4435. skb_queue_purge(&local->skb_queue_unreliable);
  4436. destroy_workqueue(local->hw.workqueue);
  4437. wiphy_unregister(local->hw.wiphy);
  4438. ieee80211_wep_free(local);
  4439. ieee80211_led_exit(local);
  4440. }
  4441. EXPORT_SYMBOL(ieee80211_unregister_hw);
  4442. void ieee80211_free_hw(struct ieee80211_hw *hw)
  4443. {
  4444. struct ieee80211_local *local = hw_to_local(hw);
  4445. ieee80211_if_free(local->mdev);
  4446. wiphy_free(local->hw.wiphy);
  4447. }
  4448. EXPORT_SYMBOL(ieee80211_free_hw);
  4449. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  4450. {
  4451. struct ieee80211_local *local = hw_to_local(hw);
  4452. if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
  4453. &local->state[queue])) {
  4454. if (test_bit(IEEE80211_LINK_STATE_PENDING,
  4455. &local->state[queue]))
  4456. tasklet_schedule(&local->tx_pending_tasklet);
  4457. else
  4458. if (!ieee80211_qdisc_installed(local->mdev)) {
  4459. if (queue == 0)
  4460. netif_wake_queue(local->mdev);
  4461. } else
  4462. __netif_schedule(local->mdev);
  4463. }
  4464. }
  4465. EXPORT_SYMBOL(ieee80211_wake_queue);
  4466. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  4467. {
  4468. struct ieee80211_local *local = hw_to_local(hw);
  4469. if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
  4470. netif_stop_queue(local->mdev);
  4471. set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  4472. }
  4473. EXPORT_SYMBOL(ieee80211_stop_queue);
  4474. void ieee80211_start_queues(struct ieee80211_hw *hw)
  4475. {
  4476. struct ieee80211_local *local = hw_to_local(hw);
  4477. int i;
  4478. for (i = 0; i < local->hw.queues; i++)
  4479. clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
  4480. if (!ieee80211_qdisc_installed(local->mdev))
  4481. netif_start_queue(local->mdev);
  4482. }
  4483. EXPORT_SYMBOL(ieee80211_start_queues);
  4484. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  4485. {
  4486. int i;
  4487. for (i = 0; i < hw->queues; i++)
  4488. ieee80211_stop_queue(hw, i);
  4489. }
  4490. EXPORT_SYMBOL(ieee80211_stop_queues);
  4491. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  4492. {
  4493. int i;
  4494. for (i = 0; i < hw->queues; i++)
  4495. ieee80211_wake_queue(hw, i);
  4496. }
  4497. EXPORT_SYMBOL(ieee80211_wake_queues);
  4498. struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
  4499. {
  4500. struct ieee80211_sub_if_data *sdata;
  4501. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  4502. return &sdata->stats;
  4503. }
  4504. static int __init ieee80211_init(void)
  4505. {
  4506. struct sk_buff *skb;
  4507. int ret;
  4508. BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
  4509. ret = ieee80211_wme_register();
  4510. if (ret) {
  4511. printk(KERN_DEBUG "ieee80211_init: failed to "
  4512. "initialize WME (err=%d)\n", ret);
  4513. return ret;
  4514. }
  4515. ieee80211_debugfs_netdev_init();
  4516. return 0;
  4517. }
  4518. static void __exit ieee80211_exit(void)
  4519. {
  4520. ieee80211_wme_unregister();
  4521. ieee80211_debugfs_netdev_exit();
  4522. }
  4523. module_init(ieee80211_init);
  4524. module_exit(ieee80211_exit);
  4525. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  4526. MODULE_LICENSE("GPL");