mlme.c 125 KB

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