mlme.c 124 KB

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