ieee80211.c 145 KB

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