mlme.c 128 KB

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