tx.c 81 KB

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