mlme.c 124 KB

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