mlme.c 143 KB

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