mlme.c 123 KB

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