mlme.c 146 KB

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