tx.c 75 KB

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