tx.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089
  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 "led.h"
  27. #include "mesh.h"
  28. #include "wep.h"
  29. #include "wpa.h"
  30. #include "wme.h"
  31. #include "rate.h"
  32. #define IEEE80211_TX_OK 0
  33. #define IEEE80211_TX_AGAIN 1
  34. #define IEEE80211_TX_FRAG_AGAIN 2
  35. /* misc utils */
  36. static __le16 ieee80211_duration(struct ieee80211_tx_data *tx, 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(tx->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 *)tx->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_NUM_BANDS:
  129. WARN_ON(1);
  130. break;
  131. }
  132. }
  133. if (rate == -1) {
  134. /* No matching basic rate found; use highest suitable mandatory
  135. * PHY rate */
  136. rate = mrate;
  137. }
  138. /* Time needed to transmit ACK
  139. * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
  140. * to closest integer */
  141. dur = ieee80211_frame_duration(local, 10, rate, erp,
  142. tx->sdata->vif.bss_conf.use_short_preamble);
  143. if (next_frag_len) {
  144. /* Frame is fragmented: duration increases with time needed to
  145. * transmit next fragment plus ACK and 2 x SIFS. */
  146. dur *= 2; /* ACK + SIFS */
  147. /* next fragment */
  148. dur += ieee80211_frame_duration(local, next_frag_len,
  149. txrate->bitrate, erp,
  150. tx->sdata->vif.bss_conf.use_short_preamble);
  151. }
  152. return cpu_to_le16(dur);
  153. }
  154. static int inline is_ieee80211_device(struct ieee80211_local *local,
  155. struct net_device *dev)
  156. {
  157. return local == wdev_priv(dev->ieee80211_ptr);
  158. }
  159. /* tx handlers */
  160. static ieee80211_tx_result debug_noinline
  161. ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
  162. {
  163. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  164. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  165. u32 sta_flags;
  166. if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
  167. return TX_CONTINUE;
  168. if (unlikely(tx->local->sw_scanning) &&
  169. !ieee80211_is_probe_req(hdr->frame_control))
  170. return TX_DROP;
  171. if (tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  172. return TX_CONTINUE;
  173. if (tx->flags & IEEE80211_TX_PS_BUFFERED)
  174. return TX_CONTINUE;
  175. sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
  176. if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
  177. if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
  178. tx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  179. ieee80211_is_data(hdr->frame_control))) {
  180. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  181. printk(KERN_DEBUG "%s: dropped data frame to not "
  182. "associated station %pM\n",
  183. tx->dev->name, hdr->addr1);
  184. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  185. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  186. return TX_DROP;
  187. }
  188. } else {
  189. if (unlikely(ieee80211_is_data(hdr->frame_control) &&
  190. tx->local->num_sta == 0 &&
  191. tx->sdata->vif.type != NL80211_IFTYPE_ADHOC)) {
  192. /*
  193. * No associated STAs - no need to send multicast
  194. * frames.
  195. */
  196. return TX_DROP;
  197. }
  198. return TX_CONTINUE;
  199. }
  200. return TX_CONTINUE;
  201. }
  202. /* This function is called whenever the AP is about to exceed the maximum limit
  203. * of buffered frames for power saving STAs. This situation should not really
  204. * happen often during normal operation, so dropping the oldest buffered packet
  205. * from each queue should be OK to make some room for new frames. */
  206. static void purge_old_ps_buffers(struct ieee80211_local *local)
  207. {
  208. int total = 0, purged = 0;
  209. struct sk_buff *skb;
  210. struct ieee80211_sub_if_data *sdata;
  211. struct sta_info *sta;
  212. /*
  213. * virtual interfaces are protected by RCU
  214. */
  215. rcu_read_lock();
  216. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  217. struct ieee80211_if_ap *ap;
  218. if (sdata->vif.type != NL80211_IFTYPE_AP)
  219. continue;
  220. ap = &sdata->u.ap;
  221. skb = skb_dequeue(&ap->ps_bc_buf);
  222. if (skb) {
  223. purged++;
  224. dev_kfree_skb(skb);
  225. }
  226. total += skb_queue_len(&ap->ps_bc_buf);
  227. }
  228. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  229. skb = skb_dequeue(&sta->ps_tx_buf);
  230. if (skb) {
  231. purged++;
  232. dev_kfree_skb(skb);
  233. }
  234. total += skb_queue_len(&sta->ps_tx_buf);
  235. }
  236. rcu_read_unlock();
  237. local->total_ps_buffered = total;
  238. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  239. printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
  240. wiphy_name(local->hw.wiphy), purged);
  241. #endif
  242. }
  243. static ieee80211_tx_result
  244. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
  245. {
  246. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  247. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  248. /*
  249. * broadcast/multicast frame
  250. *
  251. * If any of the associated stations is in power save mode,
  252. * the frame is buffered to be sent after DTIM beacon frame.
  253. * This is done either by the hardware or us.
  254. */
  255. /* powersaving STAs only in AP/VLAN mode */
  256. if (!tx->sdata->bss)
  257. return TX_CONTINUE;
  258. /* no buffering for ordered frames */
  259. if (ieee80211_has_order(hdr->frame_control))
  260. return TX_CONTINUE;
  261. /* no stations in PS mode */
  262. if (!atomic_read(&tx->sdata->bss->num_sta_ps))
  263. return TX_CONTINUE;
  264. /* buffered in mac80211 */
  265. if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
  266. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  267. purge_old_ps_buffers(tx->local);
  268. if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
  269. AP_MAX_BC_BUFFER) {
  270. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  271. if (net_ratelimit()) {
  272. printk(KERN_DEBUG "%s: BC TX buffer full - "
  273. "dropping the oldest frame\n",
  274. tx->dev->name);
  275. }
  276. #endif
  277. dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
  278. } else
  279. tx->local->total_ps_buffered++;
  280. skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
  281. return TX_QUEUED;
  282. }
  283. /* buffered in hardware */
  284. info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
  285. return TX_CONTINUE;
  286. }
  287. static ieee80211_tx_result
  288. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
  289. {
  290. struct sta_info *sta = tx->sta;
  291. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  292. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  293. u32 staflags;
  294. if (unlikely(!sta || ieee80211_is_probe_resp(hdr->frame_control)))
  295. return TX_CONTINUE;
  296. staflags = get_sta_flags(sta);
  297. if (unlikely((staflags & WLAN_STA_PS) &&
  298. !(staflags & WLAN_STA_PSPOLL))) {
  299. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  300. printk(KERN_DEBUG "STA %pM aid %d: PS buffer (entries "
  301. "before %d)\n",
  302. sta->sta.addr, sta->sta.aid,
  303. skb_queue_len(&sta->ps_tx_buf));
  304. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  305. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  306. purge_old_ps_buffers(tx->local);
  307. if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
  308. struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
  309. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  310. if (net_ratelimit()) {
  311. printk(KERN_DEBUG "%s: STA %pM TX "
  312. "buffer full - dropping oldest frame\n",
  313. tx->dev->name, sta->sta.addr);
  314. }
  315. #endif
  316. dev_kfree_skb(old);
  317. } else
  318. tx->local->total_ps_buffered++;
  319. /* Queue frame to be sent after STA sends an PS Poll frame */
  320. if (skb_queue_empty(&sta->ps_tx_buf))
  321. sta_info_set_tim_bit(sta);
  322. info->control.jiffies = jiffies;
  323. skb_queue_tail(&sta->ps_tx_buf, tx->skb);
  324. return TX_QUEUED;
  325. }
  326. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  327. else if (unlikely(test_sta_flags(sta, WLAN_STA_PS))) {
  328. printk(KERN_DEBUG "%s: STA %pM in PS mode, but pspoll "
  329. "set -> send frame\n", tx->dev->name,
  330. sta->sta.addr);
  331. }
  332. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  333. clear_sta_flags(sta, WLAN_STA_PSPOLL);
  334. return TX_CONTINUE;
  335. }
  336. static ieee80211_tx_result debug_noinline
  337. ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
  338. {
  339. if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
  340. return TX_CONTINUE;
  341. if (tx->flags & IEEE80211_TX_UNICAST)
  342. return ieee80211_tx_h_unicast_ps_buf(tx);
  343. else
  344. return ieee80211_tx_h_multicast_ps_buf(tx);
  345. }
  346. static ieee80211_tx_result debug_noinline
  347. ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
  348. {
  349. struct ieee80211_key *key;
  350. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  351. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  352. if (unlikely(tx->skb->do_not_encrypt))
  353. tx->key = NULL;
  354. else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
  355. tx->key = key;
  356. else if ((key = rcu_dereference(tx->sdata->default_key)))
  357. tx->key = key;
  358. else if (tx->sdata->drop_unencrypted &&
  359. (tx->skb->protocol != cpu_to_be16(ETH_P_PAE)) &&
  360. !(info->flags & IEEE80211_TX_CTL_INJECTED)) {
  361. I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
  362. return TX_DROP;
  363. } else
  364. tx->key = NULL;
  365. if (tx->key) {
  366. tx->key->tx_rx_count++;
  367. /* TODO: add threshold stuff again */
  368. switch (tx->key->conf.alg) {
  369. case ALG_WEP:
  370. if (ieee80211_is_auth(hdr->frame_control))
  371. break;
  372. case ALG_TKIP:
  373. case ALG_CCMP:
  374. if (!ieee80211_is_data_present(hdr->frame_control))
  375. tx->key = NULL;
  376. break;
  377. }
  378. }
  379. if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  380. tx->skb->do_not_encrypt = 1;
  381. return TX_CONTINUE;
  382. }
  383. static ieee80211_tx_result debug_noinline
  384. ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
  385. {
  386. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  387. struct ieee80211_hdr *hdr = (void *)tx->skb->data;
  388. struct ieee80211_supported_band *sband;
  389. struct ieee80211_rate *rate;
  390. int i, len;
  391. bool inval = false, rts = false, short_preamble = false;
  392. struct ieee80211_tx_rate_control txrc;
  393. memset(&txrc, 0, sizeof(txrc));
  394. sband = tx->local->hw.wiphy->bands[tx->channel->band];
  395. len = min_t(int, tx->skb->len + FCS_LEN,
  396. tx->local->fragmentation_threshold);
  397. /* set up the tx rate control struct we give the RC algo */
  398. txrc.hw = local_to_hw(tx->local);
  399. txrc.sband = sband;
  400. txrc.bss_conf = &tx->sdata->vif.bss_conf;
  401. txrc.skb = tx->skb;
  402. txrc.reported_rate.idx = -1;
  403. txrc.max_rate_idx = tx->sdata->max_ratectrl_rateidx;
  404. /* set up RTS protection if desired */
  405. if (tx->local->rts_threshold < IEEE80211_MAX_RTS_THRESHOLD &&
  406. len > tx->local->rts_threshold) {
  407. txrc.rts = rts = true;
  408. }
  409. /*
  410. * Use short preamble if the BSS can handle it, but not for
  411. * management frames unless we know the receiver can handle
  412. * that -- the management frame might be to a station that
  413. * just wants a probe response.
  414. */
  415. if (tx->sdata->vif.bss_conf.use_short_preamble &&
  416. (ieee80211_is_data(hdr->frame_control) ||
  417. (tx->sta && test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
  418. txrc.short_preamble = short_preamble = true;
  419. rate_control_get_rate(tx->sdata, tx->sta, &txrc);
  420. if (unlikely(info->control.rates[0].idx < 0))
  421. return TX_DROP;
  422. if (txrc.reported_rate.idx < 0)
  423. txrc.reported_rate = info->control.rates[0];
  424. if (tx->sta)
  425. tx->sta->last_tx_rate = txrc.reported_rate;
  426. if (unlikely(!info->control.rates[0].count))
  427. info->control.rates[0].count = 1;
  428. if (is_multicast_ether_addr(hdr->addr1)) {
  429. /*
  430. * XXX: verify the rate is in the basic rateset
  431. */
  432. return TX_CONTINUE;
  433. }
  434. /*
  435. * set up the RTS/CTS rate as the fastest basic rate
  436. * that is not faster than the data rate
  437. *
  438. * XXX: Should this check all retry rates?
  439. */
  440. if (!(info->control.rates[0].flags & IEEE80211_TX_RC_MCS)) {
  441. s8 baserate = 0;
  442. rate = &sband->bitrates[info->control.rates[0].idx];
  443. for (i = 0; i < sband->n_bitrates; i++) {
  444. /* must be a basic rate */
  445. if (!(tx->sdata->vif.bss_conf.basic_rates & BIT(i)))
  446. continue;
  447. /* must not be faster than the data rate */
  448. if (sband->bitrates[i].bitrate > rate->bitrate)
  449. continue;
  450. /* maximum */
  451. if (sband->bitrates[baserate].bitrate <
  452. sband->bitrates[i].bitrate)
  453. baserate = i;
  454. }
  455. info->control.rts_cts_rate_idx = baserate;
  456. }
  457. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  458. /*
  459. * make sure there's no valid rate following
  460. * an invalid one, just in case drivers don't
  461. * take the API seriously to stop at -1.
  462. */
  463. if (inval) {
  464. info->control.rates[i].idx = -1;
  465. continue;
  466. }
  467. if (info->control.rates[i].idx < 0) {
  468. inval = true;
  469. continue;
  470. }
  471. /*
  472. * For now assume MCS is already set up correctly, this
  473. * needs to be fixed.
  474. */
  475. if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS) {
  476. WARN_ON(info->control.rates[i].idx > 76);
  477. continue;
  478. }
  479. /* set up RTS protection if desired */
  480. if (rts)
  481. info->control.rates[i].flags |=
  482. IEEE80211_TX_RC_USE_RTS_CTS;
  483. /* RC is busted */
  484. if (WARN_ON_ONCE(info->control.rates[i].idx >=
  485. sband->n_bitrates)) {
  486. info->control.rates[i].idx = -1;
  487. continue;
  488. }
  489. rate = &sband->bitrates[info->control.rates[i].idx];
  490. /* set up short preamble */
  491. if (short_preamble &&
  492. rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
  493. info->control.rates[i].flags |=
  494. IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  495. /* set up G protection */
  496. if (!rts && tx->sdata->vif.bss_conf.use_cts_prot &&
  497. rate->flags & IEEE80211_RATE_ERP_G)
  498. info->control.rates[i].flags |=
  499. IEEE80211_TX_RC_USE_CTS_PROTECT;
  500. }
  501. return TX_CONTINUE;
  502. }
  503. static ieee80211_tx_result debug_noinline
  504. ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
  505. {
  506. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  507. if (tx->sta)
  508. info->control.sta = &tx->sta->sta;
  509. return TX_CONTINUE;
  510. }
  511. static ieee80211_tx_result debug_noinline
  512. ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
  513. {
  514. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  515. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  516. u16 *seq;
  517. u8 *qc;
  518. int tid;
  519. /*
  520. * Packet injection may want to control the sequence
  521. * number, if we have no matching interface then we
  522. * neither assign one ourselves nor ask the driver to.
  523. */
  524. if (unlikely(!info->control.vif))
  525. return TX_CONTINUE;
  526. if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
  527. return TX_CONTINUE;
  528. if (ieee80211_hdrlen(hdr->frame_control) < 24)
  529. return TX_CONTINUE;
  530. /*
  531. * Anything but QoS data that has a sequence number field
  532. * (is long enough) gets a sequence number from the global
  533. * counter.
  534. */
  535. if (!ieee80211_is_data_qos(hdr->frame_control)) {
  536. /* driver should assign sequence number */
  537. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  538. /* for pure STA mode without beacons, we can do it */
  539. hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
  540. tx->sdata->sequence_number += 0x10;
  541. tx->sdata->sequence_number &= IEEE80211_SCTL_SEQ;
  542. return TX_CONTINUE;
  543. }
  544. /*
  545. * This should be true for injected/management frames only, for
  546. * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
  547. * above since they are not QoS-data frames.
  548. */
  549. if (!tx->sta)
  550. return TX_CONTINUE;
  551. /* include per-STA, per-TID sequence counter */
  552. qc = ieee80211_get_qos_ctl(hdr);
  553. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  554. seq = &tx->sta->tid_seq[tid];
  555. hdr->seq_ctrl = cpu_to_le16(*seq);
  556. /* Increase the sequence number. */
  557. *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
  558. return TX_CONTINUE;
  559. }
  560. static ieee80211_tx_result debug_noinline
  561. ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
  562. {
  563. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  564. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  565. size_t hdrlen, per_fragm, num_fragm, payload_len, left;
  566. struct sk_buff **frags, *first, *frag;
  567. int i;
  568. u16 seq;
  569. u8 *pos;
  570. int frag_threshold = tx->local->fragmentation_threshold;
  571. if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
  572. return TX_CONTINUE;
  573. /*
  574. * Warn when submitting a fragmented A-MPDU frame and drop it.
  575. * This scenario is handled in __ieee80211_tx_prepare but extra
  576. * caution taken here as fragmented ampdu may cause Tx stop.
  577. */
  578. if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
  579. return TX_DROP;
  580. first = tx->skb;
  581. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  582. payload_len = first->len - hdrlen;
  583. per_fragm = frag_threshold - hdrlen - FCS_LEN;
  584. num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
  585. frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
  586. if (!frags)
  587. goto fail;
  588. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  589. seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
  590. pos = first->data + hdrlen + per_fragm;
  591. left = payload_len - per_fragm;
  592. for (i = 0; i < num_fragm - 1; i++) {
  593. struct ieee80211_hdr *fhdr;
  594. size_t copylen;
  595. if (left <= 0)
  596. goto fail;
  597. /* reserve enough extra head and tail room for possible
  598. * encryption */
  599. frag = frags[i] =
  600. dev_alloc_skb(tx->local->tx_headroom +
  601. frag_threshold +
  602. IEEE80211_ENCRYPT_HEADROOM +
  603. IEEE80211_ENCRYPT_TAILROOM);
  604. if (!frag)
  605. goto fail;
  606. /* Make sure that all fragments use the same priority so
  607. * that they end up using the same TX queue */
  608. frag->priority = first->priority;
  609. skb_reserve(frag, tx->local->tx_headroom +
  610. IEEE80211_ENCRYPT_HEADROOM);
  611. /* copy TX information */
  612. info = IEEE80211_SKB_CB(frag);
  613. memcpy(info, first->cb, sizeof(frag->cb));
  614. /* copy/fill in 802.11 header */
  615. fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
  616. memcpy(fhdr, first->data, hdrlen);
  617. fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
  618. if (i == num_fragm - 2) {
  619. /* clear MOREFRAGS bit for the last fragment */
  620. fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
  621. } else {
  622. /*
  623. * No multi-rate retries for fragmented frames, that
  624. * would completely throw off the NAV at other STAs.
  625. */
  626. info->control.rates[1].idx = -1;
  627. info->control.rates[2].idx = -1;
  628. info->control.rates[3].idx = -1;
  629. info->control.rates[4].idx = -1;
  630. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 5);
  631. info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  632. }
  633. /* copy data */
  634. copylen = left > per_fragm ? per_fragm : left;
  635. memcpy(skb_put(frag, copylen), pos, copylen);
  636. skb_copy_queue_mapping(frag, first);
  637. frag->do_not_encrypt = first->do_not_encrypt;
  638. frag->dev = first->dev;
  639. frag->iif = first->iif;
  640. pos += copylen;
  641. left -= copylen;
  642. }
  643. skb_trim(first, hdrlen + per_fragm);
  644. tx->num_extra_frag = num_fragm - 1;
  645. tx->extra_frag = frags;
  646. return TX_CONTINUE;
  647. fail:
  648. if (frags) {
  649. for (i = 0; i < num_fragm - 1; i++)
  650. if (frags[i])
  651. dev_kfree_skb(frags[i]);
  652. kfree(frags);
  653. }
  654. I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
  655. return TX_DROP;
  656. }
  657. static ieee80211_tx_result debug_noinline
  658. ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
  659. {
  660. if (!tx->key)
  661. return TX_CONTINUE;
  662. switch (tx->key->conf.alg) {
  663. case ALG_WEP:
  664. return ieee80211_crypto_wep_encrypt(tx);
  665. case ALG_TKIP:
  666. return ieee80211_crypto_tkip_encrypt(tx);
  667. case ALG_CCMP:
  668. return ieee80211_crypto_ccmp_encrypt(tx);
  669. }
  670. /* not reached */
  671. WARN_ON(1);
  672. return TX_DROP;
  673. }
  674. static ieee80211_tx_result debug_noinline
  675. ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
  676. {
  677. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  678. int next_len, i;
  679. int group_addr = is_multicast_ether_addr(hdr->addr1);
  680. if (!(tx->flags & IEEE80211_TX_FRAGMENTED)) {
  681. hdr->duration_id = ieee80211_duration(tx, group_addr, 0);
  682. return TX_CONTINUE;
  683. }
  684. hdr->duration_id = ieee80211_duration(tx, group_addr,
  685. tx->extra_frag[0]->len);
  686. for (i = 0; i < tx->num_extra_frag; i++) {
  687. if (i + 1 < tx->num_extra_frag)
  688. next_len = tx->extra_frag[i + 1]->len;
  689. else
  690. next_len = 0;
  691. hdr = (struct ieee80211_hdr *)tx->extra_frag[i]->data;
  692. hdr->duration_id = ieee80211_duration(tx, 0, next_len);
  693. }
  694. return TX_CONTINUE;
  695. }
  696. static ieee80211_tx_result debug_noinline
  697. ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
  698. {
  699. int i;
  700. if (!tx->sta)
  701. return TX_CONTINUE;
  702. tx->sta->tx_packets++;
  703. tx->sta->tx_fragments++;
  704. tx->sta->tx_bytes += tx->skb->len;
  705. if (tx->extra_frag) {
  706. tx->sta->tx_fragments += tx->num_extra_frag;
  707. for (i = 0; i < tx->num_extra_frag; i++)
  708. tx->sta->tx_bytes += tx->extra_frag[i]->len;
  709. }
  710. return TX_CONTINUE;
  711. }
  712. /* actual transmit path */
  713. /*
  714. * deal with packet injection down monitor interface
  715. * with Radiotap Header -- only called for monitor mode interface
  716. */
  717. static ieee80211_tx_result
  718. __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
  719. struct sk_buff *skb)
  720. {
  721. /*
  722. * this is the moment to interpret and discard the radiotap header that
  723. * must be at the start of the packet injected in Monitor mode
  724. *
  725. * Need to take some care with endian-ness since radiotap
  726. * args are little-endian
  727. */
  728. struct ieee80211_radiotap_iterator iterator;
  729. struct ieee80211_radiotap_header *rthdr =
  730. (struct ieee80211_radiotap_header *) skb->data;
  731. struct ieee80211_supported_band *sband;
  732. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
  733. sband = tx->local->hw.wiphy->bands[tx->channel->band];
  734. skb->do_not_encrypt = 1;
  735. tx->flags &= ~IEEE80211_TX_FRAGMENTED;
  736. /*
  737. * for every radiotap entry that is present
  738. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  739. * entries present, or -EINVAL on error)
  740. */
  741. while (!ret) {
  742. ret = ieee80211_radiotap_iterator_next(&iterator);
  743. if (ret)
  744. continue;
  745. /* see if this argument is something we can use */
  746. switch (iterator.this_arg_index) {
  747. /*
  748. * You must take care when dereferencing iterator.this_arg
  749. * for multibyte types... the pointer is not aligned. Use
  750. * get_unaligned((type *)iterator.this_arg) to dereference
  751. * iterator.this_arg for type "type" safely on all arches.
  752. */
  753. case IEEE80211_RADIOTAP_FLAGS:
  754. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  755. /*
  756. * this indicates that the skb we have been
  757. * handed has the 32-bit FCS CRC at the end...
  758. * we should react to that by snipping it off
  759. * because it will be recomputed and added
  760. * on transmission
  761. */
  762. if (skb->len < (iterator.max_length + FCS_LEN))
  763. return TX_DROP;
  764. skb_trim(skb, skb->len - FCS_LEN);
  765. }
  766. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
  767. tx->skb->do_not_encrypt = 0;
  768. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
  769. tx->flags |= IEEE80211_TX_FRAGMENTED;
  770. break;
  771. /*
  772. * Please update the file
  773. * Documentation/networking/mac80211-injection.txt
  774. * when parsing new fields here.
  775. */
  776. default:
  777. break;
  778. }
  779. }
  780. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  781. return TX_DROP;
  782. /*
  783. * remove the radiotap header
  784. * iterator->max_length was sanity-checked against
  785. * skb->len by iterator init
  786. */
  787. skb_pull(skb, iterator.max_length);
  788. return TX_CONTINUE;
  789. }
  790. /*
  791. * initialises @tx
  792. */
  793. static ieee80211_tx_result
  794. __ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
  795. struct sk_buff *skb,
  796. struct net_device *dev)
  797. {
  798. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  799. struct ieee80211_hdr *hdr;
  800. struct ieee80211_sub_if_data *sdata;
  801. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  802. int hdrlen, tid;
  803. u8 *qc, *state;
  804. memset(tx, 0, sizeof(*tx));
  805. tx->skb = skb;
  806. tx->dev = dev; /* use original interface */
  807. tx->local = local;
  808. tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  809. tx->channel = local->hw.conf.channel;
  810. /*
  811. * Set this flag (used below to indicate "automatic fragmentation"),
  812. * it will be cleared/left by radiotap as desired.
  813. */
  814. tx->flags |= IEEE80211_TX_FRAGMENTED;
  815. /* process and remove the injection radiotap header */
  816. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  817. if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED)) {
  818. if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
  819. return TX_DROP;
  820. /*
  821. * __ieee80211_parse_tx_radiotap has now removed
  822. * the radiotap header that was present and pre-filled
  823. * 'tx' with tx control information.
  824. */
  825. }
  826. hdr = (struct ieee80211_hdr *) skb->data;
  827. tx->sta = sta_info_get(local, hdr->addr1);
  828. if (tx->sta && ieee80211_is_data_qos(hdr->frame_control)) {
  829. qc = ieee80211_get_qos_ctl(hdr);
  830. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  831. state = &tx->sta->ampdu_mlme.tid_state_tx[tid];
  832. if (*state == HT_AGG_STATE_OPERATIONAL)
  833. info->flags |= IEEE80211_TX_CTL_AMPDU;
  834. }
  835. if (is_multicast_ether_addr(hdr->addr1)) {
  836. tx->flags &= ~IEEE80211_TX_UNICAST;
  837. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  838. } else {
  839. tx->flags |= IEEE80211_TX_UNICAST;
  840. info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
  841. }
  842. if (tx->flags & IEEE80211_TX_FRAGMENTED) {
  843. if ((tx->flags & IEEE80211_TX_UNICAST) &&
  844. skb->len + FCS_LEN > local->fragmentation_threshold &&
  845. !(info->flags & IEEE80211_TX_CTL_AMPDU))
  846. tx->flags |= IEEE80211_TX_FRAGMENTED;
  847. else
  848. tx->flags &= ~IEEE80211_TX_FRAGMENTED;
  849. }
  850. if (!tx->sta)
  851. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  852. else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
  853. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  854. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  855. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  856. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  857. tx->ethertype = (pos[0] << 8) | pos[1];
  858. }
  859. info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
  860. return TX_CONTINUE;
  861. }
  862. /*
  863. * NB: @tx is uninitialised when passed in here
  864. */
  865. static int ieee80211_tx_prepare(struct ieee80211_local *local,
  866. struct ieee80211_tx_data *tx,
  867. struct sk_buff *skb)
  868. {
  869. struct net_device *dev;
  870. dev = dev_get_by_index(&init_net, skb->iif);
  871. if (unlikely(dev && !is_ieee80211_device(local, dev))) {
  872. dev_put(dev);
  873. dev = NULL;
  874. }
  875. if (unlikely(!dev))
  876. return -ENODEV;
  877. /* initialises tx with control */
  878. __ieee80211_tx_prepare(tx, skb, dev);
  879. dev_put(dev);
  880. return 0;
  881. }
  882. static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
  883. struct ieee80211_tx_data *tx)
  884. {
  885. struct ieee80211_tx_info *info;
  886. int ret, i;
  887. if (skb) {
  888. if (netif_subqueue_stopped(local->mdev, skb))
  889. return IEEE80211_TX_AGAIN;
  890. info = IEEE80211_SKB_CB(skb);
  891. ret = local->ops->tx(local_to_hw(local), skb);
  892. if (ret)
  893. return IEEE80211_TX_AGAIN;
  894. local->mdev->trans_start = jiffies;
  895. ieee80211_led_tx(local, 1);
  896. }
  897. if (tx->extra_frag) {
  898. for (i = 0; i < tx->num_extra_frag; i++) {
  899. if (!tx->extra_frag[i])
  900. continue;
  901. info = IEEE80211_SKB_CB(tx->extra_frag[i]);
  902. info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
  903. IEEE80211_TX_CTL_FIRST_FRAGMENT);
  904. if (netif_subqueue_stopped(local->mdev,
  905. tx->extra_frag[i]))
  906. return IEEE80211_TX_FRAG_AGAIN;
  907. ret = local->ops->tx(local_to_hw(local),
  908. tx->extra_frag[i]);
  909. if (ret)
  910. return IEEE80211_TX_FRAG_AGAIN;
  911. local->mdev->trans_start = jiffies;
  912. ieee80211_led_tx(local, 1);
  913. tx->extra_frag[i] = NULL;
  914. }
  915. kfree(tx->extra_frag);
  916. tx->extra_frag = NULL;
  917. }
  918. return IEEE80211_TX_OK;
  919. }
  920. /*
  921. * Invoke TX handlers, return 0 on success and non-zero if the
  922. * frame was dropped or queued.
  923. */
  924. static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
  925. {
  926. struct sk_buff *skb = tx->skb;
  927. ieee80211_tx_result res = TX_DROP;
  928. int i;
  929. #define CALL_TXH(txh) \
  930. res = txh(tx); \
  931. if (res != TX_CONTINUE) \
  932. goto txh_done;
  933. CALL_TXH(ieee80211_tx_h_check_assoc)
  934. CALL_TXH(ieee80211_tx_h_ps_buf)
  935. CALL_TXH(ieee80211_tx_h_select_key)
  936. CALL_TXH(ieee80211_tx_h_michael_mic_add)
  937. CALL_TXH(ieee80211_tx_h_rate_ctrl)
  938. CALL_TXH(ieee80211_tx_h_misc)
  939. CALL_TXH(ieee80211_tx_h_sequence)
  940. CALL_TXH(ieee80211_tx_h_fragment)
  941. /* handlers after fragment must be aware of tx info fragmentation! */
  942. CALL_TXH(ieee80211_tx_h_encrypt)
  943. CALL_TXH(ieee80211_tx_h_calculate_duration)
  944. CALL_TXH(ieee80211_tx_h_stats)
  945. #undef CALL_TXH
  946. txh_done:
  947. if (unlikely(res == TX_DROP)) {
  948. I802_DEBUG_INC(tx->local->tx_handlers_drop);
  949. dev_kfree_skb(skb);
  950. for (i = 0; i < tx->num_extra_frag; i++)
  951. if (tx->extra_frag[i])
  952. dev_kfree_skb(tx->extra_frag[i]);
  953. kfree(tx->extra_frag);
  954. return -1;
  955. } else if (unlikely(res == TX_QUEUED)) {
  956. I802_DEBUG_INC(tx->local->tx_handlers_queued);
  957. return -1;
  958. }
  959. return 0;
  960. }
  961. static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb)
  962. {
  963. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  964. struct sta_info *sta;
  965. struct ieee80211_tx_data tx;
  966. ieee80211_tx_result res_prepare;
  967. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  968. int ret, i;
  969. u16 queue;
  970. queue = skb_get_queue_mapping(skb);
  971. WARN_ON(test_bit(queue, local->queues_pending));
  972. if (unlikely(skb->len < 10)) {
  973. dev_kfree_skb(skb);
  974. return 0;
  975. }
  976. rcu_read_lock();
  977. /* initialises tx */
  978. res_prepare = __ieee80211_tx_prepare(&tx, skb, dev);
  979. if (res_prepare == TX_DROP) {
  980. dev_kfree_skb(skb);
  981. rcu_read_unlock();
  982. return 0;
  983. }
  984. sta = tx.sta;
  985. tx.channel = local->hw.conf.channel;
  986. info->band = tx.channel->band;
  987. if (invoke_tx_handlers(&tx))
  988. goto out;
  989. retry:
  990. ret = __ieee80211_tx(local, skb, &tx);
  991. if (ret) {
  992. struct ieee80211_tx_stored_packet *store;
  993. /*
  994. * Since there are no fragmented frames on A-MPDU
  995. * queues, there's no reason for a driver to reject
  996. * a frame there, warn and drop it.
  997. */
  998. if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
  999. goto drop;
  1000. store = &local->pending_packet[queue];
  1001. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1002. skb = NULL;
  1003. set_bit(queue, local->queues_pending);
  1004. smp_mb();
  1005. /*
  1006. * When the driver gets out of buffers during sending of
  1007. * fragments and calls ieee80211_stop_queue, the netif
  1008. * subqueue is stopped. There is, however, a small window
  1009. * in which the PENDING bit is not yet set. If a buffer
  1010. * gets available in that window (i.e. driver calls
  1011. * ieee80211_wake_queue), we would end up with ieee80211_tx
  1012. * called with the PENDING bit still set. Prevent this by
  1013. * continuing transmitting here when that situation is
  1014. * possible to have happened.
  1015. */
  1016. if (!__netif_subqueue_stopped(local->mdev, queue)) {
  1017. clear_bit(queue, local->queues_pending);
  1018. goto retry;
  1019. }
  1020. store->skb = skb;
  1021. store->extra_frag = tx.extra_frag;
  1022. store->num_extra_frag = tx.num_extra_frag;
  1023. }
  1024. out:
  1025. rcu_read_unlock();
  1026. return 0;
  1027. drop:
  1028. if (skb)
  1029. dev_kfree_skb(skb);
  1030. for (i = 0; i < tx.num_extra_frag; i++)
  1031. if (tx.extra_frag[i])
  1032. dev_kfree_skb(tx.extra_frag[i]);
  1033. kfree(tx.extra_frag);
  1034. rcu_read_unlock();
  1035. return 0;
  1036. }
  1037. /* device xmit handlers */
  1038. static int ieee80211_skb_resize(struct ieee80211_local *local,
  1039. struct sk_buff *skb,
  1040. int head_need, bool may_encrypt)
  1041. {
  1042. int tail_need = 0;
  1043. /*
  1044. * This could be optimised, devices that do full hardware
  1045. * crypto (including TKIP MMIC) need no tailroom... But we
  1046. * have no drivers for such devices currently.
  1047. */
  1048. if (may_encrypt) {
  1049. tail_need = IEEE80211_ENCRYPT_TAILROOM;
  1050. tail_need -= skb_tailroom(skb);
  1051. tail_need = max_t(int, tail_need, 0);
  1052. }
  1053. if (head_need || tail_need) {
  1054. /* Sorry. Can't account for this any more */
  1055. skb_orphan(skb);
  1056. }
  1057. if (skb_header_cloned(skb))
  1058. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1059. else
  1060. I802_DEBUG_INC(local->tx_expand_skb_head);
  1061. if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
  1062. printk(KERN_DEBUG "%s: failed to reallocate TX buffer\n",
  1063. wiphy_name(local->hw.wiphy));
  1064. return -ENOMEM;
  1065. }
  1066. /* update truesize too */
  1067. skb->truesize += head_need + tail_need;
  1068. return 0;
  1069. }
  1070. int ieee80211_master_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1071. {
  1072. struct ieee80211_master_priv *mpriv = netdev_priv(dev);
  1073. struct ieee80211_local *local = mpriv->local;
  1074. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1075. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1076. struct net_device *odev = NULL;
  1077. struct ieee80211_sub_if_data *osdata;
  1078. int headroom;
  1079. bool may_encrypt;
  1080. enum {
  1081. NOT_MONITOR,
  1082. FOUND_SDATA,
  1083. UNKNOWN_ADDRESS,
  1084. } monitor_iface = NOT_MONITOR;
  1085. int ret;
  1086. if (skb->iif)
  1087. odev = dev_get_by_index(&init_net, skb->iif);
  1088. if (unlikely(odev && !is_ieee80211_device(local, odev))) {
  1089. dev_put(odev);
  1090. odev = NULL;
  1091. }
  1092. if (unlikely(!odev)) {
  1093. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1094. printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
  1095. "originating device\n", dev->name);
  1096. #endif
  1097. dev_kfree_skb(skb);
  1098. return 0;
  1099. }
  1100. memset(info, 0, sizeof(*info));
  1101. info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  1102. osdata = IEEE80211_DEV_TO_SUB_IF(odev);
  1103. if (ieee80211_vif_is_mesh(&osdata->vif) &&
  1104. ieee80211_is_data(hdr->frame_control)) {
  1105. if (is_multicast_ether_addr(hdr->addr3))
  1106. memcpy(hdr->addr1, hdr->addr3, ETH_ALEN);
  1107. else
  1108. if (mesh_nexthop_lookup(skb, osdata)) {
  1109. dev_put(odev);
  1110. return 0;
  1111. }
  1112. if (memcmp(odev->dev_addr, hdr->addr4, ETH_ALEN) != 0)
  1113. IEEE80211_IFSTA_MESH_CTR_INC(&osdata->u.mesh,
  1114. fwded_frames);
  1115. } else if (unlikely(osdata->vif.type == NL80211_IFTYPE_MONITOR)) {
  1116. struct ieee80211_sub_if_data *sdata;
  1117. int hdrlen;
  1118. u16 len_rthdr;
  1119. info->flags |= IEEE80211_TX_CTL_INJECTED;
  1120. monitor_iface = UNKNOWN_ADDRESS;
  1121. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1122. hdr = (struct ieee80211_hdr *)skb->data + len_rthdr;
  1123. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1124. /* check the header is complete in the frame */
  1125. if (likely(skb->len >= len_rthdr + hdrlen)) {
  1126. /*
  1127. * We process outgoing injected frames that have a
  1128. * local address we handle as though they are our
  1129. * own frames.
  1130. * This code here isn't entirely correct, the local
  1131. * MAC address is not necessarily enough to find
  1132. * the interface to use; for that proper VLAN/WDS
  1133. * support we will need a different mechanism.
  1134. */
  1135. rcu_read_lock();
  1136. list_for_each_entry_rcu(sdata, &local->interfaces,
  1137. list) {
  1138. if (!netif_running(sdata->dev))
  1139. continue;
  1140. if (sdata->vif.type != NL80211_IFTYPE_AP)
  1141. continue;
  1142. if (compare_ether_addr(sdata->dev->dev_addr,
  1143. hdr->addr2)) {
  1144. dev_hold(sdata->dev);
  1145. dev_put(odev);
  1146. osdata = sdata;
  1147. odev = osdata->dev;
  1148. skb->iif = sdata->dev->ifindex;
  1149. monitor_iface = FOUND_SDATA;
  1150. break;
  1151. }
  1152. }
  1153. rcu_read_unlock();
  1154. }
  1155. }
  1156. may_encrypt = !skb->do_not_encrypt;
  1157. headroom = osdata->local->tx_headroom;
  1158. if (may_encrypt)
  1159. headroom += IEEE80211_ENCRYPT_HEADROOM;
  1160. headroom -= skb_headroom(skb);
  1161. headroom = max_t(int, 0, headroom);
  1162. if (ieee80211_skb_resize(osdata->local, skb, headroom, may_encrypt)) {
  1163. dev_kfree_skb(skb);
  1164. dev_put(odev);
  1165. return 0;
  1166. }
  1167. if (osdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1168. osdata = container_of(osdata->bss,
  1169. struct ieee80211_sub_if_data,
  1170. u.ap);
  1171. if (likely(monitor_iface != UNKNOWN_ADDRESS))
  1172. info->control.vif = &osdata->vif;
  1173. ret = ieee80211_tx(odev, skb);
  1174. dev_put(odev);
  1175. return ret;
  1176. }
  1177. int ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1178. struct net_device *dev)
  1179. {
  1180. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1181. struct ieee80211_radiotap_header *prthdr =
  1182. (struct ieee80211_radiotap_header *)skb->data;
  1183. u16 len_rthdr;
  1184. /* check for not even having the fixed radiotap header part */
  1185. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1186. goto fail; /* too short to be possibly valid */
  1187. /* is it a header version we can trust to find length from? */
  1188. if (unlikely(prthdr->it_version))
  1189. goto fail; /* only version 0 is supported */
  1190. /* then there must be a radiotap header with a length we can use */
  1191. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1192. /* does the skb contain enough to deliver on the alleged length? */
  1193. if (unlikely(skb->len < len_rthdr))
  1194. goto fail; /* skb too short for claimed rt header extent */
  1195. skb->dev = local->mdev;
  1196. /* needed because we set skb device to master */
  1197. skb->iif = dev->ifindex;
  1198. /* sometimes we do encrypt injected frames, will be fixed
  1199. * up in radiotap parser if not wanted */
  1200. skb->do_not_encrypt = 0;
  1201. /*
  1202. * fix up the pointers accounting for the radiotap
  1203. * header still being in there. We are being given
  1204. * a precooked IEEE80211 header so no need for
  1205. * normal processing
  1206. */
  1207. skb_set_mac_header(skb, len_rthdr);
  1208. /*
  1209. * these are just fixed to the end of the rt area since we
  1210. * don't have any better information and at this point, nobody cares
  1211. */
  1212. skb_set_network_header(skb, len_rthdr);
  1213. skb_set_transport_header(skb, len_rthdr);
  1214. /* pass the radiotap header up to the next stage intact */
  1215. dev_queue_xmit(skb);
  1216. return NETDEV_TX_OK;
  1217. fail:
  1218. dev_kfree_skb(skb);
  1219. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  1220. }
  1221. /**
  1222. * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
  1223. * subinterfaces (wlan#, WDS, and VLAN interfaces)
  1224. * @skb: packet to be sent
  1225. * @dev: incoming interface
  1226. *
  1227. * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
  1228. * not be freed, and caller is responsible for either retrying later or freeing
  1229. * skb).
  1230. *
  1231. * This function takes in an Ethernet header and encapsulates it with suitable
  1232. * IEEE 802.11 header based on which interface the packet is coming in. The
  1233. * encapsulated packet will then be passed to master interface, wlan#.11, for
  1234. * transmission (through low-level driver).
  1235. */
  1236. int ieee80211_subif_start_xmit(struct sk_buff *skb,
  1237. struct net_device *dev)
  1238. {
  1239. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1240. struct ieee80211_local *local = sdata->local;
  1241. int ret = 1, head_need;
  1242. u16 ethertype, hdrlen, meshhdrlen = 0;
  1243. __le16 fc;
  1244. struct ieee80211_hdr hdr;
  1245. struct ieee80211s_hdr mesh_hdr;
  1246. const u8 *encaps_data;
  1247. int encaps_len, skip_header_bytes;
  1248. int nh_pos, h_pos;
  1249. struct sta_info *sta;
  1250. u32 sta_flags = 0;
  1251. if (unlikely(skb->len < ETH_HLEN)) {
  1252. ret = 0;
  1253. goto fail;
  1254. }
  1255. if (!(local->hw.flags & IEEE80211_HW_NO_STACK_DYNAMIC_PS) &&
  1256. local->dynamic_ps_timeout > 0) {
  1257. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1258. ieee80211_stop_queues_by_reason(&local->hw,
  1259. IEEE80211_QUEUE_STOP_REASON_PS);
  1260. queue_work(local->hw.workqueue,
  1261. &local->dynamic_ps_disable_work);
  1262. }
  1263. mod_timer(&local->dynamic_ps_timer, jiffies +
  1264. msecs_to_jiffies(local->dynamic_ps_timeout));
  1265. }
  1266. nh_pos = skb_network_header(skb) - skb->data;
  1267. h_pos = skb_transport_header(skb) - skb->data;
  1268. /* convert Ethernet header to proper 802.11 header (based on
  1269. * operation mode) */
  1270. ethertype = (skb->data[12] << 8) | skb->data[13];
  1271. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  1272. switch (sdata->vif.type) {
  1273. case NL80211_IFTYPE_AP:
  1274. case NL80211_IFTYPE_AP_VLAN:
  1275. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  1276. /* DA BSSID SA */
  1277. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1278. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1279. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1280. hdrlen = 24;
  1281. break;
  1282. case NL80211_IFTYPE_WDS:
  1283. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  1284. /* RA TA DA SA */
  1285. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  1286. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1287. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1288. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1289. hdrlen = 30;
  1290. break;
  1291. #ifdef CONFIG_MAC80211_MESH
  1292. case NL80211_IFTYPE_MESH_POINT:
  1293. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  1294. if (!sdata->u.mesh.mshcfg.dot11MeshTTL) {
  1295. /* Do not send frames with mesh_ttl == 0 */
  1296. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  1297. ret = 0;
  1298. goto fail;
  1299. }
  1300. memset(&mesh_hdr, 0, sizeof(mesh_hdr));
  1301. if (compare_ether_addr(dev->dev_addr,
  1302. skb->data + ETH_ALEN) == 0) {
  1303. /* RA TA DA SA */
  1304. memset(hdr.addr1, 0, ETH_ALEN);
  1305. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1306. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1307. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1308. meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr, sdata);
  1309. } else {
  1310. /* packet from other interface */
  1311. struct mesh_path *mppath;
  1312. memset(hdr.addr1, 0, ETH_ALEN);
  1313. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1314. memcpy(hdr.addr4, dev->dev_addr, ETH_ALEN);
  1315. if (is_multicast_ether_addr(skb->data))
  1316. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1317. else {
  1318. rcu_read_lock();
  1319. mppath = mpp_path_lookup(skb->data, sdata);
  1320. if (mppath)
  1321. memcpy(hdr.addr3, mppath->mpp, ETH_ALEN);
  1322. else
  1323. memset(hdr.addr3, 0xff, ETH_ALEN);
  1324. rcu_read_unlock();
  1325. }
  1326. mesh_hdr.flags |= MESH_FLAGS_AE_A5_A6;
  1327. mesh_hdr.ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
  1328. put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &mesh_hdr.seqnum);
  1329. memcpy(mesh_hdr.eaddr1, skb->data, ETH_ALEN);
  1330. memcpy(mesh_hdr.eaddr2, skb->data + ETH_ALEN, ETH_ALEN);
  1331. sdata->u.mesh.mesh_seqnum++;
  1332. meshhdrlen = 18;
  1333. }
  1334. hdrlen = 30;
  1335. break;
  1336. #endif
  1337. case NL80211_IFTYPE_STATION:
  1338. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  1339. /* BSSID SA DA */
  1340. memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
  1341. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1342. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1343. hdrlen = 24;
  1344. break;
  1345. case NL80211_IFTYPE_ADHOC:
  1346. /* DA SA BSSID */
  1347. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1348. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1349. memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
  1350. hdrlen = 24;
  1351. break;
  1352. default:
  1353. ret = 0;
  1354. goto fail;
  1355. }
  1356. /*
  1357. * There's no need to try to look up the destination
  1358. * if it is a multicast address (which can only happen
  1359. * in AP mode)
  1360. */
  1361. if (!is_multicast_ether_addr(hdr.addr1)) {
  1362. rcu_read_lock();
  1363. sta = sta_info_get(local, hdr.addr1);
  1364. if (sta)
  1365. sta_flags = get_sta_flags(sta);
  1366. rcu_read_unlock();
  1367. }
  1368. /* receiver and we are QoS enabled, use a QoS type frame */
  1369. if (sta_flags & WLAN_STA_WME &&
  1370. ieee80211_num_regular_queues(&local->hw) >= 4) {
  1371. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  1372. hdrlen += 2;
  1373. }
  1374. /*
  1375. * Drop unicast frames to unauthorised stations unless they are
  1376. * EAPOL frames from the local station.
  1377. */
  1378. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  1379. unlikely(!is_multicast_ether_addr(hdr.addr1) &&
  1380. !(sta_flags & WLAN_STA_AUTHORIZED) &&
  1381. !(ethertype == ETH_P_PAE &&
  1382. compare_ether_addr(dev->dev_addr,
  1383. skb->data + ETH_ALEN) == 0))) {
  1384. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1385. if (net_ratelimit())
  1386. printk(KERN_DEBUG "%s: dropped frame to %pM"
  1387. " (unauthorized port)\n", dev->name,
  1388. hdr.addr1);
  1389. #endif
  1390. I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
  1391. ret = 0;
  1392. goto fail;
  1393. }
  1394. hdr.frame_control = fc;
  1395. hdr.duration_id = 0;
  1396. hdr.seq_ctrl = 0;
  1397. skip_header_bytes = ETH_HLEN;
  1398. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1399. encaps_data = bridge_tunnel_header;
  1400. encaps_len = sizeof(bridge_tunnel_header);
  1401. skip_header_bytes -= 2;
  1402. } else if (ethertype >= 0x600) {
  1403. encaps_data = rfc1042_header;
  1404. encaps_len = sizeof(rfc1042_header);
  1405. skip_header_bytes -= 2;
  1406. } else {
  1407. encaps_data = NULL;
  1408. encaps_len = 0;
  1409. }
  1410. skb_pull(skb, skip_header_bytes);
  1411. nh_pos -= skip_header_bytes;
  1412. h_pos -= skip_header_bytes;
  1413. head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
  1414. /*
  1415. * So we need to modify the skb header and hence need a copy of
  1416. * that. The head_need variable above doesn't, so far, include
  1417. * the needed header space that we don't need right away. If we
  1418. * can, then we don't reallocate right now but only after the
  1419. * frame arrives at the master device (if it does...)
  1420. *
  1421. * If we cannot, however, then we will reallocate to include all
  1422. * the ever needed space. Also, if we need to reallocate it anyway,
  1423. * make it big enough for everything we may ever need.
  1424. */
  1425. if (head_need > 0 || skb_cloned(skb)) {
  1426. head_need += IEEE80211_ENCRYPT_HEADROOM;
  1427. head_need += local->tx_headroom;
  1428. head_need = max_t(int, 0, head_need);
  1429. if (ieee80211_skb_resize(local, skb, head_need, true))
  1430. goto fail;
  1431. }
  1432. if (encaps_data) {
  1433. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1434. nh_pos += encaps_len;
  1435. h_pos += encaps_len;
  1436. }
  1437. if (meshhdrlen > 0) {
  1438. memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
  1439. nh_pos += meshhdrlen;
  1440. h_pos += meshhdrlen;
  1441. }
  1442. if (ieee80211_is_data_qos(fc)) {
  1443. __le16 *qos_control;
  1444. qos_control = (__le16*) skb_push(skb, 2);
  1445. memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
  1446. /*
  1447. * Maybe we could actually set some fields here, for now just
  1448. * initialise to zero to indicate no special operation.
  1449. */
  1450. *qos_control = 0;
  1451. } else
  1452. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1453. nh_pos += hdrlen;
  1454. h_pos += hdrlen;
  1455. skb->iif = dev->ifindex;
  1456. skb->dev = local->mdev;
  1457. dev->stats.tx_packets++;
  1458. dev->stats.tx_bytes += skb->len;
  1459. /* Update skb pointers to various headers since this modified frame
  1460. * is going to go through Linux networking code that may potentially
  1461. * need things like pointer to IP header. */
  1462. skb_set_mac_header(skb, 0);
  1463. skb_set_network_header(skb, nh_pos);
  1464. skb_set_transport_header(skb, h_pos);
  1465. dev->trans_start = jiffies;
  1466. dev_queue_xmit(skb);
  1467. return 0;
  1468. fail:
  1469. if (!ret)
  1470. dev_kfree_skb(skb);
  1471. return ret;
  1472. }
  1473. /*
  1474. * ieee80211_clear_tx_pending may not be called in a context where
  1475. * it is possible that it packets could come in again.
  1476. */
  1477. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  1478. {
  1479. int i, j;
  1480. struct ieee80211_tx_stored_packet *store;
  1481. for (i = 0; i < ieee80211_num_regular_queues(&local->hw); i++) {
  1482. if (!test_bit(i, local->queues_pending))
  1483. continue;
  1484. store = &local->pending_packet[i];
  1485. kfree_skb(store->skb);
  1486. for (j = 0; j < store->num_extra_frag; j++)
  1487. kfree_skb(store->extra_frag[j]);
  1488. kfree(store->extra_frag);
  1489. clear_bit(i, local->queues_pending);
  1490. }
  1491. }
  1492. /*
  1493. * Transmit all pending packets. Called from tasklet, locks master device
  1494. * TX lock so that no new packets can come in.
  1495. */
  1496. void ieee80211_tx_pending(unsigned long data)
  1497. {
  1498. struct ieee80211_local *local = (struct ieee80211_local *)data;
  1499. struct net_device *dev = local->mdev;
  1500. struct ieee80211_tx_stored_packet *store;
  1501. struct ieee80211_tx_data tx;
  1502. int i, ret;
  1503. netif_tx_lock_bh(dev);
  1504. for (i = 0; i < ieee80211_num_regular_queues(&local->hw); i++) {
  1505. /* Check that this queue is ok */
  1506. if (__netif_subqueue_stopped(local->mdev, i) &&
  1507. !test_bit(i, local->queues_pending_run))
  1508. continue;
  1509. if (!test_bit(i, local->queues_pending)) {
  1510. clear_bit(i, local->queues_pending_run);
  1511. ieee80211_wake_queue(&local->hw, i);
  1512. continue;
  1513. }
  1514. clear_bit(i, local->queues_pending_run);
  1515. netif_start_subqueue(local->mdev, i);
  1516. store = &local->pending_packet[i];
  1517. tx.extra_frag = store->extra_frag;
  1518. tx.num_extra_frag = store->num_extra_frag;
  1519. tx.flags = 0;
  1520. ret = __ieee80211_tx(local, store->skb, &tx);
  1521. if (ret) {
  1522. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1523. store->skb = NULL;
  1524. } else {
  1525. clear_bit(i, local->queues_pending);
  1526. ieee80211_wake_queue(&local->hw, i);
  1527. }
  1528. }
  1529. netif_tx_unlock_bh(dev);
  1530. }
  1531. /* functions for drivers to get certain frames */
  1532. static void ieee80211_beacon_add_tim(struct ieee80211_if_ap *bss,
  1533. struct sk_buff *skb,
  1534. struct beacon_data *beacon)
  1535. {
  1536. u8 *pos, *tim;
  1537. int aid0 = 0;
  1538. int i, have_bits = 0, n1, n2;
  1539. /* Generate bitmap for TIM only if there are any STAs in power save
  1540. * mode. */
  1541. if (atomic_read(&bss->num_sta_ps) > 0)
  1542. /* in the hope that this is faster than
  1543. * checking byte-for-byte */
  1544. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1545. IEEE80211_MAX_AID+1);
  1546. if (bss->dtim_count == 0)
  1547. bss->dtim_count = beacon->dtim_period - 1;
  1548. else
  1549. bss->dtim_count--;
  1550. tim = pos = (u8 *) skb_put(skb, 6);
  1551. *pos++ = WLAN_EID_TIM;
  1552. *pos++ = 4;
  1553. *pos++ = bss->dtim_count;
  1554. *pos++ = beacon->dtim_period;
  1555. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1556. aid0 = 1;
  1557. if (have_bits) {
  1558. /* Find largest even number N1 so that bits numbered 1 through
  1559. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1560. * (N2 + 1) x 8 through 2007 are 0. */
  1561. n1 = 0;
  1562. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1563. if (bss->tim[i]) {
  1564. n1 = i & 0xfe;
  1565. break;
  1566. }
  1567. }
  1568. n2 = n1;
  1569. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1570. if (bss->tim[i]) {
  1571. n2 = i;
  1572. break;
  1573. }
  1574. }
  1575. /* Bitmap control */
  1576. *pos++ = n1 | aid0;
  1577. /* Part Virt Bitmap */
  1578. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1579. tim[1] = n2 - n1 + 4;
  1580. skb_put(skb, n2 - n1);
  1581. } else {
  1582. *pos++ = aid0; /* Bitmap control */
  1583. *pos++ = 0; /* Part Virt Bitmap */
  1584. }
  1585. }
  1586. struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
  1587. struct ieee80211_vif *vif)
  1588. {
  1589. struct ieee80211_local *local = hw_to_local(hw);
  1590. struct sk_buff *skb = NULL;
  1591. struct ieee80211_tx_info *info;
  1592. struct ieee80211_sub_if_data *sdata = NULL;
  1593. struct ieee80211_if_ap *ap = NULL;
  1594. struct ieee80211_if_sta *ifsta = NULL;
  1595. struct beacon_data *beacon;
  1596. struct ieee80211_supported_band *sband;
  1597. enum ieee80211_band band = local->hw.conf.channel->band;
  1598. sband = local->hw.wiphy->bands[band];
  1599. rcu_read_lock();
  1600. sdata = vif_to_sdata(vif);
  1601. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1602. ap = &sdata->u.ap;
  1603. beacon = rcu_dereference(ap->beacon);
  1604. if (ap && beacon) {
  1605. /*
  1606. * headroom, head length,
  1607. * tail length and maximum TIM length
  1608. */
  1609. skb = dev_alloc_skb(local->tx_headroom +
  1610. beacon->head_len +
  1611. beacon->tail_len + 256);
  1612. if (!skb)
  1613. goto out;
  1614. skb_reserve(skb, local->tx_headroom);
  1615. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  1616. beacon->head_len);
  1617. /*
  1618. * Not very nice, but we want to allow the driver to call
  1619. * ieee80211_beacon_get() as a response to the set_tim()
  1620. * callback. That, however, is already invoked under the
  1621. * sta_lock to guarantee consistent and race-free update
  1622. * of the tim bitmap in mac80211 and the driver.
  1623. */
  1624. if (local->tim_in_locked_section) {
  1625. ieee80211_beacon_add_tim(ap, skb, beacon);
  1626. } else {
  1627. unsigned long flags;
  1628. spin_lock_irqsave(&local->sta_lock, flags);
  1629. ieee80211_beacon_add_tim(ap, skb, beacon);
  1630. spin_unlock_irqrestore(&local->sta_lock, flags);
  1631. }
  1632. if (beacon->tail)
  1633. memcpy(skb_put(skb, beacon->tail_len),
  1634. beacon->tail, beacon->tail_len);
  1635. } else
  1636. goto out;
  1637. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  1638. struct ieee80211_hdr *hdr;
  1639. ifsta = &sdata->u.sta;
  1640. if (!ifsta->probe_resp)
  1641. goto out;
  1642. skb = skb_copy(ifsta->probe_resp, GFP_ATOMIC);
  1643. if (!skb)
  1644. goto out;
  1645. hdr = (struct ieee80211_hdr *) skb->data;
  1646. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1647. IEEE80211_STYPE_BEACON);
  1648. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1649. struct ieee80211_mgmt *mgmt;
  1650. u8 *pos;
  1651. /* headroom, head length, tail length and maximum TIM length */
  1652. skb = dev_alloc_skb(local->tx_headroom + 400);
  1653. if (!skb)
  1654. goto out;
  1655. skb_reserve(skb, local->hw.extra_tx_headroom);
  1656. mgmt = (struct ieee80211_mgmt *)
  1657. skb_put(skb, 24 + sizeof(mgmt->u.beacon));
  1658. memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  1659. mgmt->frame_control =
  1660. cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON);
  1661. memset(mgmt->da, 0xff, ETH_ALEN);
  1662. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  1663. /* BSSID is left zeroed, wildcard value */
  1664. mgmt->u.beacon.beacon_int =
  1665. cpu_to_le16(local->hw.conf.beacon_int);
  1666. mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
  1667. pos = skb_put(skb, 2);
  1668. *pos++ = WLAN_EID_SSID;
  1669. *pos++ = 0x0;
  1670. mesh_mgmt_ies_add(skb, sdata);
  1671. } else {
  1672. WARN_ON(1);
  1673. goto out;
  1674. }
  1675. info = IEEE80211_SKB_CB(skb);
  1676. skb->do_not_encrypt = 1;
  1677. info->band = band;
  1678. /*
  1679. * XXX: For now, always use the lowest rate
  1680. */
  1681. info->control.rates[0].idx = 0;
  1682. info->control.rates[0].count = 1;
  1683. info->control.rates[1].idx = -1;
  1684. info->control.rates[2].idx = -1;
  1685. info->control.rates[3].idx = -1;
  1686. info->control.rates[4].idx = -1;
  1687. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 5);
  1688. info->control.vif = vif;
  1689. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  1690. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1691. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  1692. out:
  1693. rcu_read_unlock();
  1694. return skb;
  1695. }
  1696. EXPORT_SYMBOL(ieee80211_beacon_get);
  1697. void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1698. const void *frame, size_t frame_len,
  1699. const struct ieee80211_tx_info *frame_txctl,
  1700. struct ieee80211_rts *rts)
  1701. {
  1702. const struct ieee80211_hdr *hdr = frame;
  1703. rts->frame_control =
  1704. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
  1705. rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
  1706. frame_txctl);
  1707. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  1708. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  1709. }
  1710. EXPORT_SYMBOL(ieee80211_rts_get);
  1711. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1712. const void *frame, size_t frame_len,
  1713. const struct ieee80211_tx_info *frame_txctl,
  1714. struct ieee80211_cts *cts)
  1715. {
  1716. const struct ieee80211_hdr *hdr = frame;
  1717. cts->frame_control =
  1718. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
  1719. cts->duration = ieee80211_ctstoself_duration(hw, vif,
  1720. frame_len, frame_txctl);
  1721. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  1722. }
  1723. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  1724. struct sk_buff *
  1725. ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
  1726. struct ieee80211_vif *vif)
  1727. {
  1728. struct ieee80211_local *local = hw_to_local(hw);
  1729. struct sk_buff *skb = NULL;
  1730. struct sta_info *sta;
  1731. struct ieee80211_tx_data tx;
  1732. struct ieee80211_sub_if_data *sdata;
  1733. struct ieee80211_if_ap *bss = NULL;
  1734. struct beacon_data *beacon;
  1735. struct ieee80211_tx_info *info;
  1736. sdata = vif_to_sdata(vif);
  1737. bss = &sdata->u.ap;
  1738. if (!bss)
  1739. return NULL;
  1740. rcu_read_lock();
  1741. beacon = rcu_dereference(bss->beacon);
  1742. if (sdata->vif.type != NL80211_IFTYPE_AP || !beacon || !beacon->head)
  1743. goto out;
  1744. if (bss->dtim_count != 0)
  1745. goto out; /* send buffered bc/mc only after DTIM beacon */
  1746. while (1) {
  1747. skb = skb_dequeue(&bss->ps_bc_buf);
  1748. if (!skb)
  1749. goto out;
  1750. local->total_ps_buffered--;
  1751. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  1752. struct ieee80211_hdr *hdr =
  1753. (struct ieee80211_hdr *) skb->data;
  1754. /* more buffered multicast/broadcast frames ==> set
  1755. * MoreData flag in IEEE 802.11 header to inform PS
  1756. * STAs */
  1757. hdr->frame_control |=
  1758. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1759. }
  1760. if (!ieee80211_tx_prepare(local, &tx, skb))
  1761. break;
  1762. dev_kfree_skb_any(skb);
  1763. }
  1764. info = IEEE80211_SKB_CB(skb);
  1765. sta = tx.sta;
  1766. tx.flags |= IEEE80211_TX_PS_BUFFERED;
  1767. tx.channel = local->hw.conf.channel;
  1768. info->band = tx.channel->band;
  1769. if (invoke_tx_handlers(&tx))
  1770. skb = NULL;
  1771. out:
  1772. rcu_read_unlock();
  1773. return skb;
  1774. }
  1775. EXPORT_SYMBOL(ieee80211_get_buffered_bc);