tx.c 67 KB

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