tx.c 83 KB

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