ieee80211.c 138 KB

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