mlme.c 125 KB

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