tx.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244
  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. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. *
  13. * Transmit and frame generation functions.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/bitmap.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/export.h>
  22. #include <linux/time.h>
  23. #include <net/net_namespace.h>
  24. #include <net/ieee80211_radiotap.h>
  25. #include <net/cfg80211.h>
  26. #include <net/mac80211.h>
  27. #include <asm/unaligned.h>
  28. #include "ieee80211_i.h"
  29. #include "driver-ops.h"
  30. #include "led.h"
  31. #include "mesh.h"
  32. #include "wep.h"
  33. #include "wpa.h"
  34. #include "wme.h"
  35. #include "rate.h"
  36. /* misc utils */
  37. static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
  38. struct sk_buff *skb, int group_addr,
  39. int next_frag_len)
  40. {
  41. int rate, mrate, erp, dur, i, shift = 0;
  42. struct ieee80211_rate *txrate;
  43. struct ieee80211_local *local = tx->local;
  44. struct ieee80211_supported_band *sband;
  45. struct ieee80211_hdr *hdr;
  46. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  47. struct ieee80211_chanctx_conf *chanctx_conf;
  48. u32 rate_flags = 0;
  49. rcu_read_lock();
  50. chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
  51. if (chanctx_conf) {
  52. shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
  53. rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
  54. }
  55. rcu_read_unlock();
  56. /* assume HW handles this */
  57. if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
  58. return 0;
  59. /* uh huh? */
  60. if (WARN_ON_ONCE(tx->rate.idx < 0))
  61. return 0;
  62. sband = local->hw.wiphy->bands[info->band];
  63. txrate = &sband->bitrates[tx->rate.idx];
  64. erp = txrate->flags & IEEE80211_RATE_ERP_G;
  65. /*
  66. * data and mgmt (except PS Poll):
  67. * - during CFP: 32768
  68. * - during contention period:
  69. * if addr1 is group address: 0
  70. * if more fragments = 0 and addr1 is individual address: time to
  71. * transmit one ACK plus SIFS
  72. * if more fragments = 1 and addr1 is individual address: time to
  73. * transmit next fragment plus 2 x ACK plus 3 x SIFS
  74. *
  75. * IEEE 802.11, 9.6:
  76. * - control response frame (CTS or ACK) shall be transmitted using the
  77. * same rate as the immediately previous frame in the frame exchange
  78. * sequence, if this rate belongs to the PHY mandatory rates, or else
  79. * at the highest possible rate belonging to the PHY rates in the
  80. * BSSBasicRateSet
  81. */
  82. hdr = (struct ieee80211_hdr *)skb->data;
  83. if (ieee80211_is_ctl(hdr->frame_control)) {
  84. /* TODO: These control frames are not currently sent by
  85. * mac80211, but should they be implemented, this function
  86. * needs to be updated to support duration field calculation.
  87. *
  88. * RTS: time needed to transmit pending data/mgmt frame plus
  89. * one CTS frame plus one ACK frame plus 3 x SIFS
  90. * CTS: duration of immediately previous RTS minus time
  91. * required to transmit CTS and its SIFS
  92. * ACK: 0 if immediately previous directed data/mgmt had
  93. * more=0, with more=1 duration in ACK frame is duration
  94. * from previous frame minus time needed to transmit ACK
  95. * and its SIFS
  96. * PS Poll: BIT(15) | BIT(14) | aid
  97. */
  98. return 0;
  99. }
  100. /* data/mgmt */
  101. if (0 /* FIX: data/mgmt during CFP */)
  102. return cpu_to_le16(32768);
  103. if (group_addr) /* Group address as the destination - no ACK */
  104. return 0;
  105. /* Individual destination address:
  106. * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
  107. * CTS and ACK frames shall be transmitted using the highest rate in
  108. * basic rate set that is less than or equal to the rate of the
  109. * immediately previous frame and that is using the same modulation
  110. * (CCK or OFDM). If no basic rate set matches with these requirements,
  111. * the highest mandatory rate of the PHY that is less than or equal to
  112. * the rate of the previous frame is used.
  113. * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
  114. */
  115. rate = -1;
  116. /* use lowest available if everything fails */
  117. mrate = sband->bitrates[0].bitrate;
  118. for (i = 0; i < sband->n_bitrates; i++) {
  119. struct ieee80211_rate *r = &sband->bitrates[i];
  120. if (r->bitrate > txrate->bitrate)
  121. break;
  122. if ((rate_flags & r->flags) != rate_flags)
  123. continue;
  124. if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
  125. rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
  126. switch (sband->band) {
  127. case IEEE80211_BAND_2GHZ: {
  128. u32 flag;
  129. if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  130. flag = IEEE80211_RATE_MANDATORY_G;
  131. else
  132. flag = IEEE80211_RATE_MANDATORY_B;
  133. if (r->flags & flag)
  134. mrate = r->bitrate;
  135. break;
  136. }
  137. case IEEE80211_BAND_5GHZ:
  138. if (r->flags & IEEE80211_RATE_MANDATORY_A)
  139. mrate = r->bitrate;
  140. break;
  141. case IEEE80211_BAND_60GHZ:
  142. /* TODO, for now fall through */
  143. case IEEE80211_NUM_BANDS:
  144. WARN_ON(1);
  145. break;
  146. }
  147. }
  148. if (rate == -1) {
  149. /* No matching basic rate found; use highest suitable mandatory
  150. * PHY rate */
  151. rate = DIV_ROUND_UP(mrate, 1 << shift);
  152. }
  153. /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
  154. if (ieee80211_is_data_qos(hdr->frame_control) &&
  155. *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
  156. dur = 0;
  157. else
  158. /* Time needed to transmit ACK
  159. * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
  160. * to closest integer */
  161. dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
  162. tx->sdata->vif.bss_conf.use_short_preamble,
  163. shift);
  164. if (next_frag_len) {
  165. /* Frame is fragmented: duration increases with time needed to
  166. * transmit next fragment plus ACK and 2 x SIFS. */
  167. dur *= 2; /* ACK + SIFS */
  168. /* next fragment */
  169. dur += ieee80211_frame_duration(sband->band, next_frag_len,
  170. txrate->bitrate, erp,
  171. tx->sdata->vif.bss_conf.use_short_preamble,
  172. shift);
  173. }
  174. return cpu_to_le16(dur);
  175. }
  176. /* tx handlers */
  177. static ieee80211_tx_result debug_noinline
  178. ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
  179. {
  180. struct ieee80211_local *local = tx->local;
  181. struct ieee80211_if_managed *ifmgd;
  182. /* driver doesn't support power save */
  183. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
  184. return TX_CONTINUE;
  185. /* hardware does dynamic power save */
  186. if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
  187. return TX_CONTINUE;
  188. /* dynamic power save disabled */
  189. if (local->hw.conf.dynamic_ps_timeout <= 0)
  190. return TX_CONTINUE;
  191. /* we are scanning, don't enable power save */
  192. if (local->scanning)
  193. return TX_CONTINUE;
  194. if (!local->ps_sdata)
  195. return TX_CONTINUE;
  196. /* No point if we're going to suspend */
  197. if (local->quiescing)
  198. return TX_CONTINUE;
  199. /* dynamic ps is supported only in managed mode */
  200. if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
  201. return TX_CONTINUE;
  202. ifmgd = &tx->sdata->u.mgd;
  203. /*
  204. * Don't wakeup from power save if u-apsd is enabled, voip ac has
  205. * u-apsd enabled and the frame is in voip class. This effectively
  206. * means that even if all access categories have u-apsd enabled, in
  207. * practise u-apsd is only used with the voip ac. This is a
  208. * workaround for the case when received voip class packets do not
  209. * have correct qos tag for some reason, due the network or the
  210. * peer application.
  211. *
  212. * Note: ifmgd->uapsd_queues access is racy here. If the value is
  213. * changed via debugfs, user needs to reassociate manually to have
  214. * everything in sync.
  215. */
  216. if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
  217. (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
  218. skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
  219. return TX_CONTINUE;
  220. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  221. ieee80211_stop_queues_by_reason(&local->hw,
  222. IEEE80211_MAX_QUEUE_MAP,
  223. IEEE80211_QUEUE_STOP_REASON_PS,
  224. false);
  225. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  226. ieee80211_queue_work(&local->hw,
  227. &local->dynamic_ps_disable_work);
  228. }
  229. /* Don't restart the timer if we're not disassociated */
  230. if (!ifmgd->associated)
  231. return TX_CONTINUE;
  232. mod_timer(&local->dynamic_ps_timer, jiffies +
  233. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  234. return TX_CONTINUE;
  235. }
  236. static ieee80211_tx_result debug_noinline
  237. ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
  238. {
  239. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  240. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  241. bool assoc = false;
  242. if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
  243. return TX_CONTINUE;
  244. if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
  245. test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
  246. !ieee80211_is_probe_req(hdr->frame_control) &&
  247. !ieee80211_is_nullfunc(hdr->frame_control))
  248. /*
  249. * When software scanning only nullfunc frames (to notify
  250. * the sleep state to the AP) and probe requests (for the
  251. * active scan) are allowed, all other frames should not be
  252. * sent and we should not get here, but if we do
  253. * nonetheless, drop them to avoid sending them
  254. * off-channel. See the link below and
  255. * ieee80211_start_scan() for more.
  256. *
  257. * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
  258. */
  259. return TX_DROP;
  260. if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
  261. return TX_CONTINUE;
  262. if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
  263. return TX_CONTINUE;
  264. if (tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  265. return TX_CONTINUE;
  266. if (tx->flags & IEEE80211_TX_PS_BUFFERED)
  267. return TX_CONTINUE;
  268. if (tx->sta)
  269. assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
  270. if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
  271. if (unlikely(!assoc &&
  272. ieee80211_is_data(hdr->frame_control))) {
  273. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  274. sdata_info(tx->sdata,
  275. "dropped data frame to not associated station %pM\n",
  276. hdr->addr1);
  277. #endif
  278. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  279. return TX_DROP;
  280. }
  281. } else if (unlikely(tx->sdata->vif.type == NL80211_IFTYPE_AP &&
  282. ieee80211_is_data(hdr->frame_control) &&
  283. !atomic_read(&tx->sdata->u.ap.num_mcast_sta))) {
  284. /*
  285. * No associated STAs - no need to send multicast
  286. * frames.
  287. */
  288. return TX_DROP;
  289. }
  290. return TX_CONTINUE;
  291. }
  292. /* This function is called whenever the AP is about to exceed the maximum limit
  293. * of buffered frames for power saving STAs. This situation should not really
  294. * happen often during normal operation, so dropping the oldest buffered packet
  295. * from each queue should be OK to make some room for new frames. */
  296. static void purge_old_ps_buffers(struct ieee80211_local *local)
  297. {
  298. int total = 0, purged = 0;
  299. struct sk_buff *skb;
  300. struct ieee80211_sub_if_data *sdata;
  301. struct sta_info *sta;
  302. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  303. struct ps_data *ps;
  304. if (sdata->vif.type == NL80211_IFTYPE_AP)
  305. ps = &sdata->u.ap.ps;
  306. else if (ieee80211_vif_is_mesh(&sdata->vif))
  307. ps = &sdata->u.mesh.ps;
  308. else
  309. continue;
  310. skb = skb_dequeue(&ps->bc_buf);
  311. if (skb) {
  312. purged++;
  313. dev_kfree_skb(skb);
  314. }
  315. total += skb_queue_len(&ps->bc_buf);
  316. }
  317. /*
  318. * Drop one frame from each station from the lowest-priority
  319. * AC that has frames at all.
  320. */
  321. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  322. int ac;
  323. for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
  324. skb = skb_dequeue(&sta->ps_tx_buf[ac]);
  325. total += skb_queue_len(&sta->ps_tx_buf[ac]);
  326. if (skb) {
  327. purged++;
  328. ieee80211_free_txskb(&local->hw, skb);
  329. break;
  330. }
  331. }
  332. }
  333. local->total_ps_buffered = total;
  334. ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
  335. }
  336. static ieee80211_tx_result
  337. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
  338. {
  339. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  340. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  341. struct ps_data *ps;
  342. /*
  343. * broadcast/multicast frame
  344. *
  345. * If any of the associated/peer stations is in power save mode,
  346. * the frame is buffered to be sent after DTIM beacon frame.
  347. * This is done either by the hardware or us.
  348. */
  349. /* powersaving STAs currently only in AP/VLAN/mesh mode */
  350. if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
  351. tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  352. if (!tx->sdata->bss)
  353. return TX_CONTINUE;
  354. ps = &tx->sdata->bss->ps;
  355. } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
  356. ps = &tx->sdata->u.mesh.ps;
  357. } else {
  358. return TX_CONTINUE;
  359. }
  360. /* no buffering for ordered frames */
  361. if (ieee80211_has_order(hdr->frame_control))
  362. return TX_CONTINUE;
  363. if (ieee80211_is_probe_req(hdr->frame_control))
  364. return TX_CONTINUE;
  365. if (tx->local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
  366. info->hw_queue = tx->sdata->vif.cab_queue;
  367. /* no stations in PS mode */
  368. if (!atomic_read(&ps->num_sta_ps))
  369. return TX_CONTINUE;
  370. info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
  371. /* device releases frame after DTIM beacon */
  372. if (!(tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING))
  373. return TX_CONTINUE;
  374. /* buffered in mac80211 */
  375. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  376. purge_old_ps_buffers(tx->local);
  377. if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
  378. ps_dbg(tx->sdata,
  379. "BC TX buffer full - dropping the oldest frame\n");
  380. dev_kfree_skb(skb_dequeue(&ps->bc_buf));
  381. } else
  382. tx->local->total_ps_buffered++;
  383. skb_queue_tail(&ps->bc_buf, tx->skb);
  384. return TX_QUEUED;
  385. }
  386. static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
  387. struct sk_buff *skb)
  388. {
  389. if (!ieee80211_is_mgmt(fc))
  390. return 0;
  391. if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
  392. return 0;
  393. if (!ieee80211_is_robust_mgmt_frame(skb))
  394. return 0;
  395. return 1;
  396. }
  397. static ieee80211_tx_result
  398. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
  399. {
  400. struct sta_info *sta = tx->sta;
  401. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  402. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  403. struct ieee80211_local *local = tx->local;
  404. if (unlikely(!sta))
  405. return TX_CONTINUE;
  406. if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
  407. test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
  408. test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
  409. !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
  410. int ac = skb_get_queue_mapping(tx->skb);
  411. if (ieee80211_is_mgmt(hdr->frame_control) &&
  412. !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
  413. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
  414. return TX_CONTINUE;
  415. }
  416. ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
  417. sta->sta.addr, sta->sta.aid, ac);
  418. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  419. purge_old_ps_buffers(tx->local);
  420. /* sync with ieee80211_sta_ps_deliver_wakeup */
  421. spin_lock(&sta->ps_lock);
  422. /*
  423. * STA woke up the meantime and all the frames on ps_tx_buf have
  424. * been queued to pending queue. No reordering can happen, go
  425. * ahead and Tx the packet.
  426. */
  427. if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
  428. !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
  429. !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
  430. spin_unlock(&sta->ps_lock);
  431. return TX_CONTINUE;
  432. }
  433. if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
  434. struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
  435. ps_dbg(tx->sdata,
  436. "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
  437. sta->sta.addr, ac);
  438. ieee80211_free_txskb(&local->hw, old);
  439. } else
  440. tx->local->total_ps_buffered++;
  441. info->control.jiffies = jiffies;
  442. info->control.vif = &tx->sdata->vif;
  443. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  444. info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
  445. skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
  446. spin_unlock(&sta->ps_lock);
  447. if (!timer_pending(&local->sta_cleanup))
  448. mod_timer(&local->sta_cleanup,
  449. round_jiffies(jiffies +
  450. STA_INFO_CLEANUP_INTERVAL));
  451. /*
  452. * We queued up some frames, so the TIM bit might
  453. * need to be set, recalculate it.
  454. */
  455. sta_info_recalc_tim(sta);
  456. return TX_QUEUED;
  457. } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
  458. ps_dbg(tx->sdata,
  459. "STA %pM in PS mode, but polling/in SP -> send frame\n",
  460. sta->sta.addr);
  461. }
  462. return TX_CONTINUE;
  463. }
  464. static ieee80211_tx_result debug_noinline
  465. ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
  466. {
  467. if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
  468. return TX_CONTINUE;
  469. if (tx->flags & IEEE80211_TX_UNICAST)
  470. return ieee80211_tx_h_unicast_ps_buf(tx);
  471. else
  472. return ieee80211_tx_h_multicast_ps_buf(tx);
  473. }
  474. static ieee80211_tx_result debug_noinline
  475. ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
  476. {
  477. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  478. if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
  479. if (tx->sdata->control_port_no_encrypt)
  480. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  481. info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
  482. info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
  483. }
  484. return TX_CONTINUE;
  485. }
  486. static ieee80211_tx_result debug_noinline
  487. ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
  488. {
  489. struct ieee80211_key *key;
  490. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  491. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  492. if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
  493. tx->key = NULL;
  494. else if (tx->sta &&
  495. (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
  496. tx->key = key;
  497. else if (ieee80211_is_mgmt(hdr->frame_control) &&
  498. is_multicast_ether_addr(hdr->addr1) &&
  499. ieee80211_is_robust_mgmt_frame(tx->skb) &&
  500. (key = rcu_dereference(tx->sdata->default_mgmt_key)))
  501. tx->key = key;
  502. else if (is_multicast_ether_addr(hdr->addr1) &&
  503. (key = rcu_dereference(tx->sdata->default_multicast_key)))
  504. tx->key = key;
  505. else if (!is_multicast_ether_addr(hdr->addr1) &&
  506. (key = rcu_dereference(tx->sdata->default_unicast_key)))
  507. tx->key = key;
  508. else if (info->flags & IEEE80211_TX_CTL_INJECTED)
  509. tx->key = NULL;
  510. else if (!tx->sdata->drop_unencrypted)
  511. tx->key = NULL;
  512. else if (tx->skb->protocol == tx->sdata->control_port_protocol)
  513. tx->key = NULL;
  514. else if (ieee80211_is_robust_mgmt_frame(tx->skb) &&
  515. !(ieee80211_is_action(hdr->frame_control) &&
  516. tx->sta && test_sta_flag(tx->sta, WLAN_STA_MFP)))
  517. tx->key = NULL;
  518. else if (ieee80211_is_mgmt(hdr->frame_control) &&
  519. !ieee80211_is_robust_mgmt_frame(tx->skb))
  520. tx->key = NULL;
  521. else {
  522. I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
  523. return TX_DROP;
  524. }
  525. if (tx->key) {
  526. bool skip_hw = false;
  527. tx->key->tx_rx_count++;
  528. /* TODO: add threshold stuff again */
  529. switch (tx->key->conf.cipher) {
  530. case WLAN_CIPHER_SUITE_WEP40:
  531. case WLAN_CIPHER_SUITE_WEP104:
  532. case WLAN_CIPHER_SUITE_TKIP:
  533. if (!ieee80211_is_data_present(hdr->frame_control))
  534. tx->key = NULL;
  535. break;
  536. case WLAN_CIPHER_SUITE_CCMP:
  537. case WLAN_CIPHER_SUITE_CCMP_256:
  538. case WLAN_CIPHER_SUITE_GCMP:
  539. case WLAN_CIPHER_SUITE_GCMP_256:
  540. if (!ieee80211_is_data_present(hdr->frame_control) &&
  541. !ieee80211_use_mfp(hdr->frame_control, tx->sta,
  542. tx->skb))
  543. tx->key = NULL;
  544. else
  545. skip_hw = (tx->key->conf.flags &
  546. IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
  547. ieee80211_is_mgmt(hdr->frame_control);
  548. break;
  549. case WLAN_CIPHER_SUITE_AES_CMAC:
  550. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  551. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  552. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  553. if (!ieee80211_is_mgmt(hdr->frame_control))
  554. tx->key = NULL;
  555. break;
  556. }
  557. if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
  558. !ieee80211_is_deauth(hdr->frame_control)))
  559. return TX_DROP;
  560. if (!skip_hw && tx->key &&
  561. tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
  562. info->control.hw_key = &tx->key->conf;
  563. }
  564. return TX_CONTINUE;
  565. }
  566. static ieee80211_tx_result debug_noinline
  567. ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
  568. {
  569. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  570. struct ieee80211_hdr *hdr = (void *)tx->skb->data;
  571. struct ieee80211_supported_band *sband;
  572. u32 len;
  573. struct ieee80211_tx_rate_control txrc;
  574. struct ieee80211_sta_rates *ratetbl = NULL;
  575. bool assoc = false;
  576. memset(&txrc, 0, sizeof(txrc));
  577. sband = tx->local->hw.wiphy->bands[info->band];
  578. len = min_t(u32, tx->skb->len + FCS_LEN,
  579. tx->local->hw.wiphy->frag_threshold);
  580. /* set up the tx rate control struct we give the RC algo */
  581. txrc.hw = &tx->local->hw;
  582. txrc.sband = sband;
  583. txrc.bss_conf = &tx->sdata->vif.bss_conf;
  584. txrc.skb = tx->skb;
  585. txrc.reported_rate.idx = -1;
  586. txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
  587. if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
  588. txrc.max_rate_idx = -1;
  589. else
  590. txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
  591. if (tx->sdata->rc_has_mcs_mask[info->band])
  592. txrc.rate_idx_mcs_mask =
  593. tx->sdata->rc_rateidx_mcs_mask[info->band];
  594. txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
  595. tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
  596. tx->sdata->vif.type == NL80211_IFTYPE_ADHOC);
  597. /* set up RTS protection if desired */
  598. if (len > tx->local->hw.wiphy->rts_threshold) {
  599. txrc.rts = true;
  600. }
  601. info->control.use_rts = txrc.rts;
  602. info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
  603. /*
  604. * Use short preamble if the BSS can handle it, but not for
  605. * management frames unless we know the receiver can handle
  606. * that -- the management frame might be to a station that
  607. * just wants a probe response.
  608. */
  609. if (tx->sdata->vif.bss_conf.use_short_preamble &&
  610. (ieee80211_is_data(hdr->frame_control) ||
  611. (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
  612. txrc.short_preamble = true;
  613. info->control.short_preamble = txrc.short_preamble;
  614. if (tx->sta)
  615. assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
  616. /*
  617. * Lets not bother rate control if we're associated and cannot
  618. * talk to the sta. This should not happen.
  619. */
  620. if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
  621. !rate_usable_index_exists(sband, &tx->sta->sta),
  622. "%s: Dropped data frame as no usable bitrate found while "
  623. "scanning and associated. Target station: "
  624. "%pM on %d GHz band\n",
  625. tx->sdata->name, hdr->addr1,
  626. info->band ? 5 : 2))
  627. return TX_DROP;
  628. /*
  629. * If we're associated with the sta at this point we know we can at
  630. * least send the frame at the lowest bit rate.
  631. */
  632. rate_control_get_rate(tx->sdata, tx->sta, &txrc);
  633. if (tx->sta && !info->control.skip_table)
  634. ratetbl = rcu_dereference(tx->sta->sta.rates);
  635. if (unlikely(info->control.rates[0].idx < 0)) {
  636. if (ratetbl) {
  637. struct ieee80211_tx_rate rate = {
  638. .idx = ratetbl->rate[0].idx,
  639. .flags = ratetbl->rate[0].flags,
  640. .count = ratetbl->rate[0].count
  641. };
  642. if (ratetbl->rate[0].idx < 0)
  643. return TX_DROP;
  644. tx->rate = rate;
  645. } else {
  646. return TX_DROP;
  647. }
  648. } else {
  649. tx->rate = info->control.rates[0];
  650. }
  651. if (txrc.reported_rate.idx < 0) {
  652. txrc.reported_rate = tx->rate;
  653. if (tx->sta && ieee80211_is_data(hdr->frame_control))
  654. tx->sta->last_tx_rate = txrc.reported_rate;
  655. } else if (tx->sta)
  656. tx->sta->last_tx_rate = txrc.reported_rate;
  657. if (ratetbl)
  658. return TX_CONTINUE;
  659. if (unlikely(!info->control.rates[0].count))
  660. info->control.rates[0].count = 1;
  661. if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
  662. (info->flags & IEEE80211_TX_CTL_NO_ACK)))
  663. info->control.rates[0].count = 1;
  664. return TX_CONTINUE;
  665. }
  666. static ieee80211_tx_result debug_noinline
  667. ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
  668. {
  669. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  670. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  671. u16 *seq;
  672. u8 *qc;
  673. int tid;
  674. /*
  675. * Packet injection may want to control the sequence
  676. * number, if we have no matching interface then we
  677. * neither assign one ourselves nor ask the driver to.
  678. */
  679. if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
  680. return TX_CONTINUE;
  681. if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
  682. return TX_CONTINUE;
  683. if (ieee80211_hdrlen(hdr->frame_control) < 24)
  684. return TX_CONTINUE;
  685. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  686. return TX_CONTINUE;
  687. /*
  688. * Anything but QoS data that has a sequence number field
  689. * (is long enough) gets a sequence number from the global
  690. * counter. QoS data frames with a multicast destination
  691. * also use the global counter (802.11-2012 9.3.2.10).
  692. */
  693. if (!ieee80211_is_data_qos(hdr->frame_control) ||
  694. is_multicast_ether_addr(hdr->addr1)) {
  695. /* driver should assign sequence number */
  696. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  697. /* for pure STA mode without beacons, we can do it */
  698. hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
  699. tx->sdata->sequence_number += 0x10;
  700. if (tx->sta)
  701. tx->sta->tx_msdu[IEEE80211_NUM_TIDS]++;
  702. return TX_CONTINUE;
  703. }
  704. /*
  705. * This should be true for injected/management frames only, for
  706. * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
  707. * above since they are not QoS-data frames.
  708. */
  709. if (!tx->sta)
  710. return TX_CONTINUE;
  711. /* include per-STA, per-TID sequence counter */
  712. qc = ieee80211_get_qos_ctl(hdr);
  713. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  714. seq = &tx->sta->tid_seq[tid];
  715. tx->sta->tx_msdu[tid]++;
  716. hdr->seq_ctrl = cpu_to_le16(*seq);
  717. /* Increase the sequence number. */
  718. *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
  719. return TX_CONTINUE;
  720. }
  721. static int ieee80211_fragment(struct ieee80211_tx_data *tx,
  722. struct sk_buff *skb, int hdrlen,
  723. int frag_threshold)
  724. {
  725. struct ieee80211_local *local = tx->local;
  726. struct ieee80211_tx_info *info;
  727. struct sk_buff *tmp;
  728. int per_fragm = frag_threshold - hdrlen - FCS_LEN;
  729. int pos = hdrlen + per_fragm;
  730. int rem = skb->len - hdrlen - per_fragm;
  731. if (WARN_ON(rem < 0))
  732. return -EINVAL;
  733. /* first fragment was already added to queue by caller */
  734. while (rem) {
  735. int fraglen = per_fragm;
  736. if (fraglen > rem)
  737. fraglen = rem;
  738. rem -= fraglen;
  739. tmp = dev_alloc_skb(local->tx_headroom +
  740. frag_threshold +
  741. tx->sdata->encrypt_headroom +
  742. IEEE80211_ENCRYPT_TAILROOM);
  743. if (!tmp)
  744. return -ENOMEM;
  745. __skb_queue_tail(&tx->skbs, tmp);
  746. skb_reserve(tmp,
  747. local->tx_headroom + tx->sdata->encrypt_headroom);
  748. /* copy control information */
  749. memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
  750. info = IEEE80211_SKB_CB(tmp);
  751. info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
  752. IEEE80211_TX_CTL_FIRST_FRAGMENT);
  753. if (rem)
  754. info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
  755. skb_copy_queue_mapping(tmp, skb);
  756. tmp->priority = skb->priority;
  757. tmp->dev = skb->dev;
  758. /* copy header and data */
  759. memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
  760. memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
  761. pos += fraglen;
  762. }
  763. /* adjust first fragment's length */
  764. skb_trim(skb, hdrlen + per_fragm);
  765. return 0;
  766. }
  767. static ieee80211_tx_result debug_noinline
  768. ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
  769. {
  770. struct sk_buff *skb = tx->skb;
  771. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  772. struct ieee80211_hdr *hdr = (void *)skb->data;
  773. int frag_threshold = tx->local->hw.wiphy->frag_threshold;
  774. int hdrlen;
  775. int fragnum;
  776. /* no matter what happens, tx->skb moves to tx->skbs */
  777. __skb_queue_tail(&tx->skbs, skb);
  778. tx->skb = NULL;
  779. if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
  780. return TX_CONTINUE;
  781. if (tx->local->ops->set_frag_threshold)
  782. return TX_CONTINUE;
  783. /*
  784. * Warn when submitting a fragmented A-MPDU frame and drop it.
  785. * This scenario is handled in ieee80211_tx_prepare but extra
  786. * caution taken here as fragmented ampdu may cause Tx stop.
  787. */
  788. if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
  789. return TX_DROP;
  790. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  791. /* internal error, why isn't DONTFRAG set? */
  792. if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
  793. return TX_DROP;
  794. /*
  795. * Now fragment the frame. This will allocate all the fragments and
  796. * chain them (using skb as the first fragment) to skb->next.
  797. * During transmission, we will remove the successfully transmitted
  798. * fragments from this list. When the low-level driver rejects one
  799. * of the fragments then we will simply pretend to accept the skb
  800. * but store it away as pending.
  801. */
  802. if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
  803. return TX_DROP;
  804. /* update duration/seq/flags of fragments */
  805. fragnum = 0;
  806. skb_queue_walk(&tx->skbs, skb) {
  807. const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  808. hdr = (void *)skb->data;
  809. info = IEEE80211_SKB_CB(skb);
  810. if (!skb_queue_is_last(&tx->skbs, skb)) {
  811. hdr->frame_control |= morefrags;
  812. /*
  813. * No multi-rate retries for fragmented frames, that
  814. * would completely throw off the NAV at other STAs.
  815. */
  816. info->control.rates[1].idx = -1;
  817. info->control.rates[2].idx = -1;
  818. info->control.rates[3].idx = -1;
  819. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
  820. info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  821. } else {
  822. hdr->frame_control &= ~morefrags;
  823. }
  824. hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
  825. fragnum++;
  826. }
  827. return TX_CONTINUE;
  828. }
  829. static ieee80211_tx_result debug_noinline
  830. ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
  831. {
  832. struct sk_buff *skb;
  833. int ac = -1;
  834. if (!tx->sta)
  835. return TX_CONTINUE;
  836. skb_queue_walk(&tx->skbs, skb) {
  837. ac = skb_get_queue_mapping(skb);
  838. tx->sta->tx_fragments++;
  839. tx->sta->tx_bytes[ac] += skb->len;
  840. }
  841. if (ac >= 0)
  842. tx->sta->tx_packets[ac]++;
  843. return TX_CONTINUE;
  844. }
  845. static ieee80211_tx_result debug_noinline
  846. ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
  847. {
  848. if (!tx->key)
  849. return TX_CONTINUE;
  850. switch (tx->key->conf.cipher) {
  851. case WLAN_CIPHER_SUITE_WEP40:
  852. case WLAN_CIPHER_SUITE_WEP104:
  853. return ieee80211_crypto_wep_encrypt(tx);
  854. case WLAN_CIPHER_SUITE_TKIP:
  855. return ieee80211_crypto_tkip_encrypt(tx);
  856. case WLAN_CIPHER_SUITE_CCMP:
  857. return ieee80211_crypto_ccmp_encrypt(
  858. tx, IEEE80211_CCMP_MIC_LEN);
  859. case WLAN_CIPHER_SUITE_CCMP_256:
  860. return ieee80211_crypto_ccmp_encrypt(
  861. tx, IEEE80211_CCMP_256_MIC_LEN);
  862. case WLAN_CIPHER_SUITE_AES_CMAC:
  863. return ieee80211_crypto_aes_cmac_encrypt(tx);
  864. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  865. return ieee80211_crypto_aes_cmac_256_encrypt(tx);
  866. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  867. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  868. return ieee80211_crypto_aes_gmac_encrypt(tx);
  869. case WLAN_CIPHER_SUITE_GCMP:
  870. case WLAN_CIPHER_SUITE_GCMP_256:
  871. return ieee80211_crypto_gcmp_encrypt(tx);
  872. default:
  873. return ieee80211_crypto_hw_encrypt(tx);
  874. }
  875. return TX_DROP;
  876. }
  877. static ieee80211_tx_result debug_noinline
  878. ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
  879. {
  880. struct sk_buff *skb;
  881. struct ieee80211_hdr *hdr;
  882. int next_len;
  883. bool group_addr;
  884. skb_queue_walk(&tx->skbs, skb) {
  885. hdr = (void *) skb->data;
  886. if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
  887. break; /* must not overwrite AID */
  888. if (!skb_queue_is_last(&tx->skbs, skb)) {
  889. struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
  890. next_len = next->len;
  891. } else
  892. next_len = 0;
  893. group_addr = is_multicast_ether_addr(hdr->addr1);
  894. hdr->duration_id =
  895. ieee80211_duration(tx, skb, group_addr, next_len);
  896. }
  897. return TX_CONTINUE;
  898. }
  899. /* actual transmit path */
  900. static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
  901. struct sk_buff *skb,
  902. struct ieee80211_tx_info *info,
  903. struct tid_ampdu_tx *tid_tx,
  904. int tid)
  905. {
  906. bool queued = false;
  907. bool reset_agg_timer = false;
  908. struct sk_buff *purge_skb = NULL;
  909. if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
  910. info->flags |= IEEE80211_TX_CTL_AMPDU;
  911. reset_agg_timer = true;
  912. } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
  913. /*
  914. * nothing -- this aggregation session is being started
  915. * but that might still fail with the driver
  916. */
  917. } else {
  918. spin_lock(&tx->sta->lock);
  919. /*
  920. * Need to re-check now, because we may get here
  921. *
  922. * 1) in the window during which the setup is actually
  923. * already done, but not marked yet because not all
  924. * packets are spliced over to the driver pending
  925. * queue yet -- if this happened we acquire the lock
  926. * either before or after the splice happens, but
  927. * need to recheck which of these cases happened.
  928. *
  929. * 2) during session teardown, if the OPERATIONAL bit
  930. * was cleared due to the teardown but the pointer
  931. * hasn't been assigned NULL yet (or we loaded it
  932. * before it was assigned) -- in this case it may
  933. * now be NULL which means we should just let the
  934. * packet pass through because splicing the frames
  935. * back is already done.
  936. */
  937. tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
  938. if (!tid_tx) {
  939. /* do nothing, let packet pass through */
  940. } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
  941. info->flags |= IEEE80211_TX_CTL_AMPDU;
  942. reset_agg_timer = true;
  943. } else {
  944. queued = true;
  945. info->control.vif = &tx->sdata->vif;
  946. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  947. info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
  948. __skb_queue_tail(&tid_tx->pending, skb);
  949. if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
  950. purge_skb = __skb_dequeue(&tid_tx->pending);
  951. }
  952. spin_unlock(&tx->sta->lock);
  953. if (purge_skb)
  954. ieee80211_free_txskb(&tx->local->hw, purge_skb);
  955. }
  956. /* reset session timer */
  957. if (reset_agg_timer && tid_tx->timeout)
  958. tid_tx->last_tx = jiffies;
  959. return queued;
  960. }
  961. /*
  962. * initialises @tx
  963. */
  964. static ieee80211_tx_result
  965. ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
  966. struct ieee80211_tx_data *tx,
  967. struct sk_buff *skb)
  968. {
  969. struct ieee80211_local *local = sdata->local;
  970. struct ieee80211_hdr *hdr;
  971. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  972. int tid;
  973. u8 *qc;
  974. memset(tx, 0, sizeof(*tx));
  975. tx->skb = skb;
  976. tx->local = local;
  977. tx->sdata = sdata;
  978. __skb_queue_head_init(&tx->skbs);
  979. /*
  980. * If this flag is set to true anywhere, and we get here,
  981. * we are doing the needed processing, so remove the flag
  982. * now.
  983. */
  984. info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  985. hdr = (struct ieee80211_hdr *) skb->data;
  986. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  987. tx->sta = rcu_dereference(sdata->u.vlan.sta);
  988. if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr)
  989. return TX_DROP;
  990. } else if (info->flags & (IEEE80211_TX_CTL_INJECTED |
  991. IEEE80211_TX_INTFL_NL80211_FRAME_TX) ||
  992. tx->sdata->control_port_protocol == tx->skb->protocol) {
  993. tx->sta = sta_info_get_bss(sdata, hdr->addr1);
  994. }
  995. if (!tx->sta)
  996. tx->sta = sta_info_get(sdata, hdr->addr1);
  997. if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
  998. !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
  999. (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION) &&
  1000. !(local->hw.flags & IEEE80211_HW_TX_AMPDU_SETUP_IN_HW)) {
  1001. struct tid_ampdu_tx *tid_tx;
  1002. qc = ieee80211_get_qos_ctl(hdr);
  1003. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  1004. tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
  1005. if (tid_tx) {
  1006. bool queued;
  1007. queued = ieee80211_tx_prep_agg(tx, skb, info,
  1008. tid_tx, tid);
  1009. if (unlikely(queued))
  1010. return TX_QUEUED;
  1011. }
  1012. }
  1013. if (is_multicast_ether_addr(hdr->addr1)) {
  1014. tx->flags &= ~IEEE80211_TX_UNICAST;
  1015. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  1016. } else
  1017. tx->flags |= IEEE80211_TX_UNICAST;
  1018. if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
  1019. if (!(tx->flags & IEEE80211_TX_UNICAST) ||
  1020. skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
  1021. info->flags & IEEE80211_TX_CTL_AMPDU)
  1022. info->flags |= IEEE80211_TX_CTL_DONTFRAG;
  1023. }
  1024. if (!tx->sta)
  1025. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1026. else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT))
  1027. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1028. info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
  1029. return TX_CONTINUE;
  1030. }
  1031. static bool ieee80211_tx_frags(struct ieee80211_local *local,
  1032. struct ieee80211_vif *vif,
  1033. struct ieee80211_sta *sta,
  1034. struct sk_buff_head *skbs,
  1035. bool txpending)
  1036. {
  1037. struct ieee80211_tx_control control;
  1038. struct sk_buff *skb, *tmp;
  1039. unsigned long flags;
  1040. skb_queue_walk_safe(skbs, skb, tmp) {
  1041. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1042. int q = info->hw_queue;
  1043. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1044. if (WARN_ON_ONCE(q >= local->hw.queues)) {
  1045. __skb_unlink(skb, skbs);
  1046. ieee80211_free_txskb(&local->hw, skb);
  1047. continue;
  1048. }
  1049. #endif
  1050. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1051. if (local->queue_stop_reasons[q] ||
  1052. (!txpending && !skb_queue_empty(&local->pending[q]))) {
  1053. if (unlikely(info->flags &
  1054. IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
  1055. if (local->queue_stop_reasons[q] &
  1056. ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
  1057. /*
  1058. * Drop off-channel frames if queues
  1059. * are stopped for any reason other
  1060. * than off-channel operation. Never
  1061. * queue them.
  1062. */
  1063. spin_unlock_irqrestore(
  1064. &local->queue_stop_reason_lock,
  1065. flags);
  1066. ieee80211_purge_tx_queue(&local->hw,
  1067. skbs);
  1068. return true;
  1069. }
  1070. } else {
  1071. /*
  1072. * Since queue is stopped, queue up frames for
  1073. * later transmission from the tx-pending
  1074. * tasklet when the queue is woken again.
  1075. */
  1076. if (txpending)
  1077. skb_queue_splice_init(skbs,
  1078. &local->pending[q]);
  1079. else
  1080. skb_queue_splice_tail_init(skbs,
  1081. &local->pending[q]);
  1082. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1083. flags);
  1084. return false;
  1085. }
  1086. }
  1087. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1088. info->control.vif = vif;
  1089. control.sta = sta;
  1090. __skb_unlink(skb, skbs);
  1091. drv_tx(local, &control, skb);
  1092. }
  1093. return true;
  1094. }
  1095. /*
  1096. * Returns false if the frame couldn't be transmitted but was queued instead.
  1097. */
  1098. static bool __ieee80211_tx(struct ieee80211_local *local,
  1099. struct sk_buff_head *skbs, int led_len,
  1100. struct sta_info *sta, bool txpending)
  1101. {
  1102. struct ieee80211_tx_info *info;
  1103. struct ieee80211_sub_if_data *sdata;
  1104. struct ieee80211_vif *vif;
  1105. struct ieee80211_sta *pubsta;
  1106. struct sk_buff *skb;
  1107. bool result = true;
  1108. __le16 fc;
  1109. if (WARN_ON(skb_queue_empty(skbs)))
  1110. return true;
  1111. skb = skb_peek(skbs);
  1112. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  1113. info = IEEE80211_SKB_CB(skb);
  1114. sdata = vif_to_sdata(info->control.vif);
  1115. if (sta && !sta->uploaded)
  1116. sta = NULL;
  1117. if (sta)
  1118. pubsta = &sta->sta;
  1119. else
  1120. pubsta = NULL;
  1121. switch (sdata->vif.type) {
  1122. case NL80211_IFTYPE_MONITOR:
  1123. if (sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE) {
  1124. vif = &sdata->vif;
  1125. break;
  1126. }
  1127. sdata = rcu_dereference(local->monitor_sdata);
  1128. if (sdata) {
  1129. vif = &sdata->vif;
  1130. info->hw_queue =
  1131. vif->hw_queue[skb_get_queue_mapping(skb)];
  1132. } else if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
  1133. dev_kfree_skb(skb);
  1134. return true;
  1135. } else
  1136. vif = NULL;
  1137. break;
  1138. case NL80211_IFTYPE_AP_VLAN:
  1139. sdata = container_of(sdata->bss,
  1140. struct ieee80211_sub_if_data, u.ap);
  1141. /* fall through */
  1142. default:
  1143. vif = &sdata->vif;
  1144. break;
  1145. }
  1146. result = ieee80211_tx_frags(local, vif, pubsta, skbs,
  1147. txpending);
  1148. ieee80211_tpt_led_trig_tx(local, fc, led_len);
  1149. WARN_ON_ONCE(!skb_queue_empty(skbs));
  1150. return result;
  1151. }
  1152. /*
  1153. * Invoke TX handlers, return 0 on success and non-zero if the
  1154. * frame was dropped or queued.
  1155. */
  1156. static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
  1157. {
  1158. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  1159. ieee80211_tx_result res = TX_DROP;
  1160. #define CALL_TXH(txh) \
  1161. do { \
  1162. res = txh(tx); \
  1163. if (res != TX_CONTINUE) \
  1164. goto txh_done; \
  1165. } while (0)
  1166. CALL_TXH(ieee80211_tx_h_dynamic_ps);
  1167. CALL_TXH(ieee80211_tx_h_check_assoc);
  1168. CALL_TXH(ieee80211_tx_h_ps_buf);
  1169. CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
  1170. CALL_TXH(ieee80211_tx_h_select_key);
  1171. if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
  1172. CALL_TXH(ieee80211_tx_h_rate_ctrl);
  1173. if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
  1174. __skb_queue_tail(&tx->skbs, tx->skb);
  1175. tx->skb = NULL;
  1176. goto txh_done;
  1177. }
  1178. CALL_TXH(ieee80211_tx_h_michael_mic_add);
  1179. CALL_TXH(ieee80211_tx_h_sequence);
  1180. CALL_TXH(ieee80211_tx_h_fragment);
  1181. /* handlers after fragment must be aware of tx info fragmentation! */
  1182. CALL_TXH(ieee80211_tx_h_stats);
  1183. CALL_TXH(ieee80211_tx_h_encrypt);
  1184. if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
  1185. CALL_TXH(ieee80211_tx_h_calculate_duration);
  1186. #undef CALL_TXH
  1187. txh_done:
  1188. if (unlikely(res == TX_DROP)) {
  1189. I802_DEBUG_INC(tx->local->tx_handlers_drop);
  1190. if (tx->skb)
  1191. ieee80211_free_txskb(&tx->local->hw, tx->skb);
  1192. else
  1193. ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
  1194. return -1;
  1195. } else if (unlikely(res == TX_QUEUED)) {
  1196. I802_DEBUG_INC(tx->local->tx_handlers_queued);
  1197. return -1;
  1198. }
  1199. return 0;
  1200. }
  1201. bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
  1202. struct ieee80211_vif *vif, struct sk_buff *skb,
  1203. int band, struct ieee80211_sta **sta)
  1204. {
  1205. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1206. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1207. struct ieee80211_tx_data tx;
  1208. if (ieee80211_tx_prepare(sdata, &tx, skb) == TX_DROP)
  1209. return false;
  1210. info->band = band;
  1211. info->control.vif = vif;
  1212. info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
  1213. if (invoke_tx_handlers(&tx))
  1214. return false;
  1215. if (sta) {
  1216. if (tx.sta)
  1217. *sta = &tx.sta->sta;
  1218. else
  1219. *sta = NULL;
  1220. }
  1221. return true;
  1222. }
  1223. EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
  1224. /*
  1225. * Returns false if the frame couldn't be transmitted but was queued instead.
  1226. */
  1227. static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
  1228. struct sk_buff *skb, bool txpending)
  1229. {
  1230. struct ieee80211_local *local = sdata->local;
  1231. struct ieee80211_tx_data tx;
  1232. ieee80211_tx_result res_prepare;
  1233. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1234. bool result = true;
  1235. int led_len;
  1236. if (unlikely(skb->len < 10)) {
  1237. dev_kfree_skb(skb);
  1238. return true;
  1239. }
  1240. /* initialises tx */
  1241. led_len = skb->len;
  1242. res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
  1243. if (unlikely(res_prepare == TX_DROP)) {
  1244. ieee80211_free_txskb(&local->hw, skb);
  1245. return true;
  1246. } else if (unlikely(res_prepare == TX_QUEUED)) {
  1247. return true;
  1248. }
  1249. /* set up hw_queue value early */
  1250. if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
  1251. !(local->hw.flags & IEEE80211_HW_QUEUE_CONTROL))
  1252. info->hw_queue =
  1253. sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
  1254. if (!invoke_tx_handlers(&tx))
  1255. result = __ieee80211_tx(local, &tx.skbs, led_len,
  1256. tx.sta, txpending);
  1257. return result;
  1258. }
  1259. /* device xmit handlers */
  1260. static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
  1261. struct sk_buff *skb,
  1262. int head_need, bool may_encrypt)
  1263. {
  1264. struct ieee80211_local *local = sdata->local;
  1265. int tail_need = 0;
  1266. if (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt) {
  1267. tail_need = IEEE80211_ENCRYPT_TAILROOM;
  1268. tail_need -= skb_tailroom(skb);
  1269. tail_need = max_t(int, tail_need, 0);
  1270. }
  1271. if (skb_cloned(skb) &&
  1272. (!(local->hw.flags & IEEE80211_HW_SUPPORTS_CLONED_SKBS) ||
  1273. !skb_clone_writable(skb, ETH_HLEN) ||
  1274. sdata->crypto_tx_tailroom_needed_cnt))
  1275. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1276. else if (head_need || tail_need)
  1277. I802_DEBUG_INC(local->tx_expand_skb_head);
  1278. else
  1279. return 0;
  1280. if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
  1281. wiphy_debug(local->hw.wiphy,
  1282. "failed to reallocate TX buffer\n");
  1283. return -ENOMEM;
  1284. }
  1285. return 0;
  1286. }
  1287. void ieee80211_xmit(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  1288. {
  1289. struct ieee80211_local *local = sdata->local;
  1290. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1291. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1292. int headroom;
  1293. bool may_encrypt;
  1294. may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
  1295. headroom = local->tx_headroom;
  1296. if (may_encrypt)
  1297. headroom += sdata->encrypt_headroom;
  1298. headroom -= skb_headroom(skb);
  1299. headroom = max_t(int, 0, headroom);
  1300. if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
  1301. ieee80211_free_txskb(&local->hw, skb);
  1302. return;
  1303. }
  1304. hdr = (struct ieee80211_hdr *) skb->data;
  1305. info->control.vif = &sdata->vif;
  1306. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1307. if (ieee80211_is_data(hdr->frame_control) &&
  1308. is_unicast_ether_addr(hdr->addr1)) {
  1309. if (mesh_nexthop_resolve(sdata, skb))
  1310. return; /* skb queued: don't free */
  1311. } else {
  1312. ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
  1313. }
  1314. }
  1315. ieee80211_set_qos_hdr(sdata, skb);
  1316. ieee80211_tx(sdata, skb, false);
  1317. }
  1318. static bool ieee80211_parse_tx_radiotap(struct sk_buff *skb)
  1319. {
  1320. struct ieee80211_radiotap_iterator iterator;
  1321. struct ieee80211_radiotap_header *rthdr =
  1322. (struct ieee80211_radiotap_header *) skb->data;
  1323. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1324. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
  1325. NULL);
  1326. u16 txflags;
  1327. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  1328. IEEE80211_TX_CTL_DONTFRAG;
  1329. /*
  1330. * for every radiotap entry that is present
  1331. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  1332. * entries present, or -EINVAL on error)
  1333. */
  1334. while (!ret) {
  1335. ret = ieee80211_radiotap_iterator_next(&iterator);
  1336. if (ret)
  1337. continue;
  1338. /* see if this argument is something we can use */
  1339. switch (iterator.this_arg_index) {
  1340. /*
  1341. * You must take care when dereferencing iterator.this_arg
  1342. * for multibyte types... the pointer is not aligned. Use
  1343. * get_unaligned((type *)iterator.this_arg) to dereference
  1344. * iterator.this_arg for type "type" safely on all arches.
  1345. */
  1346. case IEEE80211_RADIOTAP_FLAGS:
  1347. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  1348. /*
  1349. * this indicates that the skb we have been
  1350. * handed has the 32-bit FCS CRC at the end...
  1351. * we should react to that by snipping it off
  1352. * because it will be recomputed and added
  1353. * on transmission
  1354. */
  1355. if (skb->len < (iterator._max_length + FCS_LEN))
  1356. return false;
  1357. skb_trim(skb, skb->len - FCS_LEN);
  1358. }
  1359. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
  1360. info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1361. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
  1362. info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
  1363. break;
  1364. case IEEE80211_RADIOTAP_TX_FLAGS:
  1365. txflags = get_unaligned_le16(iterator.this_arg);
  1366. if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
  1367. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  1368. break;
  1369. /*
  1370. * Please update the file
  1371. * Documentation/networking/mac80211-injection.txt
  1372. * when parsing new fields here.
  1373. */
  1374. default:
  1375. break;
  1376. }
  1377. }
  1378. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  1379. return false;
  1380. /*
  1381. * remove the radiotap header
  1382. * iterator->_max_length was sanity-checked against
  1383. * skb->len by iterator init
  1384. */
  1385. skb_pull(skb, iterator._max_length);
  1386. return true;
  1387. }
  1388. netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1389. struct net_device *dev)
  1390. {
  1391. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1392. struct ieee80211_chanctx_conf *chanctx_conf;
  1393. struct ieee80211_radiotap_header *prthdr =
  1394. (struct ieee80211_radiotap_header *)skb->data;
  1395. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1396. struct ieee80211_hdr *hdr;
  1397. struct ieee80211_sub_if_data *tmp_sdata, *sdata;
  1398. struct cfg80211_chan_def *chandef;
  1399. u16 len_rthdr;
  1400. int hdrlen;
  1401. /* check for not even having the fixed radiotap header part */
  1402. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1403. goto fail; /* too short to be possibly valid */
  1404. /* is it a header version we can trust to find length from? */
  1405. if (unlikely(prthdr->it_version))
  1406. goto fail; /* only version 0 is supported */
  1407. /* then there must be a radiotap header with a length we can use */
  1408. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1409. /* does the skb contain enough to deliver on the alleged length? */
  1410. if (unlikely(skb->len < len_rthdr))
  1411. goto fail; /* skb too short for claimed rt header extent */
  1412. /*
  1413. * fix up the pointers accounting for the radiotap
  1414. * header still being in there. We are being given
  1415. * a precooked IEEE80211 header so no need for
  1416. * normal processing
  1417. */
  1418. skb_set_mac_header(skb, len_rthdr);
  1419. /*
  1420. * these are just fixed to the end of the rt area since we
  1421. * don't have any better information and at this point, nobody cares
  1422. */
  1423. skb_set_network_header(skb, len_rthdr);
  1424. skb_set_transport_header(skb, len_rthdr);
  1425. if (skb->len < len_rthdr + 2)
  1426. goto fail;
  1427. hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
  1428. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1429. if (skb->len < len_rthdr + hdrlen)
  1430. goto fail;
  1431. /*
  1432. * Initialize skb->protocol if the injected frame is a data frame
  1433. * carrying a rfc1042 header
  1434. */
  1435. if (ieee80211_is_data(hdr->frame_control) &&
  1436. skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
  1437. u8 *payload = (u8 *)hdr + hdrlen;
  1438. if (ether_addr_equal(payload, rfc1042_header))
  1439. skb->protocol = cpu_to_be16((payload[6] << 8) |
  1440. payload[7]);
  1441. }
  1442. memset(info, 0, sizeof(*info));
  1443. info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  1444. IEEE80211_TX_CTL_INJECTED;
  1445. /* process and remove the injection radiotap header */
  1446. if (!ieee80211_parse_tx_radiotap(skb))
  1447. goto fail;
  1448. rcu_read_lock();
  1449. /*
  1450. * We process outgoing injected frames that have a local address
  1451. * we handle as though they are non-injected frames.
  1452. * This code here isn't entirely correct, the local MAC address
  1453. * isn't always enough to find the interface to use; for proper
  1454. * VLAN/WDS support we will need a different mechanism (which
  1455. * likely isn't going to be monitor interfaces).
  1456. */
  1457. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1458. list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
  1459. if (!ieee80211_sdata_running(tmp_sdata))
  1460. continue;
  1461. if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1462. tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1463. tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
  1464. continue;
  1465. if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
  1466. sdata = tmp_sdata;
  1467. break;
  1468. }
  1469. }
  1470. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1471. if (!chanctx_conf) {
  1472. tmp_sdata = rcu_dereference(local->monitor_sdata);
  1473. if (tmp_sdata)
  1474. chanctx_conf =
  1475. rcu_dereference(tmp_sdata->vif.chanctx_conf);
  1476. }
  1477. if (chanctx_conf)
  1478. chandef = &chanctx_conf->def;
  1479. else if (!local->use_chanctx)
  1480. chandef = &local->_oper_chandef;
  1481. else
  1482. goto fail_rcu;
  1483. /*
  1484. * Frame injection is not allowed if beaconing is not allowed
  1485. * or if we need radar detection. Beaconing is usually not allowed when
  1486. * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
  1487. * Passive scan is also used in world regulatory domains where
  1488. * your country is not known and as such it should be treated as
  1489. * NO TX unless the channel is explicitly allowed in which case
  1490. * your current regulatory domain would not have the passive scan
  1491. * flag.
  1492. *
  1493. * Since AP mode uses monitor interfaces to inject/TX management
  1494. * frames we can make AP mode the exception to this rule once it
  1495. * supports radar detection as its implementation can deal with
  1496. * radar detection by itself. We can do that later by adding a
  1497. * monitor flag interfaces used for AP support.
  1498. */
  1499. if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
  1500. sdata->vif.type))
  1501. goto fail_rcu;
  1502. info->band = chandef->chan->band;
  1503. ieee80211_xmit(sdata, skb);
  1504. rcu_read_unlock();
  1505. return NETDEV_TX_OK;
  1506. fail_rcu:
  1507. rcu_read_unlock();
  1508. fail:
  1509. dev_kfree_skb(skb);
  1510. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  1511. }
  1512. /*
  1513. * Measure Tx frame arrival time for Tx latency statistics calculation
  1514. * A single Tx frame latency should be measured from when it is entering the
  1515. * Kernel until we receive Tx complete confirmation indication and the skb is
  1516. * freed.
  1517. */
  1518. static void ieee80211_tx_latency_start_msrmnt(struct ieee80211_local *local,
  1519. struct sk_buff *skb)
  1520. {
  1521. struct ieee80211_tx_latency_bin_ranges *tx_latency;
  1522. tx_latency = rcu_dereference(local->tx_latency);
  1523. if (!tx_latency)
  1524. return;
  1525. skb->tstamp = ktime_get();
  1526. }
  1527. /**
  1528. * ieee80211_build_hdr - build 802.11 header in the given frame
  1529. * @sdata: virtual interface to build the header for
  1530. * @skb: the skb to build the header in
  1531. * @info_flags: skb flags to set
  1532. *
  1533. * This function takes the skb with 802.3 header and reformats the header to
  1534. * the appropriate IEEE 802.11 header based on which interface the packet is
  1535. * being transmitted on.
  1536. *
  1537. * Note that this function also takes care of the TX status request and
  1538. * potential unsharing of the SKB - this needs to be interleaved with the
  1539. * header building.
  1540. *
  1541. * The function requires the read-side RCU lock held
  1542. *
  1543. * Returns: the (possibly reallocated) skb or an ERR_PTR() code
  1544. */
  1545. static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
  1546. struct sk_buff *skb, u32 info_flags)
  1547. {
  1548. struct ieee80211_local *local = sdata->local;
  1549. struct ieee80211_tx_info *info;
  1550. int head_need;
  1551. u16 ethertype, hdrlen, meshhdrlen = 0;
  1552. __le16 fc;
  1553. struct ieee80211_hdr hdr;
  1554. struct ieee80211s_hdr mesh_hdr __maybe_unused;
  1555. struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
  1556. const u8 *encaps_data;
  1557. int encaps_len, skip_header_bytes;
  1558. int nh_pos, h_pos;
  1559. struct sta_info *sta = NULL;
  1560. bool wme_sta = false, authorized = false, tdls_auth = false;
  1561. bool tdls_peer = false, tdls_setup_frame = false;
  1562. bool multicast;
  1563. u16 info_id = 0;
  1564. struct ieee80211_chanctx_conf *chanctx_conf;
  1565. struct ieee80211_sub_if_data *ap_sdata;
  1566. enum ieee80211_band band;
  1567. int ret;
  1568. /* convert Ethernet header to proper 802.11 header (based on
  1569. * operation mode) */
  1570. ethertype = (skb->data[12] << 8) | skb->data[13];
  1571. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  1572. switch (sdata->vif.type) {
  1573. case NL80211_IFTYPE_AP_VLAN:
  1574. sta = rcu_dereference(sdata->u.vlan.sta);
  1575. if (sta) {
  1576. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  1577. /* RA TA DA SA */
  1578. memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
  1579. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  1580. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1581. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1582. hdrlen = 30;
  1583. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  1584. wme_sta = sta->sta.wme;
  1585. }
  1586. ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  1587. u.ap);
  1588. chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
  1589. if (!chanctx_conf) {
  1590. ret = -ENOTCONN;
  1591. goto free;
  1592. }
  1593. band = chanctx_conf->def.chan->band;
  1594. if (sta)
  1595. break;
  1596. /* fall through */
  1597. case NL80211_IFTYPE_AP:
  1598. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1599. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1600. if (!chanctx_conf) {
  1601. ret = -ENOTCONN;
  1602. goto free;
  1603. }
  1604. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  1605. /* DA BSSID SA */
  1606. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1607. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  1608. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1609. hdrlen = 24;
  1610. band = chanctx_conf->def.chan->band;
  1611. break;
  1612. case NL80211_IFTYPE_WDS:
  1613. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  1614. /* RA TA DA SA */
  1615. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  1616. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  1617. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1618. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1619. hdrlen = 30;
  1620. /*
  1621. * This is the exception! WDS style interfaces are prohibited
  1622. * when channel contexts are in used so this must be valid
  1623. */
  1624. band = local->hw.conf.chandef.chan->band;
  1625. break;
  1626. #ifdef CONFIG_MAC80211_MESH
  1627. case NL80211_IFTYPE_MESH_POINT:
  1628. if (!is_multicast_ether_addr(skb->data)) {
  1629. struct sta_info *next_hop;
  1630. bool mpp_lookup = true;
  1631. mpath = mesh_path_lookup(sdata, skb->data);
  1632. if (mpath) {
  1633. mpp_lookup = false;
  1634. next_hop = rcu_dereference(mpath->next_hop);
  1635. if (!next_hop ||
  1636. !(mpath->flags & (MESH_PATH_ACTIVE |
  1637. MESH_PATH_RESOLVING)))
  1638. mpp_lookup = true;
  1639. }
  1640. if (mpp_lookup)
  1641. mppath = mpp_path_lookup(sdata, skb->data);
  1642. if (mppath && mpath)
  1643. mesh_path_del(mpath->sdata, mpath->dst);
  1644. }
  1645. /*
  1646. * Use address extension if it is a packet from
  1647. * another interface or if we know the destination
  1648. * is being proxied by a portal (i.e. portal address
  1649. * differs from proxied address)
  1650. */
  1651. if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
  1652. !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
  1653. hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
  1654. skb->data, skb->data + ETH_ALEN);
  1655. meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
  1656. NULL, NULL);
  1657. } else {
  1658. /* DS -> MBSS (802.11-2012 13.11.3.3).
  1659. * For unicast with unknown forwarding information,
  1660. * destination might be in the MBSS or if that fails
  1661. * forwarded to another mesh gate. In either case
  1662. * resolution will be handled in ieee80211_xmit(), so
  1663. * leave the original DA. This also works for mcast */
  1664. const u8 *mesh_da = skb->data;
  1665. if (mppath)
  1666. mesh_da = mppath->mpp;
  1667. else if (mpath)
  1668. mesh_da = mpath->dst;
  1669. hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
  1670. mesh_da, sdata->vif.addr);
  1671. if (is_multicast_ether_addr(mesh_da))
  1672. /* DA TA mSA AE:SA */
  1673. meshhdrlen = ieee80211_new_mesh_header(
  1674. sdata, &mesh_hdr,
  1675. skb->data + ETH_ALEN, NULL);
  1676. else
  1677. /* RA TA mDA mSA AE:DA SA */
  1678. meshhdrlen = ieee80211_new_mesh_header(
  1679. sdata, &mesh_hdr, skb->data,
  1680. skb->data + ETH_ALEN);
  1681. }
  1682. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1683. if (!chanctx_conf) {
  1684. ret = -ENOTCONN;
  1685. goto free;
  1686. }
  1687. band = chanctx_conf->def.chan->band;
  1688. break;
  1689. #endif
  1690. case NL80211_IFTYPE_STATION:
  1691. if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
  1692. sta = sta_info_get(sdata, skb->data);
  1693. if (sta) {
  1694. authorized = test_sta_flag(sta,
  1695. WLAN_STA_AUTHORIZED);
  1696. wme_sta = sta->sta.wme;
  1697. tdls_peer = test_sta_flag(sta,
  1698. WLAN_STA_TDLS_PEER);
  1699. tdls_auth = test_sta_flag(sta,
  1700. WLAN_STA_TDLS_PEER_AUTH);
  1701. }
  1702. if (tdls_peer)
  1703. tdls_setup_frame =
  1704. ethertype == ETH_P_TDLS &&
  1705. skb->len > 14 &&
  1706. skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
  1707. }
  1708. /*
  1709. * TDLS link during setup - throw out frames to peer. We allow
  1710. * TDLS-setup frames to unauthorized peers for the special case
  1711. * of a link teardown after a TDLS sta is removed due to being
  1712. * unreachable.
  1713. */
  1714. if (tdls_peer && !tdls_auth && !tdls_setup_frame) {
  1715. ret = -EINVAL;
  1716. goto free;
  1717. }
  1718. /* send direct packets to authorized TDLS peers */
  1719. if (tdls_peer && tdls_auth) {
  1720. /* DA SA BSSID */
  1721. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1722. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1723. memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
  1724. hdrlen = 24;
  1725. } else if (sdata->u.mgd.use_4addr &&
  1726. cpu_to_be16(ethertype) != sdata->control_port_protocol) {
  1727. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
  1728. IEEE80211_FCTL_TODS);
  1729. /* RA TA DA SA */
  1730. memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
  1731. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  1732. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1733. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1734. hdrlen = 30;
  1735. } else {
  1736. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  1737. /* BSSID SA DA */
  1738. memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
  1739. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1740. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1741. hdrlen = 24;
  1742. }
  1743. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1744. if (!chanctx_conf) {
  1745. ret = -ENOTCONN;
  1746. goto free;
  1747. }
  1748. band = chanctx_conf->def.chan->band;
  1749. break;
  1750. case NL80211_IFTYPE_OCB:
  1751. /* DA SA BSSID */
  1752. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1753. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1754. eth_broadcast_addr(hdr.addr3);
  1755. hdrlen = 24;
  1756. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1757. if (!chanctx_conf) {
  1758. ret = -ENOTCONN;
  1759. goto free;
  1760. }
  1761. band = chanctx_conf->def.chan->band;
  1762. break;
  1763. case NL80211_IFTYPE_ADHOC:
  1764. /* DA SA BSSID */
  1765. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1766. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1767. memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
  1768. hdrlen = 24;
  1769. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1770. if (!chanctx_conf) {
  1771. ret = -ENOTCONN;
  1772. goto free;
  1773. }
  1774. band = chanctx_conf->def.chan->band;
  1775. break;
  1776. default:
  1777. ret = -EINVAL;
  1778. goto free;
  1779. }
  1780. /*
  1781. * There's no need to try to look up the destination
  1782. * if it is a multicast address (which can only happen
  1783. * in AP mode)
  1784. */
  1785. multicast = is_multicast_ether_addr(hdr.addr1);
  1786. if (!multicast) {
  1787. sta = sta_info_get(sdata, hdr.addr1);
  1788. if (sta) {
  1789. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  1790. wme_sta = sta->sta.wme;
  1791. }
  1792. }
  1793. /* For mesh, the use of the QoS header is mandatory */
  1794. if (ieee80211_vif_is_mesh(&sdata->vif))
  1795. wme_sta = true;
  1796. /* receiver and we are QoS enabled, use a QoS type frame */
  1797. if (wme_sta && local->hw.queues >= IEEE80211_NUM_ACS) {
  1798. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  1799. hdrlen += 2;
  1800. }
  1801. /*
  1802. * Drop unicast frames to unauthorised stations unless they are
  1803. * EAPOL frames from the local station.
  1804. */
  1805. if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
  1806. (sdata->vif.type != NL80211_IFTYPE_OCB) &&
  1807. !multicast && !authorized &&
  1808. (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
  1809. !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
  1810. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1811. net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
  1812. sdata->name, hdr.addr1);
  1813. #endif
  1814. I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
  1815. ret = -EPERM;
  1816. goto free;
  1817. }
  1818. if (unlikely(!multicast && skb->sk &&
  1819. skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
  1820. struct sk_buff *ack_skb = skb_clone_sk(skb);
  1821. if (ack_skb) {
  1822. unsigned long flags;
  1823. int id;
  1824. spin_lock_irqsave(&local->ack_status_lock, flags);
  1825. id = idr_alloc(&local->ack_status_frames, ack_skb,
  1826. 1, 0x10000, GFP_ATOMIC);
  1827. spin_unlock_irqrestore(&local->ack_status_lock, flags);
  1828. if (id >= 0) {
  1829. info_id = id;
  1830. info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  1831. } else {
  1832. kfree_skb(ack_skb);
  1833. }
  1834. }
  1835. }
  1836. /*
  1837. * If the skb is shared we need to obtain our own copy.
  1838. */
  1839. if (skb_shared(skb)) {
  1840. struct sk_buff *tmp_skb = skb;
  1841. /* can't happen -- skb is a clone if info_id != 0 */
  1842. WARN_ON(info_id);
  1843. skb = skb_clone(skb, GFP_ATOMIC);
  1844. kfree_skb(tmp_skb);
  1845. if (!skb) {
  1846. ret = -ENOMEM;
  1847. goto free;
  1848. }
  1849. }
  1850. hdr.frame_control = fc;
  1851. hdr.duration_id = 0;
  1852. hdr.seq_ctrl = 0;
  1853. skip_header_bytes = ETH_HLEN;
  1854. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1855. encaps_data = bridge_tunnel_header;
  1856. encaps_len = sizeof(bridge_tunnel_header);
  1857. skip_header_bytes -= 2;
  1858. } else if (ethertype >= ETH_P_802_3_MIN) {
  1859. encaps_data = rfc1042_header;
  1860. encaps_len = sizeof(rfc1042_header);
  1861. skip_header_bytes -= 2;
  1862. } else {
  1863. encaps_data = NULL;
  1864. encaps_len = 0;
  1865. }
  1866. nh_pos = skb_network_header(skb) - skb->data;
  1867. h_pos = skb_transport_header(skb) - skb->data;
  1868. skb_pull(skb, skip_header_bytes);
  1869. nh_pos -= skip_header_bytes;
  1870. h_pos -= skip_header_bytes;
  1871. head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
  1872. /*
  1873. * So we need to modify the skb header and hence need a copy of
  1874. * that. The head_need variable above doesn't, so far, include
  1875. * the needed header space that we don't need right away. If we
  1876. * can, then we don't reallocate right now but only after the
  1877. * frame arrives at the master device (if it does...)
  1878. *
  1879. * If we cannot, however, then we will reallocate to include all
  1880. * the ever needed space. Also, if we need to reallocate it anyway,
  1881. * make it big enough for everything we may ever need.
  1882. */
  1883. if (head_need > 0 || skb_cloned(skb)) {
  1884. head_need += sdata->encrypt_headroom;
  1885. head_need += local->tx_headroom;
  1886. head_need = max_t(int, 0, head_need);
  1887. if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
  1888. ieee80211_free_txskb(&local->hw, skb);
  1889. skb = NULL;
  1890. return ERR_PTR(-ENOMEM);
  1891. }
  1892. }
  1893. if (encaps_data) {
  1894. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1895. nh_pos += encaps_len;
  1896. h_pos += encaps_len;
  1897. }
  1898. #ifdef CONFIG_MAC80211_MESH
  1899. if (meshhdrlen > 0) {
  1900. memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
  1901. nh_pos += meshhdrlen;
  1902. h_pos += meshhdrlen;
  1903. }
  1904. #endif
  1905. if (ieee80211_is_data_qos(fc)) {
  1906. __le16 *qos_control;
  1907. qos_control = (__le16 *) skb_push(skb, 2);
  1908. memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
  1909. /*
  1910. * Maybe we could actually set some fields here, for now just
  1911. * initialise to zero to indicate no special operation.
  1912. */
  1913. *qos_control = 0;
  1914. } else
  1915. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1916. nh_pos += hdrlen;
  1917. h_pos += hdrlen;
  1918. /* Update skb pointers to various headers since this modified frame
  1919. * is going to go through Linux networking code that may potentially
  1920. * need things like pointer to IP header. */
  1921. skb_set_mac_header(skb, 0);
  1922. skb_set_network_header(skb, nh_pos);
  1923. skb_set_transport_header(skb, h_pos);
  1924. info = IEEE80211_SKB_CB(skb);
  1925. memset(info, 0, sizeof(*info));
  1926. info->flags = info_flags;
  1927. info->ack_frame_id = info_id;
  1928. info->band = band;
  1929. return skb;
  1930. free:
  1931. kfree_skb(skb);
  1932. return ERR_PTR(ret);
  1933. }
  1934. void __ieee80211_subif_start_xmit(struct sk_buff *skb,
  1935. struct net_device *dev,
  1936. u32 info_flags)
  1937. {
  1938. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1939. struct ieee80211_local *local = sdata->local;
  1940. if (unlikely(skb->len < ETH_HLEN)) {
  1941. kfree_skb(skb);
  1942. return;
  1943. }
  1944. rcu_read_lock();
  1945. /* Measure frame arrival for Tx latency statistics calculation */
  1946. ieee80211_tx_latency_start_msrmnt(local, skb);
  1947. skb = ieee80211_build_hdr(sdata, skb, info_flags);
  1948. if (IS_ERR(skb))
  1949. goto out;
  1950. dev->stats.tx_packets++;
  1951. dev->stats.tx_bytes += skb->len;
  1952. dev->trans_start = jiffies;
  1953. ieee80211_xmit(sdata, skb);
  1954. out:
  1955. rcu_read_unlock();
  1956. }
  1957. /**
  1958. * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
  1959. * @skb: packet to be sent
  1960. * @dev: incoming interface
  1961. *
  1962. * On failure skb will be freed.
  1963. */
  1964. netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
  1965. struct net_device *dev)
  1966. {
  1967. __ieee80211_subif_start_xmit(skb, dev, 0);
  1968. return NETDEV_TX_OK;
  1969. }
  1970. struct sk_buff *
  1971. ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
  1972. struct sk_buff *skb, u32 info_flags)
  1973. {
  1974. struct ieee80211_hdr *hdr;
  1975. struct ieee80211_tx_data tx = {
  1976. .local = sdata->local,
  1977. .sdata = sdata,
  1978. };
  1979. rcu_read_lock();
  1980. skb = ieee80211_build_hdr(sdata, skb, info_flags);
  1981. if (IS_ERR(skb))
  1982. goto out;
  1983. hdr = (void *)skb->data;
  1984. tx.sta = sta_info_get(sdata, hdr->addr1);
  1985. tx.skb = skb;
  1986. if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
  1987. rcu_read_unlock();
  1988. kfree_skb(skb);
  1989. return ERR_PTR(-EINVAL);
  1990. }
  1991. out:
  1992. rcu_read_unlock();
  1993. return skb;
  1994. }
  1995. /*
  1996. * ieee80211_clear_tx_pending may not be called in a context where
  1997. * it is possible that it packets could come in again.
  1998. */
  1999. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  2000. {
  2001. struct sk_buff *skb;
  2002. int i;
  2003. for (i = 0; i < local->hw.queues; i++) {
  2004. while ((skb = skb_dequeue(&local->pending[i])) != NULL)
  2005. ieee80211_free_txskb(&local->hw, skb);
  2006. }
  2007. }
  2008. /*
  2009. * Returns false if the frame couldn't be transmitted but was queued instead,
  2010. * which in this case means re-queued -- take as an indication to stop sending
  2011. * more pending frames.
  2012. */
  2013. static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
  2014. struct sk_buff *skb)
  2015. {
  2016. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2017. struct ieee80211_sub_if_data *sdata;
  2018. struct sta_info *sta;
  2019. struct ieee80211_hdr *hdr;
  2020. bool result;
  2021. struct ieee80211_chanctx_conf *chanctx_conf;
  2022. sdata = vif_to_sdata(info->control.vif);
  2023. if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
  2024. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2025. if (unlikely(!chanctx_conf)) {
  2026. dev_kfree_skb(skb);
  2027. return true;
  2028. }
  2029. info->band = chanctx_conf->def.chan->band;
  2030. result = ieee80211_tx(sdata, skb, true);
  2031. } else {
  2032. struct sk_buff_head skbs;
  2033. __skb_queue_head_init(&skbs);
  2034. __skb_queue_tail(&skbs, skb);
  2035. hdr = (struct ieee80211_hdr *)skb->data;
  2036. sta = sta_info_get(sdata, hdr->addr1);
  2037. result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
  2038. }
  2039. return result;
  2040. }
  2041. /*
  2042. * Transmit all pending packets. Called from tasklet.
  2043. */
  2044. void ieee80211_tx_pending(unsigned long data)
  2045. {
  2046. struct ieee80211_local *local = (struct ieee80211_local *)data;
  2047. unsigned long flags;
  2048. int i;
  2049. bool txok;
  2050. rcu_read_lock();
  2051. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  2052. for (i = 0; i < local->hw.queues; i++) {
  2053. /*
  2054. * If queue is stopped by something other than due to pending
  2055. * frames, or we have no pending frames, proceed to next queue.
  2056. */
  2057. if (local->queue_stop_reasons[i] ||
  2058. skb_queue_empty(&local->pending[i]))
  2059. continue;
  2060. while (!skb_queue_empty(&local->pending[i])) {
  2061. struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
  2062. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2063. if (WARN_ON(!info->control.vif)) {
  2064. ieee80211_free_txskb(&local->hw, skb);
  2065. continue;
  2066. }
  2067. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  2068. flags);
  2069. txok = ieee80211_tx_pending_skb(local, skb);
  2070. spin_lock_irqsave(&local->queue_stop_reason_lock,
  2071. flags);
  2072. if (!txok)
  2073. break;
  2074. }
  2075. if (skb_queue_empty(&local->pending[i]))
  2076. ieee80211_propagate_queue_wake(local, i);
  2077. }
  2078. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  2079. rcu_read_unlock();
  2080. }
  2081. /* functions for drivers to get certain frames */
  2082. static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
  2083. struct ps_data *ps, struct sk_buff *skb,
  2084. bool is_template)
  2085. {
  2086. u8 *pos, *tim;
  2087. int aid0 = 0;
  2088. int i, have_bits = 0, n1, n2;
  2089. /* Generate bitmap for TIM only if there are any STAs in power save
  2090. * mode. */
  2091. if (atomic_read(&ps->num_sta_ps) > 0)
  2092. /* in the hope that this is faster than
  2093. * checking byte-for-byte */
  2094. have_bits = !bitmap_empty((unsigned long *)ps->tim,
  2095. IEEE80211_MAX_AID+1);
  2096. if (!is_template) {
  2097. if (ps->dtim_count == 0)
  2098. ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
  2099. else
  2100. ps->dtim_count--;
  2101. }
  2102. tim = pos = (u8 *) skb_put(skb, 6);
  2103. *pos++ = WLAN_EID_TIM;
  2104. *pos++ = 4;
  2105. *pos++ = ps->dtim_count;
  2106. *pos++ = sdata->vif.bss_conf.dtim_period;
  2107. if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
  2108. aid0 = 1;
  2109. ps->dtim_bc_mc = aid0 == 1;
  2110. if (have_bits) {
  2111. /* Find largest even number N1 so that bits numbered 1 through
  2112. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  2113. * (N2 + 1) x 8 through 2007 are 0. */
  2114. n1 = 0;
  2115. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  2116. if (ps->tim[i]) {
  2117. n1 = i & 0xfe;
  2118. break;
  2119. }
  2120. }
  2121. n2 = n1;
  2122. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  2123. if (ps->tim[i]) {
  2124. n2 = i;
  2125. break;
  2126. }
  2127. }
  2128. /* Bitmap control */
  2129. *pos++ = n1 | aid0;
  2130. /* Part Virt Bitmap */
  2131. skb_put(skb, n2 - n1);
  2132. memcpy(pos, ps->tim + n1, n2 - n1 + 1);
  2133. tim[1] = n2 - n1 + 4;
  2134. } else {
  2135. *pos++ = aid0; /* Bitmap control */
  2136. *pos++ = 0; /* Part Virt Bitmap */
  2137. }
  2138. }
  2139. static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
  2140. struct ps_data *ps, struct sk_buff *skb,
  2141. bool is_template)
  2142. {
  2143. struct ieee80211_local *local = sdata->local;
  2144. /*
  2145. * Not very nice, but we want to allow the driver to call
  2146. * ieee80211_beacon_get() as a response to the set_tim()
  2147. * callback. That, however, is already invoked under the
  2148. * sta_lock to guarantee consistent and race-free update
  2149. * of the tim bitmap in mac80211 and the driver.
  2150. */
  2151. if (local->tim_in_locked_section) {
  2152. __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
  2153. } else {
  2154. spin_lock_bh(&local->tim_lock);
  2155. __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
  2156. spin_unlock_bh(&local->tim_lock);
  2157. }
  2158. return 0;
  2159. }
  2160. static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
  2161. struct beacon_data *beacon)
  2162. {
  2163. struct probe_resp *resp;
  2164. u8 *beacon_data;
  2165. size_t beacon_data_len;
  2166. int i;
  2167. u8 count = beacon->csa_current_counter;
  2168. switch (sdata->vif.type) {
  2169. case NL80211_IFTYPE_AP:
  2170. beacon_data = beacon->tail;
  2171. beacon_data_len = beacon->tail_len;
  2172. break;
  2173. case NL80211_IFTYPE_ADHOC:
  2174. beacon_data = beacon->head;
  2175. beacon_data_len = beacon->head_len;
  2176. break;
  2177. case NL80211_IFTYPE_MESH_POINT:
  2178. beacon_data = beacon->head;
  2179. beacon_data_len = beacon->head_len;
  2180. break;
  2181. default:
  2182. return;
  2183. }
  2184. rcu_read_lock();
  2185. for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
  2186. resp = rcu_dereference(sdata->u.ap.probe_resp);
  2187. if (beacon->csa_counter_offsets[i]) {
  2188. if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
  2189. beacon_data_len)) {
  2190. rcu_read_unlock();
  2191. return;
  2192. }
  2193. beacon_data[beacon->csa_counter_offsets[i]] = count;
  2194. }
  2195. if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
  2196. resp->data[resp->csa_counter_offsets[i]] = count;
  2197. }
  2198. rcu_read_unlock();
  2199. }
  2200. u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
  2201. {
  2202. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2203. struct beacon_data *beacon = NULL;
  2204. u8 count = 0;
  2205. rcu_read_lock();
  2206. if (sdata->vif.type == NL80211_IFTYPE_AP)
  2207. beacon = rcu_dereference(sdata->u.ap.beacon);
  2208. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  2209. beacon = rcu_dereference(sdata->u.ibss.presp);
  2210. else if (ieee80211_vif_is_mesh(&sdata->vif))
  2211. beacon = rcu_dereference(sdata->u.mesh.beacon);
  2212. if (!beacon)
  2213. goto unlock;
  2214. beacon->csa_current_counter--;
  2215. /* the counter should never reach 0 */
  2216. WARN_ON_ONCE(!beacon->csa_current_counter);
  2217. count = beacon->csa_current_counter;
  2218. unlock:
  2219. rcu_read_unlock();
  2220. return count;
  2221. }
  2222. EXPORT_SYMBOL(ieee80211_csa_update_counter);
  2223. bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
  2224. {
  2225. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2226. struct beacon_data *beacon = NULL;
  2227. u8 *beacon_data;
  2228. size_t beacon_data_len;
  2229. int ret = false;
  2230. if (!ieee80211_sdata_running(sdata))
  2231. return false;
  2232. rcu_read_lock();
  2233. if (vif->type == NL80211_IFTYPE_AP) {
  2234. struct ieee80211_if_ap *ap = &sdata->u.ap;
  2235. beacon = rcu_dereference(ap->beacon);
  2236. if (WARN_ON(!beacon || !beacon->tail))
  2237. goto out;
  2238. beacon_data = beacon->tail;
  2239. beacon_data_len = beacon->tail_len;
  2240. } else if (vif->type == NL80211_IFTYPE_ADHOC) {
  2241. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  2242. beacon = rcu_dereference(ifibss->presp);
  2243. if (!beacon)
  2244. goto out;
  2245. beacon_data = beacon->head;
  2246. beacon_data_len = beacon->head_len;
  2247. } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
  2248. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  2249. beacon = rcu_dereference(ifmsh->beacon);
  2250. if (!beacon)
  2251. goto out;
  2252. beacon_data = beacon->head;
  2253. beacon_data_len = beacon->head_len;
  2254. } else {
  2255. WARN_ON(1);
  2256. goto out;
  2257. }
  2258. if (!beacon->csa_counter_offsets[0])
  2259. goto out;
  2260. if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
  2261. goto out;
  2262. if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
  2263. ret = true;
  2264. out:
  2265. rcu_read_unlock();
  2266. return ret;
  2267. }
  2268. EXPORT_SYMBOL(ieee80211_csa_is_complete);
  2269. static struct sk_buff *
  2270. __ieee80211_beacon_get(struct ieee80211_hw *hw,
  2271. struct ieee80211_vif *vif,
  2272. struct ieee80211_mutable_offsets *offs,
  2273. bool is_template)
  2274. {
  2275. struct ieee80211_local *local = hw_to_local(hw);
  2276. struct beacon_data *beacon = NULL;
  2277. struct sk_buff *skb = NULL;
  2278. struct ieee80211_tx_info *info;
  2279. struct ieee80211_sub_if_data *sdata = NULL;
  2280. enum ieee80211_band band;
  2281. struct ieee80211_tx_rate_control txrc;
  2282. struct ieee80211_chanctx_conf *chanctx_conf;
  2283. int csa_off_base = 0;
  2284. rcu_read_lock();
  2285. sdata = vif_to_sdata(vif);
  2286. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2287. if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
  2288. goto out;
  2289. if (offs)
  2290. memset(offs, 0, sizeof(*offs));
  2291. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  2292. struct ieee80211_if_ap *ap = &sdata->u.ap;
  2293. beacon = rcu_dereference(ap->beacon);
  2294. if (beacon) {
  2295. if (beacon->csa_counter_offsets[0]) {
  2296. if (!is_template)
  2297. ieee80211_csa_update_counter(vif);
  2298. ieee80211_set_csa(sdata, beacon);
  2299. }
  2300. /*
  2301. * headroom, head length,
  2302. * tail length and maximum TIM length
  2303. */
  2304. skb = dev_alloc_skb(local->tx_headroom +
  2305. beacon->head_len +
  2306. beacon->tail_len + 256 +
  2307. local->hw.extra_beacon_tailroom);
  2308. if (!skb)
  2309. goto out;
  2310. skb_reserve(skb, local->tx_headroom);
  2311. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  2312. beacon->head_len);
  2313. ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
  2314. is_template);
  2315. if (offs) {
  2316. offs->tim_offset = beacon->head_len;
  2317. offs->tim_length = skb->len - beacon->head_len;
  2318. /* for AP the csa offsets are from tail */
  2319. csa_off_base = skb->len;
  2320. }
  2321. if (beacon->tail)
  2322. memcpy(skb_put(skb, beacon->tail_len),
  2323. beacon->tail, beacon->tail_len);
  2324. } else
  2325. goto out;
  2326. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  2327. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  2328. struct ieee80211_hdr *hdr;
  2329. beacon = rcu_dereference(ifibss->presp);
  2330. if (!beacon)
  2331. goto out;
  2332. if (beacon->csa_counter_offsets[0]) {
  2333. if (!is_template)
  2334. ieee80211_csa_update_counter(vif);
  2335. ieee80211_set_csa(sdata, beacon);
  2336. }
  2337. skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
  2338. local->hw.extra_beacon_tailroom);
  2339. if (!skb)
  2340. goto out;
  2341. skb_reserve(skb, local->tx_headroom);
  2342. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  2343. beacon->head_len);
  2344. hdr = (struct ieee80211_hdr *) skb->data;
  2345. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2346. IEEE80211_STYPE_BEACON);
  2347. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2348. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  2349. beacon = rcu_dereference(ifmsh->beacon);
  2350. if (!beacon)
  2351. goto out;
  2352. if (beacon->csa_counter_offsets[0]) {
  2353. if (!is_template)
  2354. /* TODO: For mesh csa_counter is in TU, so
  2355. * decrementing it by one isn't correct, but
  2356. * for now we leave it consistent with overall
  2357. * mac80211's behavior.
  2358. */
  2359. ieee80211_csa_update_counter(vif);
  2360. ieee80211_set_csa(sdata, beacon);
  2361. }
  2362. if (ifmsh->sync_ops)
  2363. ifmsh->sync_ops->adjust_tbtt(sdata, beacon);
  2364. skb = dev_alloc_skb(local->tx_headroom +
  2365. beacon->head_len +
  2366. 256 + /* TIM IE */
  2367. beacon->tail_len +
  2368. local->hw.extra_beacon_tailroom);
  2369. if (!skb)
  2370. goto out;
  2371. skb_reserve(skb, local->tx_headroom);
  2372. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  2373. beacon->head_len);
  2374. ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
  2375. if (offs) {
  2376. offs->tim_offset = beacon->head_len;
  2377. offs->tim_length = skb->len - beacon->head_len;
  2378. }
  2379. memcpy(skb_put(skb, beacon->tail_len), beacon->tail,
  2380. beacon->tail_len);
  2381. } else {
  2382. WARN_ON(1);
  2383. goto out;
  2384. }
  2385. /* CSA offsets */
  2386. if (offs && beacon) {
  2387. int i;
  2388. for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
  2389. u16 csa_off = beacon->csa_counter_offsets[i];
  2390. if (!csa_off)
  2391. continue;
  2392. offs->csa_counter_offs[i] = csa_off_base + csa_off;
  2393. }
  2394. }
  2395. band = chanctx_conf->def.chan->band;
  2396. info = IEEE80211_SKB_CB(skb);
  2397. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  2398. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  2399. info->band = band;
  2400. memset(&txrc, 0, sizeof(txrc));
  2401. txrc.hw = hw;
  2402. txrc.sband = local->hw.wiphy->bands[band];
  2403. txrc.bss_conf = &sdata->vif.bss_conf;
  2404. txrc.skb = skb;
  2405. txrc.reported_rate.idx = -1;
  2406. txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
  2407. if (txrc.rate_idx_mask == (1 << txrc.sband->n_bitrates) - 1)
  2408. txrc.max_rate_idx = -1;
  2409. else
  2410. txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
  2411. txrc.bss = true;
  2412. rate_control_get_rate(sdata, NULL, &txrc);
  2413. info->control.vif = vif;
  2414. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
  2415. IEEE80211_TX_CTL_ASSIGN_SEQ |
  2416. IEEE80211_TX_CTL_FIRST_FRAGMENT;
  2417. out:
  2418. rcu_read_unlock();
  2419. return skb;
  2420. }
  2421. struct sk_buff *
  2422. ieee80211_beacon_get_template(struct ieee80211_hw *hw,
  2423. struct ieee80211_vif *vif,
  2424. struct ieee80211_mutable_offsets *offs)
  2425. {
  2426. return __ieee80211_beacon_get(hw, vif, offs, true);
  2427. }
  2428. EXPORT_SYMBOL(ieee80211_beacon_get_template);
  2429. struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
  2430. struct ieee80211_vif *vif,
  2431. u16 *tim_offset, u16 *tim_length)
  2432. {
  2433. struct ieee80211_mutable_offsets offs = {};
  2434. struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
  2435. if (tim_offset)
  2436. *tim_offset = offs.tim_offset;
  2437. if (tim_length)
  2438. *tim_length = offs.tim_length;
  2439. return bcn;
  2440. }
  2441. EXPORT_SYMBOL(ieee80211_beacon_get_tim);
  2442. struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
  2443. struct ieee80211_vif *vif)
  2444. {
  2445. struct ieee80211_if_ap *ap = NULL;
  2446. struct sk_buff *skb = NULL;
  2447. struct probe_resp *presp = NULL;
  2448. struct ieee80211_hdr *hdr;
  2449. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2450. if (sdata->vif.type != NL80211_IFTYPE_AP)
  2451. return NULL;
  2452. rcu_read_lock();
  2453. ap = &sdata->u.ap;
  2454. presp = rcu_dereference(ap->probe_resp);
  2455. if (!presp)
  2456. goto out;
  2457. skb = dev_alloc_skb(presp->len);
  2458. if (!skb)
  2459. goto out;
  2460. memcpy(skb_put(skb, presp->len), presp->data, presp->len);
  2461. hdr = (struct ieee80211_hdr *) skb->data;
  2462. memset(hdr->addr1, 0, sizeof(hdr->addr1));
  2463. out:
  2464. rcu_read_unlock();
  2465. return skb;
  2466. }
  2467. EXPORT_SYMBOL(ieee80211_proberesp_get);
  2468. struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
  2469. struct ieee80211_vif *vif)
  2470. {
  2471. struct ieee80211_sub_if_data *sdata;
  2472. struct ieee80211_if_managed *ifmgd;
  2473. struct ieee80211_pspoll *pspoll;
  2474. struct ieee80211_local *local;
  2475. struct sk_buff *skb;
  2476. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  2477. return NULL;
  2478. sdata = vif_to_sdata(vif);
  2479. ifmgd = &sdata->u.mgd;
  2480. local = sdata->local;
  2481. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
  2482. if (!skb)
  2483. return NULL;
  2484. skb_reserve(skb, local->hw.extra_tx_headroom);
  2485. pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
  2486. memset(pspoll, 0, sizeof(*pspoll));
  2487. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  2488. IEEE80211_STYPE_PSPOLL);
  2489. pspoll->aid = cpu_to_le16(ifmgd->aid);
  2490. /* aid in PS-Poll has its two MSBs each set to 1 */
  2491. pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
  2492. memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
  2493. memcpy(pspoll->ta, vif->addr, ETH_ALEN);
  2494. return skb;
  2495. }
  2496. EXPORT_SYMBOL(ieee80211_pspoll_get);
  2497. struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
  2498. struct ieee80211_vif *vif)
  2499. {
  2500. struct ieee80211_hdr_3addr *nullfunc;
  2501. struct ieee80211_sub_if_data *sdata;
  2502. struct ieee80211_if_managed *ifmgd;
  2503. struct ieee80211_local *local;
  2504. struct sk_buff *skb;
  2505. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  2506. return NULL;
  2507. sdata = vif_to_sdata(vif);
  2508. ifmgd = &sdata->u.mgd;
  2509. local = sdata->local;
  2510. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
  2511. if (!skb)
  2512. return NULL;
  2513. skb_reserve(skb, local->hw.extra_tx_headroom);
  2514. nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
  2515. sizeof(*nullfunc));
  2516. memset(nullfunc, 0, sizeof(*nullfunc));
  2517. nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  2518. IEEE80211_STYPE_NULLFUNC |
  2519. IEEE80211_FCTL_TODS);
  2520. memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
  2521. memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
  2522. memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
  2523. return skb;
  2524. }
  2525. EXPORT_SYMBOL(ieee80211_nullfunc_get);
  2526. struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
  2527. const u8 *src_addr,
  2528. const u8 *ssid, size_t ssid_len,
  2529. size_t tailroom)
  2530. {
  2531. struct ieee80211_local *local = hw_to_local(hw);
  2532. struct ieee80211_hdr_3addr *hdr;
  2533. struct sk_buff *skb;
  2534. size_t ie_ssid_len;
  2535. u8 *pos;
  2536. ie_ssid_len = 2 + ssid_len;
  2537. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
  2538. ie_ssid_len + tailroom);
  2539. if (!skb)
  2540. return NULL;
  2541. skb_reserve(skb, local->hw.extra_tx_headroom);
  2542. hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
  2543. memset(hdr, 0, sizeof(*hdr));
  2544. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2545. IEEE80211_STYPE_PROBE_REQ);
  2546. eth_broadcast_addr(hdr->addr1);
  2547. memcpy(hdr->addr2, src_addr, ETH_ALEN);
  2548. eth_broadcast_addr(hdr->addr3);
  2549. pos = skb_put(skb, ie_ssid_len);
  2550. *pos++ = WLAN_EID_SSID;
  2551. *pos++ = ssid_len;
  2552. if (ssid_len)
  2553. memcpy(pos, ssid, ssid_len);
  2554. pos += ssid_len;
  2555. return skb;
  2556. }
  2557. EXPORT_SYMBOL(ieee80211_probereq_get);
  2558. void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2559. const void *frame, size_t frame_len,
  2560. const struct ieee80211_tx_info *frame_txctl,
  2561. struct ieee80211_rts *rts)
  2562. {
  2563. const struct ieee80211_hdr *hdr = frame;
  2564. rts->frame_control =
  2565. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
  2566. rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
  2567. frame_txctl);
  2568. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  2569. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  2570. }
  2571. EXPORT_SYMBOL(ieee80211_rts_get);
  2572. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2573. const void *frame, size_t frame_len,
  2574. const struct ieee80211_tx_info *frame_txctl,
  2575. struct ieee80211_cts *cts)
  2576. {
  2577. const struct ieee80211_hdr *hdr = frame;
  2578. cts->frame_control =
  2579. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
  2580. cts->duration = ieee80211_ctstoself_duration(hw, vif,
  2581. frame_len, frame_txctl);
  2582. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  2583. }
  2584. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  2585. struct sk_buff *
  2586. ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
  2587. struct ieee80211_vif *vif)
  2588. {
  2589. struct ieee80211_local *local = hw_to_local(hw);
  2590. struct sk_buff *skb = NULL;
  2591. struct ieee80211_tx_data tx;
  2592. struct ieee80211_sub_if_data *sdata;
  2593. struct ps_data *ps;
  2594. struct ieee80211_tx_info *info;
  2595. struct ieee80211_chanctx_conf *chanctx_conf;
  2596. sdata = vif_to_sdata(vif);
  2597. rcu_read_lock();
  2598. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2599. if (!chanctx_conf)
  2600. goto out;
  2601. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  2602. struct beacon_data *beacon =
  2603. rcu_dereference(sdata->u.ap.beacon);
  2604. if (!beacon || !beacon->head)
  2605. goto out;
  2606. ps = &sdata->u.ap.ps;
  2607. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2608. ps = &sdata->u.mesh.ps;
  2609. } else {
  2610. goto out;
  2611. }
  2612. if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
  2613. goto out; /* send buffered bc/mc only after DTIM beacon */
  2614. while (1) {
  2615. skb = skb_dequeue(&ps->bc_buf);
  2616. if (!skb)
  2617. goto out;
  2618. local->total_ps_buffered--;
  2619. if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
  2620. struct ieee80211_hdr *hdr =
  2621. (struct ieee80211_hdr *) skb->data;
  2622. /* more buffered multicast/broadcast frames ==> set
  2623. * MoreData flag in IEEE 802.11 header to inform PS
  2624. * STAs */
  2625. hdr->frame_control |=
  2626. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  2627. }
  2628. if (sdata->vif.type == NL80211_IFTYPE_AP)
  2629. sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
  2630. if (!ieee80211_tx_prepare(sdata, &tx, skb))
  2631. break;
  2632. dev_kfree_skb_any(skb);
  2633. }
  2634. info = IEEE80211_SKB_CB(skb);
  2635. tx.flags |= IEEE80211_TX_PS_BUFFERED;
  2636. info->band = chanctx_conf->def.chan->band;
  2637. if (invoke_tx_handlers(&tx))
  2638. skb = NULL;
  2639. out:
  2640. rcu_read_unlock();
  2641. return skb;
  2642. }
  2643. EXPORT_SYMBOL(ieee80211_get_buffered_bc);
  2644. int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
  2645. {
  2646. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  2647. struct ieee80211_sub_if_data *sdata = sta->sdata;
  2648. struct ieee80211_local *local = sdata->local;
  2649. int ret;
  2650. u32 queues;
  2651. lockdep_assert_held(&local->sta_mtx);
  2652. /* only some cases are supported right now */
  2653. switch (sdata->vif.type) {
  2654. case NL80211_IFTYPE_STATION:
  2655. case NL80211_IFTYPE_AP:
  2656. case NL80211_IFTYPE_AP_VLAN:
  2657. break;
  2658. default:
  2659. WARN_ON(1);
  2660. return -EINVAL;
  2661. }
  2662. if (WARN_ON(tid >= IEEE80211_NUM_UPS))
  2663. return -EINVAL;
  2664. if (sta->reserved_tid == tid) {
  2665. ret = 0;
  2666. goto out;
  2667. }
  2668. if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
  2669. sdata_err(sdata, "TID reservation already active\n");
  2670. ret = -EALREADY;
  2671. goto out;
  2672. }
  2673. ieee80211_stop_vif_queues(sdata->local, sdata,
  2674. IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
  2675. synchronize_net();
  2676. /* Tear down BA sessions so we stop aggregating on this TID */
  2677. if (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  2678. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  2679. __ieee80211_stop_tx_ba_session(sta, tid,
  2680. AGG_STOP_LOCAL_REQUEST);
  2681. }
  2682. queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
  2683. __ieee80211_flush_queues(local, sdata, queues, false);
  2684. sta->reserved_tid = tid;
  2685. ieee80211_wake_vif_queues(local, sdata,
  2686. IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
  2687. if (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION)
  2688. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  2689. ret = 0;
  2690. out:
  2691. return ret;
  2692. }
  2693. EXPORT_SYMBOL(ieee80211_reserve_tid);
  2694. void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
  2695. {
  2696. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  2697. struct ieee80211_sub_if_data *sdata = sta->sdata;
  2698. lockdep_assert_held(&sdata->local->sta_mtx);
  2699. /* only some cases are supported right now */
  2700. switch (sdata->vif.type) {
  2701. case NL80211_IFTYPE_STATION:
  2702. case NL80211_IFTYPE_AP:
  2703. case NL80211_IFTYPE_AP_VLAN:
  2704. break;
  2705. default:
  2706. WARN_ON(1);
  2707. return;
  2708. }
  2709. if (tid != sta->reserved_tid) {
  2710. sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
  2711. return;
  2712. }
  2713. sta->reserved_tid = IEEE80211_TID_UNRESERVED;
  2714. }
  2715. EXPORT_SYMBOL(ieee80211_unreserve_tid);
  2716. void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
  2717. struct sk_buff *skb, int tid,
  2718. enum ieee80211_band band)
  2719. {
  2720. int ac = ieee802_1d_to_ac[tid & 7];
  2721. skb_set_mac_header(skb, 0);
  2722. skb_set_network_header(skb, 0);
  2723. skb_set_transport_header(skb, 0);
  2724. skb_set_queue_mapping(skb, ac);
  2725. skb->priority = tid;
  2726. skb->dev = sdata->dev;
  2727. /*
  2728. * The other path calling ieee80211_xmit is from the tasklet,
  2729. * and while we can handle concurrent transmissions locking
  2730. * requirements are that we do not come into tx with bhs on.
  2731. */
  2732. local_bh_disable();
  2733. IEEE80211_SKB_CB(skb)->band = band;
  2734. ieee80211_xmit(sdata, skb);
  2735. local_bh_enable();
  2736. }