mlme.c 143 KB

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