mlme.c 145 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107
  1. /*
  2. * BSS client mode implementation
  3. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright 2004, Instant802 Networks, Inc.
  5. * Copyright 2005, Devicescape Software, Inc.
  6. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  8. * Copyright 2013-2014 Intel Mobile Communications GmbH
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/if_ether.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/pm_qos.h>
  22. #include <linux/crc32.h>
  23. #include <linux/slab.h>
  24. #include <linux/export.h>
  25. #include <net/mac80211.h>
  26. #include <asm/unaligned.h>
  27. #include "ieee80211_i.h"
  28. #include "driver-ops.h"
  29. #include "rate.h"
  30. #include "led.h"
  31. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  32. #define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
  33. #define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
  34. #define IEEE80211_AUTH_MAX_TRIES 3
  35. #define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
  36. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  37. #define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
  38. #define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
  39. #define IEEE80211_ASSOC_MAX_TRIES 3
  40. static int max_nullfunc_tries = 2;
  41. module_param(max_nullfunc_tries, int, 0644);
  42. MODULE_PARM_DESC(max_nullfunc_tries,
  43. "Maximum nullfunc tx tries before disconnecting (reason 4).");
  44. static int max_probe_tries = 5;
  45. module_param(max_probe_tries, int, 0644);
  46. MODULE_PARM_DESC(max_probe_tries,
  47. "Maximum probe tries before disconnecting (reason 4).");
  48. /*
  49. * Beacon loss timeout is calculated as N frames times the
  50. * advertised beacon interval. This may need to be somewhat
  51. * higher than what hardware might detect to account for
  52. * delays in the host processing frames. But since we also
  53. * probe on beacon miss before declaring the connection lost
  54. * default to what we want.
  55. */
  56. static int beacon_loss_count = 7;
  57. module_param(beacon_loss_count, int, 0644);
  58. MODULE_PARM_DESC(beacon_loss_count,
  59. "Number of beacon intervals before we decide beacon was lost.");
  60. /*
  61. * Time the connection can be idle before we probe
  62. * it to see if we can still talk to the AP.
  63. */
  64. #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
  65. /*
  66. * Time we wait for a probe response after sending
  67. * a probe request because of beacon loss or for
  68. * checking the connection still works.
  69. */
  70. static int probe_wait_ms = 500;
  71. module_param(probe_wait_ms, int, 0644);
  72. MODULE_PARM_DESC(probe_wait_ms,
  73. "Maximum time(ms) to wait for probe response"
  74. " before disconnecting (reason 4).");
  75. /*
  76. * Weight given to the latest Beacon frame when calculating average signal
  77. * strength for Beacon frames received in the current BSS. This must be
  78. * between 1 and 15.
  79. */
  80. #define IEEE80211_SIGNAL_AVE_WEIGHT 3
  81. /*
  82. * How many Beacon frames need to have been used in average signal strength
  83. * before starting to indicate signal change events.
  84. */
  85. #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
  86. /*
  87. * We can have multiple work items (and connection probing)
  88. * scheduling this timer, but we need to take care to only
  89. * reschedule it when it should fire _earlier_ than it was
  90. * asked for before, or if it's not pending right now. This
  91. * function ensures that. Note that it then is required to
  92. * run this function for all timeouts after the first one
  93. * has happened -- the work that runs from this timer will
  94. * do that.
  95. */
  96. static void run_again(struct ieee80211_sub_if_data *sdata,
  97. unsigned long timeout)
  98. {
  99. sdata_assert_lock(sdata);
  100. if (!timer_pending(&sdata->u.mgd.timer) ||
  101. time_before(timeout, sdata->u.mgd.timer.expires))
  102. mod_timer(&sdata->u.mgd.timer, timeout);
  103. }
  104. void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
  105. {
  106. if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
  107. return;
  108. if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
  109. return;
  110. mod_timer(&sdata->u.mgd.bcn_mon_timer,
  111. round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
  112. }
  113. void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
  114. {
  115. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  116. if (unlikely(!sdata->u.mgd.associated))
  117. return;
  118. ifmgd->probe_send_count = 0;
  119. if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
  120. return;
  121. mod_timer(&sdata->u.mgd.conn_mon_timer,
  122. round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  123. }
  124. static int ecw2cw(int ecw)
  125. {
  126. return (1 << ecw) - 1;
  127. }
  128. static u32
  129. ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
  130. struct ieee80211_supported_band *sband,
  131. struct ieee80211_channel *channel,
  132. const struct ieee80211_ht_cap *ht_cap,
  133. const struct ieee80211_ht_operation *ht_oper,
  134. const struct ieee80211_vht_operation *vht_oper,
  135. struct cfg80211_chan_def *chandef, bool tracking)
  136. {
  137. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  138. struct cfg80211_chan_def vht_chandef;
  139. struct ieee80211_sta_ht_cap sta_ht_cap;
  140. u32 ht_cfreq, ret;
  141. memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
  142. ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
  143. chandef->chan = channel;
  144. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  145. chandef->center_freq1 = channel->center_freq;
  146. chandef->center_freq2 = 0;
  147. if (!ht_cap || !ht_oper || !sta_ht_cap.ht_supported) {
  148. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  149. goto out;
  150. }
  151. chandef->width = NL80211_CHAN_WIDTH_20;
  152. if (!(ht_cap->cap_info &
  153. cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH_20_40))) {
  154. ret = IEEE80211_STA_DISABLE_40MHZ;
  155. vht_chandef = *chandef;
  156. goto out;
  157. }
  158. ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
  159. channel->band);
  160. /* check that channel matches the right operating channel */
  161. if (!tracking && channel->center_freq != ht_cfreq) {
  162. /*
  163. * It's possible that some APs are confused here;
  164. * Netgear WNDR3700 sometimes reports 4 higher than
  165. * the actual channel in association responses, but
  166. * since we look at probe response/beacon data here
  167. * it should be OK.
  168. */
  169. sdata_info(sdata,
  170. "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
  171. channel->center_freq, ht_cfreq,
  172. ht_oper->primary_chan, channel->band);
  173. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  174. goto out;
  175. }
  176. /* check 40 MHz support, if we have it */
  177. if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  178. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  179. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  180. chandef->width = NL80211_CHAN_WIDTH_40;
  181. chandef->center_freq1 += 10;
  182. break;
  183. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  184. chandef->width = NL80211_CHAN_WIDTH_40;
  185. chandef->center_freq1 -= 10;
  186. break;
  187. }
  188. } else {
  189. /* 40 MHz (and 80 MHz) must be supported for VHT */
  190. ret = IEEE80211_STA_DISABLE_VHT;
  191. /* also mark 40 MHz disabled */
  192. ret |= IEEE80211_STA_DISABLE_40MHZ;
  193. goto out;
  194. }
  195. if (!vht_oper || !sband->vht_cap.vht_supported) {
  196. ret = IEEE80211_STA_DISABLE_VHT;
  197. goto out;
  198. }
  199. vht_chandef.chan = channel;
  200. vht_chandef.center_freq1 =
  201. ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
  202. channel->band);
  203. vht_chandef.center_freq2 = 0;
  204. switch (vht_oper->chan_width) {
  205. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  206. vht_chandef.width = chandef->width;
  207. vht_chandef.center_freq1 = chandef->center_freq1;
  208. break;
  209. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  210. vht_chandef.width = NL80211_CHAN_WIDTH_80;
  211. break;
  212. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  213. vht_chandef.width = NL80211_CHAN_WIDTH_160;
  214. break;
  215. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  216. vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
  217. vht_chandef.center_freq2 =
  218. ieee80211_channel_to_frequency(
  219. vht_oper->center_freq_seg2_idx,
  220. channel->band);
  221. break;
  222. default:
  223. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  224. sdata_info(sdata,
  225. "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
  226. vht_oper->chan_width);
  227. ret = IEEE80211_STA_DISABLE_VHT;
  228. goto out;
  229. }
  230. if (!cfg80211_chandef_valid(&vht_chandef)) {
  231. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  232. sdata_info(sdata,
  233. "AP VHT information is invalid, disable VHT\n");
  234. ret = IEEE80211_STA_DISABLE_VHT;
  235. goto out;
  236. }
  237. if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
  238. ret = 0;
  239. goto out;
  240. }
  241. if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
  242. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  243. sdata_info(sdata,
  244. "AP VHT information doesn't match HT, disable VHT\n");
  245. ret = IEEE80211_STA_DISABLE_VHT;
  246. goto out;
  247. }
  248. *chandef = vht_chandef;
  249. ret = 0;
  250. out:
  251. /*
  252. * When tracking the current AP, don't do any further checks if the
  253. * new chandef is identical to the one we're currently using for the
  254. * connection. This keeps us from playing ping-pong with regulatory,
  255. * without it the following can happen (for example):
  256. * - connect to an AP with 80 MHz, world regdom allows 80 MHz
  257. * - AP advertises regdom US
  258. * - CRDA loads regdom US with 80 MHz prohibited (old database)
  259. * - the code below detects an unsupported channel, downgrades, and
  260. * we disconnect from the AP in the caller
  261. * - disconnect causes CRDA to reload world regdomain and the game
  262. * starts anew.
  263. * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
  264. *
  265. * It seems possible that there are still scenarios with CSA or real
  266. * bandwidth changes where a this could happen, but those cases are
  267. * less common and wouldn't completely prevent using the AP.
  268. */
  269. if (tracking &&
  270. cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
  271. return ret;
  272. /* don't print the message below for VHT mismatch if VHT is disabled */
  273. if (ret & IEEE80211_STA_DISABLE_VHT)
  274. vht_chandef = *chandef;
  275. /*
  276. * Ignore the DISABLED flag when we're already connected and only
  277. * tracking the APs beacon for bandwidth changes - otherwise we
  278. * might get disconnected here if we connect to an AP, update our
  279. * regulatory information based on the AP's country IE and the
  280. * information we have is wrong/outdated and disables the channel
  281. * that we're actually using for the connection to the AP.
  282. */
  283. while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  284. tracking ? 0 :
  285. IEEE80211_CHAN_DISABLED)) {
  286. if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
  287. ret = IEEE80211_STA_DISABLE_HT |
  288. IEEE80211_STA_DISABLE_VHT;
  289. break;
  290. }
  291. ret |= ieee80211_chandef_downgrade(chandef);
  292. }
  293. if (chandef->width != vht_chandef.width && !tracking)
  294. sdata_info(sdata,
  295. "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
  296. WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
  297. return ret;
  298. }
  299. static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
  300. struct sta_info *sta,
  301. const struct ieee80211_ht_cap *ht_cap,
  302. const struct ieee80211_ht_operation *ht_oper,
  303. const struct ieee80211_vht_operation *vht_oper,
  304. const u8 *bssid, u32 *changed)
  305. {
  306. struct ieee80211_local *local = sdata->local;
  307. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  308. struct ieee80211_supported_band *sband;
  309. struct ieee80211_channel *chan;
  310. struct cfg80211_chan_def chandef;
  311. u16 ht_opmode;
  312. u32 flags;
  313. enum ieee80211_sta_rx_bandwidth new_sta_bw;
  314. int ret;
  315. /* if HT was/is disabled, don't track any bandwidth changes */
  316. if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
  317. return 0;
  318. /* don't check VHT if we associated as non-VHT station */
  319. if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
  320. vht_oper = NULL;
  321. if (WARN_ON_ONCE(!sta))
  322. return -EINVAL;
  323. /*
  324. * if bss configuration changed store the new one -
  325. * this may be applicable even if channel is identical
  326. */
  327. ht_opmode = le16_to_cpu(ht_oper->operation_mode);
  328. if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
  329. *changed |= BSS_CHANGED_HT;
  330. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  331. }
  332. chan = sdata->vif.bss_conf.chandef.chan;
  333. sband = local->hw.wiphy->bands[chan->band];
  334. /* calculate new channel (type) based on HT/VHT operation IEs */
  335. flags = ieee80211_determine_chantype(sdata, sband, chan,
  336. ht_cap, ht_oper, vht_oper,
  337. &chandef, true);
  338. /*
  339. * Downgrade the new channel if we associated with restricted
  340. * capabilities. For example, if we associated as a 20 MHz STA
  341. * to a 40 MHz AP (due to regulatory, capabilities or config
  342. * reasons) then switching to a 40 MHz channel now won't do us
  343. * any good -- we couldn't use it with the AP.
  344. */
  345. if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
  346. chandef.width == NL80211_CHAN_WIDTH_80P80)
  347. flags |= ieee80211_chandef_downgrade(&chandef);
  348. if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
  349. chandef.width == NL80211_CHAN_WIDTH_160)
  350. flags |= ieee80211_chandef_downgrade(&chandef);
  351. if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
  352. chandef.width > NL80211_CHAN_WIDTH_20)
  353. flags |= ieee80211_chandef_downgrade(&chandef);
  354. if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
  355. return 0;
  356. sdata_info(sdata,
  357. "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
  358. ifmgd->bssid, chandef.chan->center_freq, chandef.width,
  359. chandef.center_freq1, chandef.center_freq2);
  360. if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
  361. IEEE80211_STA_DISABLE_VHT |
  362. IEEE80211_STA_DISABLE_40MHZ |
  363. IEEE80211_STA_DISABLE_80P80MHZ |
  364. IEEE80211_STA_DISABLE_160MHZ)) ||
  365. !cfg80211_chandef_valid(&chandef)) {
  366. sdata_info(sdata,
  367. "AP %pM changed bandwidth in a way we can't support - disconnect\n",
  368. ifmgd->bssid);
  369. return -EINVAL;
  370. }
  371. switch (chandef.width) {
  372. case NL80211_CHAN_WIDTH_20_NOHT:
  373. case NL80211_CHAN_WIDTH_20:
  374. new_sta_bw = IEEE80211_STA_RX_BW_20;
  375. break;
  376. case NL80211_CHAN_WIDTH_40:
  377. new_sta_bw = IEEE80211_STA_RX_BW_40;
  378. break;
  379. case NL80211_CHAN_WIDTH_80:
  380. new_sta_bw = IEEE80211_STA_RX_BW_80;
  381. break;
  382. case NL80211_CHAN_WIDTH_80P80:
  383. case NL80211_CHAN_WIDTH_160:
  384. new_sta_bw = IEEE80211_STA_RX_BW_160;
  385. break;
  386. default:
  387. return -EINVAL;
  388. }
  389. if (new_sta_bw > sta->cur_max_bandwidth)
  390. new_sta_bw = sta->cur_max_bandwidth;
  391. if (new_sta_bw < sta->sta.bandwidth) {
  392. sta->sta.bandwidth = new_sta_bw;
  393. rate_control_rate_update(local, sband, sta,
  394. IEEE80211_RC_BW_CHANGED);
  395. }
  396. ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
  397. if (ret) {
  398. sdata_info(sdata,
  399. "AP %pM changed bandwidth to incompatible one - disconnect\n",
  400. ifmgd->bssid);
  401. return ret;
  402. }
  403. if (new_sta_bw > sta->sta.bandwidth) {
  404. sta->sta.bandwidth = new_sta_bw;
  405. rate_control_rate_update(local, sband, sta,
  406. IEEE80211_RC_BW_CHANGED);
  407. }
  408. return 0;
  409. }
  410. /* frame sending functions */
  411. static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
  412. struct sk_buff *skb, u8 ap_ht_param,
  413. struct ieee80211_supported_band *sband,
  414. struct ieee80211_channel *channel,
  415. enum ieee80211_smps_mode smps)
  416. {
  417. u8 *pos;
  418. u32 flags = channel->flags;
  419. u16 cap;
  420. struct ieee80211_sta_ht_cap ht_cap;
  421. BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
  422. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  423. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  424. /* determine capability flags */
  425. cap = ht_cap.cap;
  426. switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  427. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  428. if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
  429. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  430. cap &= ~IEEE80211_HT_CAP_SGI_40;
  431. }
  432. break;
  433. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  434. if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
  435. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  436. cap &= ~IEEE80211_HT_CAP_SGI_40;
  437. }
  438. break;
  439. }
  440. /*
  441. * If 40 MHz was disabled associate as though we weren't
  442. * capable of 40 MHz -- some broken APs will never fall
  443. * back to trying to transmit in 20 MHz.
  444. */
  445. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
  446. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  447. cap &= ~IEEE80211_HT_CAP_SGI_40;
  448. }
  449. /* set SM PS mode properly */
  450. cap &= ~IEEE80211_HT_CAP_SM_PS;
  451. switch (smps) {
  452. case IEEE80211_SMPS_AUTOMATIC:
  453. case IEEE80211_SMPS_NUM_MODES:
  454. WARN_ON(1);
  455. case IEEE80211_SMPS_OFF:
  456. cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
  457. IEEE80211_HT_CAP_SM_PS_SHIFT;
  458. break;
  459. case IEEE80211_SMPS_STATIC:
  460. cap |= WLAN_HT_CAP_SM_PS_STATIC <<
  461. IEEE80211_HT_CAP_SM_PS_SHIFT;
  462. break;
  463. case IEEE80211_SMPS_DYNAMIC:
  464. cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
  465. IEEE80211_HT_CAP_SM_PS_SHIFT;
  466. break;
  467. }
  468. /* reserve and fill IE */
  469. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  470. ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
  471. }
  472. static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
  473. struct sk_buff *skb,
  474. struct ieee80211_supported_band *sband,
  475. struct ieee80211_vht_cap *ap_vht_cap)
  476. {
  477. u8 *pos;
  478. u32 cap;
  479. struct ieee80211_sta_vht_cap vht_cap;
  480. u32 mask, ap_bf_sts, our_bf_sts;
  481. BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
  482. memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
  483. ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
  484. /* determine capability flags */
  485. cap = vht_cap.cap;
  486. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
  487. u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
  488. cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
  489. if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
  490. bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
  491. cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
  492. }
  493. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
  494. cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
  495. cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
  496. }
  497. /*
  498. * Some APs apparently get confused if our capabilities are better
  499. * than theirs, so restrict what we advertise in the assoc request.
  500. */
  501. if (!(ap_vht_cap->vht_cap_info &
  502. cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
  503. cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
  504. mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
  505. ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
  506. our_bf_sts = cap & mask;
  507. if (ap_bf_sts < our_bf_sts) {
  508. cap &= ~mask;
  509. cap |= ap_bf_sts;
  510. }
  511. /* reserve and fill IE */
  512. pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
  513. ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
  514. }
  515. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
  516. {
  517. struct ieee80211_local *local = sdata->local;
  518. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  519. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  520. struct sk_buff *skb;
  521. struct ieee80211_mgmt *mgmt;
  522. u8 *pos, qos_info;
  523. size_t offset = 0, noffset;
  524. int i, count, rates_len, supp_rates_len, shift;
  525. u16 capab;
  526. struct ieee80211_supported_band *sband;
  527. struct ieee80211_chanctx_conf *chanctx_conf;
  528. struct ieee80211_channel *chan;
  529. u32 rate_flags, rates = 0;
  530. sdata_assert_lock(sdata);
  531. rcu_read_lock();
  532. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  533. if (WARN_ON(!chanctx_conf)) {
  534. rcu_read_unlock();
  535. return;
  536. }
  537. chan = chanctx_conf->def.chan;
  538. rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
  539. rcu_read_unlock();
  540. sband = local->hw.wiphy->bands[chan->band];
  541. shift = ieee80211_vif_get_shift(&sdata->vif);
  542. if (assoc_data->supp_rates_len) {
  543. /*
  544. * Get all rates supported by the device and the AP as
  545. * some APs don't like getting a superset of their rates
  546. * in the association request (e.g. D-Link DAP 1353 in
  547. * b-only mode)...
  548. */
  549. rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
  550. assoc_data->supp_rates,
  551. assoc_data->supp_rates_len,
  552. &rates);
  553. } else {
  554. /*
  555. * In case AP not provide any supported rates information
  556. * before association, we send information element(s) with
  557. * all rates that we support.
  558. */
  559. rates_len = 0;
  560. for (i = 0; i < sband->n_bitrates; i++) {
  561. if ((rate_flags & sband->bitrates[i].flags)
  562. != rate_flags)
  563. continue;
  564. rates |= BIT(i);
  565. rates_len++;
  566. }
  567. }
  568. skb = alloc_skb(local->hw.extra_tx_headroom +
  569. sizeof(*mgmt) + /* bit too much but doesn't matter */
  570. 2 + assoc_data->ssid_len + /* SSID */
  571. 4 + rates_len + /* (extended) rates */
  572. 4 + /* power capability */
  573. 2 + 2 * sband->n_channels + /* supported channels */
  574. 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
  575. 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
  576. assoc_data->ie_len + /* extra IEs */
  577. 9, /* WMM */
  578. GFP_KERNEL);
  579. if (!skb)
  580. return;
  581. skb_reserve(skb, local->hw.extra_tx_headroom);
  582. capab = WLAN_CAPABILITY_ESS;
  583. if (sband->band == IEEE80211_BAND_2GHZ) {
  584. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  585. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  586. }
  587. if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
  588. capab |= WLAN_CAPABILITY_PRIVACY;
  589. if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  590. ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
  591. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  592. if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
  593. capab |= WLAN_CAPABILITY_RADIO_MEASURE;
  594. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  595. memset(mgmt, 0, 24);
  596. memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
  597. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  598. memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
  599. if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
  600. skb_put(skb, 10);
  601. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  602. IEEE80211_STYPE_REASSOC_REQ);
  603. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  604. mgmt->u.reassoc_req.listen_interval =
  605. cpu_to_le16(local->hw.conf.listen_interval);
  606. memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
  607. ETH_ALEN);
  608. } else {
  609. skb_put(skb, 4);
  610. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  611. IEEE80211_STYPE_ASSOC_REQ);
  612. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  613. mgmt->u.assoc_req.listen_interval =
  614. cpu_to_le16(local->hw.conf.listen_interval);
  615. }
  616. /* SSID */
  617. pos = skb_put(skb, 2 + assoc_data->ssid_len);
  618. *pos++ = WLAN_EID_SSID;
  619. *pos++ = assoc_data->ssid_len;
  620. memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
  621. /* add all rates which were marked to be used above */
  622. supp_rates_len = rates_len;
  623. if (supp_rates_len > 8)
  624. supp_rates_len = 8;
  625. pos = skb_put(skb, supp_rates_len + 2);
  626. *pos++ = WLAN_EID_SUPP_RATES;
  627. *pos++ = supp_rates_len;
  628. count = 0;
  629. for (i = 0; i < sband->n_bitrates; i++) {
  630. if (BIT(i) & rates) {
  631. int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  632. 5 * (1 << shift));
  633. *pos++ = (u8) rate;
  634. if (++count == 8)
  635. break;
  636. }
  637. }
  638. if (rates_len > count) {
  639. pos = skb_put(skb, rates_len - count + 2);
  640. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  641. *pos++ = rates_len - count;
  642. for (i++; i < sband->n_bitrates; i++) {
  643. if (BIT(i) & rates) {
  644. int rate;
  645. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  646. 5 * (1 << shift));
  647. *pos++ = (u8) rate;
  648. }
  649. }
  650. }
  651. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
  652. capab & WLAN_CAPABILITY_RADIO_MEASURE) {
  653. pos = skb_put(skb, 4);
  654. *pos++ = WLAN_EID_PWR_CAPABILITY;
  655. *pos++ = 2;
  656. *pos++ = 0; /* min tx power */
  657. /* max tx power */
  658. *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
  659. }
  660. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  661. /* TODO: get this in reg domain format */
  662. pos = skb_put(skb, 2 * sband->n_channels + 2);
  663. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  664. *pos++ = 2 * sband->n_channels;
  665. for (i = 0; i < sband->n_channels; i++) {
  666. *pos++ = ieee80211_frequency_to_channel(
  667. sband->channels[i].center_freq);
  668. *pos++ = 1; /* one channel in the subband*/
  669. }
  670. }
  671. /* if present, add any custom IEs that go before HT */
  672. if (assoc_data->ie_len) {
  673. static const u8 before_ht[] = {
  674. WLAN_EID_SSID,
  675. WLAN_EID_SUPP_RATES,
  676. WLAN_EID_EXT_SUPP_RATES,
  677. WLAN_EID_PWR_CAPABILITY,
  678. WLAN_EID_SUPPORTED_CHANNELS,
  679. WLAN_EID_RSN,
  680. WLAN_EID_QOS_CAPA,
  681. WLAN_EID_RRM_ENABLED_CAPABILITIES,
  682. WLAN_EID_MOBILITY_DOMAIN,
  683. WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
  684. WLAN_EID_RIC_DATA, /* reassoc only */
  685. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  686. };
  687. static const u8 after_ric[] = {
  688. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  689. WLAN_EID_HT_CAPABILITY,
  690. WLAN_EID_BSS_COEX_2040,
  691. WLAN_EID_EXT_CAPABILITY,
  692. WLAN_EID_QOS_TRAFFIC_CAPA,
  693. WLAN_EID_TIM_BCAST_REQ,
  694. WLAN_EID_INTERWORKING,
  695. /* 60GHz doesn't happen right now */
  696. WLAN_EID_VHT_CAPABILITY,
  697. WLAN_EID_OPMODE_NOTIF,
  698. };
  699. noffset = ieee80211_ie_split_ric(assoc_data->ie,
  700. assoc_data->ie_len,
  701. before_ht,
  702. ARRAY_SIZE(before_ht),
  703. after_ric,
  704. ARRAY_SIZE(after_ric),
  705. offset);
  706. pos = skb_put(skb, noffset - offset);
  707. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  708. offset = noffset;
  709. }
  710. if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  711. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
  712. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  713. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  714. ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
  715. sband, chan, sdata->smps_mode);
  716. /* if present, add any custom IEs that go before VHT */
  717. if (assoc_data->ie_len) {
  718. static const u8 before_vht[] = {
  719. WLAN_EID_SSID,
  720. WLAN_EID_SUPP_RATES,
  721. WLAN_EID_EXT_SUPP_RATES,
  722. WLAN_EID_PWR_CAPABILITY,
  723. WLAN_EID_SUPPORTED_CHANNELS,
  724. WLAN_EID_RSN,
  725. WLAN_EID_QOS_CAPA,
  726. WLAN_EID_RRM_ENABLED_CAPABILITIES,
  727. WLAN_EID_MOBILITY_DOMAIN,
  728. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  729. WLAN_EID_HT_CAPABILITY,
  730. WLAN_EID_BSS_COEX_2040,
  731. WLAN_EID_EXT_CAPABILITY,
  732. WLAN_EID_QOS_TRAFFIC_CAPA,
  733. WLAN_EID_TIM_BCAST_REQ,
  734. WLAN_EID_INTERWORKING,
  735. };
  736. /* RIC already taken above, so no need to handle here anymore */
  737. noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
  738. before_vht, ARRAY_SIZE(before_vht),
  739. offset);
  740. pos = skb_put(skb, noffset - offset);
  741. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  742. offset = noffset;
  743. }
  744. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  745. ieee80211_add_vht_ie(sdata, skb, sband,
  746. &assoc_data->ap_vht_cap);
  747. /* if present, add any custom non-vendor IEs that go after HT */
  748. if (assoc_data->ie_len) {
  749. noffset = ieee80211_ie_split_vendor(assoc_data->ie,
  750. assoc_data->ie_len,
  751. offset);
  752. pos = skb_put(skb, noffset - offset);
  753. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  754. offset = noffset;
  755. }
  756. if (assoc_data->wmm) {
  757. if (assoc_data->uapsd) {
  758. qos_info = ifmgd->uapsd_queues;
  759. qos_info |= (ifmgd->uapsd_max_sp_len <<
  760. IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
  761. } else {
  762. qos_info = 0;
  763. }
  764. pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
  765. }
  766. /* add any remaining custom (i.e. vendor specific here) IEs */
  767. if (assoc_data->ie_len) {
  768. noffset = assoc_data->ie_len;
  769. pos = skb_put(skb, noffset - offset);
  770. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  771. }
  772. drv_mgd_prepare_tx(local, sdata);
  773. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  774. if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
  775. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
  776. IEEE80211_TX_INTFL_MLME_CONN_TX;
  777. ieee80211_tx_skb(sdata, skb);
  778. }
  779. void ieee80211_send_pspoll(struct ieee80211_local *local,
  780. struct ieee80211_sub_if_data *sdata)
  781. {
  782. struct ieee80211_pspoll *pspoll;
  783. struct sk_buff *skb;
  784. skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
  785. if (!skb)
  786. return;
  787. pspoll = (struct ieee80211_pspoll *) skb->data;
  788. pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  789. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  790. ieee80211_tx_skb(sdata, skb);
  791. }
  792. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  793. struct ieee80211_sub_if_data *sdata,
  794. int powersave)
  795. {
  796. struct sk_buff *skb;
  797. struct ieee80211_hdr_3addr *nullfunc;
  798. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  799. skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
  800. if (!skb)
  801. return;
  802. nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
  803. if (powersave)
  804. nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  805. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  806. IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
  807. if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
  808. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  809. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
  810. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
  811. ieee80211_tx_skb(sdata, skb);
  812. }
  813. static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
  814. struct ieee80211_sub_if_data *sdata)
  815. {
  816. struct sk_buff *skb;
  817. struct ieee80211_hdr *nullfunc;
  818. __le16 fc;
  819. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  820. return;
  821. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
  822. if (!skb)
  823. return;
  824. skb_reserve(skb, local->hw.extra_tx_headroom);
  825. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
  826. memset(nullfunc, 0, 30);
  827. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  828. IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  829. nullfunc->frame_control = fc;
  830. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  831. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  832. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  833. memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
  834. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  835. ieee80211_tx_skb(sdata, skb);
  836. }
  837. /* spectrum management related things */
  838. static void ieee80211_chswitch_work(struct work_struct *work)
  839. {
  840. struct ieee80211_sub_if_data *sdata =
  841. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  842. struct ieee80211_local *local = sdata->local;
  843. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  844. int ret;
  845. if (!ieee80211_sdata_running(sdata))
  846. return;
  847. sdata_lock(sdata);
  848. mutex_lock(&local->mtx);
  849. mutex_lock(&local->chanctx_mtx);
  850. if (!ifmgd->associated)
  851. goto out;
  852. if (!sdata->vif.csa_active)
  853. goto out;
  854. /*
  855. * using reservation isn't immediate as it may be deferred until later
  856. * with multi-vif. once reservation is complete it will re-schedule the
  857. * work with no reserved_chanctx so verify chandef to check if it
  858. * completed successfully
  859. */
  860. if (sdata->reserved_chanctx) {
  861. /*
  862. * with multi-vif csa driver may call ieee80211_csa_finish()
  863. * many times while waiting for other interfaces to use their
  864. * reservations
  865. */
  866. if (sdata->reserved_ready)
  867. goto out;
  868. ret = ieee80211_vif_use_reserved_context(sdata);
  869. if (ret) {
  870. sdata_info(sdata,
  871. "failed to use reserved channel context, disconnecting (err=%d)\n",
  872. ret);
  873. ieee80211_queue_work(&sdata->local->hw,
  874. &ifmgd->csa_connection_drop_work);
  875. goto out;
  876. }
  877. goto out;
  878. }
  879. if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
  880. &sdata->csa_chandef)) {
  881. sdata_info(sdata,
  882. "failed to finalize channel switch, disconnecting\n");
  883. ieee80211_queue_work(&sdata->local->hw,
  884. &ifmgd->csa_connection_drop_work);
  885. goto out;
  886. }
  887. /* XXX: shouldn't really modify cfg80211-owned data! */
  888. ifmgd->associated->channel = sdata->csa_chandef.chan;
  889. ifmgd->csa_waiting_bcn = true;
  890. ieee80211_sta_reset_beacon_monitor(sdata);
  891. ieee80211_sta_reset_conn_monitor(sdata);
  892. out:
  893. mutex_unlock(&local->chanctx_mtx);
  894. mutex_unlock(&local->mtx);
  895. sdata_unlock(sdata);
  896. }
  897. static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
  898. {
  899. struct ieee80211_local *local = sdata->local;
  900. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  901. int ret;
  902. sdata_assert_lock(sdata);
  903. WARN_ON(!sdata->vif.csa_active);
  904. if (sdata->csa_block_tx) {
  905. ieee80211_wake_vif_queues(local, sdata,
  906. IEEE80211_QUEUE_STOP_REASON_CSA);
  907. sdata->csa_block_tx = false;
  908. }
  909. sdata->vif.csa_active = false;
  910. ifmgd->csa_waiting_bcn = false;
  911. ret = drv_post_channel_switch(sdata);
  912. if (ret) {
  913. sdata_info(sdata,
  914. "driver post channel switch failed, disconnecting\n");
  915. ieee80211_queue_work(&local->hw,
  916. &ifmgd->csa_connection_drop_work);
  917. return;
  918. }
  919. cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
  920. }
  921. void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
  922. {
  923. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  924. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  925. trace_api_chswitch_done(sdata, success);
  926. if (!success) {
  927. sdata_info(sdata,
  928. "driver channel switch failed, disconnecting\n");
  929. ieee80211_queue_work(&sdata->local->hw,
  930. &ifmgd->csa_connection_drop_work);
  931. } else {
  932. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  933. }
  934. }
  935. EXPORT_SYMBOL(ieee80211_chswitch_done);
  936. static void ieee80211_chswitch_timer(unsigned long data)
  937. {
  938. struct ieee80211_sub_if_data *sdata =
  939. (struct ieee80211_sub_if_data *) data;
  940. ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
  941. }
  942. static void ieee80211_teardown_tdls_peers(struct ieee80211_sub_if_data *sdata)
  943. {
  944. struct sta_info *sta;
  945. u16 reason = WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED;
  946. rcu_read_lock();
  947. list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
  948. if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
  949. !test_sta_flag(sta, WLAN_STA_AUTHORIZED))
  950. continue;
  951. ieee80211_tdls_oper_request(&sdata->vif, sta->sta.addr,
  952. NL80211_TDLS_TEARDOWN, reason,
  953. GFP_ATOMIC);
  954. }
  955. rcu_read_unlock();
  956. }
  957. static void
  958. ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  959. u64 timestamp, u32 device_timestamp,
  960. struct ieee802_11_elems *elems,
  961. bool beacon)
  962. {
  963. struct ieee80211_local *local = sdata->local;
  964. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  965. struct cfg80211_bss *cbss = ifmgd->associated;
  966. struct ieee80211_chanctx_conf *conf;
  967. struct ieee80211_chanctx *chanctx;
  968. enum ieee80211_band current_band;
  969. struct ieee80211_csa_ie csa_ie;
  970. struct ieee80211_channel_switch ch_switch;
  971. int res;
  972. sdata_assert_lock(sdata);
  973. if (!cbss)
  974. return;
  975. if (local->scanning)
  976. return;
  977. /* disregard subsequent announcements if we are already processing */
  978. if (sdata->vif.csa_active)
  979. return;
  980. current_band = cbss->channel->band;
  981. memset(&csa_ie, 0, sizeof(csa_ie));
  982. res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
  983. ifmgd->flags,
  984. ifmgd->associated->bssid, &csa_ie);
  985. if (res < 0)
  986. ieee80211_queue_work(&local->hw,
  987. &ifmgd->csa_connection_drop_work);
  988. if (res)
  989. return;
  990. if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
  991. IEEE80211_CHAN_DISABLED)) {
  992. sdata_info(sdata,
  993. "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
  994. ifmgd->associated->bssid,
  995. csa_ie.chandef.chan->center_freq,
  996. csa_ie.chandef.width, csa_ie.chandef.center_freq1,
  997. csa_ie.chandef.center_freq2);
  998. ieee80211_queue_work(&local->hw,
  999. &ifmgd->csa_connection_drop_work);
  1000. return;
  1001. }
  1002. if (cfg80211_chandef_identical(&csa_ie.chandef,
  1003. &sdata->vif.bss_conf.chandef)) {
  1004. if (ifmgd->csa_ignored_same_chan)
  1005. return;
  1006. sdata_info(sdata,
  1007. "AP %pM tries to chanswitch to same channel, ignore\n",
  1008. ifmgd->associated->bssid);
  1009. ifmgd->csa_ignored_same_chan = true;
  1010. return;
  1011. }
  1012. /*
  1013. * Drop all TDLS peers - either we disconnect or move to a different
  1014. * channel from this point on. There's no telling what our peer will do.
  1015. * The TDLS WIDER_BW scenario is also problematic, as peers might now
  1016. * have an incompatible wider chandef.
  1017. */
  1018. ieee80211_teardown_tdls_peers(sdata);
  1019. mutex_lock(&local->mtx);
  1020. mutex_lock(&local->chanctx_mtx);
  1021. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1022. lockdep_is_held(&local->chanctx_mtx));
  1023. if (!conf) {
  1024. sdata_info(sdata,
  1025. "no channel context assigned to vif?, disconnecting\n");
  1026. goto drop_connection;
  1027. }
  1028. chanctx = container_of(conf, struct ieee80211_chanctx, conf);
  1029. if (local->use_chanctx &&
  1030. !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
  1031. sdata_info(sdata,
  1032. "driver doesn't support chan-switch with channel contexts\n");
  1033. goto drop_connection;
  1034. }
  1035. ch_switch.timestamp = timestamp;
  1036. ch_switch.device_timestamp = device_timestamp;
  1037. ch_switch.block_tx = csa_ie.mode;
  1038. ch_switch.chandef = csa_ie.chandef;
  1039. ch_switch.count = csa_ie.count;
  1040. if (drv_pre_channel_switch(sdata, &ch_switch)) {
  1041. sdata_info(sdata,
  1042. "preparing for channel switch failed, disconnecting\n");
  1043. goto drop_connection;
  1044. }
  1045. res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
  1046. chanctx->mode, false);
  1047. if (res) {
  1048. sdata_info(sdata,
  1049. "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
  1050. res);
  1051. goto drop_connection;
  1052. }
  1053. mutex_unlock(&local->chanctx_mtx);
  1054. sdata->vif.csa_active = true;
  1055. sdata->csa_chandef = csa_ie.chandef;
  1056. sdata->csa_block_tx = csa_ie.mode;
  1057. ifmgd->csa_ignored_same_chan = false;
  1058. if (sdata->csa_block_tx)
  1059. ieee80211_stop_vif_queues(local, sdata,
  1060. IEEE80211_QUEUE_STOP_REASON_CSA);
  1061. mutex_unlock(&local->mtx);
  1062. cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
  1063. csa_ie.count);
  1064. if (local->ops->channel_switch) {
  1065. /* use driver's channel switch callback */
  1066. drv_channel_switch(local, sdata, &ch_switch);
  1067. return;
  1068. }
  1069. /* channel switch handled in software */
  1070. if (csa_ie.count <= 1)
  1071. ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
  1072. else
  1073. mod_timer(&ifmgd->chswitch_timer,
  1074. TU_TO_EXP_TIME((csa_ie.count - 1) *
  1075. cbss->beacon_interval));
  1076. return;
  1077. drop_connection:
  1078. ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
  1079. mutex_unlock(&local->chanctx_mtx);
  1080. mutex_unlock(&local->mtx);
  1081. }
  1082. static bool
  1083. ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
  1084. struct ieee80211_channel *channel,
  1085. const u8 *country_ie, u8 country_ie_len,
  1086. const u8 *pwr_constr_elem,
  1087. int *chan_pwr, int *pwr_reduction)
  1088. {
  1089. struct ieee80211_country_ie_triplet *triplet;
  1090. int chan = ieee80211_frequency_to_channel(channel->center_freq);
  1091. int i, chan_increment;
  1092. bool have_chan_pwr = false;
  1093. /* Invalid IE */
  1094. if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1095. return false;
  1096. triplet = (void *)(country_ie + 3);
  1097. country_ie_len -= 3;
  1098. switch (channel->band) {
  1099. default:
  1100. WARN_ON_ONCE(1);
  1101. /* fall through */
  1102. case IEEE80211_BAND_2GHZ:
  1103. case IEEE80211_BAND_60GHZ:
  1104. chan_increment = 1;
  1105. break;
  1106. case IEEE80211_BAND_5GHZ:
  1107. chan_increment = 4;
  1108. break;
  1109. }
  1110. /* find channel */
  1111. while (country_ie_len >= 3) {
  1112. u8 first_channel = triplet->chans.first_channel;
  1113. if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
  1114. goto next;
  1115. for (i = 0; i < triplet->chans.num_channels; i++) {
  1116. if (first_channel + i * chan_increment == chan) {
  1117. have_chan_pwr = true;
  1118. *chan_pwr = triplet->chans.max_power;
  1119. break;
  1120. }
  1121. }
  1122. if (have_chan_pwr)
  1123. break;
  1124. next:
  1125. triplet++;
  1126. country_ie_len -= 3;
  1127. }
  1128. if (have_chan_pwr && pwr_constr_elem)
  1129. *pwr_reduction = *pwr_constr_elem;
  1130. else
  1131. *pwr_reduction = 0;
  1132. return have_chan_pwr;
  1133. }
  1134. static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
  1135. struct ieee80211_channel *channel,
  1136. const u8 *cisco_dtpc_ie,
  1137. int *pwr_level)
  1138. {
  1139. /* From practical testing, the first data byte of the DTPC element
  1140. * seems to contain the requested dBm level, and the CLI on Cisco
  1141. * APs clearly state the range is -127 to 127 dBm, which indicates
  1142. * a signed byte, although it seemingly never actually goes negative.
  1143. * The other byte seems to always be zero.
  1144. */
  1145. *pwr_level = (__s8)cisco_dtpc_ie[4];
  1146. }
  1147. static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  1148. struct ieee80211_channel *channel,
  1149. struct ieee80211_mgmt *mgmt,
  1150. const u8 *country_ie, u8 country_ie_len,
  1151. const u8 *pwr_constr_ie,
  1152. const u8 *cisco_dtpc_ie)
  1153. {
  1154. bool has_80211h_pwr = false, has_cisco_pwr = false;
  1155. int chan_pwr = 0, pwr_reduction_80211h = 0;
  1156. int pwr_level_cisco, pwr_level_80211h;
  1157. int new_ap_level;
  1158. __le16 capab = mgmt->u.probe_resp.capab_info;
  1159. if (country_ie &&
  1160. (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
  1161. capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
  1162. has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
  1163. sdata, channel, country_ie, country_ie_len,
  1164. pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
  1165. pwr_level_80211h =
  1166. max_t(int, 0, chan_pwr - pwr_reduction_80211h);
  1167. }
  1168. if (cisco_dtpc_ie) {
  1169. ieee80211_find_cisco_dtpc(
  1170. sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
  1171. has_cisco_pwr = true;
  1172. }
  1173. if (!has_80211h_pwr && !has_cisco_pwr)
  1174. return 0;
  1175. /* If we have both 802.11h and Cisco DTPC, apply both limits
  1176. * by picking the smallest of the two power levels advertised.
  1177. */
  1178. if (has_80211h_pwr &&
  1179. (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
  1180. sdata_dbg(sdata,
  1181. "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
  1182. pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
  1183. sdata->u.mgd.bssid);
  1184. new_ap_level = pwr_level_80211h;
  1185. } else { /* has_cisco_pwr is always true here. */
  1186. sdata_dbg(sdata,
  1187. "Limiting TX power to %d dBm as advertised by %pM\n",
  1188. pwr_level_cisco, sdata->u.mgd.bssid);
  1189. new_ap_level = pwr_level_cisco;
  1190. }
  1191. if (sdata->ap_power_level == new_ap_level)
  1192. return 0;
  1193. sdata->ap_power_level = new_ap_level;
  1194. if (__ieee80211_recalc_txpower(sdata))
  1195. return BSS_CHANGED_TXPOWER;
  1196. return 0;
  1197. }
  1198. /* powersave */
  1199. static void ieee80211_enable_ps(struct ieee80211_local *local,
  1200. struct ieee80211_sub_if_data *sdata)
  1201. {
  1202. struct ieee80211_conf *conf = &local->hw.conf;
  1203. /*
  1204. * If we are scanning right now then the parameters will
  1205. * take effect when scan finishes.
  1206. */
  1207. if (local->scanning)
  1208. return;
  1209. if (conf->dynamic_ps_timeout > 0 &&
  1210. !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
  1211. mod_timer(&local->dynamic_ps_timer, jiffies +
  1212. msecs_to_jiffies(conf->dynamic_ps_timeout));
  1213. } else {
  1214. if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
  1215. ieee80211_send_nullfunc(local, sdata, 1);
  1216. if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
  1217. ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
  1218. return;
  1219. conf->flags |= IEEE80211_CONF_PS;
  1220. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1221. }
  1222. }
  1223. static void ieee80211_change_ps(struct ieee80211_local *local)
  1224. {
  1225. struct ieee80211_conf *conf = &local->hw.conf;
  1226. if (local->ps_sdata) {
  1227. ieee80211_enable_ps(local, local->ps_sdata);
  1228. } else if (conf->flags & IEEE80211_CONF_PS) {
  1229. conf->flags &= ~IEEE80211_CONF_PS;
  1230. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1231. del_timer_sync(&local->dynamic_ps_timer);
  1232. cancel_work_sync(&local->dynamic_ps_enable_work);
  1233. }
  1234. }
  1235. static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
  1236. {
  1237. struct ieee80211_if_managed *mgd = &sdata->u.mgd;
  1238. struct sta_info *sta = NULL;
  1239. bool authorized = false;
  1240. if (!mgd->powersave)
  1241. return false;
  1242. if (mgd->broken_ap)
  1243. return false;
  1244. if (!mgd->associated)
  1245. return false;
  1246. if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
  1247. return false;
  1248. if (!mgd->have_beacon)
  1249. return false;
  1250. rcu_read_lock();
  1251. sta = sta_info_get(sdata, mgd->bssid);
  1252. if (sta)
  1253. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  1254. rcu_read_unlock();
  1255. return authorized;
  1256. }
  1257. /* need to hold RTNL or interface lock */
  1258. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  1259. {
  1260. struct ieee80211_sub_if_data *sdata, *found = NULL;
  1261. int count = 0;
  1262. int timeout;
  1263. if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
  1264. local->ps_sdata = NULL;
  1265. return;
  1266. }
  1267. list_for_each_entry(sdata, &local->interfaces, list) {
  1268. if (!ieee80211_sdata_running(sdata))
  1269. continue;
  1270. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1271. /* If an AP vif is found, then disable PS
  1272. * by setting the count to zero thereby setting
  1273. * ps_sdata to NULL.
  1274. */
  1275. count = 0;
  1276. break;
  1277. }
  1278. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1279. continue;
  1280. found = sdata;
  1281. count++;
  1282. }
  1283. if (count == 1 && ieee80211_powersave_allowed(found)) {
  1284. s32 beaconint_us;
  1285. if (latency < 0)
  1286. latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
  1287. beaconint_us = ieee80211_tu_to_usec(
  1288. found->vif.bss_conf.beacon_int);
  1289. timeout = local->dynamic_ps_forced_timeout;
  1290. if (timeout < 0) {
  1291. /*
  1292. * Go to full PSM if the user configures a very low
  1293. * latency requirement.
  1294. * The 2000 second value is there for compatibility
  1295. * until the PM_QOS_NETWORK_LATENCY is configured
  1296. * with real values.
  1297. */
  1298. if (latency > (1900 * USEC_PER_MSEC) &&
  1299. latency != (2000 * USEC_PER_SEC))
  1300. timeout = 0;
  1301. else
  1302. timeout = 100;
  1303. }
  1304. local->hw.conf.dynamic_ps_timeout = timeout;
  1305. if (beaconint_us > latency) {
  1306. local->ps_sdata = NULL;
  1307. } else {
  1308. int maxslp = 1;
  1309. u8 dtimper = found->u.mgd.dtim_period;
  1310. /* If the TIM IE is invalid, pretend the value is 1 */
  1311. if (!dtimper)
  1312. dtimper = 1;
  1313. else if (dtimper > 1)
  1314. maxslp = min_t(int, dtimper,
  1315. latency / beaconint_us);
  1316. local->hw.conf.max_sleep_period = maxslp;
  1317. local->hw.conf.ps_dtim_period = dtimper;
  1318. local->ps_sdata = found;
  1319. }
  1320. } else {
  1321. local->ps_sdata = NULL;
  1322. }
  1323. ieee80211_change_ps(local);
  1324. }
  1325. void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
  1326. {
  1327. bool ps_allowed = ieee80211_powersave_allowed(sdata);
  1328. if (sdata->vif.bss_conf.ps != ps_allowed) {
  1329. sdata->vif.bss_conf.ps = ps_allowed;
  1330. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
  1331. }
  1332. }
  1333. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  1334. {
  1335. struct ieee80211_local *local =
  1336. container_of(work, struct ieee80211_local,
  1337. dynamic_ps_disable_work);
  1338. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1339. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1340. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1341. }
  1342. ieee80211_wake_queues_by_reason(&local->hw,
  1343. IEEE80211_MAX_QUEUE_MAP,
  1344. IEEE80211_QUEUE_STOP_REASON_PS,
  1345. false);
  1346. }
  1347. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  1348. {
  1349. struct ieee80211_local *local =
  1350. container_of(work, struct ieee80211_local,
  1351. dynamic_ps_enable_work);
  1352. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  1353. struct ieee80211_if_managed *ifmgd;
  1354. unsigned long flags;
  1355. int q;
  1356. /* can only happen when PS was just disabled anyway */
  1357. if (!sdata)
  1358. return;
  1359. ifmgd = &sdata->u.mgd;
  1360. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  1361. return;
  1362. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1363. /* don't enter PS if TX frames are pending */
  1364. if (drv_tx_frames_pending(local)) {
  1365. mod_timer(&local->dynamic_ps_timer, jiffies +
  1366. msecs_to_jiffies(
  1367. local->hw.conf.dynamic_ps_timeout));
  1368. return;
  1369. }
  1370. /*
  1371. * transmission can be stopped by others which leads to
  1372. * dynamic_ps_timer expiry. Postpone the ps timer if it
  1373. * is not the actual idle state.
  1374. */
  1375. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1376. for (q = 0; q < local->hw.queues; q++) {
  1377. if (local->queue_stop_reasons[q]) {
  1378. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1379. flags);
  1380. mod_timer(&local->dynamic_ps_timer, jiffies +
  1381. msecs_to_jiffies(
  1382. local->hw.conf.dynamic_ps_timeout));
  1383. return;
  1384. }
  1385. }
  1386. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1387. }
  1388. if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
  1389. !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  1390. if (drv_tx_frames_pending(local)) {
  1391. mod_timer(&local->dynamic_ps_timer, jiffies +
  1392. msecs_to_jiffies(
  1393. local->hw.conf.dynamic_ps_timeout));
  1394. } else {
  1395. ieee80211_send_nullfunc(local, sdata, 1);
  1396. /* Flush to get the tx status of nullfunc frame */
  1397. ieee80211_flush_queues(local, sdata, false);
  1398. }
  1399. }
  1400. if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
  1401. ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
  1402. (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  1403. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  1404. local->hw.conf.flags |= IEEE80211_CONF_PS;
  1405. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1406. }
  1407. }
  1408. void ieee80211_dynamic_ps_timer(unsigned long data)
  1409. {
  1410. struct ieee80211_local *local = (void *) data;
  1411. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  1412. }
  1413. void ieee80211_dfs_cac_timer_work(struct work_struct *work)
  1414. {
  1415. struct delayed_work *delayed_work =
  1416. container_of(work, struct delayed_work, work);
  1417. struct ieee80211_sub_if_data *sdata =
  1418. container_of(delayed_work, struct ieee80211_sub_if_data,
  1419. dfs_cac_timer_work);
  1420. struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
  1421. mutex_lock(&sdata->local->mtx);
  1422. if (sdata->wdev.cac_started) {
  1423. ieee80211_vif_release_channel(sdata);
  1424. cfg80211_cac_event(sdata->dev, &chandef,
  1425. NL80211_RADAR_CAC_FINISHED,
  1426. GFP_KERNEL);
  1427. }
  1428. mutex_unlock(&sdata->local->mtx);
  1429. }
  1430. static bool
  1431. __ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
  1432. {
  1433. struct ieee80211_local *local = sdata->local;
  1434. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1435. bool ret = false;
  1436. int ac;
  1437. if (local->hw.queues < IEEE80211_NUM_ACS)
  1438. return false;
  1439. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1440. struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
  1441. int non_acm_ac;
  1442. unsigned long now = jiffies;
  1443. if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
  1444. tx_tspec->admitted_time &&
  1445. time_after(now, tx_tspec->time_slice_start + HZ)) {
  1446. tx_tspec->consumed_tx_time = 0;
  1447. tx_tspec->time_slice_start = now;
  1448. if (tx_tspec->downgraded)
  1449. tx_tspec->action =
  1450. TX_TSPEC_ACTION_STOP_DOWNGRADE;
  1451. }
  1452. switch (tx_tspec->action) {
  1453. case TX_TSPEC_ACTION_STOP_DOWNGRADE:
  1454. /* take the original parameters */
  1455. if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
  1456. sdata_err(sdata,
  1457. "failed to set TX queue parameters for queue %d\n",
  1458. ac);
  1459. tx_tspec->action = TX_TSPEC_ACTION_NONE;
  1460. tx_tspec->downgraded = false;
  1461. ret = true;
  1462. break;
  1463. case TX_TSPEC_ACTION_DOWNGRADE:
  1464. if (time_after(now, tx_tspec->time_slice_start + HZ)) {
  1465. tx_tspec->action = TX_TSPEC_ACTION_NONE;
  1466. ret = true;
  1467. break;
  1468. }
  1469. /* downgrade next lower non-ACM AC */
  1470. for (non_acm_ac = ac + 1;
  1471. non_acm_ac < IEEE80211_NUM_ACS;
  1472. non_acm_ac++)
  1473. if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
  1474. break;
  1475. /* The loop will result in using BK even if it requires
  1476. * admission control, such configuration makes no sense
  1477. * and we have to transmit somehow - the AC selection
  1478. * does the same thing.
  1479. */
  1480. if (drv_conf_tx(local, sdata, ac,
  1481. &sdata->tx_conf[non_acm_ac]))
  1482. sdata_err(sdata,
  1483. "failed to set TX queue parameters for queue %d\n",
  1484. ac);
  1485. tx_tspec->action = TX_TSPEC_ACTION_NONE;
  1486. ret = true;
  1487. schedule_delayed_work(&ifmgd->tx_tspec_wk,
  1488. tx_tspec->time_slice_start + HZ - now + 1);
  1489. break;
  1490. case TX_TSPEC_ACTION_NONE:
  1491. /* nothing now */
  1492. break;
  1493. }
  1494. }
  1495. return ret;
  1496. }
  1497. void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
  1498. {
  1499. if (__ieee80211_sta_handle_tspec_ac_params(sdata))
  1500. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
  1501. }
  1502. static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
  1503. {
  1504. struct ieee80211_sub_if_data *sdata;
  1505. sdata = container_of(work, struct ieee80211_sub_if_data,
  1506. u.mgd.tx_tspec_wk.work);
  1507. ieee80211_sta_handle_tspec_ac_params(sdata);
  1508. }
  1509. /* MLME */
  1510. static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
  1511. struct ieee80211_sub_if_data *sdata,
  1512. const u8 *wmm_param, size_t wmm_param_len)
  1513. {
  1514. struct ieee80211_tx_queue_params params;
  1515. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1516. size_t left;
  1517. int count;
  1518. const u8 *pos;
  1519. u8 uapsd_queues = 0;
  1520. if (!local->ops->conf_tx)
  1521. return false;
  1522. if (local->hw.queues < IEEE80211_NUM_ACS)
  1523. return false;
  1524. if (!wmm_param)
  1525. return false;
  1526. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  1527. return false;
  1528. if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  1529. uapsd_queues = ifmgd->uapsd_queues;
  1530. count = wmm_param[6] & 0x0f;
  1531. if (count == ifmgd->wmm_last_param_set)
  1532. return false;
  1533. ifmgd->wmm_last_param_set = count;
  1534. pos = wmm_param + 8;
  1535. left = wmm_param_len - 8;
  1536. memset(&params, 0, sizeof(params));
  1537. sdata->wmm_acm = 0;
  1538. for (; left >= 4; left -= 4, pos += 4) {
  1539. int aci = (pos[0] >> 5) & 0x03;
  1540. int acm = (pos[0] >> 4) & 0x01;
  1541. bool uapsd = false;
  1542. int queue;
  1543. switch (aci) {
  1544. case 1: /* AC_BK */
  1545. queue = 3;
  1546. if (acm)
  1547. sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  1548. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  1549. uapsd = true;
  1550. break;
  1551. case 2: /* AC_VI */
  1552. queue = 1;
  1553. if (acm)
  1554. sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  1555. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  1556. uapsd = true;
  1557. break;
  1558. case 3: /* AC_VO */
  1559. queue = 0;
  1560. if (acm)
  1561. sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  1562. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  1563. uapsd = true;
  1564. break;
  1565. case 0: /* AC_BE */
  1566. default:
  1567. queue = 2;
  1568. if (acm)
  1569. sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  1570. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  1571. uapsd = true;
  1572. break;
  1573. }
  1574. params.aifs = pos[0] & 0x0f;
  1575. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  1576. params.cw_min = ecw2cw(pos[1] & 0x0f);
  1577. params.txop = get_unaligned_le16(pos + 2);
  1578. params.acm = acm;
  1579. params.uapsd = uapsd;
  1580. mlme_dbg(sdata,
  1581. "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
  1582. queue, aci, acm,
  1583. params.aifs, params.cw_min, params.cw_max,
  1584. params.txop, params.uapsd,
  1585. ifmgd->tx_tspec[queue].downgraded);
  1586. sdata->tx_conf[queue] = params;
  1587. if (!ifmgd->tx_tspec[queue].downgraded &&
  1588. drv_conf_tx(local, sdata, queue, &params))
  1589. sdata_err(sdata,
  1590. "failed to set TX queue parameters for queue %d\n",
  1591. queue);
  1592. }
  1593. /* enable WMM or activate new settings */
  1594. sdata->vif.bss_conf.qos = true;
  1595. return true;
  1596. }
  1597. static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1598. {
  1599. lockdep_assert_held(&sdata->local->mtx);
  1600. sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
  1601. ieee80211_run_deferred_scan(sdata->local);
  1602. }
  1603. static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1604. {
  1605. mutex_lock(&sdata->local->mtx);
  1606. __ieee80211_stop_poll(sdata);
  1607. mutex_unlock(&sdata->local->mtx);
  1608. }
  1609. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  1610. u16 capab, bool erp_valid, u8 erp)
  1611. {
  1612. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1613. u32 changed = 0;
  1614. bool use_protection;
  1615. bool use_short_preamble;
  1616. bool use_short_slot;
  1617. if (erp_valid) {
  1618. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  1619. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  1620. } else {
  1621. use_protection = false;
  1622. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  1623. }
  1624. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1625. if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
  1626. use_short_slot = true;
  1627. if (use_protection != bss_conf->use_cts_prot) {
  1628. bss_conf->use_cts_prot = use_protection;
  1629. changed |= BSS_CHANGED_ERP_CTS_PROT;
  1630. }
  1631. if (use_short_preamble != bss_conf->use_short_preamble) {
  1632. bss_conf->use_short_preamble = use_short_preamble;
  1633. changed |= BSS_CHANGED_ERP_PREAMBLE;
  1634. }
  1635. if (use_short_slot != bss_conf->use_short_slot) {
  1636. bss_conf->use_short_slot = use_short_slot;
  1637. changed |= BSS_CHANGED_ERP_SLOT;
  1638. }
  1639. return changed;
  1640. }
  1641. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  1642. struct cfg80211_bss *cbss,
  1643. u32 bss_info_changed)
  1644. {
  1645. struct ieee80211_bss *bss = (void *)cbss->priv;
  1646. struct ieee80211_local *local = sdata->local;
  1647. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1648. bss_info_changed |= BSS_CHANGED_ASSOC;
  1649. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  1650. bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
  1651. sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
  1652. beacon_loss_count * bss_conf->beacon_int));
  1653. sdata->u.mgd.associated = cbss;
  1654. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  1655. sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
  1656. if (sdata->vif.p2p) {
  1657. const struct cfg80211_bss_ies *ies;
  1658. rcu_read_lock();
  1659. ies = rcu_dereference(cbss->ies);
  1660. if (ies) {
  1661. int ret;
  1662. ret = cfg80211_get_p2p_attr(
  1663. ies->data, ies->len,
  1664. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  1665. (u8 *) &bss_conf->p2p_noa_attr,
  1666. sizeof(bss_conf->p2p_noa_attr));
  1667. if (ret >= 2) {
  1668. sdata->u.mgd.p2p_noa_index =
  1669. bss_conf->p2p_noa_attr.index;
  1670. bss_info_changed |= BSS_CHANGED_P2P_PS;
  1671. }
  1672. }
  1673. rcu_read_unlock();
  1674. }
  1675. /* just to be sure */
  1676. ieee80211_stop_poll(sdata);
  1677. ieee80211_led_assoc(local, 1);
  1678. if (sdata->u.mgd.have_beacon) {
  1679. /*
  1680. * If the AP is buggy we may get here with no DTIM period
  1681. * known, so assume it's 1 which is the only safe assumption
  1682. * in that case, although if the TIM IE is broken powersave
  1683. * probably just won't work at all.
  1684. */
  1685. bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
  1686. bss_conf->beacon_rate = bss->beacon_rate;
  1687. bss_info_changed |= BSS_CHANGED_BEACON_INFO;
  1688. } else {
  1689. bss_conf->beacon_rate = NULL;
  1690. bss_conf->dtim_period = 0;
  1691. }
  1692. bss_conf->assoc = 1;
  1693. /* Tell the driver to monitor connection quality (if supported) */
  1694. if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
  1695. bss_conf->cqm_rssi_thold)
  1696. bss_info_changed |= BSS_CHANGED_CQM;
  1697. /* Enable ARP filtering */
  1698. if (bss_conf->arp_addr_cnt)
  1699. bss_info_changed |= BSS_CHANGED_ARP_FILTER;
  1700. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  1701. mutex_lock(&local->iflist_mtx);
  1702. ieee80211_recalc_ps(local, -1);
  1703. mutex_unlock(&local->iflist_mtx);
  1704. ieee80211_recalc_smps(sdata);
  1705. ieee80211_recalc_ps_vif(sdata);
  1706. netif_carrier_on(sdata->dev);
  1707. }
  1708. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  1709. u16 stype, u16 reason, bool tx,
  1710. u8 *frame_buf)
  1711. {
  1712. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1713. struct ieee80211_local *local = sdata->local;
  1714. u32 changed = 0;
  1715. sdata_assert_lock(sdata);
  1716. if (WARN_ON_ONCE(tx && !frame_buf))
  1717. return;
  1718. if (WARN_ON(!ifmgd->associated))
  1719. return;
  1720. ieee80211_stop_poll(sdata);
  1721. ifmgd->associated = NULL;
  1722. netif_carrier_off(sdata->dev);
  1723. /*
  1724. * if we want to get out of ps before disassoc (why?) we have
  1725. * to do it before sending disassoc, as otherwise the null-packet
  1726. * won't be valid.
  1727. */
  1728. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1729. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1730. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1731. }
  1732. local->ps_sdata = NULL;
  1733. /* disable per-vif ps */
  1734. ieee80211_recalc_ps_vif(sdata);
  1735. /* make sure ongoing transmission finishes */
  1736. synchronize_net();
  1737. /*
  1738. * drop any frame before deauth/disassoc, this can be data or
  1739. * management frame. Since we are disconnecting, we should not
  1740. * insist sending these frames which can take time and delay
  1741. * the disconnection and possible the roaming.
  1742. */
  1743. if (tx)
  1744. ieee80211_flush_queues(local, sdata, true);
  1745. /* deauthenticate/disassociate now */
  1746. if (tx || frame_buf)
  1747. ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
  1748. reason, tx, frame_buf);
  1749. /* flush out frame - make sure the deauth was actually sent */
  1750. if (tx)
  1751. ieee80211_flush_queues(local, sdata, false);
  1752. /* clear bssid only after building the needed mgmt frames */
  1753. eth_zero_addr(ifmgd->bssid);
  1754. /* remove AP and TDLS peers */
  1755. sta_info_flush(sdata);
  1756. /* finally reset all BSS / config parameters */
  1757. changed |= ieee80211_reset_erp_info(sdata);
  1758. ieee80211_led_assoc(local, 0);
  1759. changed |= BSS_CHANGED_ASSOC;
  1760. sdata->vif.bss_conf.assoc = false;
  1761. ifmgd->p2p_noa_index = -1;
  1762. memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
  1763. sizeof(sdata->vif.bss_conf.p2p_noa_attr));
  1764. /* on the next assoc, re-program HT/VHT parameters */
  1765. memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
  1766. memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
  1767. memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
  1768. memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
  1769. sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1770. del_timer_sync(&local->dynamic_ps_timer);
  1771. cancel_work_sync(&local->dynamic_ps_enable_work);
  1772. /* Disable ARP filtering */
  1773. if (sdata->vif.bss_conf.arp_addr_cnt)
  1774. changed |= BSS_CHANGED_ARP_FILTER;
  1775. sdata->vif.bss_conf.qos = false;
  1776. changed |= BSS_CHANGED_QOS;
  1777. /* The BSSID (not really interesting) and HT changed */
  1778. changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
  1779. ieee80211_bss_info_change_notify(sdata, changed);
  1780. /* disassociated - set to defaults now */
  1781. ieee80211_set_wmm_default(sdata, false);
  1782. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  1783. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  1784. del_timer_sync(&sdata->u.mgd.timer);
  1785. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  1786. sdata->vif.bss_conf.dtim_period = 0;
  1787. sdata->vif.bss_conf.beacon_rate = NULL;
  1788. ifmgd->have_beacon = false;
  1789. ifmgd->flags = 0;
  1790. mutex_lock(&local->mtx);
  1791. ieee80211_vif_release_channel(sdata);
  1792. sdata->vif.csa_active = false;
  1793. ifmgd->csa_waiting_bcn = false;
  1794. ifmgd->csa_ignored_same_chan = false;
  1795. if (sdata->csa_block_tx) {
  1796. ieee80211_wake_vif_queues(local, sdata,
  1797. IEEE80211_QUEUE_STOP_REASON_CSA);
  1798. sdata->csa_block_tx = false;
  1799. }
  1800. mutex_unlock(&local->mtx);
  1801. /* existing TX TSPEC sessions no longer exist */
  1802. memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
  1803. cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
  1804. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1805. }
  1806. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  1807. struct ieee80211_hdr *hdr)
  1808. {
  1809. /*
  1810. * We can postpone the mgd.timer whenever receiving unicast frames
  1811. * from AP because we know that the connection is working both ways
  1812. * at that time. But multicast frames (and hence also beacons) must
  1813. * be ignored here, because we need to trigger the timer during
  1814. * data idle periods for sending the periodic probe request to the
  1815. * AP we're connected to.
  1816. */
  1817. if (is_multicast_ether_addr(hdr->addr1))
  1818. return;
  1819. ieee80211_sta_reset_conn_monitor(sdata);
  1820. }
  1821. static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
  1822. {
  1823. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1824. struct ieee80211_local *local = sdata->local;
  1825. mutex_lock(&local->mtx);
  1826. if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
  1827. goto out;
  1828. __ieee80211_stop_poll(sdata);
  1829. mutex_lock(&local->iflist_mtx);
  1830. ieee80211_recalc_ps(local, -1);
  1831. mutex_unlock(&local->iflist_mtx);
  1832. if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
  1833. goto out;
  1834. /*
  1835. * We've received a probe response, but are not sure whether
  1836. * we have or will be receiving any beacons or data, so let's
  1837. * schedule the timers again, just in case.
  1838. */
  1839. ieee80211_sta_reset_beacon_monitor(sdata);
  1840. mod_timer(&ifmgd->conn_mon_timer,
  1841. round_jiffies_up(jiffies +
  1842. IEEE80211_CONNECTION_IDLE_TIME));
  1843. out:
  1844. mutex_unlock(&local->mtx);
  1845. }
  1846. static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
  1847. struct ieee80211_hdr *hdr,
  1848. u16 tx_time)
  1849. {
  1850. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1851. u16 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  1852. int ac = ieee80211_ac_from_tid(tid);
  1853. struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
  1854. unsigned long now = jiffies;
  1855. if (likely(!tx_tspec->admitted_time))
  1856. return;
  1857. if (time_after(now, tx_tspec->time_slice_start + HZ)) {
  1858. tx_tspec->consumed_tx_time = 0;
  1859. tx_tspec->time_slice_start = now;
  1860. if (tx_tspec->downgraded) {
  1861. tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
  1862. schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
  1863. }
  1864. }
  1865. if (tx_tspec->downgraded)
  1866. return;
  1867. tx_tspec->consumed_tx_time += tx_time;
  1868. if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
  1869. tx_tspec->downgraded = true;
  1870. tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
  1871. schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
  1872. }
  1873. }
  1874. void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
  1875. struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
  1876. {
  1877. ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
  1878. if (!ieee80211_is_data(hdr->frame_control))
  1879. return;
  1880. if (ieee80211_is_nullfunc(hdr->frame_control) &&
  1881. sdata->u.mgd.probe_send_count > 0) {
  1882. if (ack)
  1883. ieee80211_sta_reset_conn_monitor(sdata);
  1884. else
  1885. sdata->u.mgd.nullfunc_failed = true;
  1886. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1887. return;
  1888. }
  1889. if (ack)
  1890. ieee80211_sta_reset_conn_monitor(sdata);
  1891. }
  1892. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  1893. {
  1894. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1895. const u8 *ssid;
  1896. u8 *dst = ifmgd->associated->bssid;
  1897. u8 unicast_limit = max(1, max_probe_tries - 3);
  1898. /*
  1899. * Try sending broadcast probe requests for the last three
  1900. * probe requests after the first ones failed since some
  1901. * buggy APs only support broadcast probe requests.
  1902. */
  1903. if (ifmgd->probe_send_count >= unicast_limit)
  1904. dst = NULL;
  1905. /*
  1906. * When the hardware reports an accurate Tx ACK status, it's
  1907. * better to send a nullfunc frame instead of a probe request,
  1908. * as it will kick us off the AP quickly if we aren't associated
  1909. * anymore. The timeout will be reset if the frame is ACKed by
  1910. * the AP.
  1911. */
  1912. ifmgd->probe_send_count++;
  1913. if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
  1914. ifmgd->nullfunc_failed = false;
  1915. ieee80211_send_nullfunc(sdata->local, sdata, 0);
  1916. } else {
  1917. int ssid_len;
  1918. rcu_read_lock();
  1919. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  1920. if (WARN_ON_ONCE(ssid == NULL))
  1921. ssid_len = 0;
  1922. else
  1923. ssid_len = ssid[1];
  1924. ieee80211_send_probe_req(sdata, sdata->vif.addr, dst,
  1925. ssid + 2, ssid_len, NULL,
  1926. 0, (u32) -1, true, 0,
  1927. ifmgd->associated->channel, false);
  1928. rcu_read_unlock();
  1929. }
  1930. ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
  1931. run_again(sdata, ifmgd->probe_timeout);
  1932. }
  1933. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  1934. bool beacon)
  1935. {
  1936. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1937. bool already = false;
  1938. if (!ieee80211_sdata_running(sdata))
  1939. return;
  1940. sdata_lock(sdata);
  1941. if (!ifmgd->associated)
  1942. goto out;
  1943. mutex_lock(&sdata->local->mtx);
  1944. if (sdata->local->tmp_channel || sdata->local->scanning) {
  1945. mutex_unlock(&sdata->local->mtx);
  1946. goto out;
  1947. }
  1948. if (beacon) {
  1949. mlme_dbg_ratelimited(sdata,
  1950. "detected beacon loss from AP (missed %d beacons) - probing\n",
  1951. beacon_loss_count);
  1952. ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
  1953. }
  1954. /*
  1955. * The driver/our work has already reported this event or the
  1956. * connection monitoring has kicked in and we have already sent
  1957. * a probe request. Or maybe the AP died and the driver keeps
  1958. * reporting until we disassociate...
  1959. *
  1960. * In either case we have to ignore the current call to this
  1961. * function (except for setting the correct probe reason bit)
  1962. * because otherwise we would reset the timer every time and
  1963. * never check whether we received a probe response!
  1964. */
  1965. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
  1966. already = true;
  1967. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  1968. mutex_unlock(&sdata->local->mtx);
  1969. if (already)
  1970. goto out;
  1971. mutex_lock(&sdata->local->iflist_mtx);
  1972. ieee80211_recalc_ps(sdata->local, -1);
  1973. mutex_unlock(&sdata->local->iflist_mtx);
  1974. ifmgd->probe_send_count = 0;
  1975. ieee80211_mgd_probe_ap_send(sdata);
  1976. out:
  1977. sdata_unlock(sdata);
  1978. }
  1979. struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
  1980. struct ieee80211_vif *vif)
  1981. {
  1982. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1983. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1984. struct cfg80211_bss *cbss;
  1985. struct sk_buff *skb;
  1986. const u8 *ssid;
  1987. int ssid_len;
  1988. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1989. return NULL;
  1990. sdata_assert_lock(sdata);
  1991. if (ifmgd->associated)
  1992. cbss = ifmgd->associated;
  1993. else if (ifmgd->auth_data)
  1994. cbss = ifmgd->auth_data->bss;
  1995. else if (ifmgd->assoc_data)
  1996. cbss = ifmgd->assoc_data->bss;
  1997. else
  1998. return NULL;
  1999. rcu_read_lock();
  2000. ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
  2001. if (WARN_ON_ONCE(ssid == NULL))
  2002. ssid_len = 0;
  2003. else
  2004. ssid_len = ssid[1];
  2005. skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
  2006. (u32) -1, cbss->channel,
  2007. ssid + 2, ssid_len,
  2008. NULL, 0, true);
  2009. rcu_read_unlock();
  2010. return skb;
  2011. }
  2012. EXPORT_SYMBOL(ieee80211_ap_probereq_get);
  2013. static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
  2014. const u8 *buf, size_t len, bool tx,
  2015. u16 reason)
  2016. {
  2017. struct ieee80211_event event = {
  2018. .type = MLME_EVENT,
  2019. .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
  2020. .u.mlme.reason = reason,
  2021. };
  2022. if (tx)
  2023. cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
  2024. else
  2025. cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
  2026. drv_event_callback(sdata->local, sdata, &event);
  2027. }
  2028. static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
  2029. {
  2030. struct ieee80211_local *local = sdata->local;
  2031. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2032. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2033. sdata_lock(sdata);
  2034. if (!ifmgd->associated) {
  2035. sdata_unlock(sdata);
  2036. return;
  2037. }
  2038. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  2039. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  2040. true, frame_buf);
  2041. mutex_lock(&local->mtx);
  2042. sdata->vif.csa_active = false;
  2043. ifmgd->csa_waiting_bcn = false;
  2044. if (sdata->csa_block_tx) {
  2045. ieee80211_wake_vif_queues(local, sdata,
  2046. IEEE80211_QUEUE_STOP_REASON_CSA);
  2047. sdata->csa_block_tx = false;
  2048. }
  2049. mutex_unlock(&local->mtx);
  2050. ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
  2051. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2052. sdata_unlock(sdata);
  2053. }
  2054. static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  2055. {
  2056. struct ieee80211_sub_if_data *sdata =
  2057. container_of(work, struct ieee80211_sub_if_data,
  2058. u.mgd.beacon_connection_loss_work);
  2059. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2060. struct sta_info *sta;
  2061. if (ifmgd->associated) {
  2062. rcu_read_lock();
  2063. sta = sta_info_get(sdata, ifmgd->bssid);
  2064. if (sta)
  2065. sta->beacon_loss_count++;
  2066. rcu_read_unlock();
  2067. }
  2068. if (ifmgd->connection_loss) {
  2069. sdata_info(sdata, "Connection to AP %pM lost\n",
  2070. ifmgd->bssid);
  2071. __ieee80211_disconnect(sdata);
  2072. } else {
  2073. ieee80211_mgd_probe_ap(sdata, true);
  2074. }
  2075. }
  2076. static void ieee80211_csa_connection_drop_work(struct work_struct *work)
  2077. {
  2078. struct ieee80211_sub_if_data *sdata =
  2079. container_of(work, struct ieee80211_sub_if_data,
  2080. u.mgd.csa_connection_drop_work);
  2081. __ieee80211_disconnect(sdata);
  2082. }
  2083. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  2084. {
  2085. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2086. struct ieee80211_hw *hw = &sdata->local->hw;
  2087. trace_api_beacon_loss(sdata);
  2088. sdata->u.mgd.connection_loss = false;
  2089. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  2090. }
  2091. EXPORT_SYMBOL(ieee80211_beacon_loss);
  2092. void ieee80211_connection_loss(struct ieee80211_vif *vif)
  2093. {
  2094. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2095. struct ieee80211_hw *hw = &sdata->local->hw;
  2096. trace_api_connection_loss(sdata);
  2097. sdata->u.mgd.connection_loss = true;
  2098. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  2099. }
  2100. EXPORT_SYMBOL(ieee80211_connection_loss);
  2101. static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
  2102. bool assoc)
  2103. {
  2104. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  2105. sdata_assert_lock(sdata);
  2106. if (!assoc) {
  2107. /*
  2108. * we are not authenticated yet, the only timer that could be
  2109. * running is the timeout for the authentication response which
  2110. * which is not relevant anymore.
  2111. */
  2112. del_timer_sync(&sdata->u.mgd.timer);
  2113. sta_info_destroy_addr(sdata, auth_data->bss->bssid);
  2114. eth_zero_addr(sdata->u.mgd.bssid);
  2115. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2116. sdata->u.mgd.flags = 0;
  2117. mutex_lock(&sdata->local->mtx);
  2118. ieee80211_vif_release_channel(sdata);
  2119. mutex_unlock(&sdata->local->mtx);
  2120. }
  2121. cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
  2122. kfree(auth_data);
  2123. sdata->u.mgd.auth_data = NULL;
  2124. }
  2125. static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
  2126. bool assoc)
  2127. {
  2128. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  2129. sdata_assert_lock(sdata);
  2130. if (!assoc) {
  2131. /*
  2132. * we are not associated yet, the only timer that could be
  2133. * running is the timeout for the association response which
  2134. * which is not relevant anymore.
  2135. */
  2136. del_timer_sync(&sdata->u.mgd.timer);
  2137. sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
  2138. eth_zero_addr(sdata->u.mgd.bssid);
  2139. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2140. sdata->u.mgd.flags = 0;
  2141. mutex_lock(&sdata->local->mtx);
  2142. ieee80211_vif_release_channel(sdata);
  2143. mutex_unlock(&sdata->local->mtx);
  2144. }
  2145. kfree(assoc_data);
  2146. sdata->u.mgd.assoc_data = NULL;
  2147. }
  2148. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  2149. struct ieee80211_mgmt *mgmt, size_t len)
  2150. {
  2151. struct ieee80211_local *local = sdata->local;
  2152. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  2153. u8 *pos;
  2154. struct ieee802_11_elems elems;
  2155. u32 tx_flags = 0;
  2156. pos = mgmt->u.auth.variable;
  2157. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  2158. if (!elems.challenge)
  2159. return;
  2160. auth_data->expected_transaction = 4;
  2161. drv_mgd_prepare_tx(sdata->local, sdata);
  2162. if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
  2163. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  2164. IEEE80211_TX_INTFL_MLME_CONN_TX;
  2165. ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
  2166. elems.challenge - 2, elems.challenge_len + 2,
  2167. auth_data->bss->bssid, auth_data->bss->bssid,
  2168. auth_data->key, auth_data->key_len,
  2169. auth_data->key_idx, tx_flags);
  2170. }
  2171. static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  2172. struct ieee80211_mgmt *mgmt, size_t len)
  2173. {
  2174. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2175. u8 bssid[ETH_ALEN];
  2176. u16 auth_alg, auth_transaction, status_code;
  2177. struct sta_info *sta;
  2178. struct ieee80211_event event = {
  2179. .type = MLME_EVENT,
  2180. .u.mlme.data = AUTH_EVENT,
  2181. };
  2182. sdata_assert_lock(sdata);
  2183. if (len < 24 + 6)
  2184. return;
  2185. if (!ifmgd->auth_data || ifmgd->auth_data->done)
  2186. return;
  2187. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  2188. if (!ether_addr_equal(bssid, mgmt->bssid))
  2189. return;
  2190. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  2191. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  2192. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  2193. if (auth_alg != ifmgd->auth_data->algorithm ||
  2194. auth_transaction != ifmgd->auth_data->expected_transaction) {
  2195. sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
  2196. mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
  2197. auth_transaction,
  2198. ifmgd->auth_data->expected_transaction);
  2199. return;
  2200. }
  2201. if (status_code != WLAN_STATUS_SUCCESS) {
  2202. sdata_info(sdata, "%pM denied authentication (status %d)\n",
  2203. mgmt->sa, status_code);
  2204. ieee80211_destroy_auth_data(sdata, false);
  2205. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2206. event.u.mlme.status = MLME_DENIED;
  2207. event.u.mlme.reason = status_code;
  2208. drv_event_callback(sdata->local, sdata, &event);
  2209. return;
  2210. }
  2211. switch (ifmgd->auth_data->algorithm) {
  2212. case WLAN_AUTH_OPEN:
  2213. case WLAN_AUTH_LEAP:
  2214. case WLAN_AUTH_FT:
  2215. case WLAN_AUTH_SAE:
  2216. break;
  2217. case WLAN_AUTH_SHARED_KEY:
  2218. if (ifmgd->auth_data->expected_transaction != 4) {
  2219. ieee80211_auth_challenge(sdata, mgmt, len);
  2220. /* need another frame */
  2221. return;
  2222. }
  2223. break;
  2224. default:
  2225. WARN_ONCE(1, "invalid auth alg %d",
  2226. ifmgd->auth_data->algorithm);
  2227. return;
  2228. }
  2229. event.u.mlme.status = MLME_SUCCESS;
  2230. drv_event_callback(sdata->local, sdata, &event);
  2231. sdata_info(sdata, "authenticated\n");
  2232. ifmgd->auth_data->done = true;
  2233. ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
  2234. ifmgd->auth_data->timeout_started = true;
  2235. run_again(sdata, ifmgd->auth_data->timeout);
  2236. if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
  2237. ifmgd->auth_data->expected_transaction != 2) {
  2238. /*
  2239. * Report auth frame to user space for processing since another
  2240. * round of Authentication frames is still needed.
  2241. */
  2242. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2243. return;
  2244. }
  2245. /* move station state to auth */
  2246. mutex_lock(&sdata->local->sta_mtx);
  2247. sta = sta_info_get(sdata, bssid);
  2248. if (!sta) {
  2249. WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
  2250. goto out_err;
  2251. }
  2252. if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
  2253. sdata_info(sdata, "failed moving %pM to auth\n", bssid);
  2254. goto out_err;
  2255. }
  2256. mutex_unlock(&sdata->local->sta_mtx);
  2257. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2258. return;
  2259. out_err:
  2260. mutex_unlock(&sdata->local->sta_mtx);
  2261. /* ignore frame -- wait for timeout */
  2262. }
  2263. #define case_WLAN(type) \
  2264. case WLAN_REASON_##type: return #type
  2265. static const char *ieee80211_get_reason_code_string(u16 reason_code)
  2266. {
  2267. switch (reason_code) {
  2268. case_WLAN(UNSPECIFIED);
  2269. case_WLAN(PREV_AUTH_NOT_VALID);
  2270. case_WLAN(DEAUTH_LEAVING);
  2271. case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
  2272. case_WLAN(DISASSOC_AP_BUSY);
  2273. case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
  2274. case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
  2275. case_WLAN(DISASSOC_STA_HAS_LEFT);
  2276. case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
  2277. case_WLAN(DISASSOC_BAD_POWER);
  2278. case_WLAN(DISASSOC_BAD_SUPP_CHAN);
  2279. case_WLAN(INVALID_IE);
  2280. case_WLAN(MIC_FAILURE);
  2281. case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
  2282. case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
  2283. case_WLAN(IE_DIFFERENT);
  2284. case_WLAN(INVALID_GROUP_CIPHER);
  2285. case_WLAN(INVALID_PAIRWISE_CIPHER);
  2286. case_WLAN(INVALID_AKMP);
  2287. case_WLAN(UNSUPP_RSN_VERSION);
  2288. case_WLAN(INVALID_RSN_IE_CAP);
  2289. case_WLAN(IEEE8021X_FAILED);
  2290. case_WLAN(CIPHER_SUITE_REJECTED);
  2291. case_WLAN(DISASSOC_UNSPECIFIED_QOS);
  2292. case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
  2293. case_WLAN(DISASSOC_LOW_ACK);
  2294. case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
  2295. case_WLAN(QSTA_LEAVE_QBSS);
  2296. case_WLAN(QSTA_NOT_USE);
  2297. case_WLAN(QSTA_REQUIRE_SETUP);
  2298. case_WLAN(QSTA_TIMEOUT);
  2299. case_WLAN(QSTA_CIPHER_NOT_SUPP);
  2300. case_WLAN(MESH_PEER_CANCELED);
  2301. case_WLAN(MESH_MAX_PEERS);
  2302. case_WLAN(MESH_CONFIG);
  2303. case_WLAN(MESH_CLOSE);
  2304. case_WLAN(MESH_MAX_RETRIES);
  2305. case_WLAN(MESH_CONFIRM_TIMEOUT);
  2306. case_WLAN(MESH_INVALID_GTK);
  2307. case_WLAN(MESH_INCONSISTENT_PARAM);
  2308. case_WLAN(MESH_INVALID_SECURITY);
  2309. case_WLAN(MESH_PATH_ERROR);
  2310. case_WLAN(MESH_PATH_NOFORWARD);
  2311. case_WLAN(MESH_PATH_DEST_UNREACHABLE);
  2312. case_WLAN(MAC_EXISTS_IN_MBSS);
  2313. case_WLAN(MESH_CHAN_REGULATORY);
  2314. case_WLAN(MESH_CHAN);
  2315. default: return "<unknown>";
  2316. }
  2317. }
  2318. static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  2319. struct ieee80211_mgmt *mgmt, size_t len)
  2320. {
  2321. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2322. u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  2323. sdata_assert_lock(sdata);
  2324. if (len < 24 + 2)
  2325. return;
  2326. if (ifmgd->associated &&
  2327. ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
  2328. const u8 *bssid = ifmgd->associated->bssid;
  2329. sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
  2330. bssid, reason_code,
  2331. ieee80211_get_reason_code_string(reason_code));
  2332. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  2333. ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
  2334. reason_code);
  2335. return;
  2336. }
  2337. if (ifmgd->assoc_data &&
  2338. ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
  2339. const u8 *bssid = ifmgd->assoc_data->bss->bssid;
  2340. sdata_info(sdata,
  2341. "deauthenticated from %pM while associating (Reason: %u=%s)\n",
  2342. bssid, reason_code,
  2343. ieee80211_get_reason_code_string(reason_code));
  2344. ieee80211_destroy_assoc_data(sdata, false);
  2345. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2346. return;
  2347. }
  2348. }
  2349. static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  2350. struct ieee80211_mgmt *mgmt, size_t len)
  2351. {
  2352. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2353. u16 reason_code;
  2354. sdata_assert_lock(sdata);
  2355. if (len < 24 + 2)
  2356. return;
  2357. if (!ifmgd->associated ||
  2358. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2359. return;
  2360. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  2361. sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
  2362. mgmt->sa, reason_code);
  2363. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  2364. ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
  2365. }
  2366. static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
  2367. u8 *supp_rates, unsigned int supp_rates_len,
  2368. u32 *rates, u32 *basic_rates,
  2369. bool *have_higher_than_11mbit,
  2370. int *min_rate, int *min_rate_index,
  2371. int shift, u32 rate_flags)
  2372. {
  2373. int i, j;
  2374. for (i = 0; i < supp_rates_len; i++) {
  2375. int rate = supp_rates[i] & 0x7f;
  2376. bool is_basic = !!(supp_rates[i] & 0x80);
  2377. if ((rate * 5 * (1 << shift)) > 110)
  2378. *have_higher_than_11mbit = true;
  2379. /*
  2380. * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
  2381. * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
  2382. *
  2383. * Note: Even through the membership selector and the basic
  2384. * rate flag share the same bit, they are not exactly
  2385. * the same.
  2386. */
  2387. if (!!(supp_rates[i] & 0x80) &&
  2388. (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  2389. continue;
  2390. for (j = 0; j < sband->n_bitrates; j++) {
  2391. struct ieee80211_rate *br;
  2392. int brate;
  2393. br = &sband->bitrates[j];
  2394. if ((rate_flags & br->flags) != rate_flags)
  2395. continue;
  2396. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  2397. if (brate == rate) {
  2398. *rates |= BIT(j);
  2399. if (is_basic)
  2400. *basic_rates |= BIT(j);
  2401. if ((rate * 5) < *min_rate) {
  2402. *min_rate = rate * 5;
  2403. *min_rate_index = j;
  2404. }
  2405. break;
  2406. }
  2407. }
  2408. }
  2409. }
  2410. static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
  2411. struct cfg80211_bss *cbss,
  2412. struct ieee80211_mgmt *mgmt, size_t len)
  2413. {
  2414. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2415. struct ieee80211_local *local = sdata->local;
  2416. struct ieee80211_supported_band *sband;
  2417. struct sta_info *sta;
  2418. u8 *pos;
  2419. u16 capab_info, aid;
  2420. struct ieee802_11_elems elems;
  2421. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  2422. const struct cfg80211_bss_ies *bss_ies = NULL;
  2423. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  2424. u32 changed = 0;
  2425. int err;
  2426. bool ret;
  2427. /* AssocResp and ReassocResp have identical structure */
  2428. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  2429. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  2430. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  2431. sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
  2432. aid);
  2433. aid &= ~(BIT(15) | BIT(14));
  2434. ifmgd->broken_ap = false;
  2435. if (aid == 0 || aid > IEEE80211_MAX_AID) {
  2436. sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
  2437. aid);
  2438. aid = 0;
  2439. ifmgd->broken_ap = true;
  2440. }
  2441. pos = mgmt->u.assoc_resp.variable;
  2442. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  2443. if (!elems.supp_rates) {
  2444. sdata_info(sdata, "no SuppRates element in AssocResp\n");
  2445. return false;
  2446. }
  2447. ifmgd->aid = aid;
  2448. ifmgd->tdls_chan_switch_prohibited =
  2449. elems.ext_capab && elems.ext_capab_len >= 5 &&
  2450. (elems.ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
  2451. /*
  2452. * Some APs are erroneously not including some information in their
  2453. * (re)association response frames. Try to recover by using the data
  2454. * from the beacon or probe response. This seems to afflict mobile
  2455. * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
  2456. * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
  2457. */
  2458. if ((assoc_data->wmm && !elems.wmm_param) ||
  2459. (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  2460. (!elems.ht_cap_elem || !elems.ht_operation)) ||
  2461. (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  2462. (!elems.vht_cap_elem || !elems.vht_operation))) {
  2463. const struct cfg80211_bss_ies *ies;
  2464. struct ieee802_11_elems bss_elems;
  2465. rcu_read_lock();
  2466. ies = rcu_dereference(cbss->ies);
  2467. if (ies)
  2468. bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
  2469. GFP_ATOMIC);
  2470. rcu_read_unlock();
  2471. if (!bss_ies)
  2472. return false;
  2473. ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
  2474. false, &bss_elems);
  2475. if (assoc_data->wmm &&
  2476. !elems.wmm_param && bss_elems.wmm_param) {
  2477. elems.wmm_param = bss_elems.wmm_param;
  2478. sdata_info(sdata,
  2479. "AP bug: WMM param missing from AssocResp\n");
  2480. }
  2481. /*
  2482. * Also check if we requested HT/VHT, otherwise the AP doesn't
  2483. * have to include the IEs in the (re)association response.
  2484. */
  2485. if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
  2486. !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
  2487. elems.ht_cap_elem = bss_elems.ht_cap_elem;
  2488. sdata_info(sdata,
  2489. "AP bug: HT capability missing from AssocResp\n");
  2490. }
  2491. if (!elems.ht_operation && bss_elems.ht_operation &&
  2492. !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
  2493. elems.ht_operation = bss_elems.ht_operation;
  2494. sdata_info(sdata,
  2495. "AP bug: HT operation missing from AssocResp\n");
  2496. }
  2497. if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
  2498. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
  2499. elems.vht_cap_elem = bss_elems.vht_cap_elem;
  2500. sdata_info(sdata,
  2501. "AP bug: VHT capa missing from AssocResp\n");
  2502. }
  2503. if (!elems.vht_operation && bss_elems.vht_operation &&
  2504. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
  2505. elems.vht_operation = bss_elems.vht_operation;
  2506. sdata_info(sdata,
  2507. "AP bug: VHT operation missing from AssocResp\n");
  2508. }
  2509. }
  2510. /*
  2511. * We previously checked these in the beacon/probe response, so
  2512. * they should be present here. This is just a safety net.
  2513. */
  2514. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  2515. (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
  2516. sdata_info(sdata,
  2517. "HT AP is missing WMM params or HT capability/operation\n");
  2518. ret = false;
  2519. goto out;
  2520. }
  2521. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  2522. (!elems.vht_cap_elem || !elems.vht_operation)) {
  2523. sdata_info(sdata,
  2524. "VHT AP is missing VHT capability/operation\n");
  2525. ret = false;
  2526. goto out;
  2527. }
  2528. mutex_lock(&sdata->local->sta_mtx);
  2529. /*
  2530. * station info was already allocated and inserted before
  2531. * the association and should be available to us
  2532. */
  2533. sta = sta_info_get(sdata, cbss->bssid);
  2534. if (WARN_ON(!sta)) {
  2535. mutex_unlock(&sdata->local->sta_mtx);
  2536. ret = false;
  2537. goto out;
  2538. }
  2539. sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
  2540. /* Set up internal HT/VHT capabilities */
  2541. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  2542. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  2543. elems.ht_cap_elem, sta);
  2544. if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  2545. ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
  2546. elems.vht_cap_elem, sta);
  2547. /*
  2548. * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
  2549. * in their association response, so ignore that data for our own
  2550. * configuration. If it changed since the last beacon, we'll get the
  2551. * next beacon and update then.
  2552. */
  2553. /*
  2554. * If an operating mode notification IE is present, override the
  2555. * NSS calculation (that would be done in rate_control_rate_init())
  2556. * and use the # of streams from that element.
  2557. */
  2558. if (elems.opmode_notif &&
  2559. !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
  2560. u8 nss;
  2561. nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
  2562. nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
  2563. nss += 1;
  2564. sta->sta.rx_nss = nss;
  2565. }
  2566. rate_control_rate_init(sta);
  2567. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
  2568. set_sta_flag(sta, WLAN_STA_MFP);
  2569. sta->sta.mfp = true;
  2570. } else {
  2571. sta->sta.mfp = false;
  2572. }
  2573. sta->sta.wme = elems.wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
  2574. err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  2575. if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  2576. err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  2577. if (err) {
  2578. sdata_info(sdata,
  2579. "failed to move station %pM to desired state\n",
  2580. sta->sta.addr);
  2581. WARN_ON(__sta_info_destroy(sta));
  2582. mutex_unlock(&sdata->local->sta_mtx);
  2583. ret = false;
  2584. goto out;
  2585. }
  2586. mutex_unlock(&sdata->local->sta_mtx);
  2587. /*
  2588. * Always handle WMM once after association regardless
  2589. * of the first value the AP uses. Setting -1 here has
  2590. * that effect because the AP values is an unsigned
  2591. * 4-bit value.
  2592. */
  2593. ifmgd->wmm_last_param_set = -1;
  2594. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) && elems.wmm_param)
  2595. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2596. elems.wmm_param_len);
  2597. else
  2598. ieee80211_set_wmm_default(sdata, false);
  2599. changed |= BSS_CHANGED_QOS;
  2600. /* set AID and assoc capability,
  2601. * ieee80211_set_associated() will tell the driver */
  2602. bss_conf->aid = aid;
  2603. bss_conf->assoc_capability = capab_info;
  2604. ieee80211_set_associated(sdata, cbss, changed);
  2605. /*
  2606. * If we're using 4-addr mode, let the AP know that we're
  2607. * doing so, so that it can create the STA VLAN on its side
  2608. */
  2609. if (ifmgd->use_4addr)
  2610. ieee80211_send_4addr_nullfunc(local, sdata);
  2611. /*
  2612. * Start timer to probe the connection to the AP now.
  2613. * Also start the timer that will detect beacon loss.
  2614. */
  2615. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  2616. ieee80211_sta_reset_beacon_monitor(sdata);
  2617. ret = true;
  2618. out:
  2619. kfree(bss_ies);
  2620. return ret;
  2621. }
  2622. static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  2623. struct ieee80211_mgmt *mgmt,
  2624. size_t len)
  2625. {
  2626. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2627. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  2628. u16 capab_info, status_code, aid;
  2629. struct ieee802_11_elems elems;
  2630. int ac, uapsd_queues = -1;
  2631. u8 *pos;
  2632. bool reassoc;
  2633. struct cfg80211_bss *bss;
  2634. struct ieee80211_event event = {
  2635. .type = MLME_EVENT,
  2636. .u.mlme.data = ASSOC_EVENT,
  2637. };
  2638. sdata_assert_lock(sdata);
  2639. if (!assoc_data)
  2640. return;
  2641. if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
  2642. return;
  2643. /*
  2644. * AssocResp and ReassocResp have identical structure, so process both
  2645. * of them in this function.
  2646. */
  2647. if (len < 24 + 6)
  2648. return;
  2649. reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
  2650. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  2651. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  2652. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  2653. sdata_info(sdata,
  2654. "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
  2655. reassoc ? "Rea" : "A", mgmt->sa,
  2656. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  2657. pos = mgmt->u.assoc_resp.variable;
  2658. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  2659. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  2660. elems.timeout_int &&
  2661. elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  2662. u32 tu, ms;
  2663. tu = le32_to_cpu(elems.timeout_int->value);
  2664. ms = tu * 1024 / 1000;
  2665. sdata_info(sdata,
  2666. "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
  2667. mgmt->sa, tu, ms);
  2668. assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
  2669. assoc_data->timeout_started = true;
  2670. if (ms > IEEE80211_ASSOC_TIMEOUT)
  2671. run_again(sdata, assoc_data->timeout);
  2672. return;
  2673. }
  2674. bss = assoc_data->bss;
  2675. if (status_code != WLAN_STATUS_SUCCESS) {
  2676. sdata_info(sdata, "%pM denied association (code=%d)\n",
  2677. mgmt->sa, status_code);
  2678. ieee80211_destroy_assoc_data(sdata, false);
  2679. event.u.mlme.status = MLME_DENIED;
  2680. event.u.mlme.reason = status_code;
  2681. drv_event_callback(sdata->local, sdata, &event);
  2682. } else {
  2683. if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
  2684. /* oops -- internal error -- send timeout for now */
  2685. ieee80211_destroy_assoc_data(sdata, false);
  2686. cfg80211_assoc_timeout(sdata->dev, bss);
  2687. return;
  2688. }
  2689. event.u.mlme.status = MLME_SUCCESS;
  2690. drv_event_callback(sdata->local, sdata, &event);
  2691. sdata_info(sdata, "associated\n");
  2692. /*
  2693. * destroy assoc_data afterwards, as otherwise an idle
  2694. * recalc after assoc_data is NULL but before associated
  2695. * is set can cause the interface to go idle
  2696. */
  2697. ieee80211_destroy_assoc_data(sdata, true);
  2698. /* get uapsd queues configuration */
  2699. uapsd_queues = 0;
  2700. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  2701. if (sdata->tx_conf[ac].uapsd)
  2702. uapsd_queues |= BIT(ac);
  2703. }
  2704. cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len, uapsd_queues);
  2705. }
  2706. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  2707. struct ieee80211_mgmt *mgmt, size_t len,
  2708. struct ieee80211_rx_status *rx_status,
  2709. struct ieee802_11_elems *elems)
  2710. {
  2711. struct ieee80211_local *local = sdata->local;
  2712. struct ieee80211_bss *bss;
  2713. struct ieee80211_channel *channel;
  2714. sdata_assert_lock(sdata);
  2715. channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
  2716. if (!channel)
  2717. return;
  2718. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  2719. channel);
  2720. if (bss) {
  2721. sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
  2722. ieee80211_rx_bss_put(local, bss);
  2723. }
  2724. }
  2725. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  2726. struct sk_buff *skb)
  2727. {
  2728. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  2729. struct ieee80211_if_managed *ifmgd;
  2730. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  2731. size_t baselen, len = skb->len;
  2732. struct ieee802_11_elems elems;
  2733. ifmgd = &sdata->u.mgd;
  2734. sdata_assert_lock(sdata);
  2735. if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
  2736. return; /* ignore ProbeResp to foreign address */
  2737. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  2738. if (baselen > len)
  2739. return;
  2740. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  2741. false, &elems);
  2742. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2743. if (ifmgd->associated &&
  2744. ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2745. ieee80211_reset_ap_probe(sdata);
  2746. if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
  2747. ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
  2748. /* got probe response, continue with auth */
  2749. sdata_info(sdata, "direct probe responded\n");
  2750. ifmgd->auth_data->tries = 0;
  2751. ifmgd->auth_data->timeout = jiffies;
  2752. ifmgd->auth_data->timeout_started = true;
  2753. run_again(sdata, ifmgd->auth_data->timeout);
  2754. }
  2755. }
  2756. /*
  2757. * This is the canonical list of information elements we care about,
  2758. * the filter code also gives us all changes to the Microsoft OUI
  2759. * (00:50:F2) vendor IE which is used for WMM which we need to track,
  2760. * as well as the DTPC IE (part of the Cisco OUI) used for signaling
  2761. * changes to requested client power.
  2762. *
  2763. * We implement beacon filtering in software since that means we can
  2764. * avoid processing the frame here and in cfg80211, and userspace
  2765. * will not be able to tell whether the hardware supports it or not.
  2766. *
  2767. * XXX: This list needs to be dynamic -- userspace needs to be able to
  2768. * add items it requires. It also needs to be able to tell us to
  2769. * look out for other vendor IEs.
  2770. */
  2771. static const u64 care_about_ies =
  2772. (1ULL << WLAN_EID_COUNTRY) |
  2773. (1ULL << WLAN_EID_ERP_INFO) |
  2774. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  2775. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  2776. (1ULL << WLAN_EID_HT_CAPABILITY) |
  2777. (1ULL << WLAN_EID_HT_OPERATION) |
  2778. (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
  2779. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  2780. struct ieee80211_mgmt *mgmt, size_t len,
  2781. struct ieee80211_rx_status *rx_status)
  2782. {
  2783. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2784. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  2785. size_t baselen;
  2786. struct ieee802_11_elems elems;
  2787. struct ieee80211_local *local = sdata->local;
  2788. struct ieee80211_chanctx_conf *chanctx_conf;
  2789. struct ieee80211_channel *chan;
  2790. struct sta_info *sta;
  2791. u32 changed = 0;
  2792. bool erp_valid;
  2793. u8 erp_value = 0;
  2794. u32 ncrc;
  2795. u8 *bssid;
  2796. u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2797. sdata_assert_lock(sdata);
  2798. /* Process beacon from the current BSS */
  2799. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  2800. if (baselen > len)
  2801. return;
  2802. rcu_read_lock();
  2803. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2804. if (!chanctx_conf) {
  2805. rcu_read_unlock();
  2806. return;
  2807. }
  2808. if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
  2809. rcu_read_unlock();
  2810. return;
  2811. }
  2812. chan = chanctx_conf->def.chan;
  2813. rcu_read_unlock();
  2814. if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
  2815. ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
  2816. ieee802_11_parse_elems(mgmt->u.beacon.variable,
  2817. len - baselen, false, &elems);
  2818. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2819. if (elems.tim && !elems.parse_error) {
  2820. const struct ieee80211_tim_ie *tim_ie = elems.tim;
  2821. ifmgd->dtim_period = tim_ie->dtim_period;
  2822. }
  2823. ifmgd->have_beacon = true;
  2824. ifmgd->assoc_data->need_beacon = false;
  2825. if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
  2826. sdata->vif.bss_conf.sync_tsf =
  2827. le64_to_cpu(mgmt->u.beacon.timestamp);
  2828. sdata->vif.bss_conf.sync_device_ts =
  2829. rx_status->device_timestamp;
  2830. if (elems.tim)
  2831. sdata->vif.bss_conf.sync_dtim_count =
  2832. elems.tim->dtim_count;
  2833. else
  2834. sdata->vif.bss_conf.sync_dtim_count = 0;
  2835. }
  2836. /* continue assoc process */
  2837. ifmgd->assoc_data->timeout = jiffies;
  2838. ifmgd->assoc_data->timeout_started = true;
  2839. run_again(sdata, ifmgd->assoc_data->timeout);
  2840. return;
  2841. }
  2842. if (!ifmgd->associated ||
  2843. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2844. return;
  2845. bssid = ifmgd->associated->bssid;
  2846. /* Track average RSSI from the Beacon frames of the current AP */
  2847. ifmgd->last_beacon_signal = rx_status->signal;
  2848. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  2849. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  2850. ifmgd->ave_beacon_signal = rx_status->signal * 16;
  2851. ifmgd->last_cqm_event_signal = 0;
  2852. ifmgd->count_beacon_signal = 1;
  2853. ifmgd->last_ave_beacon_signal = 0;
  2854. } else {
  2855. ifmgd->ave_beacon_signal =
  2856. (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
  2857. (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
  2858. ifmgd->ave_beacon_signal) / 16;
  2859. ifmgd->count_beacon_signal++;
  2860. }
  2861. if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
  2862. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
  2863. int sig = ifmgd->ave_beacon_signal;
  2864. int last_sig = ifmgd->last_ave_beacon_signal;
  2865. struct ieee80211_event event = {
  2866. .type = RSSI_EVENT,
  2867. };
  2868. /*
  2869. * if signal crosses either of the boundaries, invoke callback
  2870. * with appropriate parameters
  2871. */
  2872. if (sig > ifmgd->rssi_max_thold &&
  2873. (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
  2874. ifmgd->last_ave_beacon_signal = sig;
  2875. event.u.rssi.data = RSSI_EVENT_HIGH;
  2876. drv_event_callback(local, sdata, &event);
  2877. } else if (sig < ifmgd->rssi_min_thold &&
  2878. (last_sig >= ifmgd->rssi_max_thold ||
  2879. last_sig == 0)) {
  2880. ifmgd->last_ave_beacon_signal = sig;
  2881. event.u.rssi.data = RSSI_EVENT_LOW;
  2882. drv_event_callback(local, sdata, &event);
  2883. }
  2884. }
  2885. if (bss_conf->cqm_rssi_thold &&
  2886. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  2887. !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
  2888. int sig = ifmgd->ave_beacon_signal / 16;
  2889. int last_event = ifmgd->last_cqm_event_signal;
  2890. int thold = bss_conf->cqm_rssi_thold;
  2891. int hyst = bss_conf->cqm_rssi_hyst;
  2892. if (sig < thold &&
  2893. (last_event == 0 || sig < last_event - hyst)) {
  2894. ifmgd->last_cqm_event_signal = sig;
  2895. ieee80211_cqm_rssi_notify(
  2896. &sdata->vif,
  2897. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  2898. GFP_KERNEL);
  2899. } else if (sig > thold &&
  2900. (last_event == 0 || sig > last_event + hyst)) {
  2901. ifmgd->last_cqm_event_signal = sig;
  2902. ieee80211_cqm_rssi_notify(
  2903. &sdata->vif,
  2904. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  2905. GFP_KERNEL);
  2906. }
  2907. }
  2908. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
  2909. mlme_dbg_ratelimited(sdata,
  2910. "cancelling AP probe due to a received beacon\n");
  2911. ieee80211_reset_ap_probe(sdata);
  2912. }
  2913. /*
  2914. * Push the beacon loss detection into the future since
  2915. * we are processing a beacon from the AP just now.
  2916. */
  2917. ieee80211_sta_reset_beacon_monitor(sdata);
  2918. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  2919. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  2920. len - baselen, false, &elems,
  2921. care_about_ies, ncrc);
  2922. if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) {
  2923. bool directed_tim = ieee80211_check_tim(elems.tim,
  2924. elems.tim_len,
  2925. ifmgd->aid);
  2926. if (directed_tim) {
  2927. if (local->hw.conf.dynamic_ps_timeout > 0) {
  2928. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  2929. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2930. ieee80211_hw_config(local,
  2931. IEEE80211_CONF_CHANGE_PS);
  2932. }
  2933. ieee80211_send_nullfunc(local, sdata, 0);
  2934. } else if (!local->pspolling && sdata->u.mgd.powersave) {
  2935. local->pspolling = true;
  2936. /*
  2937. * Here is assumed that the driver will be
  2938. * able to send ps-poll frame and receive a
  2939. * response even though power save mode is
  2940. * enabled, but some drivers might require
  2941. * to disable power save here. This needs
  2942. * to be investigated.
  2943. */
  2944. ieee80211_send_pspoll(local, sdata);
  2945. }
  2946. }
  2947. }
  2948. if (sdata->vif.p2p) {
  2949. struct ieee80211_p2p_noa_attr noa = {};
  2950. int ret;
  2951. ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
  2952. len - baselen,
  2953. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  2954. (u8 *) &noa, sizeof(noa));
  2955. if (ret >= 2) {
  2956. if (sdata->u.mgd.p2p_noa_index != noa.index) {
  2957. /* valid noa_attr and index changed */
  2958. sdata->u.mgd.p2p_noa_index = noa.index;
  2959. memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
  2960. changed |= BSS_CHANGED_P2P_PS;
  2961. /*
  2962. * make sure we update all information, the CRC
  2963. * mechanism doesn't look at P2P attributes.
  2964. */
  2965. ifmgd->beacon_crc_valid = false;
  2966. }
  2967. } else if (sdata->u.mgd.p2p_noa_index != -1) {
  2968. /* noa_attr not found and we had valid noa_attr before */
  2969. sdata->u.mgd.p2p_noa_index = -1;
  2970. memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
  2971. changed |= BSS_CHANGED_P2P_PS;
  2972. ifmgd->beacon_crc_valid = false;
  2973. }
  2974. }
  2975. if (ifmgd->csa_waiting_bcn)
  2976. ieee80211_chswitch_post_beacon(sdata);
  2977. /*
  2978. * Update beacon timing and dtim count on every beacon appearance. This
  2979. * will allow the driver to use the most updated values. Do it before
  2980. * comparing this one with last received beacon.
  2981. * IMPORTANT: These parameters would possibly be out of sync by the time
  2982. * the driver will use them. The synchronized view is currently
  2983. * guaranteed only in certain callbacks.
  2984. */
  2985. if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
  2986. sdata->vif.bss_conf.sync_tsf =
  2987. le64_to_cpu(mgmt->u.beacon.timestamp);
  2988. sdata->vif.bss_conf.sync_device_ts =
  2989. rx_status->device_timestamp;
  2990. if (elems.tim)
  2991. sdata->vif.bss_conf.sync_dtim_count =
  2992. elems.tim->dtim_count;
  2993. else
  2994. sdata->vif.bss_conf.sync_dtim_count = 0;
  2995. }
  2996. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  2997. return;
  2998. ifmgd->beacon_crc = ncrc;
  2999. ifmgd->beacon_crc_valid = true;
  3000. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  3001. ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
  3002. rx_status->device_timestamp,
  3003. &elems, true);
  3004. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
  3005. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  3006. elems.wmm_param_len))
  3007. changed |= BSS_CHANGED_QOS;
  3008. /*
  3009. * If we haven't had a beacon before, tell the driver about the
  3010. * DTIM period (and beacon timing if desired) now.
  3011. */
  3012. if (!ifmgd->have_beacon) {
  3013. /* a few bogus AP send dtim_period = 0 or no TIM IE */
  3014. if (elems.tim)
  3015. bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
  3016. else
  3017. bss_conf->dtim_period = 1;
  3018. changed |= BSS_CHANGED_BEACON_INFO;
  3019. ifmgd->have_beacon = true;
  3020. mutex_lock(&local->iflist_mtx);
  3021. ieee80211_recalc_ps(local, -1);
  3022. mutex_unlock(&local->iflist_mtx);
  3023. ieee80211_recalc_ps_vif(sdata);
  3024. }
  3025. if (elems.erp_info) {
  3026. erp_valid = true;
  3027. erp_value = elems.erp_info[0];
  3028. } else {
  3029. erp_valid = false;
  3030. }
  3031. changed |= ieee80211_handle_bss_capability(sdata,
  3032. le16_to_cpu(mgmt->u.beacon.capab_info),
  3033. erp_valid, erp_value);
  3034. mutex_lock(&local->sta_mtx);
  3035. sta = sta_info_get(sdata, bssid);
  3036. if (ieee80211_config_bw(sdata, sta,
  3037. elems.ht_cap_elem, elems.ht_operation,
  3038. elems.vht_operation, bssid, &changed)) {
  3039. mutex_unlock(&local->sta_mtx);
  3040. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3041. WLAN_REASON_DEAUTH_LEAVING,
  3042. true, deauth_buf);
  3043. ieee80211_report_disconnect(sdata, deauth_buf,
  3044. sizeof(deauth_buf), true,
  3045. WLAN_REASON_DEAUTH_LEAVING);
  3046. return;
  3047. }
  3048. if (sta && elems.opmode_notif)
  3049. ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
  3050. rx_status->band, true);
  3051. mutex_unlock(&local->sta_mtx);
  3052. changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
  3053. elems.country_elem,
  3054. elems.country_elem_len,
  3055. elems.pwr_constr_elem,
  3056. elems.cisco_dtpc_elem);
  3057. ieee80211_bss_info_change_notify(sdata, changed);
  3058. }
  3059. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  3060. struct sk_buff *skb)
  3061. {
  3062. struct ieee80211_rx_status *rx_status;
  3063. struct ieee80211_mgmt *mgmt;
  3064. u16 fc;
  3065. struct ieee802_11_elems elems;
  3066. int ies_len;
  3067. rx_status = (struct ieee80211_rx_status *) skb->cb;
  3068. mgmt = (struct ieee80211_mgmt *) skb->data;
  3069. fc = le16_to_cpu(mgmt->frame_control);
  3070. sdata_lock(sdata);
  3071. switch (fc & IEEE80211_FCTL_STYPE) {
  3072. case IEEE80211_STYPE_BEACON:
  3073. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  3074. break;
  3075. case IEEE80211_STYPE_PROBE_RESP:
  3076. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  3077. break;
  3078. case IEEE80211_STYPE_AUTH:
  3079. ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  3080. break;
  3081. case IEEE80211_STYPE_DEAUTH:
  3082. ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  3083. break;
  3084. case IEEE80211_STYPE_DISASSOC:
  3085. ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  3086. break;
  3087. case IEEE80211_STYPE_ASSOC_RESP:
  3088. case IEEE80211_STYPE_REASSOC_RESP:
  3089. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
  3090. break;
  3091. case IEEE80211_STYPE_ACTION:
  3092. if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
  3093. ies_len = skb->len -
  3094. offsetof(struct ieee80211_mgmt,
  3095. u.action.u.chan_switch.variable);
  3096. if (ies_len < 0)
  3097. break;
  3098. ieee802_11_parse_elems(
  3099. mgmt->u.action.u.chan_switch.variable,
  3100. ies_len, true, &elems);
  3101. if (elems.parse_error)
  3102. break;
  3103. ieee80211_sta_process_chanswitch(sdata,
  3104. rx_status->mactime,
  3105. rx_status->device_timestamp,
  3106. &elems, false);
  3107. } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
  3108. ies_len = skb->len -
  3109. offsetof(struct ieee80211_mgmt,
  3110. u.action.u.ext_chan_switch.variable);
  3111. if (ies_len < 0)
  3112. break;
  3113. ieee802_11_parse_elems(
  3114. mgmt->u.action.u.ext_chan_switch.variable,
  3115. ies_len, true, &elems);
  3116. if (elems.parse_error)
  3117. break;
  3118. /* for the handling code pretend this was also an IE */
  3119. elems.ext_chansw_ie =
  3120. &mgmt->u.action.u.ext_chan_switch.data;
  3121. ieee80211_sta_process_chanswitch(sdata,
  3122. rx_status->mactime,
  3123. rx_status->device_timestamp,
  3124. &elems, false);
  3125. }
  3126. break;
  3127. }
  3128. sdata_unlock(sdata);
  3129. }
  3130. static void ieee80211_sta_timer(unsigned long data)
  3131. {
  3132. struct ieee80211_sub_if_data *sdata =
  3133. (struct ieee80211_sub_if_data *) data;
  3134. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  3135. }
  3136. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  3137. u8 *bssid, u8 reason, bool tx)
  3138. {
  3139. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3140. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
  3141. tx, frame_buf);
  3142. ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
  3143. reason);
  3144. }
  3145. static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
  3146. {
  3147. struct ieee80211_local *local = sdata->local;
  3148. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3149. struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
  3150. u32 tx_flags = 0;
  3151. sdata_assert_lock(sdata);
  3152. if (WARN_ON_ONCE(!auth_data))
  3153. return -EINVAL;
  3154. auth_data->tries++;
  3155. if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
  3156. sdata_info(sdata, "authentication with %pM timed out\n",
  3157. auth_data->bss->bssid);
  3158. /*
  3159. * Most likely AP is not in the range so remove the
  3160. * bss struct for that AP.
  3161. */
  3162. cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
  3163. return -ETIMEDOUT;
  3164. }
  3165. drv_mgd_prepare_tx(local, sdata);
  3166. if (auth_data->bss->proberesp_ies) {
  3167. u16 trans = 1;
  3168. u16 status = 0;
  3169. sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
  3170. auth_data->bss->bssid, auth_data->tries,
  3171. IEEE80211_AUTH_MAX_TRIES);
  3172. auth_data->expected_transaction = 2;
  3173. if (auth_data->algorithm == WLAN_AUTH_SAE) {
  3174. trans = auth_data->sae_trans;
  3175. status = auth_data->sae_status;
  3176. auth_data->expected_transaction = trans;
  3177. }
  3178. if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
  3179. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  3180. IEEE80211_TX_INTFL_MLME_CONN_TX;
  3181. ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
  3182. auth_data->data, auth_data->data_len,
  3183. auth_data->bss->bssid,
  3184. auth_data->bss->bssid, NULL, 0, 0,
  3185. tx_flags);
  3186. } else {
  3187. const u8 *ssidie;
  3188. sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
  3189. auth_data->bss->bssid, auth_data->tries,
  3190. IEEE80211_AUTH_MAX_TRIES);
  3191. rcu_read_lock();
  3192. ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
  3193. if (!ssidie) {
  3194. rcu_read_unlock();
  3195. return -EINVAL;
  3196. }
  3197. /*
  3198. * Direct probe is sent to broadcast address as some APs
  3199. * will not answer to direct packet in unassociated state.
  3200. */
  3201. ieee80211_send_probe_req(sdata, sdata->vif.addr, NULL,
  3202. ssidie + 2, ssidie[1],
  3203. NULL, 0, (u32) -1, true, 0,
  3204. auth_data->bss->channel, false);
  3205. rcu_read_unlock();
  3206. }
  3207. if (tx_flags == 0) {
  3208. auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  3209. auth_data->timeout_started = true;
  3210. run_again(sdata, auth_data->timeout);
  3211. } else {
  3212. auth_data->timeout =
  3213. round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
  3214. auth_data->timeout_started = true;
  3215. run_again(sdata, auth_data->timeout);
  3216. }
  3217. return 0;
  3218. }
  3219. static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
  3220. {
  3221. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  3222. struct ieee80211_local *local = sdata->local;
  3223. sdata_assert_lock(sdata);
  3224. assoc_data->tries++;
  3225. if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
  3226. sdata_info(sdata, "association with %pM timed out\n",
  3227. assoc_data->bss->bssid);
  3228. /*
  3229. * Most likely AP is not in the range so remove the
  3230. * bss struct for that AP.
  3231. */
  3232. cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
  3233. return -ETIMEDOUT;
  3234. }
  3235. sdata_info(sdata, "associate with %pM (try %d/%d)\n",
  3236. assoc_data->bss->bssid, assoc_data->tries,
  3237. IEEE80211_ASSOC_MAX_TRIES);
  3238. ieee80211_send_assoc(sdata);
  3239. if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
  3240. assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  3241. assoc_data->timeout_started = true;
  3242. run_again(sdata, assoc_data->timeout);
  3243. } else {
  3244. assoc_data->timeout =
  3245. round_jiffies_up(jiffies +
  3246. IEEE80211_ASSOC_TIMEOUT_LONG);
  3247. assoc_data->timeout_started = true;
  3248. run_again(sdata, assoc_data->timeout);
  3249. }
  3250. return 0;
  3251. }
  3252. void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
  3253. __le16 fc, bool acked)
  3254. {
  3255. struct ieee80211_local *local = sdata->local;
  3256. sdata->u.mgd.status_fc = fc;
  3257. sdata->u.mgd.status_acked = acked;
  3258. sdata->u.mgd.status_received = true;
  3259. ieee80211_queue_work(&local->hw, &sdata->work);
  3260. }
  3261. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  3262. {
  3263. struct ieee80211_local *local = sdata->local;
  3264. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3265. sdata_lock(sdata);
  3266. if (ifmgd->status_received) {
  3267. __le16 fc = ifmgd->status_fc;
  3268. bool status_acked = ifmgd->status_acked;
  3269. ifmgd->status_received = false;
  3270. if (ifmgd->auth_data &&
  3271. (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
  3272. if (status_acked) {
  3273. ifmgd->auth_data->timeout =
  3274. jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
  3275. run_again(sdata, ifmgd->auth_data->timeout);
  3276. } else {
  3277. ifmgd->auth_data->timeout = jiffies - 1;
  3278. }
  3279. ifmgd->auth_data->timeout_started = true;
  3280. } else if (ifmgd->assoc_data &&
  3281. (ieee80211_is_assoc_req(fc) ||
  3282. ieee80211_is_reassoc_req(fc))) {
  3283. if (status_acked) {
  3284. ifmgd->assoc_data->timeout =
  3285. jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
  3286. run_again(sdata, ifmgd->assoc_data->timeout);
  3287. } else {
  3288. ifmgd->assoc_data->timeout = jiffies - 1;
  3289. }
  3290. ifmgd->assoc_data->timeout_started = true;
  3291. }
  3292. }
  3293. if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
  3294. time_after(jiffies, ifmgd->auth_data->timeout)) {
  3295. if (ifmgd->auth_data->done) {
  3296. /*
  3297. * ok ... we waited for assoc but userspace didn't,
  3298. * so let's just kill the auth data
  3299. */
  3300. ieee80211_destroy_auth_data(sdata, false);
  3301. } else if (ieee80211_probe_auth(sdata)) {
  3302. u8 bssid[ETH_ALEN];
  3303. struct ieee80211_event event = {
  3304. .type = MLME_EVENT,
  3305. .u.mlme.data = AUTH_EVENT,
  3306. .u.mlme.status = MLME_TIMEOUT,
  3307. };
  3308. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  3309. ieee80211_destroy_auth_data(sdata, false);
  3310. cfg80211_auth_timeout(sdata->dev, bssid);
  3311. drv_event_callback(sdata->local, sdata, &event);
  3312. }
  3313. } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
  3314. run_again(sdata, ifmgd->auth_data->timeout);
  3315. if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
  3316. time_after(jiffies, ifmgd->assoc_data->timeout)) {
  3317. if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
  3318. ieee80211_do_assoc(sdata)) {
  3319. struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
  3320. struct ieee80211_event event = {
  3321. .type = MLME_EVENT,
  3322. .u.mlme.data = ASSOC_EVENT,
  3323. .u.mlme.status = MLME_TIMEOUT,
  3324. };
  3325. ieee80211_destroy_assoc_data(sdata, false);
  3326. cfg80211_assoc_timeout(sdata->dev, bss);
  3327. drv_event_callback(sdata->local, sdata, &event);
  3328. }
  3329. } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
  3330. run_again(sdata, ifmgd->assoc_data->timeout);
  3331. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
  3332. ifmgd->associated) {
  3333. u8 bssid[ETH_ALEN];
  3334. int max_tries;
  3335. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  3336. if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
  3337. max_tries = max_nullfunc_tries;
  3338. else
  3339. max_tries = max_probe_tries;
  3340. /* ACK received for nullfunc probing frame */
  3341. if (!ifmgd->probe_send_count)
  3342. ieee80211_reset_ap_probe(sdata);
  3343. else if (ifmgd->nullfunc_failed) {
  3344. if (ifmgd->probe_send_count < max_tries) {
  3345. mlme_dbg(sdata,
  3346. "No ack for nullfunc frame to AP %pM, try %d/%i\n",
  3347. bssid, ifmgd->probe_send_count,
  3348. max_tries);
  3349. ieee80211_mgd_probe_ap_send(sdata);
  3350. } else {
  3351. mlme_dbg(sdata,
  3352. "No ack for nullfunc frame to AP %pM, disconnecting.\n",
  3353. bssid);
  3354. ieee80211_sta_connection_lost(sdata, bssid,
  3355. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  3356. false);
  3357. }
  3358. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  3359. run_again(sdata, ifmgd->probe_timeout);
  3360. else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
  3361. mlme_dbg(sdata,
  3362. "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
  3363. bssid, probe_wait_ms);
  3364. ieee80211_sta_connection_lost(sdata, bssid,
  3365. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  3366. } else if (ifmgd->probe_send_count < max_tries) {
  3367. mlme_dbg(sdata,
  3368. "No probe response from AP %pM after %dms, try %d/%i\n",
  3369. bssid, probe_wait_ms,
  3370. ifmgd->probe_send_count, max_tries);
  3371. ieee80211_mgd_probe_ap_send(sdata);
  3372. } else {
  3373. /*
  3374. * We actually lost the connection ... or did we?
  3375. * Let's make sure!
  3376. */
  3377. wiphy_debug(local->hw.wiphy,
  3378. "%s: No probe response from AP %pM"
  3379. " after %dms, disconnecting.\n",
  3380. sdata->name,
  3381. bssid, probe_wait_ms);
  3382. ieee80211_sta_connection_lost(sdata, bssid,
  3383. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  3384. }
  3385. }
  3386. sdata_unlock(sdata);
  3387. }
  3388. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  3389. {
  3390. struct ieee80211_sub_if_data *sdata =
  3391. (struct ieee80211_sub_if_data *) data;
  3392. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3393. if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
  3394. return;
  3395. sdata->u.mgd.connection_loss = false;
  3396. ieee80211_queue_work(&sdata->local->hw,
  3397. &sdata->u.mgd.beacon_connection_loss_work);
  3398. }
  3399. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  3400. {
  3401. struct ieee80211_sub_if_data *sdata =
  3402. (struct ieee80211_sub_if_data *) data;
  3403. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3404. struct ieee80211_local *local = sdata->local;
  3405. if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
  3406. return;
  3407. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  3408. }
  3409. static void ieee80211_sta_monitor_work(struct work_struct *work)
  3410. {
  3411. struct ieee80211_sub_if_data *sdata =
  3412. container_of(work, struct ieee80211_sub_if_data,
  3413. u.mgd.monitor_work);
  3414. ieee80211_mgd_probe_ap(sdata, false);
  3415. }
  3416. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  3417. {
  3418. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  3419. __ieee80211_stop_poll(sdata);
  3420. /* let's probe the connection once */
  3421. if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
  3422. ieee80211_queue_work(&sdata->local->hw,
  3423. &sdata->u.mgd.monitor_work);
  3424. /* and do all the other regular work too */
  3425. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  3426. }
  3427. }
  3428. #ifdef CONFIG_PM
  3429. void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
  3430. {
  3431. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3432. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3433. sdata_lock(sdata);
  3434. if (ifmgd->auth_data || ifmgd->assoc_data) {
  3435. const u8 *bssid = ifmgd->auth_data ?
  3436. ifmgd->auth_data->bss->bssid :
  3437. ifmgd->assoc_data->bss->bssid;
  3438. /*
  3439. * If we are trying to authenticate / associate while suspending,
  3440. * cfg80211 won't know and won't actually abort those attempts,
  3441. * thus we need to do that ourselves.
  3442. */
  3443. ieee80211_send_deauth_disassoc(sdata, bssid,
  3444. IEEE80211_STYPE_DEAUTH,
  3445. WLAN_REASON_DEAUTH_LEAVING,
  3446. false, frame_buf);
  3447. if (ifmgd->assoc_data)
  3448. ieee80211_destroy_assoc_data(sdata, false);
  3449. if (ifmgd->auth_data)
  3450. ieee80211_destroy_auth_data(sdata, false);
  3451. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  3452. IEEE80211_DEAUTH_FRAME_LEN);
  3453. }
  3454. /* This is a bit of a hack - we should find a better and more generic
  3455. * solution to this. Normally when suspending, cfg80211 will in fact
  3456. * deauthenticate. However, it doesn't (and cannot) stop an ongoing
  3457. * auth (not so important) or assoc (this is the problem) process.
  3458. *
  3459. * As a consequence, it can happen that we are in the process of both
  3460. * associating and suspending, and receive an association response
  3461. * after cfg80211 has checked if it needs to disconnect, but before
  3462. * we actually set the flag to drop incoming frames. This will then
  3463. * cause the workqueue flush to process the association response in
  3464. * the suspend, resulting in a successful association just before it
  3465. * tries to remove the interface from the driver, which now though
  3466. * has a channel context assigned ... this results in issues.
  3467. *
  3468. * To work around this (for now) simply deauth here again if we're
  3469. * now connected.
  3470. */
  3471. if (ifmgd->associated && !sdata->local->wowlan) {
  3472. u8 bssid[ETH_ALEN];
  3473. struct cfg80211_deauth_request req = {
  3474. .reason_code = WLAN_REASON_DEAUTH_LEAVING,
  3475. .bssid = bssid,
  3476. };
  3477. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  3478. ieee80211_mgd_deauth(sdata, &req);
  3479. }
  3480. sdata_unlock(sdata);
  3481. }
  3482. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  3483. {
  3484. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3485. sdata_lock(sdata);
  3486. if (!ifmgd->associated) {
  3487. sdata_unlock(sdata);
  3488. return;
  3489. }
  3490. if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
  3491. sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
  3492. mlme_dbg(sdata, "driver requested disconnect after resume\n");
  3493. ieee80211_sta_connection_lost(sdata,
  3494. ifmgd->associated->bssid,
  3495. WLAN_REASON_UNSPECIFIED,
  3496. true);
  3497. sdata_unlock(sdata);
  3498. return;
  3499. }
  3500. sdata_unlock(sdata);
  3501. }
  3502. #endif
  3503. /* interface setup */
  3504. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  3505. {
  3506. struct ieee80211_if_managed *ifmgd;
  3507. ifmgd = &sdata->u.mgd;
  3508. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  3509. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  3510. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  3511. ieee80211_beacon_connection_loss_work);
  3512. INIT_WORK(&ifmgd->csa_connection_drop_work,
  3513. ieee80211_csa_connection_drop_work);
  3514. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
  3515. INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
  3516. ieee80211_tdls_peer_del_work);
  3517. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  3518. (unsigned long) sdata);
  3519. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  3520. (unsigned long) sdata);
  3521. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  3522. (unsigned long) sdata);
  3523. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  3524. (unsigned long) sdata);
  3525. INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
  3526. ieee80211_sta_handle_tspec_ac_params_wk);
  3527. ifmgd->flags = 0;
  3528. ifmgd->powersave = sdata->wdev.ps;
  3529. ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
  3530. ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
  3531. ifmgd->p2p_noa_index = -1;
  3532. if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
  3533. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  3534. else
  3535. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  3536. /* Setup TDLS data */
  3537. spin_lock_init(&ifmgd->teardown_lock);
  3538. ifmgd->teardown_skb = NULL;
  3539. ifmgd->orig_teardown_skb = NULL;
  3540. }
  3541. /* scan finished notification */
  3542. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  3543. {
  3544. struct ieee80211_sub_if_data *sdata;
  3545. /* Restart STA timers */
  3546. rcu_read_lock();
  3547. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  3548. if (ieee80211_sdata_running(sdata))
  3549. ieee80211_restart_sta_timer(sdata);
  3550. }
  3551. rcu_read_unlock();
  3552. }
  3553. int ieee80211_max_network_latency(struct notifier_block *nb,
  3554. unsigned long data, void *dummy)
  3555. {
  3556. s32 latency_usec = (s32) data;
  3557. struct ieee80211_local *local =
  3558. container_of(nb, struct ieee80211_local,
  3559. network_latency_notifier);
  3560. mutex_lock(&local->iflist_mtx);
  3561. ieee80211_recalc_ps(local, latency_usec);
  3562. mutex_unlock(&local->iflist_mtx);
  3563. return NOTIFY_OK;
  3564. }
  3565. static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
  3566. struct cfg80211_bss *cbss)
  3567. {
  3568. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3569. const u8 *ht_cap_ie, *vht_cap_ie;
  3570. const struct ieee80211_ht_cap *ht_cap;
  3571. const struct ieee80211_vht_cap *vht_cap;
  3572. u8 chains = 1;
  3573. if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
  3574. return chains;
  3575. ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  3576. if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
  3577. ht_cap = (void *)(ht_cap_ie + 2);
  3578. chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
  3579. /*
  3580. * TODO: use "Tx Maximum Number Spatial Streams Supported" and
  3581. * "Tx Unequal Modulation Supported" fields.
  3582. */
  3583. }
  3584. if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
  3585. return chains;
  3586. vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  3587. if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
  3588. u8 nss;
  3589. u16 tx_mcs_map;
  3590. vht_cap = (void *)(vht_cap_ie + 2);
  3591. tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
  3592. for (nss = 8; nss > 0; nss--) {
  3593. if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
  3594. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  3595. break;
  3596. }
  3597. /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
  3598. chains = max(chains, nss);
  3599. }
  3600. return chains;
  3601. }
  3602. static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
  3603. struct cfg80211_bss *cbss)
  3604. {
  3605. struct ieee80211_local *local = sdata->local;
  3606. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3607. const struct ieee80211_ht_cap *ht_cap = NULL;
  3608. const struct ieee80211_ht_operation *ht_oper = NULL;
  3609. const struct ieee80211_vht_operation *vht_oper = NULL;
  3610. struct ieee80211_supported_band *sband;
  3611. struct cfg80211_chan_def chandef;
  3612. int ret;
  3613. sband = local->hw.wiphy->bands[cbss->channel->band];
  3614. ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
  3615. IEEE80211_STA_DISABLE_80P80MHZ |
  3616. IEEE80211_STA_DISABLE_160MHZ);
  3617. rcu_read_lock();
  3618. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  3619. sband->ht_cap.ht_supported) {
  3620. const u8 *ht_oper_ie, *ht_cap_ie;
  3621. ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
  3622. if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
  3623. ht_oper = (void *)(ht_oper_ie + 2);
  3624. ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  3625. if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
  3626. ht_cap = (void *)(ht_cap_ie + 2);
  3627. if (!ht_cap) {
  3628. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3629. ht_oper = NULL;
  3630. }
  3631. }
  3632. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  3633. sband->vht_cap.vht_supported) {
  3634. const u8 *vht_oper_ie, *vht_cap;
  3635. vht_oper_ie = ieee80211_bss_get_ie(cbss,
  3636. WLAN_EID_VHT_OPERATION);
  3637. if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
  3638. vht_oper = (void *)(vht_oper_ie + 2);
  3639. if (vht_oper && !ht_oper) {
  3640. vht_oper = NULL;
  3641. sdata_info(sdata,
  3642. "AP advertised VHT without HT, disabling both\n");
  3643. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3644. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3645. }
  3646. vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  3647. if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
  3648. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3649. vht_oper = NULL;
  3650. }
  3651. }
  3652. ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
  3653. cbss->channel,
  3654. ht_cap, ht_oper, vht_oper,
  3655. &chandef, false);
  3656. sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
  3657. local->rx_chains);
  3658. rcu_read_unlock();
  3659. /* will change later if needed */
  3660. sdata->smps_mode = IEEE80211_SMPS_OFF;
  3661. mutex_lock(&local->mtx);
  3662. /*
  3663. * If this fails (possibly due to channel context sharing
  3664. * on incompatible channels, e.g. 80+80 and 160 sharing the
  3665. * same control channel) try to use a smaller bandwidth.
  3666. */
  3667. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3668. IEEE80211_CHANCTX_SHARED);
  3669. /* don't downgrade for 5 and 10 MHz channels, though. */
  3670. if (chandef.width == NL80211_CHAN_WIDTH_5 ||
  3671. chandef.width == NL80211_CHAN_WIDTH_10)
  3672. goto out;
  3673. while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
  3674. ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
  3675. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3676. IEEE80211_CHANCTX_SHARED);
  3677. }
  3678. out:
  3679. mutex_unlock(&local->mtx);
  3680. return ret;
  3681. }
  3682. static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
  3683. struct cfg80211_bss *cbss, bool assoc,
  3684. bool override)
  3685. {
  3686. struct ieee80211_local *local = sdata->local;
  3687. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3688. struct ieee80211_bss *bss = (void *)cbss->priv;
  3689. struct sta_info *new_sta = NULL;
  3690. struct ieee80211_supported_band *sband;
  3691. bool have_sta = false;
  3692. int err;
  3693. sband = local->hw.wiphy->bands[cbss->channel->band];
  3694. if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
  3695. return -EINVAL;
  3696. if (assoc) {
  3697. rcu_read_lock();
  3698. have_sta = sta_info_get(sdata, cbss->bssid);
  3699. rcu_read_unlock();
  3700. }
  3701. if (!have_sta) {
  3702. new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  3703. if (!new_sta)
  3704. return -ENOMEM;
  3705. }
  3706. if (new_sta || override) {
  3707. err = ieee80211_prep_channel(sdata, cbss);
  3708. if (err) {
  3709. if (new_sta)
  3710. sta_info_free(local, new_sta);
  3711. return -EINVAL;
  3712. }
  3713. }
  3714. if (new_sta) {
  3715. u32 rates = 0, basic_rates = 0;
  3716. bool have_higher_than_11mbit;
  3717. int min_rate = INT_MAX, min_rate_index = -1;
  3718. struct ieee80211_chanctx_conf *chanctx_conf;
  3719. const struct cfg80211_bss_ies *ies;
  3720. int shift = ieee80211_vif_get_shift(&sdata->vif);
  3721. u32 rate_flags;
  3722. rcu_read_lock();
  3723. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  3724. if (WARN_ON(!chanctx_conf)) {
  3725. rcu_read_unlock();
  3726. sta_info_free(local, new_sta);
  3727. return -EINVAL;
  3728. }
  3729. rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
  3730. rcu_read_unlock();
  3731. ieee80211_get_rates(sband, bss->supp_rates,
  3732. bss->supp_rates_len,
  3733. &rates, &basic_rates,
  3734. &have_higher_than_11mbit,
  3735. &min_rate, &min_rate_index,
  3736. shift, rate_flags);
  3737. /*
  3738. * This used to be a workaround for basic rates missing
  3739. * in the association response frame. Now that we no
  3740. * longer use the basic rates from there, it probably
  3741. * doesn't happen any more, but keep the workaround so
  3742. * in case some *other* APs are buggy in different ways
  3743. * we can connect -- with a warning.
  3744. */
  3745. if (!basic_rates && min_rate_index >= 0) {
  3746. sdata_info(sdata,
  3747. "No basic rates, using min rate instead\n");
  3748. basic_rates = BIT(min_rate_index);
  3749. }
  3750. new_sta->sta.supp_rates[cbss->channel->band] = rates;
  3751. sdata->vif.bss_conf.basic_rates = basic_rates;
  3752. /* cf. IEEE 802.11 9.2.12 */
  3753. if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
  3754. have_higher_than_11mbit)
  3755. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  3756. else
  3757. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  3758. memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
  3759. /* set timing information */
  3760. sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
  3761. rcu_read_lock();
  3762. ies = rcu_dereference(cbss->beacon_ies);
  3763. if (ies) {
  3764. const u8 *tim_ie;
  3765. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3766. sdata->vif.bss_conf.sync_device_ts =
  3767. bss->device_ts_beacon;
  3768. tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  3769. ies->data, ies->len);
  3770. if (tim_ie && tim_ie[1] >= 2)
  3771. sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
  3772. else
  3773. sdata->vif.bss_conf.sync_dtim_count = 0;
  3774. } else if (!ieee80211_hw_check(&sdata->local->hw,
  3775. TIMING_BEACON_ONLY)) {
  3776. ies = rcu_dereference(cbss->proberesp_ies);
  3777. /* must be non-NULL since beacon IEs were NULL */
  3778. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3779. sdata->vif.bss_conf.sync_device_ts =
  3780. bss->device_ts_presp;
  3781. sdata->vif.bss_conf.sync_dtim_count = 0;
  3782. } else {
  3783. sdata->vif.bss_conf.sync_tsf = 0;
  3784. sdata->vif.bss_conf.sync_device_ts = 0;
  3785. sdata->vif.bss_conf.sync_dtim_count = 0;
  3786. }
  3787. rcu_read_unlock();
  3788. /* tell driver about BSSID, basic rates and timing */
  3789. ieee80211_bss_info_change_notify(sdata,
  3790. BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
  3791. BSS_CHANGED_BEACON_INT);
  3792. if (assoc)
  3793. sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
  3794. err = sta_info_insert(new_sta);
  3795. new_sta = NULL;
  3796. if (err) {
  3797. sdata_info(sdata,
  3798. "failed to insert STA entry for the AP (error %d)\n",
  3799. err);
  3800. return err;
  3801. }
  3802. } else
  3803. WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
  3804. /* Cancel scan to ensure that nothing interferes with connection */
  3805. if (local->scanning)
  3806. ieee80211_scan_cancel(local);
  3807. return 0;
  3808. }
  3809. /* config hooks */
  3810. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  3811. struct cfg80211_auth_request *req)
  3812. {
  3813. struct ieee80211_local *local = sdata->local;
  3814. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3815. struct ieee80211_mgd_auth_data *auth_data;
  3816. u16 auth_alg;
  3817. int err;
  3818. /* prepare auth data structure */
  3819. switch (req->auth_type) {
  3820. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  3821. auth_alg = WLAN_AUTH_OPEN;
  3822. break;
  3823. case NL80211_AUTHTYPE_SHARED_KEY:
  3824. if (IS_ERR(local->wep_tx_tfm))
  3825. return -EOPNOTSUPP;
  3826. auth_alg = WLAN_AUTH_SHARED_KEY;
  3827. break;
  3828. case NL80211_AUTHTYPE_FT:
  3829. auth_alg = WLAN_AUTH_FT;
  3830. break;
  3831. case NL80211_AUTHTYPE_NETWORK_EAP:
  3832. auth_alg = WLAN_AUTH_LEAP;
  3833. break;
  3834. case NL80211_AUTHTYPE_SAE:
  3835. auth_alg = WLAN_AUTH_SAE;
  3836. break;
  3837. default:
  3838. return -EOPNOTSUPP;
  3839. }
  3840. auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
  3841. req->ie_len, GFP_KERNEL);
  3842. if (!auth_data)
  3843. return -ENOMEM;
  3844. auth_data->bss = req->bss;
  3845. if (req->sae_data_len >= 4) {
  3846. __le16 *pos = (__le16 *) req->sae_data;
  3847. auth_data->sae_trans = le16_to_cpu(pos[0]);
  3848. auth_data->sae_status = le16_to_cpu(pos[1]);
  3849. memcpy(auth_data->data, req->sae_data + 4,
  3850. req->sae_data_len - 4);
  3851. auth_data->data_len += req->sae_data_len - 4;
  3852. }
  3853. if (req->ie && req->ie_len) {
  3854. memcpy(&auth_data->data[auth_data->data_len],
  3855. req->ie, req->ie_len);
  3856. auth_data->data_len += req->ie_len;
  3857. }
  3858. if (req->key && req->key_len) {
  3859. auth_data->key_len = req->key_len;
  3860. auth_data->key_idx = req->key_idx;
  3861. memcpy(auth_data->key, req->key, req->key_len);
  3862. }
  3863. auth_data->algorithm = auth_alg;
  3864. /* try to authenticate/probe */
  3865. if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
  3866. ifmgd->assoc_data) {
  3867. err = -EBUSY;
  3868. goto err_free;
  3869. }
  3870. if (ifmgd->auth_data)
  3871. ieee80211_destroy_auth_data(sdata, false);
  3872. /* prep auth_data so we don't go into idle on disassoc */
  3873. ifmgd->auth_data = auth_data;
  3874. if (ifmgd->associated) {
  3875. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3876. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3877. WLAN_REASON_UNSPECIFIED,
  3878. false, frame_buf);
  3879. ieee80211_report_disconnect(sdata, frame_buf,
  3880. sizeof(frame_buf), true,
  3881. WLAN_REASON_UNSPECIFIED);
  3882. }
  3883. sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
  3884. err = ieee80211_prep_connection(sdata, req->bss, false, false);
  3885. if (err)
  3886. goto err_clear;
  3887. err = ieee80211_probe_auth(sdata);
  3888. if (err) {
  3889. sta_info_destroy_addr(sdata, req->bss->bssid);
  3890. goto err_clear;
  3891. }
  3892. /* hold our own reference */
  3893. cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
  3894. return 0;
  3895. err_clear:
  3896. eth_zero_addr(ifmgd->bssid);
  3897. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  3898. ifmgd->auth_data = NULL;
  3899. mutex_lock(&sdata->local->mtx);
  3900. ieee80211_vif_release_channel(sdata);
  3901. mutex_unlock(&sdata->local->mtx);
  3902. err_free:
  3903. kfree(auth_data);
  3904. return err;
  3905. }
  3906. static bool ieee80211_usable_wmm_params(struct ieee80211_sub_if_data *sdata,
  3907. const u8 *wmm_param, int len)
  3908. {
  3909. const u8 *pos;
  3910. size_t left;
  3911. if (len < 8)
  3912. return false;
  3913. if (wmm_param[5] != 1 /* version */)
  3914. return false;
  3915. pos = wmm_param + 8;
  3916. left = len - 8;
  3917. for (; left >= 4; left -= 4, pos += 4) {
  3918. u8 aifsn = pos[0] & 0x0f;
  3919. u8 ecwmin = pos[1] & 0x0f;
  3920. u8 ecwmax = (pos[1] & 0xf0) >> 4;
  3921. int aci = (pos[0] >> 5) & 0x03;
  3922. if (aifsn < 2) {
  3923. sdata_info(sdata,
  3924. "AP has invalid WMM params (AIFSN=%d for ACI %d), disabling WMM\n",
  3925. aifsn, aci);
  3926. return false;
  3927. }
  3928. if (ecwmin > ecwmax) {
  3929. sdata_info(sdata,
  3930. "AP has invalid WMM params (ECWmin/max=%d/%d for ACI %d), disabling WMM\n",
  3931. ecwmin, ecwmax, aci);
  3932. return false;
  3933. }
  3934. }
  3935. return true;
  3936. }
  3937. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  3938. struct cfg80211_assoc_request *req)
  3939. {
  3940. struct ieee80211_local *local = sdata->local;
  3941. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3942. struct ieee80211_bss *bss = (void *)req->bss->priv;
  3943. struct ieee80211_mgd_assoc_data *assoc_data;
  3944. const struct cfg80211_bss_ies *beacon_ies;
  3945. struct ieee80211_supported_band *sband;
  3946. const u8 *ssidie, *ht_ie, *vht_ie;
  3947. int i, err;
  3948. bool override = false;
  3949. assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
  3950. if (!assoc_data)
  3951. return -ENOMEM;
  3952. rcu_read_lock();
  3953. ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  3954. if (!ssidie) {
  3955. rcu_read_unlock();
  3956. kfree(assoc_data);
  3957. return -EINVAL;
  3958. }
  3959. memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
  3960. assoc_data->ssid_len = ssidie[1];
  3961. rcu_read_unlock();
  3962. if (ifmgd->associated) {
  3963. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3964. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3965. WLAN_REASON_UNSPECIFIED,
  3966. false, frame_buf);
  3967. ieee80211_report_disconnect(sdata, frame_buf,
  3968. sizeof(frame_buf), true,
  3969. WLAN_REASON_UNSPECIFIED);
  3970. }
  3971. if (ifmgd->auth_data && !ifmgd->auth_data->done) {
  3972. err = -EBUSY;
  3973. goto err_free;
  3974. }
  3975. if (ifmgd->assoc_data) {
  3976. err = -EBUSY;
  3977. goto err_free;
  3978. }
  3979. if (ifmgd->auth_data) {
  3980. bool match;
  3981. /* keep sta info, bssid if matching */
  3982. match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
  3983. ieee80211_destroy_auth_data(sdata, match);
  3984. }
  3985. /* prepare assoc data */
  3986. ifmgd->beacon_crc_valid = false;
  3987. assoc_data->wmm = bss->wmm_used &&
  3988. (local->hw.queues >= IEEE80211_NUM_ACS);
  3989. if (assoc_data->wmm) {
  3990. /* try to check validity of WMM params IE */
  3991. const struct cfg80211_bss_ies *ies;
  3992. const u8 *wp, *start, *end;
  3993. rcu_read_lock();
  3994. ies = rcu_dereference(req->bss->ies);
  3995. start = ies->data;
  3996. end = start + ies->len;
  3997. while (true) {
  3998. wp = cfg80211_find_vendor_ie(
  3999. WLAN_OUI_MICROSOFT,
  4000. WLAN_OUI_TYPE_MICROSOFT_WMM,
  4001. start, end - start);
  4002. if (!wp)
  4003. break;
  4004. start = wp + wp[1] + 2;
  4005. /* if this IE is too short, try the next */
  4006. if (wp[1] <= 4)
  4007. continue;
  4008. /* if this IE is WMM params, we found what we wanted */
  4009. if (wp[6] == 1)
  4010. break;
  4011. }
  4012. if (!wp || !ieee80211_usable_wmm_params(sdata, wp + 2,
  4013. wp[1] - 2)) {
  4014. assoc_data->wmm = false;
  4015. ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
  4016. }
  4017. rcu_read_unlock();
  4018. }
  4019. /*
  4020. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  4021. * We still associate in non-HT mode (11a/b/g) if any one of these
  4022. * ciphers is configured as pairwise.
  4023. * We can set this to true for non-11n hardware, that'll be checked
  4024. * separately along with the peer capabilities.
  4025. */
  4026. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
  4027. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  4028. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  4029. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
  4030. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  4031. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  4032. netdev_info(sdata->dev,
  4033. "disabling HT/VHT due to WEP/TKIP use\n");
  4034. }
  4035. }
  4036. /* Also disable HT if we don't support it or the AP doesn't use WMM */
  4037. sband = local->hw.wiphy->bands[req->bss->channel->band];
  4038. if (!sband->ht_cap.ht_supported ||
  4039. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
  4040. ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
  4041. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  4042. if (!bss->wmm_used &&
  4043. !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
  4044. netdev_info(sdata->dev,
  4045. "disabling HT as WMM/QoS is not supported by the AP\n");
  4046. }
  4047. /* disable VHT if we don't support it or the AP doesn't use WMM */
  4048. if (!sband->vht_cap.vht_supported ||
  4049. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
  4050. ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
  4051. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  4052. if (!bss->wmm_used &&
  4053. !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
  4054. netdev_info(sdata->dev,
  4055. "disabling VHT as WMM/QoS is not supported by the AP\n");
  4056. }
  4057. memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
  4058. memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
  4059. sizeof(ifmgd->ht_capa_mask));
  4060. memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
  4061. memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
  4062. sizeof(ifmgd->vht_capa_mask));
  4063. if (req->ie && req->ie_len) {
  4064. memcpy(assoc_data->ie, req->ie, req->ie_len);
  4065. assoc_data->ie_len = req->ie_len;
  4066. }
  4067. assoc_data->bss = req->bss;
  4068. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  4069. if (ifmgd->powersave)
  4070. sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
  4071. else
  4072. sdata->smps_mode = IEEE80211_SMPS_OFF;
  4073. } else
  4074. sdata->smps_mode = ifmgd->req_smps;
  4075. assoc_data->capability = req->bss->capability;
  4076. assoc_data->supp_rates = bss->supp_rates;
  4077. assoc_data->supp_rates_len = bss->supp_rates_len;
  4078. rcu_read_lock();
  4079. ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
  4080. if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
  4081. assoc_data->ap_ht_param =
  4082. ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
  4083. else
  4084. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  4085. vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
  4086. if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
  4087. memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
  4088. sizeof(struct ieee80211_vht_cap));
  4089. else
  4090. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  4091. rcu_read_unlock();
  4092. if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
  4093. ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
  4094. "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
  4095. sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
  4096. if (bss->wmm_used && bss->uapsd_supported &&
  4097. (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
  4098. assoc_data->uapsd = true;
  4099. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  4100. } else {
  4101. assoc_data->uapsd = false;
  4102. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  4103. }
  4104. if (req->prev_bssid)
  4105. memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
  4106. if (req->use_mfp) {
  4107. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  4108. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  4109. } else {
  4110. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  4111. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  4112. }
  4113. if (req->flags & ASSOC_REQ_USE_RRM)
  4114. ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
  4115. else
  4116. ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
  4117. if (req->crypto.control_port)
  4118. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  4119. else
  4120. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  4121. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  4122. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  4123. sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
  4124. sdata->vif.type);
  4125. /* kick off associate process */
  4126. ifmgd->assoc_data = assoc_data;
  4127. ifmgd->dtim_period = 0;
  4128. ifmgd->have_beacon = false;
  4129. /* override HT/VHT configuration only if the AP and we support it */
  4130. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
  4131. struct ieee80211_sta_ht_cap sta_ht_cap;
  4132. if (req->flags & ASSOC_REQ_DISABLE_HT)
  4133. override = true;
  4134. memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
  4135. ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
  4136. /* check for 40 MHz disable override */
  4137. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
  4138. sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  4139. !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  4140. override = true;
  4141. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  4142. req->flags & ASSOC_REQ_DISABLE_VHT)
  4143. override = true;
  4144. }
  4145. if (req->flags & ASSOC_REQ_DISABLE_HT) {
  4146. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  4147. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  4148. }
  4149. if (req->flags & ASSOC_REQ_DISABLE_VHT)
  4150. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  4151. err = ieee80211_prep_connection(sdata, req->bss, true, override);
  4152. if (err)
  4153. goto err_clear;
  4154. rcu_read_lock();
  4155. beacon_ies = rcu_dereference(req->bss->beacon_ies);
  4156. if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
  4157. !beacon_ies) {
  4158. /*
  4159. * Wait up to one beacon interval ...
  4160. * should this be more if we miss one?
  4161. */
  4162. sdata_info(sdata, "waiting for beacon from %pM\n",
  4163. ifmgd->bssid);
  4164. assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
  4165. assoc_data->timeout_started = true;
  4166. assoc_data->need_beacon = true;
  4167. } else if (beacon_ies) {
  4168. const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  4169. beacon_ies->data,
  4170. beacon_ies->len);
  4171. u8 dtim_count = 0;
  4172. if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
  4173. const struct ieee80211_tim_ie *tim;
  4174. tim = (void *)(tim_ie + 2);
  4175. ifmgd->dtim_period = tim->dtim_period;
  4176. dtim_count = tim->dtim_count;
  4177. }
  4178. ifmgd->have_beacon = true;
  4179. assoc_data->timeout = jiffies;
  4180. assoc_data->timeout_started = true;
  4181. if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
  4182. sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
  4183. sdata->vif.bss_conf.sync_device_ts =
  4184. bss->device_ts_beacon;
  4185. sdata->vif.bss_conf.sync_dtim_count = dtim_count;
  4186. }
  4187. } else {
  4188. assoc_data->timeout = jiffies;
  4189. assoc_data->timeout_started = true;
  4190. }
  4191. rcu_read_unlock();
  4192. run_again(sdata, assoc_data->timeout);
  4193. if (bss->corrupt_data) {
  4194. char *corrupt_type = "data";
  4195. if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
  4196. if (bss->corrupt_data &
  4197. IEEE80211_BSS_CORRUPT_PROBE_RESP)
  4198. corrupt_type = "beacon and probe response";
  4199. else
  4200. corrupt_type = "beacon";
  4201. } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
  4202. corrupt_type = "probe response";
  4203. sdata_info(sdata, "associating with AP with corrupt %s\n",
  4204. corrupt_type);
  4205. }
  4206. return 0;
  4207. err_clear:
  4208. eth_zero_addr(ifmgd->bssid);
  4209. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  4210. ifmgd->assoc_data = NULL;
  4211. err_free:
  4212. kfree(assoc_data);
  4213. return err;
  4214. }
  4215. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  4216. struct cfg80211_deauth_request *req)
  4217. {
  4218. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  4219. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  4220. bool tx = !req->local_state_change;
  4221. if (ifmgd->auth_data &&
  4222. ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
  4223. sdata_info(sdata,
  4224. "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
  4225. req->bssid, req->reason_code,
  4226. ieee80211_get_reason_code_string(req->reason_code));
  4227. drv_mgd_prepare_tx(sdata->local, sdata);
  4228. ieee80211_send_deauth_disassoc(sdata, req->bssid,
  4229. IEEE80211_STYPE_DEAUTH,
  4230. req->reason_code, tx,
  4231. frame_buf);
  4232. ieee80211_destroy_auth_data(sdata, false);
  4233. ieee80211_report_disconnect(sdata, frame_buf,
  4234. sizeof(frame_buf), true,
  4235. req->reason_code);
  4236. return 0;
  4237. }
  4238. if (ifmgd->associated &&
  4239. ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
  4240. sdata_info(sdata,
  4241. "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
  4242. req->bssid, req->reason_code,
  4243. ieee80211_get_reason_code_string(req->reason_code));
  4244. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  4245. req->reason_code, tx, frame_buf);
  4246. ieee80211_report_disconnect(sdata, frame_buf,
  4247. sizeof(frame_buf), true,
  4248. req->reason_code);
  4249. return 0;
  4250. }
  4251. return -ENOTCONN;
  4252. }
  4253. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  4254. struct cfg80211_disassoc_request *req)
  4255. {
  4256. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  4257. u8 bssid[ETH_ALEN];
  4258. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  4259. /*
  4260. * cfg80211 should catch this ... but it's racy since
  4261. * we can receive a disassoc frame, process it, hand it
  4262. * to cfg80211 while that's in a locked section already
  4263. * trying to tell us that the user wants to disconnect.
  4264. */
  4265. if (ifmgd->associated != req->bss)
  4266. return -ENOLINK;
  4267. sdata_info(sdata,
  4268. "disassociating from %pM by local choice (Reason: %u=%s)\n",
  4269. req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
  4270. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  4271. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
  4272. req->reason_code, !req->local_state_change,
  4273. frame_buf);
  4274. ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
  4275. req->reason_code);
  4276. return 0;
  4277. }
  4278. void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
  4279. {
  4280. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  4281. /*
  4282. * Make sure some work items will not run after this,
  4283. * they will not do anything but might not have been
  4284. * cancelled when disconnecting.
  4285. */
  4286. cancel_work_sync(&ifmgd->monitor_work);
  4287. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  4288. cancel_work_sync(&ifmgd->request_smps_work);
  4289. cancel_work_sync(&ifmgd->csa_connection_drop_work);
  4290. cancel_work_sync(&ifmgd->chswitch_work);
  4291. cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
  4292. sdata_lock(sdata);
  4293. if (ifmgd->assoc_data) {
  4294. struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
  4295. ieee80211_destroy_assoc_data(sdata, false);
  4296. cfg80211_assoc_timeout(sdata->dev, bss);
  4297. }
  4298. if (ifmgd->auth_data)
  4299. ieee80211_destroy_auth_data(sdata, false);
  4300. spin_lock_bh(&ifmgd->teardown_lock);
  4301. if (ifmgd->teardown_skb) {
  4302. kfree_skb(ifmgd->teardown_skb);
  4303. ifmgd->teardown_skb = NULL;
  4304. ifmgd->orig_teardown_skb = NULL;
  4305. }
  4306. spin_unlock_bh(&ifmgd->teardown_lock);
  4307. del_timer_sync(&ifmgd->timer);
  4308. sdata_unlock(sdata);
  4309. }
  4310. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  4311. enum nl80211_cqm_rssi_threshold_event rssi_event,
  4312. gfp_t gfp)
  4313. {
  4314. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4315. trace_api_cqm_rssi_notify(sdata, rssi_event);
  4316. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
  4317. }
  4318. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
  4319. void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
  4320. {
  4321. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4322. trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
  4323. cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
  4324. }
  4325. EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);