tx.c 57 KB

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