ieee80211.c 145 KB

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