mlme.c 122 KB

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