mlme.c 145 KB

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