mlme.c 138 KB

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