util.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * utilities for mac80211
  12. */
  13. #include <net/mac80211.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/export.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/crc32.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include <net/rtnetlink.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "mesh.h"
  30. #include "wme.h"
  31. #include "led.h"
  32. #include "wep.h"
  33. /* privid for wiphys to determine whether they belong to us or not */
  34. const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
  35. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  36. {
  37. struct ieee80211_local *local;
  38. BUG_ON(!wiphy);
  39. local = wiphy_priv(wiphy);
  40. return &local->hw;
  41. }
  42. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  43. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  44. enum nl80211_iftype type)
  45. {
  46. __le16 fc = hdr->frame_control;
  47. /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
  48. if (len < 16)
  49. return NULL;
  50. if (ieee80211_is_data(fc)) {
  51. if (len < 24) /* drop incorrect hdr len (data) */
  52. return NULL;
  53. if (ieee80211_has_a4(fc))
  54. return NULL;
  55. if (ieee80211_has_tods(fc))
  56. return hdr->addr1;
  57. if (ieee80211_has_fromds(fc))
  58. return hdr->addr2;
  59. return hdr->addr3;
  60. }
  61. if (ieee80211_is_mgmt(fc)) {
  62. if (len < 24) /* drop incorrect hdr len (mgmt) */
  63. return NULL;
  64. return hdr->addr3;
  65. }
  66. if (ieee80211_is_ctl(fc)) {
  67. if (ieee80211_is_pspoll(fc))
  68. return hdr->addr1;
  69. if (ieee80211_is_back_req(fc)) {
  70. switch (type) {
  71. case NL80211_IFTYPE_STATION:
  72. return hdr->addr2;
  73. case NL80211_IFTYPE_AP:
  74. case NL80211_IFTYPE_AP_VLAN:
  75. return hdr->addr1;
  76. default:
  77. break; /* fall through to the return */
  78. }
  79. }
  80. }
  81. return NULL;
  82. }
  83. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  84. {
  85. struct sk_buff *skb;
  86. struct ieee80211_hdr *hdr;
  87. skb_queue_walk(&tx->skbs, skb) {
  88. hdr = (struct ieee80211_hdr *) skb->data;
  89. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  90. }
  91. }
  92. int ieee80211_frame_duration(enum ieee80211_band band, size_t len,
  93. int rate, int erp, int short_preamble,
  94. int shift)
  95. {
  96. int dur;
  97. /* calculate duration (in microseconds, rounded up to next higher
  98. * integer if it includes a fractional microsecond) to send frame of
  99. * len bytes (does not include FCS) at the given rate. Duration will
  100. * also include SIFS.
  101. *
  102. * rate is in 100 kbps, so divident is multiplied by 10 in the
  103. * DIV_ROUND_UP() operations.
  104. *
  105. * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
  106. * is assumed to be 0 otherwise.
  107. */
  108. if (band == IEEE80211_BAND_5GHZ || erp) {
  109. /*
  110. * OFDM:
  111. *
  112. * N_DBPS = DATARATE x 4
  113. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  114. * (16 = SIGNAL time, 6 = tail bits)
  115. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  116. *
  117. * T_SYM = 4 usec
  118. * 802.11a - 18.5.2: aSIFSTime = 16 usec
  119. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  120. * signal ext = 6 usec
  121. */
  122. dur = 16; /* SIFS + signal ext */
  123. dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
  124. dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
  125. /* IEEE 802.11-2012 18.3.2.4: all values above are:
  126. * * times 4 for 5 MHz
  127. * * times 2 for 10 MHz
  128. */
  129. dur *= 1 << shift;
  130. /* rates should already consider the channel bandwidth,
  131. * don't apply divisor again.
  132. */
  133. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  134. 4 * rate); /* T_SYM x N_SYM */
  135. } else {
  136. /*
  137. * 802.11b or 802.11g with 802.11b compatibility:
  138. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  139. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  140. *
  141. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  142. * aSIFSTime = 10 usec
  143. * aPreambleLength = 144 usec or 72 usec with short preamble
  144. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  145. */
  146. dur = 10; /* aSIFSTime = 10 usec */
  147. dur += short_preamble ? (72 + 24) : (144 + 48);
  148. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  149. }
  150. return dur;
  151. }
  152. /* Exported duration function for driver use */
  153. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  154. struct ieee80211_vif *vif,
  155. enum ieee80211_band band,
  156. size_t frame_len,
  157. struct ieee80211_rate *rate)
  158. {
  159. struct ieee80211_sub_if_data *sdata;
  160. u16 dur;
  161. int erp, shift = 0;
  162. bool short_preamble = false;
  163. erp = 0;
  164. if (vif) {
  165. sdata = vif_to_sdata(vif);
  166. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  167. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  168. erp = rate->flags & IEEE80211_RATE_ERP_G;
  169. shift = ieee80211_vif_get_shift(vif);
  170. }
  171. dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
  172. short_preamble, shift);
  173. return cpu_to_le16(dur);
  174. }
  175. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  176. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  177. struct ieee80211_vif *vif, size_t frame_len,
  178. const struct ieee80211_tx_info *frame_txctl)
  179. {
  180. struct ieee80211_local *local = hw_to_local(hw);
  181. struct ieee80211_rate *rate;
  182. struct ieee80211_sub_if_data *sdata;
  183. bool short_preamble;
  184. int erp, shift = 0, bitrate;
  185. u16 dur;
  186. struct ieee80211_supported_band *sband;
  187. sband = local->hw.wiphy->bands[frame_txctl->band];
  188. short_preamble = false;
  189. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  190. erp = 0;
  191. if (vif) {
  192. sdata = vif_to_sdata(vif);
  193. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  194. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  195. erp = rate->flags & IEEE80211_RATE_ERP_G;
  196. shift = ieee80211_vif_get_shift(vif);
  197. }
  198. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  199. /* CTS duration */
  200. dur = ieee80211_frame_duration(sband->band, 10, bitrate,
  201. erp, short_preamble, shift);
  202. /* Data frame duration */
  203. dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
  204. erp, short_preamble, shift);
  205. /* ACK duration */
  206. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  207. erp, short_preamble, shift);
  208. return cpu_to_le16(dur);
  209. }
  210. EXPORT_SYMBOL(ieee80211_rts_duration);
  211. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  212. struct ieee80211_vif *vif,
  213. size_t frame_len,
  214. const struct ieee80211_tx_info *frame_txctl)
  215. {
  216. struct ieee80211_local *local = hw_to_local(hw);
  217. struct ieee80211_rate *rate;
  218. struct ieee80211_sub_if_data *sdata;
  219. bool short_preamble;
  220. int erp, shift = 0, bitrate;
  221. u16 dur;
  222. struct ieee80211_supported_band *sband;
  223. sband = local->hw.wiphy->bands[frame_txctl->band];
  224. short_preamble = false;
  225. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  226. erp = 0;
  227. if (vif) {
  228. sdata = vif_to_sdata(vif);
  229. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  230. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  231. erp = rate->flags & IEEE80211_RATE_ERP_G;
  232. shift = ieee80211_vif_get_shift(vif);
  233. }
  234. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  235. /* Data frame duration */
  236. dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
  237. erp, short_preamble, shift);
  238. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  239. /* ACK duration */
  240. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  241. erp, short_preamble, shift);
  242. }
  243. return cpu_to_le16(dur);
  244. }
  245. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  246. void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
  247. {
  248. struct ieee80211_sub_if_data *sdata;
  249. int n_acs = IEEE80211_NUM_ACS;
  250. if (local->hw.queues < IEEE80211_NUM_ACS)
  251. n_acs = 1;
  252. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  253. int ac;
  254. if (!sdata->dev)
  255. continue;
  256. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  257. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  258. continue;
  259. for (ac = 0; ac < n_acs; ac++) {
  260. int ac_queue = sdata->vif.hw_queue[ac];
  261. if (ac_queue == queue ||
  262. (sdata->vif.cab_queue == queue &&
  263. local->queue_stop_reasons[ac_queue] == 0 &&
  264. skb_queue_empty(&local->pending[ac_queue])))
  265. netif_wake_subqueue(sdata->dev, ac);
  266. }
  267. }
  268. }
  269. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  270. enum queue_stop_reason reason)
  271. {
  272. struct ieee80211_local *local = hw_to_local(hw);
  273. trace_wake_queue(local, queue, reason);
  274. if (WARN_ON(queue >= hw->queues))
  275. return;
  276. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  277. return;
  278. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  279. if (local->queue_stop_reasons[queue] != 0)
  280. /* someone still has this queue stopped */
  281. return;
  282. if (skb_queue_empty(&local->pending[queue])) {
  283. rcu_read_lock();
  284. ieee80211_propagate_queue_wake(local, queue);
  285. rcu_read_unlock();
  286. } else
  287. tasklet_schedule(&local->tx_pending_tasklet);
  288. }
  289. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  290. enum queue_stop_reason reason)
  291. {
  292. struct ieee80211_local *local = hw_to_local(hw);
  293. unsigned long flags;
  294. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  295. __ieee80211_wake_queue(hw, queue, reason);
  296. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  297. }
  298. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  299. {
  300. ieee80211_wake_queue_by_reason(hw, queue,
  301. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  302. }
  303. EXPORT_SYMBOL(ieee80211_wake_queue);
  304. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  305. enum queue_stop_reason reason)
  306. {
  307. struct ieee80211_local *local = hw_to_local(hw);
  308. struct ieee80211_sub_if_data *sdata;
  309. int n_acs = IEEE80211_NUM_ACS;
  310. trace_stop_queue(local, queue, reason);
  311. if (WARN_ON(queue >= hw->queues))
  312. return;
  313. if (test_bit(reason, &local->queue_stop_reasons[queue]))
  314. return;
  315. __set_bit(reason, &local->queue_stop_reasons[queue]);
  316. if (local->hw.queues < IEEE80211_NUM_ACS)
  317. n_acs = 1;
  318. rcu_read_lock();
  319. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  320. int ac;
  321. if (!sdata->dev)
  322. continue;
  323. for (ac = 0; ac < n_acs; ac++) {
  324. if (sdata->vif.hw_queue[ac] == queue ||
  325. sdata->vif.cab_queue == queue)
  326. netif_stop_subqueue(sdata->dev, ac);
  327. }
  328. }
  329. rcu_read_unlock();
  330. }
  331. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  332. enum queue_stop_reason reason)
  333. {
  334. struct ieee80211_local *local = hw_to_local(hw);
  335. unsigned long flags;
  336. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  337. __ieee80211_stop_queue(hw, queue, reason);
  338. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  339. }
  340. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  341. {
  342. ieee80211_stop_queue_by_reason(hw, queue,
  343. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  344. }
  345. EXPORT_SYMBOL(ieee80211_stop_queue);
  346. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  347. struct sk_buff *skb)
  348. {
  349. struct ieee80211_hw *hw = &local->hw;
  350. unsigned long flags;
  351. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  352. int queue = info->hw_queue;
  353. if (WARN_ON(!info->control.vif)) {
  354. ieee80211_free_txskb(&local->hw, skb);
  355. return;
  356. }
  357. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  358. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  359. __skb_queue_tail(&local->pending[queue], skb);
  360. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  361. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  362. }
  363. void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  364. struct sk_buff_head *skbs,
  365. void (*fn)(void *data), void *data)
  366. {
  367. struct ieee80211_hw *hw = &local->hw;
  368. struct sk_buff *skb;
  369. unsigned long flags;
  370. int queue, i;
  371. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  372. while ((skb = skb_dequeue(skbs))) {
  373. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  374. if (WARN_ON(!info->control.vif)) {
  375. ieee80211_free_txskb(&local->hw, skb);
  376. continue;
  377. }
  378. queue = info->hw_queue;
  379. __ieee80211_stop_queue(hw, queue,
  380. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  381. __skb_queue_tail(&local->pending[queue], skb);
  382. }
  383. if (fn)
  384. fn(data);
  385. for (i = 0; i < hw->queues; i++)
  386. __ieee80211_wake_queue(hw, i,
  387. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  388. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  389. }
  390. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  391. unsigned long queues,
  392. enum queue_stop_reason reason)
  393. {
  394. struct ieee80211_local *local = hw_to_local(hw);
  395. unsigned long flags;
  396. int i;
  397. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  398. for_each_set_bit(i, &queues, hw->queues)
  399. __ieee80211_stop_queue(hw, i, reason);
  400. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  401. }
  402. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  403. {
  404. ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  405. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  406. }
  407. EXPORT_SYMBOL(ieee80211_stop_queues);
  408. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  409. {
  410. struct ieee80211_local *local = hw_to_local(hw);
  411. unsigned long flags;
  412. int ret;
  413. if (WARN_ON(queue >= hw->queues))
  414. return true;
  415. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  416. ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
  417. &local->queue_stop_reasons[queue]);
  418. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  419. return ret;
  420. }
  421. EXPORT_SYMBOL(ieee80211_queue_stopped);
  422. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  423. unsigned long queues,
  424. enum queue_stop_reason reason)
  425. {
  426. struct ieee80211_local *local = hw_to_local(hw);
  427. unsigned long flags;
  428. int i;
  429. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  430. for_each_set_bit(i, &queues, hw->queues)
  431. __ieee80211_wake_queue(hw, i, reason);
  432. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  433. }
  434. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  435. {
  436. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  437. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  438. }
  439. EXPORT_SYMBOL(ieee80211_wake_queues);
  440. void ieee80211_flush_queues(struct ieee80211_local *local,
  441. struct ieee80211_sub_if_data *sdata)
  442. {
  443. u32 queues;
  444. if (!local->ops->flush)
  445. return;
  446. if (sdata && local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
  447. int ac;
  448. queues = 0;
  449. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  450. queues |= BIT(sdata->vif.hw_queue[ac]);
  451. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
  452. queues |= BIT(sdata->vif.cab_queue);
  453. } else {
  454. /* all queues */
  455. queues = BIT(local->hw.queues) - 1;
  456. }
  457. ieee80211_stop_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
  458. IEEE80211_QUEUE_STOP_REASON_FLUSH);
  459. drv_flush(local, queues, false);
  460. ieee80211_wake_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
  461. IEEE80211_QUEUE_STOP_REASON_FLUSH);
  462. }
  463. static void __iterate_active_interfaces(struct ieee80211_local *local,
  464. u32 iter_flags,
  465. void (*iterator)(void *data, u8 *mac,
  466. struct ieee80211_vif *vif),
  467. void *data)
  468. {
  469. struct ieee80211_sub_if_data *sdata;
  470. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  471. switch (sdata->vif.type) {
  472. case NL80211_IFTYPE_MONITOR:
  473. if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  474. continue;
  475. break;
  476. case NL80211_IFTYPE_AP_VLAN:
  477. continue;
  478. default:
  479. break;
  480. }
  481. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  482. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  483. continue;
  484. if (ieee80211_sdata_running(sdata))
  485. iterator(data, sdata->vif.addr,
  486. &sdata->vif);
  487. }
  488. sdata = rcu_dereference_check(local->monitor_sdata,
  489. lockdep_is_held(&local->iflist_mtx) ||
  490. lockdep_rtnl_is_held());
  491. if (sdata &&
  492. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
  493. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  494. iterator(data, sdata->vif.addr, &sdata->vif);
  495. }
  496. void ieee80211_iterate_active_interfaces(
  497. struct ieee80211_hw *hw, u32 iter_flags,
  498. void (*iterator)(void *data, u8 *mac,
  499. struct ieee80211_vif *vif),
  500. void *data)
  501. {
  502. struct ieee80211_local *local = hw_to_local(hw);
  503. mutex_lock(&local->iflist_mtx);
  504. __iterate_active_interfaces(local, iter_flags, iterator, data);
  505. mutex_unlock(&local->iflist_mtx);
  506. }
  507. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  508. void ieee80211_iterate_active_interfaces_atomic(
  509. struct ieee80211_hw *hw, u32 iter_flags,
  510. void (*iterator)(void *data, u8 *mac,
  511. struct ieee80211_vif *vif),
  512. void *data)
  513. {
  514. struct ieee80211_local *local = hw_to_local(hw);
  515. rcu_read_lock();
  516. __iterate_active_interfaces(local, iter_flags, iterator, data);
  517. rcu_read_unlock();
  518. }
  519. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  520. void ieee80211_iterate_active_interfaces_rtnl(
  521. struct ieee80211_hw *hw, u32 iter_flags,
  522. void (*iterator)(void *data, u8 *mac,
  523. struct ieee80211_vif *vif),
  524. void *data)
  525. {
  526. struct ieee80211_local *local = hw_to_local(hw);
  527. ASSERT_RTNL();
  528. __iterate_active_interfaces(local, iter_flags, iterator, data);
  529. }
  530. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
  531. struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
  532. {
  533. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  534. if (!ieee80211_sdata_running(sdata) ||
  535. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  536. return NULL;
  537. return &sdata->vif;
  538. }
  539. EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
  540. /*
  541. * Nothing should have been stuffed into the workqueue during
  542. * the suspend->resume cycle. If this WARN is seen then there
  543. * is a bug with either the driver suspend or something in
  544. * mac80211 stuffing into the workqueue which we haven't yet
  545. * cleared during mac80211's suspend cycle.
  546. */
  547. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  548. {
  549. if (WARN(local->suspended && !local->resuming,
  550. "queueing ieee80211 work while going to suspend\n"))
  551. return false;
  552. return true;
  553. }
  554. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  555. {
  556. struct ieee80211_local *local = hw_to_local(hw);
  557. if (!ieee80211_can_queue_work(local))
  558. return;
  559. queue_work(local->workqueue, work);
  560. }
  561. EXPORT_SYMBOL(ieee80211_queue_work);
  562. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  563. struct delayed_work *dwork,
  564. unsigned long delay)
  565. {
  566. struct ieee80211_local *local = hw_to_local(hw);
  567. if (!ieee80211_can_queue_work(local))
  568. return;
  569. queue_delayed_work(local->workqueue, dwork, delay);
  570. }
  571. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  572. u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
  573. struct ieee802_11_elems *elems,
  574. u64 filter, u32 crc)
  575. {
  576. size_t left = len;
  577. const u8 *pos = start;
  578. bool calc_crc = filter != 0;
  579. DECLARE_BITMAP(seen_elems, 256);
  580. const u8 *ie;
  581. bitmap_zero(seen_elems, 256);
  582. memset(elems, 0, sizeof(*elems));
  583. elems->ie_start = start;
  584. elems->total_len = len;
  585. while (left >= 2) {
  586. u8 id, elen;
  587. bool elem_parse_failed;
  588. id = *pos++;
  589. elen = *pos++;
  590. left -= 2;
  591. if (elen > left) {
  592. elems->parse_error = true;
  593. break;
  594. }
  595. switch (id) {
  596. case WLAN_EID_SSID:
  597. case WLAN_EID_SUPP_RATES:
  598. case WLAN_EID_FH_PARAMS:
  599. case WLAN_EID_DS_PARAMS:
  600. case WLAN_EID_CF_PARAMS:
  601. case WLAN_EID_TIM:
  602. case WLAN_EID_IBSS_PARAMS:
  603. case WLAN_EID_CHALLENGE:
  604. case WLAN_EID_RSN:
  605. case WLAN_EID_ERP_INFO:
  606. case WLAN_EID_EXT_SUPP_RATES:
  607. case WLAN_EID_HT_CAPABILITY:
  608. case WLAN_EID_HT_OPERATION:
  609. case WLAN_EID_VHT_CAPABILITY:
  610. case WLAN_EID_VHT_OPERATION:
  611. case WLAN_EID_MESH_ID:
  612. case WLAN_EID_MESH_CONFIG:
  613. case WLAN_EID_PEER_MGMT:
  614. case WLAN_EID_PREQ:
  615. case WLAN_EID_PREP:
  616. case WLAN_EID_PERR:
  617. case WLAN_EID_RANN:
  618. case WLAN_EID_CHANNEL_SWITCH:
  619. case WLAN_EID_EXT_CHANSWITCH_ANN:
  620. case WLAN_EID_COUNTRY:
  621. case WLAN_EID_PWR_CONSTRAINT:
  622. case WLAN_EID_TIMEOUT_INTERVAL:
  623. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  624. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  625. case WLAN_EID_CHAN_SWITCH_PARAM:
  626. /*
  627. * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
  628. * that if the content gets bigger it might be needed more than once
  629. */
  630. if (test_bit(id, seen_elems)) {
  631. elems->parse_error = true;
  632. left -= elen;
  633. pos += elen;
  634. continue;
  635. }
  636. break;
  637. }
  638. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  639. crc = crc32_be(crc, pos - 2, elen + 2);
  640. elem_parse_failed = false;
  641. switch (id) {
  642. case WLAN_EID_SSID:
  643. elems->ssid = pos;
  644. elems->ssid_len = elen;
  645. break;
  646. case WLAN_EID_SUPP_RATES:
  647. elems->supp_rates = pos;
  648. elems->supp_rates_len = elen;
  649. break;
  650. case WLAN_EID_DS_PARAMS:
  651. if (elen >= 1)
  652. elems->ds_params = pos;
  653. else
  654. elem_parse_failed = true;
  655. break;
  656. case WLAN_EID_TIM:
  657. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  658. elems->tim = (void *)pos;
  659. elems->tim_len = elen;
  660. } else
  661. elem_parse_failed = true;
  662. break;
  663. case WLAN_EID_CHALLENGE:
  664. elems->challenge = pos;
  665. elems->challenge_len = elen;
  666. break;
  667. case WLAN_EID_VENDOR_SPECIFIC:
  668. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  669. pos[2] == 0xf2) {
  670. /* Microsoft OUI (00:50:F2) */
  671. if (calc_crc)
  672. crc = crc32_be(crc, pos - 2, elen + 2);
  673. if (elen >= 5 && pos[3] == 2) {
  674. /* OUI Type 2 - WMM IE */
  675. if (pos[4] == 0) {
  676. elems->wmm_info = pos;
  677. elems->wmm_info_len = elen;
  678. } else if (pos[4] == 1) {
  679. elems->wmm_param = pos;
  680. elems->wmm_param_len = elen;
  681. }
  682. }
  683. }
  684. break;
  685. case WLAN_EID_RSN:
  686. elems->rsn = pos;
  687. elems->rsn_len = elen;
  688. break;
  689. case WLAN_EID_ERP_INFO:
  690. if (elen >= 1)
  691. elems->erp_info = pos;
  692. else
  693. elem_parse_failed = true;
  694. break;
  695. case WLAN_EID_EXT_SUPP_RATES:
  696. elems->ext_supp_rates = pos;
  697. elems->ext_supp_rates_len = elen;
  698. break;
  699. case WLAN_EID_HT_CAPABILITY:
  700. if (elen >= sizeof(struct ieee80211_ht_cap))
  701. elems->ht_cap_elem = (void *)pos;
  702. else
  703. elem_parse_failed = true;
  704. break;
  705. case WLAN_EID_HT_OPERATION:
  706. if (elen >= sizeof(struct ieee80211_ht_operation))
  707. elems->ht_operation = (void *)pos;
  708. else
  709. elem_parse_failed = true;
  710. break;
  711. case WLAN_EID_VHT_CAPABILITY:
  712. if (elen >= sizeof(struct ieee80211_vht_cap))
  713. elems->vht_cap_elem = (void *)pos;
  714. else
  715. elem_parse_failed = true;
  716. break;
  717. case WLAN_EID_VHT_OPERATION:
  718. if (elen >= sizeof(struct ieee80211_vht_operation))
  719. elems->vht_operation = (void *)pos;
  720. else
  721. elem_parse_failed = true;
  722. break;
  723. case WLAN_EID_OPMODE_NOTIF:
  724. if (elen > 0)
  725. elems->opmode_notif = pos;
  726. else
  727. elem_parse_failed = true;
  728. break;
  729. case WLAN_EID_MESH_ID:
  730. elems->mesh_id = pos;
  731. elems->mesh_id_len = elen;
  732. break;
  733. case WLAN_EID_MESH_CONFIG:
  734. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  735. elems->mesh_config = (void *)pos;
  736. else
  737. elem_parse_failed = true;
  738. break;
  739. case WLAN_EID_PEER_MGMT:
  740. elems->peering = pos;
  741. elems->peering_len = elen;
  742. break;
  743. case WLAN_EID_MESH_AWAKE_WINDOW:
  744. if (elen >= 2)
  745. elems->awake_window = (void *)pos;
  746. break;
  747. case WLAN_EID_PREQ:
  748. elems->preq = pos;
  749. elems->preq_len = elen;
  750. break;
  751. case WLAN_EID_PREP:
  752. elems->prep = pos;
  753. elems->prep_len = elen;
  754. break;
  755. case WLAN_EID_PERR:
  756. elems->perr = pos;
  757. elems->perr_len = elen;
  758. break;
  759. case WLAN_EID_RANN:
  760. if (elen >= sizeof(struct ieee80211_rann_ie))
  761. elems->rann = (void *)pos;
  762. else
  763. elem_parse_failed = true;
  764. break;
  765. case WLAN_EID_CHANNEL_SWITCH:
  766. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  767. elem_parse_failed = true;
  768. break;
  769. }
  770. elems->ch_switch_ie = (void *)pos;
  771. break;
  772. case WLAN_EID_EXT_CHANSWITCH_ANN:
  773. if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
  774. elem_parse_failed = true;
  775. break;
  776. }
  777. elems->ext_chansw_ie = (void *)pos;
  778. break;
  779. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  780. if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
  781. elem_parse_failed = true;
  782. break;
  783. }
  784. elems->sec_chan_offs = (void *)pos;
  785. break;
  786. case WLAN_EID_CHAN_SWITCH_PARAM:
  787. if (elen !=
  788. sizeof(*elems->mesh_chansw_params_ie)) {
  789. elem_parse_failed = true;
  790. break;
  791. }
  792. elems->mesh_chansw_params_ie = (void *)pos;
  793. break;
  794. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  795. if (!action ||
  796. elen != sizeof(*elems->wide_bw_chansw_ie)) {
  797. elem_parse_failed = true;
  798. break;
  799. }
  800. elems->wide_bw_chansw_ie = (void *)pos;
  801. break;
  802. case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
  803. if (action) {
  804. elem_parse_failed = true;
  805. break;
  806. }
  807. /*
  808. * This is a bit tricky, but as we only care about
  809. * the wide bandwidth channel switch element, so
  810. * just parse it out manually.
  811. */
  812. ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
  813. pos, elen);
  814. if (ie) {
  815. if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
  816. elems->wide_bw_chansw_ie =
  817. (void *)(ie + 2);
  818. else
  819. elem_parse_failed = true;
  820. }
  821. break;
  822. case WLAN_EID_COUNTRY:
  823. elems->country_elem = pos;
  824. elems->country_elem_len = elen;
  825. break;
  826. case WLAN_EID_PWR_CONSTRAINT:
  827. if (elen != 1) {
  828. elem_parse_failed = true;
  829. break;
  830. }
  831. elems->pwr_constr_elem = pos;
  832. break;
  833. case WLAN_EID_TIMEOUT_INTERVAL:
  834. if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
  835. elems->timeout_int = (void *)pos;
  836. else
  837. elem_parse_failed = true;
  838. break;
  839. default:
  840. break;
  841. }
  842. if (elem_parse_failed)
  843. elems->parse_error = true;
  844. else
  845. __set_bit(id, seen_elems);
  846. left -= elen;
  847. pos += elen;
  848. }
  849. if (left != 0)
  850. elems->parse_error = true;
  851. return crc;
  852. }
  853. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  854. bool bss_notify)
  855. {
  856. struct ieee80211_local *local = sdata->local;
  857. struct ieee80211_tx_queue_params qparam;
  858. struct ieee80211_chanctx_conf *chanctx_conf;
  859. int ac;
  860. bool use_11b, enable_qos;
  861. int aCWmin, aCWmax;
  862. if (!local->ops->conf_tx)
  863. return;
  864. if (local->hw.queues < IEEE80211_NUM_ACS)
  865. return;
  866. memset(&qparam, 0, sizeof(qparam));
  867. rcu_read_lock();
  868. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  869. use_11b = (chanctx_conf &&
  870. chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ) &&
  871. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  872. rcu_read_unlock();
  873. /*
  874. * By default disable QoS in STA mode for old access points, which do
  875. * not support 802.11e. New APs will provide proper queue parameters,
  876. * that we will configure later.
  877. */
  878. enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
  879. /* Set defaults according to 802.11-2007 Table 7-37 */
  880. aCWmax = 1023;
  881. if (use_11b)
  882. aCWmin = 31;
  883. else
  884. aCWmin = 15;
  885. /* Confiure old 802.11b/g medium access rules. */
  886. qparam.cw_max = aCWmax;
  887. qparam.cw_min = aCWmin;
  888. qparam.txop = 0;
  889. qparam.aifs = 2;
  890. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  891. /* Update if QoS is enabled. */
  892. if (enable_qos) {
  893. switch (ac) {
  894. case IEEE80211_AC_BK:
  895. qparam.cw_max = aCWmax;
  896. qparam.cw_min = aCWmin;
  897. qparam.txop = 0;
  898. qparam.aifs = 7;
  899. break;
  900. /* never happens but let's not leave undefined */
  901. default:
  902. case IEEE80211_AC_BE:
  903. qparam.cw_max = aCWmax;
  904. qparam.cw_min = aCWmin;
  905. qparam.txop = 0;
  906. qparam.aifs = 3;
  907. break;
  908. case IEEE80211_AC_VI:
  909. qparam.cw_max = aCWmin;
  910. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  911. if (use_11b)
  912. qparam.txop = 6016/32;
  913. else
  914. qparam.txop = 3008/32;
  915. qparam.aifs = 2;
  916. break;
  917. case IEEE80211_AC_VO:
  918. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  919. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  920. if (use_11b)
  921. qparam.txop = 3264/32;
  922. else
  923. qparam.txop = 1504/32;
  924. qparam.aifs = 2;
  925. break;
  926. }
  927. }
  928. qparam.uapsd = false;
  929. sdata->tx_conf[ac] = qparam;
  930. drv_conf_tx(local, sdata, ac, &qparam);
  931. }
  932. if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  933. sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) {
  934. sdata->vif.bss_conf.qos = enable_qos;
  935. if (bss_notify)
  936. ieee80211_bss_info_change_notify(sdata,
  937. BSS_CHANGED_QOS);
  938. }
  939. }
  940. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  941. u16 transaction, u16 auth_alg, u16 status,
  942. const u8 *extra, size_t extra_len, const u8 *da,
  943. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
  944. u32 tx_flags)
  945. {
  946. struct ieee80211_local *local = sdata->local;
  947. struct sk_buff *skb;
  948. struct ieee80211_mgmt *mgmt;
  949. int err;
  950. /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
  951. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24 + 6 + extra_len);
  952. if (!skb)
  953. return;
  954. skb_reserve(skb, local->hw.extra_tx_headroom);
  955. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  956. memset(mgmt, 0, 24 + 6);
  957. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  958. IEEE80211_STYPE_AUTH);
  959. memcpy(mgmt->da, da, ETH_ALEN);
  960. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  961. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  962. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  963. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  964. mgmt->u.auth.status_code = cpu_to_le16(status);
  965. if (extra)
  966. memcpy(skb_put(skb, extra_len), extra, extra_len);
  967. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  968. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  969. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  970. WARN_ON(err);
  971. }
  972. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  973. tx_flags;
  974. ieee80211_tx_skb(sdata, skb);
  975. }
  976. void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  977. const u8 *bssid, u16 stype, u16 reason,
  978. bool send_frame, u8 *frame_buf)
  979. {
  980. struct ieee80211_local *local = sdata->local;
  981. struct sk_buff *skb;
  982. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  983. /* build frame */
  984. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  985. mgmt->duration = 0; /* initialize only */
  986. mgmt->seq_ctrl = 0; /* initialize only */
  987. memcpy(mgmt->da, bssid, ETH_ALEN);
  988. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  989. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  990. /* u.deauth.reason_code == u.disassoc.reason_code */
  991. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  992. if (send_frame) {
  993. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  994. IEEE80211_DEAUTH_FRAME_LEN);
  995. if (!skb)
  996. return;
  997. skb_reserve(skb, local->hw.extra_tx_headroom);
  998. /* copy in frame */
  999. memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
  1000. mgmt, IEEE80211_DEAUTH_FRAME_LEN);
  1001. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  1002. !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
  1003. IEEE80211_SKB_CB(skb)->flags |=
  1004. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1005. ieee80211_tx_skb(sdata, skb);
  1006. }
  1007. }
  1008. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  1009. size_t buffer_len, const u8 *ie, size_t ie_len,
  1010. enum ieee80211_band band, u32 rate_mask,
  1011. struct cfg80211_chan_def *chandef)
  1012. {
  1013. struct ieee80211_supported_band *sband;
  1014. u8 *pos = buffer, *end = buffer + buffer_len;
  1015. size_t offset = 0, noffset;
  1016. int supp_rates_len, i;
  1017. u8 rates[32];
  1018. int num_rates;
  1019. int ext_rates_len;
  1020. int shift;
  1021. u32 rate_flags;
  1022. sband = local->hw.wiphy->bands[band];
  1023. if (WARN_ON_ONCE(!sband))
  1024. return 0;
  1025. rate_flags = ieee80211_chandef_rate_flags(chandef);
  1026. shift = ieee80211_chandef_get_shift(chandef);
  1027. num_rates = 0;
  1028. for (i = 0; i < sband->n_bitrates; i++) {
  1029. if ((BIT(i) & rate_mask) == 0)
  1030. continue; /* skip rate */
  1031. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1032. continue;
  1033. rates[num_rates++] =
  1034. (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1035. (1 << shift) * 5);
  1036. }
  1037. supp_rates_len = min_t(int, num_rates, 8);
  1038. if (end - pos < 2 + supp_rates_len)
  1039. goto out_err;
  1040. *pos++ = WLAN_EID_SUPP_RATES;
  1041. *pos++ = supp_rates_len;
  1042. memcpy(pos, rates, supp_rates_len);
  1043. pos += supp_rates_len;
  1044. /* insert "request information" if in custom IEs */
  1045. if (ie && ie_len) {
  1046. static const u8 before_extrates[] = {
  1047. WLAN_EID_SSID,
  1048. WLAN_EID_SUPP_RATES,
  1049. WLAN_EID_REQUEST,
  1050. };
  1051. noffset = ieee80211_ie_split(ie, ie_len,
  1052. before_extrates,
  1053. ARRAY_SIZE(before_extrates),
  1054. offset);
  1055. if (end - pos < noffset - offset)
  1056. goto out_err;
  1057. memcpy(pos, ie + offset, noffset - offset);
  1058. pos += noffset - offset;
  1059. offset = noffset;
  1060. }
  1061. ext_rates_len = num_rates - supp_rates_len;
  1062. if (ext_rates_len > 0) {
  1063. if (end - pos < 2 + ext_rates_len)
  1064. goto out_err;
  1065. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1066. *pos++ = ext_rates_len;
  1067. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  1068. pos += ext_rates_len;
  1069. }
  1070. if (chandef->chan && sband->band == IEEE80211_BAND_2GHZ) {
  1071. if (end - pos < 3)
  1072. goto out_err;
  1073. *pos++ = WLAN_EID_DS_PARAMS;
  1074. *pos++ = 1;
  1075. *pos++ = ieee80211_frequency_to_channel(
  1076. chandef->chan->center_freq);
  1077. }
  1078. /* insert custom IEs that go before HT */
  1079. if (ie && ie_len) {
  1080. static const u8 before_ht[] = {
  1081. WLAN_EID_SSID,
  1082. WLAN_EID_SUPP_RATES,
  1083. WLAN_EID_REQUEST,
  1084. WLAN_EID_EXT_SUPP_RATES,
  1085. WLAN_EID_DS_PARAMS,
  1086. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1087. };
  1088. noffset = ieee80211_ie_split(ie, ie_len,
  1089. before_ht, ARRAY_SIZE(before_ht),
  1090. offset);
  1091. if (end - pos < noffset - offset)
  1092. goto out_err;
  1093. memcpy(pos, ie + offset, noffset - offset);
  1094. pos += noffset - offset;
  1095. offset = noffset;
  1096. }
  1097. if (sband->ht_cap.ht_supported) {
  1098. if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
  1099. goto out_err;
  1100. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  1101. sband->ht_cap.cap);
  1102. }
  1103. /*
  1104. * If adding more here, adjust code in main.c
  1105. * that calculates local->scan_ies_len.
  1106. */
  1107. /* insert custom IEs that go before VHT */
  1108. if (ie && ie_len) {
  1109. static const u8 before_vht[] = {
  1110. WLAN_EID_SSID,
  1111. WLAN_EID_SUPP_RATES,
  1112. WLAN_EID_REQUEST,
  1113. WLAN_EID_EXT_SUPP_RATES,
  1114. WLAN_EID_DS_PARAMS,
  1115. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1116. WLAN_EID_HT_CAPABILITY,
  1117. WLAN_EID_BSS_COEX_2040,
  1118. WLAN_EID_EXT_CAPABILITY,
  1119. WLAN_EID_SSID_LIST,
  1120. WLAN_EID_CHANNEL_USAGE,
  1121. WLAN_EID_INTERWORKING,
  1122. /* mesh ID can't happen here */
  1123. /* 60 GHz can't happen here right now */
  1124. };
  1125. noffset = ieee80211_ie_split(ie, ie_len,
  1126. before_vht, ARRAY_SIZE(before_vht),
  1127. offset);
  1128. if (end - pos < noffset - offset)
  1129. goto out_err;
  1130. memcpy(pos, ie + offset, noffset - offset);
  1131. pos += noffset - offset;
  1132. offset = noffset;
  1133. }
  1134. if (sband->vht_cap.vht_supported) {
  1135. if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
  1136. goto out_err;
  1137. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  1138. sband->vht_cap.cap);
  1139. }
  1140. /* add any remaining custom IEs */
  1141. if (ie && ie_len) {
  1142. noffset = ie_len;
  1143. if (end - pos < noffset - offset)
  1144. goto out_err;
  1145. memcpy(pos, ie + offset, noffset - offset);
  1146. pos += noffset - offset;
  1147. }
  1148. return pos - buffer;
  1149. out_err:
  1150. WARN_ONCE(1, "not enough space for preq IEs\n");
  1151. return pos - buffer;
  1152. }
  1153. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  1154. u8 *dst, u32 ratemask,
  1155. struct ieee80211_channel *chan,
  1156. const u8 *ssid, size_t ssid_len,
  1157. const u8 *ie, size_t ie_len,
  1158. bool directed)
  1159. {
  1160. struct ieee80211_local *local = sdata->local;
  1161. struct cfg80211_chan_def chandef;
  1162. struct sk_buff *skb;
  1163. struct ieee80211_mgmt *mgmt;
  1164. int ies_len;
  1165. /*
  1166. * Do not send DS Channel parameter for directed probe requests
  1167. * in order to maximize the chance that we get a response. Some
  1168. * badly-behaved APs don't respond when this parameter is included.
  1169. */
  1170. chandef.width = sdata->vif.bss_conf.chandef.width;
  1171. if (directed)
  1172. chandef.chan = NULL;
  1173. else
  1174. chandef.chan = chan;
  1175. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  1176. ssid, ssid_len, 100 + ie_len);
  1177. if (!skb)
  1178. return NULL;
  1179. ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
  1180. skb_tailroom(skb),
  1181. ie, ie_len, chan->band,
  1182. ratemask, &chandef);
  1183. skb_put(skb, ies_len);
  1184. if (dst) {
  1185. mgmt = (struct ieee80211_mgmt *) skb->data;
  1186. memcpy(mgmt->da, dst, ETH_ALEN);
  1187. memcpy(mgmt->bssid, dst, ETH_ALEN);
  1188. }
  1189. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1190. return skb;
  1191. }
  1192. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  1193. const u8 *ssid, size_t ssid_len,
  1194. const u8 *ie, size_t ie_len,
  1195. u32 ratemask, bool directed, u32 tx_flags,
  1196. struct ieee80211_channel *channel, bool scan)
  1197. {
  1198. struct sk_buff *skb;
  1199. skb = ieee80211_build_probe_req(sdata, dst, ratemask, channel,
  1200. ssid, ssid_len,
  1201. ie, ie_len, directed);
  1202. if (skb) {
  1203. IEEE80211_SKB_CB(skb)->flags |= tx_flags;
  1204. if (scan)
  1205. ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
  1206. else
  1207. ieee80211_tx_skb(sdata, skb);
  1208. }
  1209. }
  1210. u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
  1211. struct ieee802_11_elems *elems,
  1212. enum ieee80211_band band, u32 *basic_rates)
  1213. {
  1214. struct ieee80211_supported_band *sband;
  1215. size_t num_rates;
  1216. u32 supp_rates, rate_flags;
  1217. int i, j, shift;
  1218. sband = sdata->local->hw.wiphy->bands[band];
  1219. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1220. shift = ieee80211_vif_get_shift(&sdata->vif);
  1221. if (WARN_ON(!sband))
  1222. return 1;
  1223. num_rates = sband->n_bitrates;
  1224. supp_rates = 0;
  1225. for (i = 0; i < elems->supp_rates_len +
  1226. elems->ext_supp_rates_len; i++) {
  1227. u8 rate = 0;
  1228. int own_rate;
  1229. bool is_basic;
  1230. if (i < elems->supp_rates_len)
  1231. rate = elems->supp_rates[i];
  1232. else if (elems->ext_supp_rates)
  1233. rate = elems->ext_supp_rates
  1234. [i - elems->supp_rates_len];
  1235. own_rate = 5 * (rate & 0x7f);
  1236. is_basic = !!(rate & 0x80);
  1237. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1238. continue;
  1239. for (j = 0; j < num_rates; j++) {
  1240. int brate;
  1241. if ((rate_flags & sband->bitrates[j].flags)
  1242. != rate_flags)
  1243. continue;
  1244. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  1245. 1 << shift);
  1246. if (brate == own_rate) {
  1247. supp_rates |= BIT(j);
  1248. if (basic_rates && is_basic)
  1249. *basic_rates |= BIT(j);
  1250. }
  1251. }
  1252. }
  1253. return supp_rates;
  1254. }
  1255. void ieee80211_stop_device(struct ieee80211_local *local)
  1256. {
  1257. ieee80211_led_radio(local, false);
  1258. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1259. cancel_work_sync(&local->reconfig_filter);
  1260. flush_workqueue(local->workqueue);
  1261. drv_stop(local);
  1262. }
  1263. static void ieee80211_assign_chanctx(struct ieee80211_local *local,
  1264. struct ieee80211_sub_if_data *sdata)
  1265. {
  1266. struct ieee80211_chanctx_conf *conf;
  1267. struct ieee80211_chanctx *ctx;
  1268. if (!local->use_chanctx)
  1269. return;
  1270. mutex_lock(&local->chanctx_mtx);
  1271. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1272. lockdep_is_held(&local->chanctx_mtx));
  1273. if (conf) {
  1274. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1275. drv_assign_vif_chanctx(local, sdata, ctx);
  1276. }
  1277. mutex_unlock(&local->chanctx_mtx);
  1278. }
  1279. int ieee80211_reconfig(struct ieee80211_local *local)
  1280. {
  1281. struct ieee80211_hw *hw = &local->hw;
  1282. struct ieee80211_sub_if_data *sdata;
  1283. struct ieee80211_chanctx *ctx;
  1284. struct sta_info *sta;
  1285. int res, i;
  1286. bool reconfig_due_to_wowlan = false;
  1287. struct ieee80211_sub_if_data *sched_scan_sdata;
  1288. bool sched_scan_stopped = false;
  1289. #ifdef CONFIG_PM
  1290. if (local->suspended)
  1291. local->resuming = true;
  1292. if (local->wowlan) {
  1293. res = drv_resume(local);
  1294. local->wowlan = false;
  1295. if (res < 0) {
  1296. local->resuming = false;
  1297. return res;
  1298. }
  1299. if (res == 0)
  1300. goto wake_up;
  1301. WARN_ON(res > 1);
  1302. /*
  1303. * res is 1, which means the driver requested
  1304. * to go through a regular reset on wakeup.
  1305. */
  1306. reconfig_due_to_wowlan = true;
  1307. }
  1308. #endif
  1309. /* everything else happens only if HW was up & running */
  1310. if (!local->open_count)
  1311. goto wake_up;
  1312. /*
  1313. * Upon resume hardware can sometimes be goofy due to
  1314. * various platform / driver / bus issues, so restarting
  1315. * the device may at times not work immediately. Propagate
  1316. * the error.
  1317. */
  1318. res = drv_start(local);
  1319. if (res) {
  1320. WARN(local->suspended, "Hardware became unavailable "
  1321. "upon resume. This could be a software issue "
  1322. "prior to suspend or a hardware issue.\n");
  1323. return res;
  1324. }
  1325. /* setup fragmentation threshold */
  1326. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1327. /* setup RTS threshold */
  1328. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1329. /* reset coverage class */
  1330. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1331. ieee80211_led_radio(local, true);
  1332. ieee80211_mod_tpt_led_trig(local,
  1333. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1334. /* add interfaces */
  1335. sdata = rtnl_dereference(local->monitor_sdata);
  1336. if (sdata) {
  1337. /* in HW restart it exists already */
  1338. WARN_ON(local->resuming);
  1339. res = drv_add_interface(local, sdata);
  1340. if (WARN_ON(res)) {
  1341. rcu_assign_pointer(local->monitor_sdata, NULL);
  1342. synchronize_net();
  1343. kfree(sdata);
  1344. }
  1345. }
  1346. list_for_each_entry(sdata, &local->interfaces, list) {
  1347. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1348. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1349. ieee80211_sdata_running(sdata))
  1350. res = drv_add_interface(local, sdata);
  1351. }
  1352. /* add channel contexts */
  1353. if (local->use_chanctx) {
  1354. mutex_lock(&local->chanctx_mtx);
  1355. list_for_each_entry(ctx, &local->chanctx_list, list)
  1356. WARN_ON(drv_add_chanctx(local, ctx));
  1357. mutex_unlock(&local->chanctx_mtx);
  1358. }
  1359. list_for_each_entry(sdata, &local->interfaces, list) {
  1360. if (!ieee80211_sdata_running(sdata))
  1361. continue;
  1362. ieee80211_assign_chanctx(local, sdata);
  1363. }
  1364. sdata = rtnl_dereference(local->monitor_sdata);
  1365. if (sdata && ieee80211_sdata_running(sdata))
  1366. ieee80211_assign_chanctx(local, sdata);
  1367. /* add STAs back */
  1368. mutex_lock(&local->sta_mtx);
  1369. list_for_each_entry(sta, &local->sta_list, list) {
  1370. enum ieee80211_sta_state state;
  1371. if (!sta->uploaded)
  1372. continue;
  1373. /* AP-mode stations will be added later */
  1374. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1375. continue;
  1376. for (state = IEEE80211_STA_NOTEXIST;
  1377. state < sta->sta_state; state++)
  1378. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1379. state + 1));
  1380. }
  1381. mutex_unlock(&local->sta_mtx);
  1382. /* reconfigure tx conf */
  1383. if (hw->queues >= IEEE80211_NUM_ACS) {
  1384. list_for_each_entry(sdata, &local->interfaces, list) {
  1385. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1386. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1387. !ieee80211_sdata_running(sdata))
  1388. continue;
  1389. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1390. drv_conf_tx(local, sdata, i,
  1391. &sdata->tx_conf[i]);
  1392. }
  1393. }
  1394. /* reconfigure hardware */
  1395. ieee80211_hw_config(local, ~0);
  1396. ieee80211_configure_filter(local);
  1397. /* Finally also reconfigure all the BSS information */
  1398. list_for_each_entry(sdata, &local->interfaces, list) {
  1399. u32 changed;
  1400. if (!ieee80211_sdata_running(sdata))
  1401. continue;
  1402. /* common change flags for all interface types */
  1403. changed = BSS_CHANGED_ERP_CTS_PROT |
  1404. BSS_CHANGED_ERP_PREAMBLE |
  1405. BSS_CHANGED_ERP_SLOT |
  1406. BSS_CHANGED_HT |
  1407. BSS_CHANGED_BASIC_RATES |
  1408. BSS_CHANGED_BEACON_INT |
  1409. BSS_CHANGED_BSSID |
  1410. BSS_CHANGED_CQM |
  1411. BSS_CHANGED_QOS |
  1412. BSS_CHANGED_IDLE |
  1413. BSS_CHANGED_TXPOWER;
  1414. switch (sdata->vif.type) {
  1415. case NL80211_IFTYPE_STATION:
  1416. changed |= BSS_CHANGED_ASSOC |
  1417. BSS_CHANGED_ARP_FILTER |
  1418. BSS_CHANGED_PS;
  1419. /* Re-send beacon info report to the driver */
  1420. if (sdata->u.mgd.have_beacon)
  1421. changed |= BSS_CHANGED_BEACON_INFO;
  1422. sdata_lock(sdata);
  1423. ieee80211_bss_info_change_notify(sdata, changed);
  1424. sdata_unlock(sdata);
  1425. break;
  1426. case NL80211_IFTYPE_ADHOC:
  1427. changed |= BSS_CHANGED_IBSS;
  1428. /* fall through */
  1429. case NL80211_IFTYPE_AP:
  1430. changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
  1431. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1432. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1433. if (rcu_access_pointer(sdata->u.ap.beacon))
  1434. drv_start_ap(local, sdata);
  1435. }
  1436. /* fall through */
  1437. case NL80211_IFTYPE_MESH_POINT:
  1438. if (sdata->vif.bss_conf.enable_beacon) {
  1439. changed |= BSS_CHANGED_BEACON |
  1440. BSS_CHANGED_BEACON_ENABLED;
  1441. ieee80211_bss_info_change_notify(sdata, changed);
  1442. }
  1443. break;
  1444. case NL80211_IFTYPE_WDS:
  1445. break;
  1446. case NL80211_IFTYPE_AP_VLAN:
  1447. case NL80211_IFTYPE_MONITOR:
  1448. /* ignore virtual */
  1449. break;
  1450. case NL80211_IFTYPE_P2P_DEVICE:
  1451. changed = BSS_CHANGED_IDLE;
  1452. break;
  1453. case NL80211_IFTYPE_UNSPECIFIED:
  1454. case NUM_NL80211_IFTYPES:
  1455. case NL80211_IFTYPE_P2P_CLIENT:
  1456. case NL80211_IFTYPE_P2P_GO:
  1457. WARN_ON(1);
  1458. break;
  1459. }
  1460. }
  1461. ieee80211_recalc_ps(local, -1);
  1462. /*
  1463. * The sta might be in psm against the ap (e.g. because
  1464. * this was the state before a hw restart), so we
  1465. * explicitly send a null packet in order to make sure
  1466. * it'll sync against the ap (and get out of psm).
  1467. */
  1468. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1469. list_for_each_entry(sdata, &local->interfaces, list) {
  1470. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1471. continue;
  1472. if (!sdata->u.mgd.associated)
  1473. continue;
  1474. ieee80211_send_nullfunc(local, sdata, 0);
  1475. }
  1476. }
  1477. /* APs are now beaconing, add back stations */
  1478. mutex_lock(&local->sta_mtx);
  1479. list_for_each_entry(sta, &local->sta_list, list) {
  1480. enum ieee80211_sta_state state;
  1481. if (!sta->uploaded)
  1482. continue;
  1483. if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
  1484. continue;
  1485. for (state = IEEE80211_STA_NOTEXIST;
  1486. state < sta->sta_state; state++)
  1487. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1488. state + 1));
  1489. }
  1490. mutex_unlock(&local->sta_mtx);
  1491. /* add back keys */
  1492. list_for_each_entry(sdata, &local->interfaces, list)
  1493. if (ieee80211_sdata_running(sdata))
  1494. ieee80211_enable_keys(sdata);
  1495. wake_up:
  1496. local->in_reconfig = false;
  1497. barrier();
  1498. if (local->monitors == local->open_count && local->monitors > 0)
  1499. ieee80211_add_virtual_monitor(local);
  1500. /*
  1501. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1502. * sessions can be established after a resume.
  1503. *
  1504. * Also tear down aggregation sessions since reconfiguring
  1505. * them in a hardware restart scenario is not easily done
  1506. * right now, and the hardware will have lost information
  1507. * about the sessions, but we and the AP still think they
  1508. * are active. This is really a workaround though.
  1509. */
  1510. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  1511. mutex_lock(&local->sta_mtx);
  1512. list_for_each_entry(sta, &local->sta_list, list) {
  1513. ieee80211_sta_tear_down_BA_sessions(
  1514. sta, AGG_STOP_LOCAL_REQUEST);
  1515. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1516. }
  1517. mutex_unlock(&local->sta_mtx);
  1518. }
  1519. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  1520. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  1521. /*
  1522. * If this is for hw restart things are still running.
  1523. * We may want to change that later, however.
  1524. */
  1525. if (!local->suspended || reconfig_due_to_wowlan)
  1526. drv_restart_complete(local);
  1527. if (!local->suspended)
  1528. return 0;
  1529. #ifdef CONFIG_PM
  1530. /* first set suspended false, then resuming */
  1531. local->suspended = false;
  1532. mb();
  1533. local->resuming = false;
  1534. list_for_each_entry(sdata, &local->interfaces, list) {
  1535. if (!ieee80211_sdata_running(sdata))
  1536. continue;
  1537. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1538. ieee80211_sta_restart(sdata);
  1539. }
  1540. mod_timer(&local->sta_cleanup, jiffies + 1);
  1541. #else
  1542. WARN_ON(1);
  1543. #endif
  1544. /*
  1545. * Reconfigure sched scan if it was interrupted by FW restart or
  1546. * suspend.
  1547. */
  1548. mutex_lock(&local->mtx);
  1549. sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
  1550. lockdep_is_held(&local->mtx));
  1551. if (sched_scan_sdata && local->sched_scan_req)
  1552. /*
  1553. * Sched scan stopped, but we don't want to report it. Instead,
  1554. * we're trying to reschedule.
  1555. */
  1556. if (__ieee80211_request_sched_scan_start(sched_scan_sdata,
  1557. local->sched_scan_req))
  1558. sched_scan_stopped = true;
  1559. mutex_unlock(&local->mtx);
  1560. if (sched_scan_stopped)
  1561. cfg80211_sched_scan_stopped(local->hw.wiphy);
  1562. return 0;
  1563. }
  1564. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1565. {
  1566. struct ieee80211_sub_if_data *sdata;
  1567. struct ieee80211_local *local;
  1568. struct ieee80211_key *key;
  1569. if (WARN_ON(!vif))
  1570. return;
  1571. sdata = vif_to_sdata(vif);
  1572. local = sdata->local;
  1573. if (WARN_ON(!local->resuming))
  1574. return;
  1575. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1576. return;
  1577. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1578. mutex_lock(&local->key_mtx);
  1579. list_for_each_entry(key, &sdata->key_list, list)
  1580. key->flags |= KEY_FLAG_TAINTED;
  1581. mutex_unlock(&local->key_mtx);
  1582. }
  1583. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1584. void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
  1585. {
  1586. struct ieee80211_local *local = sdata->local;
  1587. struct ieee80211_chanctx_conf *chanctx_conf;
  1588. struct ieee80211_chanctx *chanctx;
  1589. mutex_lock(&local->chanctx_mtx);
  1590. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1591. lockdep_is_held(&local->chanctx_mtx));
  1592. if (WARN_ON_ONCE(!chanctx_conf))
  1593. goto unlock;
  1594. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1595. ieee80211_recalc_smps_chanctx(local, chanctx);
  1596. unlock:
  1597. mutex_unlock(&local->chanctx_mtx);
  1598. }
  1599. void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
  1600. {
  1601. struct ieee80211_local *local = sdata->local;
  1602. struct ieee80211_chanctx_conf *chanctx_conf;
  1603. struct ieee80211_chanctx *chanctx;
  1604. mutex_lock(&local->chanctx_mtx);
  1605. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1606. lockdep_is_held(&local->chanctx_mtx));
  1607. if (WARN_ON_ONCE(!chanctx_conf))
  1608. goto unlock;
  1609. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1610. ieee80211_recalc_chanctx_min_def(local, chanctx);
  1611. unlock:
  1612. mutex_unlock(&local->chanctx_mtx);
  1613. }
  1614. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1615. {
  1616. int i;
  1617. for (i = 0; i < n_ids; i++)
  1618. if (ids[i] == id)
  1619. return true;
  1620. return false;
  1621. }
  1622. /**
  1623. * ieee80211_ie_split - split an IE buffer according to ordering
  1624. *
  1625. * @ies: the IE buffer
  1626. * @ielen: the length of the IE buffer
  1627. * @ids: an array with element IDs that are allowed before
  1628. * the split
  1629. * @n_ids: the size of the element ID array
  1630. * @offset: offset where to start splitting in the buffer
  1631. *
  1632. * This function splits an IE buffer by updating the @offset
  1633. * variable to point to the location where the buffer should be
  1634. * split.
  1635. *
  1636. * It assumes that the given IE buffer is well-formed, this
  1637. * has to be guaranteed by the caller!
  1638. *
  1639. * It also assumes that the IEs in the buffer are ordered
  1640. * correctly, if not the result of using this function will not
  1641. * be ordered correctly either, i.e. it does no reordering.
  1642. *
  1643. * The function returns the offset where the next part of the
  1644. * buffer starts, which may be @ielen if the entire (remainder)
  1645. * of the buffer should be used.
  1646. */
  1647. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1648. const u8 *ids, int n_ids, size_t offset)
  1649. {
  1650. size_t pos = offset;
  1651. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1652. pos += 2 + ies[pos + 1];
  1653. return pos;
  1654. }
  1655. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1656. {
  1657. size_t pos = offset;
  1658. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1659. pos += 2 + ies[pos + 1];
  1660. return pos;
  1661. }
  1662. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1663. int rssi_min_thold,
  1664. int rssi_max_thold)
  1665. {
  1666. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1667. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1668. return;
  1669. /*
  1670. * Scale up threshold values before storing it, as the RSSI averaging
  1671. * algorithm uses a scaled up value as well. Change this scaling
  1672. * factor if the RSSI averaging algorithm changes.
  1673. */
  1674. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1675. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1676. }
  1677. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1678. int rssi_min_thold,
  1679. int rssi_max_thold)
  1680. {
  1681. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1682. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1683. rssi_min_thold > rssi_max_thold);
  1684. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1685. rssi_max_thold);
  1686. }
  1687. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1688. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1689. {
  1690. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1691. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1692. }
  1693. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1694. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1695. u16 cap)
  1696. {
  1697. __le16 tmp;
  1698. *pos++ = WLAN_EID_HT_CAPABILITY;
  1699. *pos++ = sizeof(struct ieee80211_ht_cap);
  1700. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1701. /* capability flags */
  1702. tmp = cpu_to_le16(cap);
  1703. memcpy(pos, &tmp, sizeof(u16));
  1704. pos += sizeof(u16);
  1705. /* AMPDU parameters */
  1706. *pos++ = ht_cap->ampdu_factor |
  1707. (ht_cap->ampdu_density <<
  1708. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1709. /* MCS set */
  1710. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  1711. pos += sizeof(ht_cap->mcs);
  1712. /* extended capabilities */
  1713. pos += sizeof(__le16);
  1714. /* BF capabilities */
  1715. pos += sizeof(__le32);
  1716. /* antenna selection */
  1717. pos += sizeof(u8);
  1718. return pos;
  1719. }
  1720. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  1721. u32 cap)
  1722. {
  1723. __le32 tmp;
  1724. *pos++ = WLAN_EID_VHT_CAPABILITY;
  1725. *pos++ = sizeof(struct ieee80211_vht_cap);
  1726. memset(pos, 0, sizeof(struct ieee80211_vht_cap));
  1727. /* capability flags */
  1728. tmp = cpu_to_le32(cap);
  1729. memcpy(pos, &tmp, sizeof(u32));
  1730. pos += sizeof(u32);
  1731. /* VHT MCS set */
  1732. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  1733. pos += sizeof(vht_cap->vht_mcs);
  1734. return pos;
  1735. }
  1736. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1737. const struct cfg80211_chan_def *chandef,
  1738. u16 prot_mode)
  1739. {
  1740. struct ieee80211_ht_operation *ht_oper;
  1741. /* Build HT Information */
  1742. *pos++ = WLAN_EID_HT_OPERATION;
  1743. *pos++ = sizeof(struct ieee80211_ht_operation);
  1744. ht_oper = (struct ieee80211_ht_operation *)pos;
  1745. ht_oper->primary_chan = ieee80211_frequency_to_channel(
  1746. chandef->chan->center_freq);
  1747. switch (chandef->width) {
  1748. case NL80211_CHAN_WIDTH_160:
  1749. case NL80211_CHAN_WIDTH_80P80:
  1750. case NL80211_CHAN_WIDTH_80:
  1751. case NL80211_CHAN_WIDTH_40:
  1752. if (chandef->center_freq1 > chandef->chan->center_freq)
  1753. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1754. else
  1755. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1756. break;
  1757. default:
  1758. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1759. break;
  1760. }
  1761. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  1762. chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  1763. chandef->width != NL80211_CHAN_WIDTH_20)
  1764. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1765. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  1766. ht_oper->stbc_param = 0x0000;
  1767. /* It seems that Basic MCS set and Supported MCS set
  1768. are identical for the first 10 bytes */
  1769. memset(&ht_oper->basic_set, 0, 16);
  1770. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  1771. return pos + sizeof(struct ieee80211_ht_operation);
  1772. }
  1773. void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
  1774. const struct ieee80211_ht_operation *ht_oper,
  1775. struct cfg80211_chan_def *chandef)
  1776. {
  1777. enum nl80211_channel_type channel_type;
  1778. if (!ht_oper) {
  1779. cfg80211_chandef_create(chandef, control_chan,
  1780. NL80211_CHAN_NO_HT);
  1781. return;
  1782. }
  1783. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1784. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1785. channel_type = NL80211_CHAN_HT20;
  1786. break;
  1787. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1788. channel_type = NL80211_CHAN_HT40PLUS;
  1789. break;
  1790. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1791. channel_type = NL80211_CHAN_HT40MINUS;
  1792. break;
  1793. default:
  1794. channel_type = NL80211_CHAN_NO_HT;
  1795. }
  1796. cfg80211_chandef_create(chandef, control_chan, channel_type);
  1797. }
  1798. int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
  1799. const struct ieee80211_supported_band *sband,
  1800. const u8 *srates, int srates_len, u32 *rates)
  1801. {
  1802. u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
  1803. int shift = ieee80211_chandef_get_shift(chandef);
  1804. struct ieee80211_rate *br;
  1805. int brate, rate, i, j, count = 0;
  1806. *rates = 0;
  1807. for (i = 0; i < srates_len; i++) {
  1808. rate = srates[i] & 0x7f;
  1809. for (j = 0; j < sband->n_bitrates; j++) {
  1810. br = &sband->bitrates[j];
  1811. if ((rate_flags & br->flags) != rate_flags)
  1812. continue;
  1813. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  1814. if (brate == rate) {
  1815. *rates |= BIT(j);
  1816. count++;
  1817. break;
  1818. }
  1819. }
  1820. }
  1821. return count;
  1822. }
  1823. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  1824. struct sk_buff *skb, bool need_basic,
  1825. enum ieee80211_band band)
  1826. {
  1827. struct ieee80211_local *local = sdata->local;
  1828. struct ieee80211_supported_band *sband;
  1829. int rate, shift;
  1830. u8 i, rates, *pos;
  1831. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1832. u32 rate_flags;
  1833. shift = ieee80211_vif_get_shift(&sdata->vif);
  1834. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1835. sband = local->hw.wiphy->bands[band];
  1836. rates = 0;
  1837. for (i = 0; i < sband->n_bitrates; i++) {
  1838. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1839. continue;
  1840. rates++;
  1841. }
  1842. if (rates > 8)
  1843. rates = 8;
  1844. if (skb_tailroom(skb) < rates + 2)
  1845. return -ENOMEM;
  1846. pos = skb_put(skb, rates + 2);
  1847. *pos++ = WLAN_EID_SUPP_RATES;
  1848. *pos++ = rates;
  1849. for (i = 0; i < rates; i++) {
  1850. u8 basic = 0;
  1851. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1852. continue;
  1853. if (need_basic && basic_rates & BIT(i))
  1854. basic = 0x80;
  1855. rate = sband->bitrates[i].bitrate;
  1856. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1857. 5 * (1 << shift));
  1858. *pos++ = basic | (u8) rate;
  1859. }
  1860. return 0;
  1861. }
  1862. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  1863. struct sk_buff *skb, bool need_basic,
  1864. enum ieee80211_band band)
  1865. {
  1866. struct ieee80211_local *local = sdata->local;
  1867. struct ieee80211_supported_band *sband;
  1868. int rate, shift;
  1869. u8 i, exrates, *pos;
  1870. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1871. u32 rate_flags;
  1872. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1873. shift = ieee80211_vif_get_shift(&sdata->vif);
  1874. sband = local->hw.wiphy->bands[band];
  1875. exrates = 0;
  1876. for (i = 0; i < sband->n_bitrates; i++) {
  1877. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1878. continue;
  1879. exrates++;
  1880. }
  1881. if (exrates > 8)
  1882. exrates -= 8;
  1883. else
  1884. exrates = 0;
  1885. if (skb_tailroom(skb) < exrates + 2)
  1886. return -ENOMEM;
  1887. if (exrates) {
  1888. pos = skb_put(skb, exrates + 2);
  1889. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1890. *pos++ = exrates;
  1891. for (i = 8; i < sband->n_bitrates; i++) {
  1892. u8 basic = 0;
  1893. if ((rate_flags & sband->bitrates[i].flags)
  1894. != rate_flags)
  1895. continue;
  1896. if (need_basic && basic_rates & BIT(i))
  1897. basic = 0x80;
  1898. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1899. 5 * (1 << shift));
  1900. *pos++ = basic | (u8) rate;
  1901. }
  1902. }
  1903. return 0;
  1904. }
  1905. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  1906. {
  1907. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1908. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1909. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  1910. /* non-managed type inferfaces */
  1911. return 0;
  1912. }
  1913. return ifmgd->ave_beacon_signal / 16;
  1914. }
  1915. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
  1916. u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
  1917. {
  1918. if (!mcs)
  1919. return 1;
  1920. /* TODO: consider rx_highest */
  1921. if (mcs->rx_mask[3])
  1922. return 4;
  1923. if (mcs->rx_mask[2])
  1924. return 3;
  1925. if (mcs->rx_mask[1])
  1926. return 2;
  1927. return 1;
  1928. }
  1929. /**
  1930. * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
  1931. * @local: mac80211 hw info struct
  1932. * @status: RX status
  1933. * @mpdu_len: total MPDU length (including FCS)
  1934. * @mpdu_offset: offset into MPDU to calculate timestamp at
  1935. *
  1936. * This function calculates the RX timestamp at the given MPDU offset, taking
  1937. * into account what the RX timestamp was. An offset of 0 will just normalize
  1938. * the timestamp to TSF at beginning of MPDU reception.
  1939. */
  1940. u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
  1941. struct ieee80211_rx_status *status,
  1942. unsigned int mpdu_len,
  1943. unsigned int mpdu_offset)
  1944. {
  1945. u64 ts = status->mactime;
  1946. struct rate_info ri;
  1947. u16 rate;
  1948. if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
  1949. return 0;
  1950. memset(&ri, 0, sizeof(ri));
  1951. /* Fill cfg80211 rate info */
  1952. if (status->flag & RX_FLAG_HT) {
  1953. ri.mcs = status->rate_idx;
  1954. ri.flags |= RATE_INFO_FLAGS_MCS;
  1955. if (status->flag & RX_FLAG_40MHZ)
  1956. ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1957. if (status->flag & RX_FLAG_SHORT_GI)
  1958. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1959. } else if (status->flag & RX_FLAG_VHT) {
  1960. ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
  1961. ri.mcs = status->rate_idx;
  1962. ri.nss = status->vht_nss;
  1963. if (status->flag & RX_FLAG_40MHZ)
  1964. ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1965. if (status->vht_flag & RX_VHT_FLAG_80MHZ)
  1966. ri.flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
  1967. if (status->vht_flag & RX_VHT_FLAG_80P80MHZ)
  1968. ri.flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
  1969. if (status->vht_flag & RX_VHT_FLAG_160MHZ)
  1970. ri.flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
  1971. if (status->flag & RX_FLAG_SHORT_GI)
  1972. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1973. } else {
  1974. struct ieee80211_supported_band *sband;
  1975. int shift = 0;
  1976. int bitrate;
  1977. if (status->flag & RX_FLAG_10MHZ)
  1978. shift = 1;
  1979. if (status->flag & RX_FLAG_5MHZ)
  1980. shift = 2;
  1981. sband = local->hw.wiphy->bands[status->band];
  1982. bitrate = sband->bitrates[status->rate_idx].bitrate;
  1983. ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
  1984. }
  1985. rate = cfg80211_calculate_bitrate(&ri);
  1986. if (WARN_ONCE(!rate,
  1987. "Invalid bitrate: flags=0x%x, idx=%d, vht_nss=%d\n",
  1988. status->flag, status->rate_idx, status->vht_nss))
  1989. return 0;
  1990. /* rewind from end of MPDU */
  1991. if (status->flag & RX_FLAG_MACTIME_END)
  1992. ts -= mpdu_len * 8 * 10 / rate;
  1993. ts += mpdu_offset * 8 * 10 / rate;
  1994. return ts;
  1995. }
  1996. void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
  1997. {
  1998. struct ieee80211_sub_if_data *sdata;
  1999. struct cfg80211_chan_def chandef;
  2000. mutex_lock(&local->mtx);
  2001. mutex_lock(&local->iflist_mtx);
  2002. list_for_each_entry(sdata, &local->interfaces, list) {
  2003. /* it might be waiting for the local->mtx, but then
  2004. * by the time it gets it, sdata->wdev.cac_started
  2005. * will no longer be true
  2006. */
  2007. cancel_delayed_work(&sdata->dfs_cac_timer_work);
  2008. if (sdata->wdev.cac_started) {
  2009. chandef = sdata->vif.bss_conf.chandef;
  2010. ieee80211_vif_release_channel(sdata);
  2011. cfg80211_cac_event(sdata->dev,
  2012. &chandef,
  2013. NL80211_RADAR_CAC_ABORTED,
  2014. GFP_KERNEL);
  2015. }
  2016. }
  2017. mutex_unlock(&local->iflist_mtx);
  2018. mutex_unlock(&local->mtx);
  2019. }
  2020. void ieee80211_dfs_radar_detected_work(struct work_struct *work)
  2021. {
  2022. struct ieee80211_local *local =
  2023. container_of(work, struct ieee80211_local, radar_detected_work);
  2024. struct cfg80211_chan_def chandef = local->hw.conf.chandef;
  2025. ieee80211_dfs_cac_cancel(local);
  2026. if (local->use_chanctx)
  2027. /* currently not handled */
  2028. WARN_ON(1);
  2029. else
  2030. cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
  2031. }
  2032. void ieee80211_radar_detected(struct ieee80211_hw *hw)
  2033. {
  2034. struct ieee80211_local *local = hw_to_local(hw);
  2035. trace_api_radar_detected(local);
  2036. ieee80211_queue_work(hw, &local->radar_detected_work);
  2037. }
  2038. EXPORT_SYMBOL(ieee80211_radar_detected);
  2039. u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
  2040. {
  2041. u32 ret;
  2042. int tmp;
  2043. switch (c->width) {
  2044. case NL80211_CHAN_WIDTH_20:
  2045. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2046. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2047. break;
  2048. case NL80211_CHAN_WIDTH_40:
  2049. c->width = NL80211_CHAN_WIDTH_20;
  2050. c->center_freq1 = c->chan->center_freq;
  2051. ret = IEEE80211_STA_DISABLE_40MHZ |
  2052. IEEE80211_STA_DISABLE_VHT;
  2053. break;
  2054. case NL80211_CHAN_WIDTH_80:
  2055. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  2056. /* n_P40 */
  2057. tmp /= 2;
  2058. /* freq_P40 */
  2059. c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
  2060. c->width = NL80211_CHAN_WIDTH_40;
  2061. ret = IEEE80211_STA_DISABLE_VHT;
  2062. break;
  2063. case NL80211_CHAN_WIDTH_80P80:
  2064. c->center_freq2 = 0;
  2065. c->width = NL80211_CHAN_WIDTH_80;
  2066. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2067. IEEE80211_STA_DISABLE_160MHZ;
  2068. break;
  2069. case NL80211_CHAN_WIDTH_160:
  2070. /* n_P20 */
  2071. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  2072. /* n_P80 */
  2073. tmp /= 4;
  2074. c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
  2075. c->width = NL80211_CHAN_WIDTH_80;
  2076. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2077. IEEE80211_STA_DISABLE_160MHZ;
  2078. break;
  2079. default:
  2080. case NL80211_CHAN_WIDTH_20_NOHT:
  2081. WARN_ON_ONCE(1);
  2082. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2083. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2084. break;
  2085. case NL80211_CHAN_WIDTH_5:
  2086. case NL80211_CHAN_WIDTH_10:
  2087. WARN_ON_ONCE(1);
  2088. /* keep c->width */
  2089. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2090. break;
  2091. }
  2092. WARN_ON_ONCE(!cfg80211_chandef_valid(c));
  2093. return ret;
  2094. }
  2095. /*
  2096. * Returns true if smps_mode_new is strictly more restrictive than
  2097. * smps_mode_old.
  2098. */
  2099. bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
  2100. enum ieee80211_smps_mode smps_mode_new)
  2101. {
  2102. if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
  2103. smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
  2104. return false;
  2105. switch (smps_mode_old) {
  2106. case IEEE80211_SMPS_STATIC:
  2107. return false;
  2108. case IEEE80211_SMPS_DYNAMIC:
  2109. return smps_mode_new == IEEE80211_SMPS_STATIC;
  2110. case IEEE80211_SMPS_OFF:
  2111. return smps_mode_new != IEEE80211_SMPS_OFF;
  2112. default:
  2113. WARN_ON(1);
  2114. }
  2115. return false;
  2116. }
  2117. int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
  2118. struct cfg80211_csa_settings *csa_settings)
  2119. {
  2120. struct sk_buff *skb;
  2121. struct ieee80211_mgmt *mgmt;
  2122. struct ieee80211_local *local = sdata->local;
  2123. int freq;
  2124. int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.chan_switch) +
  2125. sizeof(mgmt->u.action.u.chan_switch);
  2126. u8 *pos;
  2127. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2128. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2129. return -EOPNOTSUPP;
  2130. skb = dev_alloc_skb(local->tx_headroom + hdr_len +
  2131. 5 + /* channel switch announcement element */
  2132. 3 + /* secondary channel offset element */
  2133. 8); /* mesh channel switch parameters element */
  2134. if (!skb)
  2135. return -ENOMEM;
  2136. skb_reserve(skb, local->tx_headroom);
  2137. mgmt = (struct ieee80211_mgmt *)skb_put(skb, hdr_len);
  2138. memset(mgmt, 0, hdr_len);
  2139. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2140. IEEE80211_STYPE_ACTION);
  2141. eth_broadcast_addr(mgmt->da);
  2142. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  2143. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2144. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  2145. } else {
  2146. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  2147. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  2148. }
  2149. mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
  2150. mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
  2151. pos = skb_put(skb, 5);
  2152. *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
  2153. *pos++ = 3; /* IE length */
  2154. *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
  2155. freq = csa_settings->chandef.chan->center_freq;
  2156. *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
  2157. *pos++ = csa_settings->count; /* count */
  2158. if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
  2159. enum nl80211_channel_type ch_type;
  2160. skb_put(skb, 3);
  2161. *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
  2162. *pos++ = 1; /* IE length */
  2163. ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
  2164. if (ch_type == NL80211_CHAN_HT40PLUS)
  2165. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2166. else
  2167. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2168. }
  2169. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2170. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  2171. skb_put(skb, 8);
  2172. *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
  2173. *pos++ = 6; /* IE length */
  2174. *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
  2175. *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
  2176. *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
  2177. *pos++ |= csa_settings->block_tx ?
  2178. WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
  2179. put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
  2180. pos += 2;
  2181. put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
  2182. pos += 2;
  2183. }
  2184. ieee80211_tx_skb(sdata, skb);
  2185. return 0;
  2186. }
  2187. bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
  2188. {
  2189. return !(cs == NULL || cs->cipher == 0 ||
  2190. cs->hdr_len < cs->pn_len + cs->pn_off ||
  2191. cs->hdr_len <= cs->key_idx_off ||
  2192. cs->key_idx_shift > 7 ||
  2193. cs->key_idx_mask == 0);
  2194. }
  2195. bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
  2196. {
  2197. int i;
  2198. /* Ensure we have enough iftype bitmap space for all iftype values */
  2199. WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
  2200. for (i = 0; i < n; i++)
  2201. if (!ieee80211_cs_valid(&cs[i]))
  2202. return false;
  2203. return true;
  2204. }
  2205. const struct ieee80211_cipher_scheme *
  2206. ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
  2207. enum nl80211_iftype iftype)
  2208. {
  2209. const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
  2210. int n = local->hw.n_cipher_schemes;
  2211. int i;
  2212. const struct ieee80211_cipher_scheme *cs = NULL;
  2213. for (i = 0; i < n; i++) {
  2214. if (l[i].cipher == cipher) {
  2215. cs = &l[i];
  2216. break;
  2217. }
  2218. }
  2219. if (!cs || !(cs->iftype & BIT(iftype)))
  2220. return NULL;
  2221. return cs;
  2222. }
  2223. int ieee80211_cs_headroom(struct ieee80211_local *local,
  2224. struct cfg80211_crypto_settings *crypto,
  2225. enum nl80211_iftype iftype)
  2226. {
  2227. const struct ieee80211_cipher_scheme *cs;
  2228. int headroom = IEEE80211_ENCRYPT_HEADROOM;
  2229. int i;
  2230. for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
  2231. cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
  2232. iftype);
  2233. if (cs && headroom < cs->hdr_len)
  2234. headroom = cs->hdr_len;
  2235. }
  2236. cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
  2237. if (cs && headroom < cs->hdr_len)
  2238. headroom = cs->hdr_len;
  2239. return headroom;
  2240. }
  2241. static bool
  2242. ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
  2243. {
  2244. s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
  2245. int skip;
  2246. if (end > 0)
  2247. return false;
  2248. /* End time is in the past, check for repetitions */
  2249. skip = DIV_ROUND_UP(-end, data->desc[i].interval);
  2250. if (data->count[i] < 255) {
  2251. if (data->count[i] <= skip) {
  2252. data->count[i] = 0;
  2253. return false;
  2254. }
  2255. data->count[i] -= skip;
  2256. }
  2257. data->desc[i].start += skip * data->desc[i].interval;
  2258. return true;
  2259. }
  2260. static bool
  2261. ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
  2262. s32 *offset)
  2263. {
  2264. bool ret = false;
  2265. int i;
  2266. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2267. s32 cur;
  2268. if (!data->count[i])
  2269. continue;
  2270. if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
  2271. ret = true;
  2272. cur = data->desc[i].start - tsf;
  2273. if (cur > *offset)
  2274. continue;
  2275. cur = data->desc[i].start + data->desc[i].duration - tsf;
  2276. if (cur > *offset)
  2277. *offset = cur;
  2278. }
  2279. return ret;
  2280. }
  2281. static u32
  2282. ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
  2283. {
  2284. s32 offset = 0;
  2285. int tries = 0;
  2286. /*
  2287. * arbitrary limit, used to avoid infinite loops when combined NoA
  2288. * descriptors cover the full time period.
  2289. */
  2290. int max_tries = 5;
  2291. ieee80211_extend_absent_time(data, tsf, &offset);
  2292. do {
  2293. if (!ieee80211_extend_absent_time(data, tsf, &offset))
  2294. break;
  2295. tries++;
  2296. } while (tries < max_tries);
  2297. return offset;
  2298. }
  2299. void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
  2300. {
  2301. u32 next_offset = BIT(31) - 1;
  2302. int i;
  2303. data->absent = 0;
  2304. data->has_next_tsf = false;
  2305. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2306. s32 start;
  2307. if (!data->count[i])
  2308. continue;
  2309. ieee80211_extend_noa_desc(data, tsf, i);
  2310. start = data->desc[i].start - tsf;
  2311. if (start <= 0)
  2312. data->absent |= BIT(i);
  2313. if (next_offset > start)
  2314. next_offset = start;
  2315. data->has_next_tsf = true;
  2316. }
  2317. if (data->absent)
  2318. next_offset = ieee80211_get_noa_absent_time(data, tsf);
  2319. data->next_tsf = tsf + next_offset;
  2320. }
  2321. EXPORT_SYMBOL(ieee80211_update_p2p_noa);
  2322. int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
  2323. struct ieee80211_noa_data *data, u32 tsf)
  2324. {
  2325. int ret = 0;
  2326. int i;
  2327. memset(data, 0, sizeof(*data));
  2328. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2329. const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
  2330. if (!desc->count || !desc->duration)
  2331. continue;
  2332. data->count[i] = desc->count;
  2333. data->desc[i].start = le32_to_cpu(desc->start_time);
  2334. data->desc[i].duration = le32_to_cpu(desc->duration);
  2335. data->desc[i].interval = le32_to_cpu(desc->interval);
  2336. if (data->count[i] > 1 &&
  2337. data->desc[i].interval < data->desc[i].duration)
  2338. continue;
  2339. ieee80211_extend_noa_desc(data, tsf, i);
  2340. ret++;
  2341. }
  2342. if (ret)
  2343. ieee80211_update_p2p_noa(data, tsf);
  2344. return ret;
  2345. }
  2346. EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
  2347. void ieee80211_recalc_dtim(struct ieee80211_local *local,
  2348. struct ieee80211_sub_if_data *sdata)
  2349. {
  2350. u64 tsf = drv_get_tsf(local, sdata);
  2351. u64 dtim_count = 0;
  2352. u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
  2353. u8 dtim_period = sdata->vif.bss_conf.dtim_period;
  2354. struct ps_data *ps;
  2355. u8 bcns_from_dtim;
  2356. if (tsf == -1ULL || !beacon_int || !dtim_period)
  2357. return;
  2358. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  2359. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  2360. if (!sdata->bss)
  2361. return;
  2362. ps = &sdata->bss->ps;
  2363. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2364. ps = &sdata->u.mesh.ps;
  2365. } else {
  2366. return;
  2367. }
  2368. /*
  2369. * actually finds last dtim_count, mac80211 will update in
  2370. * __beacon_add_tim().
  2371. * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
  2372. */
  2373. do_div(tsf, beacon_int);
  2374. bcns_from_dtim = do_div(tsf, dtim_period);
  2375. /* just had a DTIM */
  2376. if (!bcns_from_dtim)
  2377. dtim_count = 0;
  2378. else
  2379. dtim_count = dtim_period - bcns_from_dtim;
  2380. ps->dtim_count = dtim_count;
  2381. }