mlme.c 126 KB

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