mlme.c 124 KB

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