mlme.c 126 KB

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