mlme.c 144 KB

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