tx.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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 <net/ieee80211_radiotap.h>
  20. #include <net/cfg80211.h>
  21. #include <net/mac80211.h>
  22. #include <asm/unaligned.h>
  23. #include "ieee80211_i.h"
  24. #include "ieee80211_led.h"
  25. #include "wep.h"
  26. #include "wpa.h"
  27. #include "wme.h"
  28. #include "ieee80211_rate.h"
  29. #define IEEE80211_TX_OK 0
  30. #define IEEE80211_TX_AGAIN 1
  31. #define IEEE80211_TX_FRAG_AGAIN 2
  32. /* misc utils */
  33. static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
  34. struct ieee80211_hdr *hdr)
  35. {
  36. /* Set the sequence number for this frame. */
  37. hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
  38. /* Increase the sequence number. */
  39. sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
  40. }
  41. #ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
  42. static void ieee80211_dump_frame(const char *ifname, const char *title,
  43. const struct sk_buff *skb)
  44. {
  45. const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  46. u16 fc;
  47. int hdrlen;
  48. printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
  49. if (skb->len < 4) {
  50. printk("\n");
  51. return;
  52. }
  53. fc = le16_to_cpu(hdr->frame_control);
  54. hdrlen = ieee80211_get_hdrlen(fc);
  55. if (hdrlen > skb->len)
  56. hdrlen = skb->len;
  57. if (hdrlen >= 4)
  58. printk(" FC=0x%04x DUR=0x%04x",
  59. fc, le16_to_cpu(hdr->duration_id));
  60. if (hdrlen >= 10)
  61. printk(" A1=" MAC_FMT, MAC_ARG(hdr->addr1));
  62. if (hdrlen >= 16)
  63. printk(" A2=" MAC_FMT, MAC_ARG(hdr->addr2));
  64. if (hdrlen >= 24)
  65. printk(" A3=" MAC_FMT, MAC_ARG(hdr->addr3));
  66. if (hdrlen >= 30)
  67. printk(" A4=" MAC_FMT, MAC_ARG(hdr->addr4));
  68. printk("\n");
  69. }
  70. #else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
  71. static inline void ieee80211_dump_frame(const char *ifname, const char *title,
  72. struct sk_buff *skb)
  73. {
  74. }
  75. #endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
  76. static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
  77. int next_frag_len)
  78. {
  79. int rate, mrate, erp, dur, i;
  80. struct ieee80211_rate *txrate = tx->u.tx.rate;
  81. struct ieee80211_local *local = tx->local;
  82. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  83. erp = txrate->flags & IEEE80211_RATE_ERP;
  84. /*
  85. * data and mgmt (except PS Poll):
  86. * - during CFP: 32768
  87. * - during contention period:
  88. * if addr1 is group address: 0
  89. * if more fragments = 0 and addr1 is individual address: time to
  90. * transmit one ACK plus SIFS
  91. * if more fragments = 1 and addr1 is individual address: time to
  92. * transmit next fragment plus 2 x ACK plus 3 x SIFS
  93. *
  94. * IEEE 802.11, 9.6:
  95. * - control response frame (CTS or ACK) shall be transmitted using the
  96. * same rate as the immediately previous frame in the frame exchange
  97. * sequence, if this rate belongs to the PHY mandatory rates, or else
  98. * at the highest possible rate belonging to the PHY rates in the
  99. * BSSBasicRateSet
  100. */
  101. if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
  102. /* TODO: These control frames are not currently sent by
  103. * 80211.o, but should they be implemented, this function
  104. * needs to be updated to support duration field calculation.
  105. *
  106. * RTS: time needed to transmit pending data/mgmt frame plus
  107. * one CTS frame plus one ACK frame plus 3 x SIFS
  108. * CTS: duration of immediately previous RTS minus time
  109. * required to transmit CTS and its SIFS
  110. * ACK: 0 if immediately previous directed data/mgmt had
  111. * more=0, with more=1 duration in ACK frame is duration
  112. * from previous frame minus time needed to transmit ACK
  113. * and its SIFS
  114. * PS Poll: BIT(15) | BIT(14) | aid
  115. */
  116. return 0;
  117. }
  118. /* data/mgmt */
  119. if (0 /* FIX: data/mgmt during CFP */)
  120. return 32768;
  121. if (group_addr) /* Group address as the destination - no ACK */
  122. return 0;
  123. /* Individual destination address:
  124. * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
  125. * CTS and ACK frames shall be transmitted using the highest rate in
  126. * basic rate set that is less than or equal to the rate of the
  127. * immediately previous frame and that is using the same modulation
  128. * (CCK or OFDM). If no basic rate set matches with these requirements,
  129. * the highest mandatory rate of the PHY that is less than or equal to
  130. * the rate of the previous frame is used.
  131. * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
  132. */
  133. rate = -1;
  134. mrate = 10; /* use 1 Mbps if everything fails */
  135. for (i = 0; i < mode->num_rates; i++) {
  136. struct ieee80211_rate *r = &mode->rates[i];
  137. if (r->rate > txrate->rate)
  138. break;
  139. if (IEEE80211_RATE_MODULATION(txrate->flags) !=
  140. IEEE80211_RATE_MODULATION(r->flags))
  141. continue;
  142. if (r->flags & IEEE80211_RATE_BASIC)
  143. rate = r->rate;
  144. else if (r->flags & IEEE80211_RATE_MANDATORY)
  145. mrate = r->rate;
  146. }
  147. if (rate == -1) {
  148. /* No matching basic rate found; use highest suitable mandatory
  149. * PHY rate */
  150. rate = mrate;
  151. }
  152. /* Time needed to transmit ACK
  153. * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
  154. * to closest integer */
  155. dur = ieee80211_frame_duration(local, 10, rate, erp,
  156. tx->sdata->short_preamble);
  157. if (next_frag_len) {
  158. /* Frame is fragmented: duration increases with time needed to
  159. * transmit next fragment plus ACK and 2 x SIFS. */
  160. dur *= 2; /* ACK + SIFS */
  161. /* next fragment */
  162. dur += ieee80211_frame_duration(local, next_frag_len,
  163. txrate->rate, erp,
  164. tx->sdata->short_preamble);
  165. }
  166. return dur;
  167. }
  168. static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
  169. int queue)
  170. {
  171. return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  172. }
  173. static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
  174. int queue)
  175. {
  176. return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
  177. }
  178. static int inline is_ieee80211_device(struct net_device *dev,
  179. struct net_device *master)
  180. {
  181. return (wdev_priv(dev->ieee80211_ptr) ==
  182. wdev_priv(master->ieee80211_ptr));
  183. }
  184. /* tx handlers */
  185. static ieee80211_txrx_result
  186. ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
  187. {
  188. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  189. struct sk_buff *skb = tx->skb;
  190. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  191. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  192. u32 sta_flags;
  193. if (unlikely(tx->local->sta_scanning != 0) &&
  194. ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  195. (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
  196. return TXRX_DROP;
  197. if (tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED)
  198. return TXRX_CONTINUE;
  199. sta_flags = tx->sta ? tx->sta->flags : 0;
  200. if (likely(tx->flags & IEEE80211_TXRXD_TXUNICAST)) {
  201. if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
  202. tx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
  203. (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
  204. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  205. printk(KERN_DEBUG "%s: dropped data frame to not "
  206. "associated station " MAC_FMT "\n",
  207. tx->dev->name, MAC_ARG(hdr->addr1));
  208. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  209. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  210. return TXRX_DROP;
  211. }
  212. } else {
  213. if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  214. tx->local->num_sta == 0 &&
  215. tx->sdata->type != IEEE80211_IF_TYPE_IBSS)) {
  216. /*
  217. * No associated STAs - no need to send multicast
  218. * frames.
  219. */
  220. return TXRX_DROP;
  221. }
  222. return TXRX_CONTINUE;
  223. }
  224. if (unlikely(!tx->u.tx.mgmt_interface && tx->sdata->ieee802_1x &&
  225. !(sta_flags & WLAN_STA_AUTHORIZED))) {
  226. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  227. printk(KERN_DEBUG "%s: dropped frame to " MAC_FMT
  228. " (unauthorized port)\n", tx->dev->name,
  229. MAC_ARG(hdr->addr1));
  230. #endif
  231. I802_DEBUG_INC(tx->local->tx_handlers_drop_unauth_port);
  232. return TXRX_DROP;
  233. }
  234. return TXRX_CONTINUE;
  235. }
  236. static ieee80211_txrx_result
  237. ieee80211_tx_h_sequence(struct ieee80211_txrx_data *tx)
  238. {
  239. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  240. if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
  241. ieee80211_include_sequence(tx->sdata, hdr);
  242. return TXRX_CONTINUE;
  243. }
  244. /* This function is called whenever the AP is about to exceed the maximum limit
  245. * of buffered frames for power saving STAs. This situation should not really
  246. * happen often during normal operation, so dropping the oldest buffered packet
  247. * from each queue should be OK to make some room for new frames. */
  248. static void purge_old_ps_buffers(struct ieee80211_local *local)
  249. {
  250. int total = 0, purged = 0;
  251. struct sk_buff *skb;
  252. struct ieee80211_sub_if_data *sdata;
  253. struct sta_info *sta;
  254. read_lock(&local->sub_if_lock);
  255. list_for_each_entry(sdata, &local->sub_if_list, list) {
  256. struct ieee80211_if_ap *ap;
  257. if (sdata->dev == local->mdev ||
  258. sdata->type != IEEE80211_IF_TYPE_AP)
  259. continue;
  260. ap = &sdata->u.ap;
  261. skb = skb_dequeue(&ap->ps_bc_buf);
  262. if (skb) {
  263. purged++;
  264. dev_kfree_skb(skb);
  265. }
  266. total += skb_queue_len(&ap->ps_bc_buf);
  267. }
  268. read_unlock(&local->sub_if_lock);
  269. read_lock_bh(&local->sta_lock);
  270. list_for_each_entry(sta, &local->sta_list, list) {
  271. skb = skb_dequeue(&sta->ps_tx_buf);
  272. if (skb) {
  273. purged++;
  274. dev_kfree_skb(skb);
  275. }
  276. total += skb_queue_len(&sta->ps_tx_buf);
  277. }
  278. read_unlock_bh(&local->sta_lock);
  279. local->total_ps_buffered = total;
  280. printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
  281. local->mdev->name, purged);
  282. }
  283. static inline ieee80211_txrx_result
  284. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
  285. {
  286. /* broadcast/multicast frame */
  287. /* If any of the associated stations is in power save mode,
  288. * the frame is buffered to be sent after DTIM beacon frame */
  289. if ((tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) &&
  290. tx->sdata->type != IEEE80211_IF_TYPE_WDS &&
  291. tx->sdata->bss && atomic_read(&tx->sdata->bss->num_sta_ps) &&
  292. !(tx->fc & IEEE80211_FCTL_ORDER)) {
  293. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  294. purge_old_ps_buffers(tx->local);
  295. if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
  296. AP_MAX_BC_BUFFER) {
  297. if (net_ratelimit()) {
  298. printk(KERN_DEBUG "%s: BC TX buffer full - "
  299. "dropping the oldest frame\n",
  300. tx->dev->name);
  301. }
  302. dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
  303. } else
  304. tx->local->total_ps_buffered++;
  305. skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
  306. return TXRX_QUEUED;
  307. }
  308. return TXRX_CONTINUE;
  309. }
  310. static inline ieee80211_txrx_result
  311. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
  312. {
  313. struct sta_info *sta = tx->sta;
  314. if (unlikely(!sta ||
  315. ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
  316. (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
  317. return TXRX_CONTINUE;
  318. if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
  319. struct ieee80211_tx_packet_data *pkt_data;
  320. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  321. printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS buffer (entries "
  322. "before %d)\n",
  323. MAC_ARG(sta->addr), sta->aid,
  324. skb_queue_len(&sta->ps_tx_buf));
  325. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  326. sta->flags |= WLAN_STA_TIM;
  327. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  328. purge_old_ps_buffers(tx->local);
  329. if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
  330. struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
  331. if (net_ratelimit()) {
  332. printk(KERN_DEBUG "%s: STA " MAC_FMT " TX "
  333. "buffer full - dropping oldest frame\n",
  334. tx->dev->name, MAC_ARG(sta->addr));
  335. }
  336. dev_kfree_skb(old);
  337. } else
  338. tx->local->total_ps_buffered++;
  339. /* Queue frame to be sent after STA sends an PS Poll frame */
  340. if (skb_queue_empty(&sta->ps_tx_buf)) {
  341. if (tx->local->ops->set_tim)
  342. tx->local->ops->set_tim(local_to_hw(tx->local),
  343. sta->aid, 1);
  344. if (tx->sdata->bss)
  345. bss_tim_set(tx->local, tx->sdata->bss, sta->aid);
  346. }
  347. pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
  348. pkt_data->jiffies = jiffies;
  349. skb_queue_tail(&sta->ps_tx_buf, tx->skb);
  350. return TXRX_QUEUED;
  351. }
  352. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  353. else if (unlikely(sta->flags & WLAN_STA_PS)) {
  354. printk(KERN_DEBUG "%s: STA " MAC_FMT " in PS mode, but pspoll "
  355. "set -> send frame\n", tx->dev->name,
  356. MAC_ARG(sta->addr));
  357. }
  358. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  359. sta->pspoll = 0;
  360. return TXRX_CONTINUE;
  361. }
  362. static ieee80211_txrx_result
  363. ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
  364. {
  365. if (unlikely(tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED))
  366. return TXRX_CONTINUE;
  367. if (tx->flags & IEEE80211_TXRXD_TXUNICAST)
  368. return ieee80211_tx_h_unicast_ps_buf(tx);
  369. else
  370. return ieee80211_tx_h_multicast_ps_buf(tx);
  371. }
  372. static ieee80211_txrx_result
  373. ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
  374. {
  375. if (tx->sta)
  376. tx->u.tx.control->key_idx = tx->sta->key_idx_compression;
  377. else
  378. tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
  379. if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
  380. tx->key = NULL;
  381. else if (tx->sta && tx->sta->key)
  382. tx->key = tx->sta->key;
  383. else if (tx->sdata->default_key)
  384. tx->key = tx->sdata->default_key;
  385. else if (tx->sdata->drop_unencrypted &&
  386. !(tx->sdata->eapol && ieee80211_is_eapol(tx->skb))) {
  387. I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
  388. return TXRX_DROP;
  389. } else
  390. tx->key = NULL;
  391. if (tx->key) {
  392. tx->key->tx_rx_count++;
  393. if (unlikely(tx->local->key_tx_rx_threshold &&
  394. tx->key->tx_rx_count >
  395. tx->local->key_tx_rx_threshold)) {
  396. ieee80211_key_threshold_notify(tx->dev, tx->key,
  397. tx->sta);
  398. }
  399. }
  400. return TXRX_CONTINUE;
  401. }
  402. static ieee80211_txrx_result
  403. ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
  404. {
  405. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  406. size_t hdrlen, per_fragm, num_fragm, payload_len, left;
  407. struct sk_buff **frags, *first, *frag;
  408. int i;
  409. u16 seq;
  410. u8 *pos;
  411. int frag_threshold = tx->local->fragmentation_threshold;
  412. if (!(tx->flags & IEEE80211_TXRXD_FRAGMENTED))
  413. return TXRX_CONTINUE;
  414. first = tx->skb;
  415. hdrlen = ieee80211_get_hdrlen(tx->fc);
  416. payload_len = first->len - hdrlen;
  417. per_fragm = frag_threshold - hdrlen - FCS_LEN;
  418. num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
  419. frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
  420. if (!frags)
  421. goto fail;
  422. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  423. seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
  424. pos = first->data + hdrlen + per_fragm;
  425. left = payload_len - per_fragm;
  426. for (i = 0; i < num_fragm - 1; i++) {
  427. struct ieee80211_hdr *fhdr;
  428. size_t copylen;
  429. if (left <= 0)
  430. goto fail;
  431. /* reserve enough extra head and tail room for possible
  432. * encryption */
  433. frag = frags[i] =
  434. dev_alloc_skb(tx->local->tx_headroom +
  435. frag_threshold +
  436. IEEE80211_ENCRYPT_HEADROOM +
  437. IEEE80211_ENCRYPT_TAILROOM);
  438. if (!frag)
  439. goto fail;
  440. /* Make sure that all fragments use the same priority so
  441. * that they end up using the same TX queue */
  442. frag->priority = first->priority;
  443. skb_reserve(frag, tx->local->tx_headroom +
  444. IEEE80211_ENCRYPT_HEADROOM);
  445. fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
  446. memcpy(fhdr, first->data, hdrlen);
  447. if (i == num_fragm - 2)
  448. fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
  449. fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
  450. copylen = left > per_fragm ? per_fragm : left;
  451. memcpy(skb_put(frag, copylen), pos, copylen);
  452. pos += copylen;
  453. left -= copylen;
  454. }
  455. skb_trim(first, hdrlen + per_fragm);
  456. tx->u.tx.num_extra_frag = num_fragm - 1;
  457. tx->u.tx.extra_frag = frags;
  458. return TXRX_CONTINUE;
  459. fail:
  460. printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
  461. if (frags) {
  462. for (i = 0; i < num_fragm - 1; i++)
  463. if (frags[i])
  464. dev_kfree_skb(frags[i]);
  465. kfree(frags);
  466. }
  467. I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
  468. return TXRX_DROP;
  469. }
  470. static int wep_encrypt_skb(struct ieee80211_txrx_data *tx, struct sk_buff *skb)
  471. {
  472. if (tx->key->force_sw_encrypt) {
  473. if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
  474. return -1;
  475. } else {
  476. tx->u.tx.control->key_idx = tx->key->hw_key_idx;
  477. if (tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
  478. if (ieee80211_wep_add_iv(tx->local, skb, tx->key) ==
  479. NULL)
  480. return -1;
  481. }
  482. }
  483. return 0;
  484. }
  485. static ieee80211_txrx_result
  486. ieee80211_tx_h_wep_encrypt(struct ieee80211_txrx_data *tx)
  487. {
  488. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  489. u16 fc;
  490. fc = le16_to_cpu(hdr->frame_control);
  491. if (!tx->key || tx->key->alg != ALG_WEP ||
  492. ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  493. ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  494. (fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
  495. return TXRX_CONTINUE;
  496. tx->u.tx.control->iv_len = WEP_IV_LEN;
  497. tx->u.tx.control->icv_len = WEP_ICV_LEN;
  498. ieee80211_tx_set_iswep(tx);
  499. if (wep_encrypt_skb(tx, tx->skb) < 0) {
  500. I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
  501. return TXRX_DROP;
  502. }
  503. if (tx->u.tx.extra_frag) {
  504. int i;
  505. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  506. if (wep_encrypt_skb(tx, tx->u.tx.extra_frag[i]) < 0) {
  507. I802_DEBUG_INC(tx->local->
  508. tx_handlers_drop_wep);
  509. return TXRX_DROP;
  510. }
  511. }
  512. }
  513. return TXRX_CONTINUE;
  514. }
  515. static ieee80211_txrx_result
  516. ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
  517. {
  518. struct rate_control_extra extra;
  519. memset(&extra, 0, sizeof(extra));
  520. extra.mode = tx->u.tx.mode;
  521. extra.mgmt_data = tx->sdata &&
  522. tx->sdata->type == IEEE80211_IF_TYPE_MGMT;
  523. extra.ethertype = tx->ethertype;
  524. tx->u.tx.rate = rate_control_get_rate(tx->local, tx->dev, tx->skb,
  525. &extra);
  526. if (unlikely(extra.probe != NULL)) {
  527. tx->u.tx.control->flags |= IEEE80211_TXCTL_RATE_CTRL_PROBE;
  528. tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  529. tx->u.tx.control->alt_retry_rate = tx->u.tx.rate->val;
  530. tx->u.tx.rate = extra.probe;
  531. } else {
  532. tx->u.tx.control->alt_retry_rate = -1;
  533. }
  534. if (!tx->u.tx.rate)
  535. return TXRX_DROP;
  536. if (tx->u.tx.mode->mode == MODE_IEEE80211G &&
  537. tx->sdata->use_protection &&
  538. (tx->flags & IEEE80211_TXRXD_FRAGMENTED) && extra.nonerp) {
  539. tx->u.tx.last_frag_rate = tx->u.tx.rate;
  540. if (extra.probe)
  541. tx->flags &= ~IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  542. else
  543. tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  544. tx->u.tx.rate = extra.nonerp;
  545. tx->u.tx.control->rate = extra.nonerp;
  546. tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  547. } else {
  548. tx->u.tx.last_frag_rate = tx->u.tx.rate;
  549. tx->u.tx.control->rate = tx->u.tx.rate;
  550. }
  551. tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
  552. return TXRX_CONTINUE;
  553. }
  554. static ieee80211_txrx_result
  555. ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
  556. {
  557. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  558. u16 fc = le16_to_cpu(hdr->frame_control);
  559. u16 dur;
  560. struct ieee80211_tx_control *control = tx->u.tx.control;
  561. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  562. if (!is_multicast_ether_addr(hdr->addr1)) {
  563. if (tx->skb->len + FCS_LEN > tx->local->rts_threshold &&
  564. tx->local->rts_threshold < IEEE80211_MAX_RTS_THRESHOLD) {
  565. control->flags |= IEEE80211_TXCTL_USE_RTS_CTS;
  566. control->flags |= IEEE80211_TXCTL_LONG_RETRY_LIMIT;
  567. control->retry_limit =
  568. tx->local->long_retry_limit;
  569. } else {
  570. control->retry_limit =
  571. tx->local->short_retry_limit;
  572. }
  573. } else {
  574. control->retry_limit = 1;
  575. }
  576. if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
  577. /* Do not use multiple retry rates when sending fragmented
  578. * frames.
  579. * TODO: The last fragment could still use multiple retry
  580. * rates. */
  581. control->alt_retry_rate = -1;
  582. }
  583. /* Use CTS protection for unicast frames sent using extended rates if
  584. * there are associated non-ERP stations and RTS/CTS is not configured
  585. * for the frame. */
  586. if (mode->mode == MODE_IEEE80211G &&
  587. (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
  588. (tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
  589. tx->sdata->use_protection &&
  590. !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
  591. control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
  592. /* Transmit data frames using short preambles if the driver supports
  593. * short preambles at the selected rate and short preambles are
  594. * available on the network at the current point in time. */
  595. if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  596. (tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
  597. tx->sdata->short_preamble &&
  598. (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
  599. tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
  600. }
  601. /* Setup duration field for the first fragment of the frame. Duration
  602. * for remaining fragments will be updated when they are being sent
  603. * to low-level driver in ieee80211_tx(). */
  604. dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
  605. (tx->flags & IEEE80211_TXRXD_FRAGMENTED) ?
  606. tx->u.tx.extra_frag[0]->len : 0);
  607. hdr->duration_id = cpu_to_le16(dur);
  608. if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
  609. (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
  610. struct ieee80211_rate *rate;
  611. /* Do not use multiple retry rates when using RTS/CTS */
  612. control->alt_retry_rate = -1;
  613. /* Use min(data rate, max base rate) as CTS/RTS rate */
  614. rate = tx->u.tx.rate;
  615. while (rate > mode->rates &&
  616. !(rate->flags & IEEE80211_RATE_BASIC))
  617. rate--;
  618. control->rts_cts_rate = rate->val;
  619. control->rts_rate = rate;
  620. }
  621. if (tx->sta) {
  622. tx->sta->tx_packets++;
  623. tx->sta->tx_fragments++;
  624. tx->sta->tx_bytes += tx->skb->len;
  625. if (tx->u.tx.extra_frag) {
  626. int i;
  627. tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
  628. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  629. tx->sta->tx_bytes +=
  630. tx->u.tx.extra_frag[i]->len;
  631. }
  632. }
  633. }
  634. return TXRX_CONTINUE;
  635. }
  636. static ieee80211_txrx_result
  637. ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
  638. {
  639. struct ieee80211_local *local = tx->local;
  640. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  641. struct sk_buff *skb = tx->skb;
  642. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  643. u32 load = 0, hdrtime;
  644. /* TODO: this could be part of tx_status handling, so that the number
  645. * of retries would be known; TX rate should in that case be stored
  646. * somewhere with the packet */
  647. /* Estimate total channel use caused by this frame */
  648. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  649. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  650. if (mode->mode == MODE_IEEE80211A ||
  651. mode->mode == MODE_ATHEROS_TURBO ||
  652. mode->mode == MODE_ATHEROS_TURBOG ||
  653. (mode->mode == MODE_IEEE80211G &&
  654. tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
  655. hdrtime = CHAN_UTIL_HDR_SHORT;
  656. else
  657. hdrtime = CHAN_UTIL_HDR_LONG;
  658. load = hdrtime;
  659. if (!is_multicast_ether_addr(hdr->addr1))
  660. load += hdrtime;
  661. if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  662. load += 2 * hdrtime;
  663. else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  664. load += hdrtime;
  665. load += skb->len * tx->u.tx.rate->rate_inv;
  666. if (tx->u.tx.extra_frag) {
  667. int i;
  668. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  669. load += 2 * hdrtime;
  670. load += tx->u.tx.extra_frag[i]->len *
  671. tx->u.tx.rate->rate;
  672. }
  673. }
  674. /* Divide channel_use by 8 to avoid wrapping around the counter */
  675. load >>= CHAN_UTIL_SHIFT;
  676. local->channel_use_raw += load;
  677. if (tx->sta)
  678. tx->sta->channel_use_raw += load;
  679. tx->sdata->channel_use_raw += load;
  680. return TXRX_CONTINUE;
  681. }
  682. /* TODO: implement register/unregister functions for adding TX/RX handlers
  683. * into ordered list */
  684. ieee80211_tx_handler ieee80211_tx_handlers[] =
  685. {
  686. ieee80211_tx_h_check_assoc,
  687. ieee80211_tx_h_sequence,
  688. ieee80211_tx_h_ps_buf,
  689. ieee80211_tx_h_select_key,
  690. ieee80211_tx_h_michael_mic_add,
  691. ieee80211_tx_h_fragment,
  692. ieee80211_tx_h_tkip_encrypt,
  693. ieee80211_tx_h_ccmp_encrypt,
  694. ieee80211_tx_h_wep_encrypt,
  695. ieee80211_tx_h_rate_ctrl,
  696. ieee80211_tx_h_misc,
  697. ieee80211_tx_h_load_stats,
  698. NULL
  699. };
  700. /* actual transmit path */
  701. /*
  702. * deal with packet injection down monitor interface
  703. * with Radiotap Header -- only called for monitor mode interface
  704. */
  705. static ieee80211_txrx_result
  706. __ieee80211_parse_tx_radiotap(
  707. struct ieee80211_txrx_data *tx,
  708. struct sk_buff *skb, struct ieee80211_tx_control *control)
  709. {
  710. /*
  711. * this is the moment to interpret and discard the radiotap header that
  712. * must be at the start of the packet injected in Monitor mode
  713. *
  714. * Need to take some care with endian-ness since radiotap
  715. * args are little-endian
  716. */
  717. struct ieee80211_radiotap_iterator iterator;
  718. struct ieee80211_radiotap_header *rthdr =
  719. (struct ieee80211_radiotap_header *) skb->data;
  720. struct ieee80211_hw_mode *mode = tx->local->hw.conf.mode;
  721. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
  722. /*
  723. * default control situation for all injected packets
  724. * FIXME: this does not suit all usage cases, expand to allow control
  725. */
  726. control->retry_limit = 1; /* no retry */
  727. control->key_idx = -1; /* no encryption key */
  728. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  729. IEEE80211_TXCTL_USE_CTS_PROTECT);
  730. control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT |
  731. IEEE80211_TXCTL_NO_ACK;
  732. control->antenna_sel_tx = 0; /* default to default antenna */
  733. /*
  734. * for every radiotap entry that is present
  735. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  736. * entries present, or -EINVAL on error)
  737. */
  738. while (!ret) {
  739. int i, target_rate;
  740. ret = ieee80211_radiotap_iterator_next(&iterator);
  741. if (ret)
  742. continue;
  743. /* see if this argument is something we can use */
  744. switch (iterator.this_arg_index) {
  745. /*
  746. * You must take care when dereferencing iterator.this_arg
  747. * for multibyte types... the pointer is not aligned. Use
  748. * get_unaligned((type *)iterator.this_arg) to dereference
  749. * iterator.this_arg for type "type" safely on all arches.
  750. */
  751. case IEEE80211_RADIOTAP_RATE:
  752. /*
  753. * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
  754. * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
  755. */
  756. target_rate = (*iterator.this_arg) * 5;
  757. for (i = 0; i < mode->num_rates; i++) {
  758. struct ieee80211_rate *r = &mode->rates[i];
  759. if (r->rate > target_rate)
  760. continue;
  761. control->rate = r;
  762. if (r->flags & IEEE80211_RATE_PREAMBLE2)
  763. control->tx_rate = r->val2;
  764. else
  765. control->tx_rate = r->val;
  766. /* end on exact match */
  767. if (r->rate == target_rate)
  768. i = mode->num_rates;
  769. }
  770. break;
  771. case IEEE80211_RADIOTAP_ANTENNA:
  772. /*
  773. * radiotap uses 0 for 1st ant, mac80211 is 1 for
  774. * 1st ant
  775. */
  776. control->antenna_sel_tx = (*iterator.this_arg) + 1;
  777. break;
  778. case IEEE80211_RADIOTAP_DBM_TX_POWER:
  779. control->power_level = *iterator.this_arg;
  780. break;
  781. case IEEE80211_RADIOTAP_FLAGS:
  782. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  783. /*
  784. * this indicates that the skb we have been
  785. * handed has the 32-bit FCS CRC at the end...
  786. * we should react to that by snipping it off
  787. * because it will be recomputed and added
  788. * on transmission
  789. */
  790. if (skb->len < (iterator.max_length + FCS_LEN))
  791. return TXRX_DROP;
  792. skb_trim(skb, skb->len - FCS_LEN);
  793. }
  794. break;
  795. default:
  796. break;
  797. }
  798. }
  799. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  800. return TXRX_DROP;
  801. /*
  802. * remove the radiotap header
  803. * iterator->max_length was sanity-checked against
  804. * skb->len by iterator init
  805. */
  806. skb_pull(skb, iterator.max_length);
  807. return TXRX_CONTINUE;
  808. }
  809. static ieee80211_txrx_result inline
  810. __ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  811. struct sk_buff *skb,
  812. struct net_device *dev,
  813. struct ieee80211_tx_control *control)
  814. {
  815. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  816. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  817. struct ieee80211_sub_if_data *sdata;
  818. ieee80211_txrx_result res = TXRX_CONTINUE;
  819. int hdrlen;
  820. memset(tx, 0, sizeof(*tx));
  821. tx->skb = skb;
  822. tx->dev = dev; /* use original interface */
  823. tx->local = local;
  824. tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  825. tx->sta = sta_info_get(local, hdr->addr1);
  826. tx->fc = le16_to_cpu(hdr->frame_control);
  827. /*
  828. * set defaults for things that can be set by
  829. * injected radiotap headers
  830. */
  831. control->power_level = local->hw.conf.power_level;
  832. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  833. /* process and remove the injection radiotap header */
  834. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  835. if (unlikely(sdata->type == IEEE80211_IF_TYPE_MNTR)) {
  836. if (__ieee80211_parse_tx_radiotap(tx, skb, control) ==
  837. TXRX_DROP) {
  838. return TXRX_DROP;
  839. }
  840. /*
  841. * we removed the radiotap header after this point,
  842. * we filled control with what we could use
  843. * set to the actual ieee header now
  844. */
  845. hdr = (struct ieee80211_hdr *) skb->data;
  846. res = TXRX_QUEUED; /* indication it was monitor packet */
  847. }
  848. tx->u.tx.control = control;
  849. if (is_multicast_ether_addr(hdr->addr1)) {
  850. tx->flags &= ~IEEE80211_TXRXD_TXUNICAST;
  851. control->flags |= IEEE80211_TXCTL_NO_ACK;
  852. } else {
  853. tx->flags |= IEEE80211_TXRXD_TXUNICAST;
  854. control->flags &= ~IEEE80211_TXCTL_NO_ACK;
  855. }
  856. if (local->fragmentation_threshold < IEEE80211_MAX_FRAG_THRESHOLD &&
  857. (tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
  858. skb->len + FCS_LEN > local->fragmentation_threshold &&
  859. !local->ops->set_frag_threshold)
  860. tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
  861. else
  862. tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
  863. if (!tx->sta)
  864. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  865. else if (tx->sta->clear_dst_mask) {
  866. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  867. tx->sta->clear_dst_mask = 0;
  868. }
  869. hdrlen = ieee80211_get_hdrlen(tx->fc);
  870. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  871. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  872. tx->ethertype = (pos[0] << 8) | pos[1];
  873. }
  874. control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
  875. return res;
  876. }
  877. /* Device in tx->dev has a reference added; use dev_put(tx->dev) when
  878. * finished with it. */
  879. static int inline ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  880. struct sk_buff *skb,
  881. struct net_device *mdev,
  882. struct ieee80211_tx_control *control)
  883. {
  884. struct ieee80211_tx_packet_data *pkt_data;
  885. struct net_device *dev;
  886. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  887. dev = dev_get_by_index(pkt_data->ifindex);
  888. if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
  889. dev_put(dev);
  890. dev = NULL;
  891. }
  892. if (unlikely(!dev))
  893. return -ENODEV;
  894. __ieee80211_tx_prepare(tx, skb, dev, control);
  895. return 0;
  896. }
  897. static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
  898. struct ieee80211_txrx_data *tx)
  899. {
  900. struct ieee80211_tx_control *control = tx->u.tx.control;
  901. int ret, i;
  902. if (!ieee80211_qdisc_installed(local->mdev) &&
  903. __ieee80211_queue_stopped(local, 0)) {
  904. netif_stop_queue(local->mdev);
  905. return IEEE80211_TX_AGAIN;
  906. }
  907. if (skb) {
  908. ieee80211_dump_frame(local->mdev->name, "TX to low-level driver", skb);
  909. ret = local->ops->tx(local_to_hw(local), skb, control);
  910. if (ret)
  911. return IEEE80211_TX_AGAIN;
  912. local->mdev->trans_start = jiffies;
  913. ieee80211_led_tx(local, 1);
  914. }
  915. if (tx->u.tx.extra_frag) {
  916. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  917. IEEE80211_TXCTL_USE_CTS_PROTECT |
  918. IEEE80211_TXCTL_CLEAR_DST_MASK |
  919. IEEE80211_TXCTL_FIRST_FRAGMENT);
  920. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  921. if (!tx->u.tx.extra_frag[i])
  922. continue;
  923. if (__ieee80211_queue_stopped(local, control->queue))
  924. return IEEE80211_TX_FRAG_AGAIN;
  925. if (i == tx->u.tx.num_extra_frag) {
  926. control->tx_rate = tx->u.tx.last_frag_hwrate;
  927. control->rate = tx->u.tx.last_frag_rate;
  928. if (tx->flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG)
  929. control->flags |=
  930. IEEE80211_TXCTL_RATE_CTRL_PROBE;
  931. else
  932. control->flags &=
  933. ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  934. }
  935. ieee80211_dump_frame(local->mdev->name,
  936. "TX to low-level driver",
  937. tx->u.tx.extra_frag[i]);
  938. ret = local->ops->tx(local_to_hw(local),
  939. tx->u.tx.extra_frag[i],
  940. control);
  941. if (ret)
  942. return IEEE80211_TX_FRAG_AGAIN;
  943. local->mdev->trans_start = jiffies;
  944. ieee80211_led_tx(local, 1);
  945. tx->u.tx.extra_frag[i] = NULL;
  946. }
  947. kfree(tx->u.tx.extra_frag);
  948. tx->u.tx.extra_frag = NULL;
  949. }
  950. return IEEE80211_TX_OK;
  951. }
  952. static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
  953. struct ieee80211_tx_control *control, int mgmt)
  954. {
  955. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  956. struct sta_info *sta;
  957. ieee80211_tx_handler *handler;
  958. struct ieee80211_txrx_data tx;
  959. ieee80211_txrx_result res = TXRX_DROP, res_prepare;
  960. int ret, i;
  961. WARN_ON(__ieee80211_queue_pending(local, control->queue));
  962. if (unlikely(skb->len < 10)) {
  963. dev_kfree_skb(skb);
  964. return 0;
  965. }
  966. res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
  967. if (res_prepare == TXRX_DROP) {
  968. dev_kfree_skb(skb);
  969. return 0;
  970. }
  971. sta = tx.sta;
  972. tx.u.tx.mgmt_interface = mgmt;
  973. tx.u.tx.mode = local->hw.conf.mode;
  974. if (res_prepare == TXRX_QUEUED) { /* if it was an injected packet */
  975. res = TXRX_CONTINUE;
  976. } else {
  977. for (handler = local->tx_handlers; *handler != NULL;
  978. handler++) {
  979. res = (*handler)(&tx);
  980. if (res != TXRX_CONTINUE)
  981. break;
  982. }
  983. }
  984. skb = tx.skb; /* handlers are allowed to change skb */
  985. if (sta)
  986. sta_info_put(sta);
  987. if (unlikely(res == TXRX_DROP)) {
  988. I802_DEBUG_INC(local->tx_handlers_drop);
  989. goto drop;
  990. }
  991. if (unlikely(res == TXRX_QUEUED)) {
  992. I802_DEBUG_INC(local->tx_handlers_queued);
  993. return 0;
  994. }
  995. if (tx.u.tx.extra_frag) {
  996. for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
  997. int next_len, dur;
  998. struct ieee80211_hdr *hdr =
  999. (struct ieee80211_hdr *)
  1000. tx.u.tx.extra_frag[i]->data;
  1001. if (i + 1 < tx.u.tx.num_extra_frag) {
  1002. next_len = tx.u.tx.extra_frag[i + 1]->len;
  1003. } else {
  1004. next_len = 0;
  1005. tx.u.tx.rate = tx.u.tx.last_frag_rate;
  1006. tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
  1007. }
  1008. dur = ieee80211_duration(&tx, 0, next_len);
  1009. hdr->duration_id = cpu_to_le16(dur);
  1010. }
  1011. }
  1012. retry:
  1013. ret = __ieee80211_tx(local, skb, &tx);
  1014. if (ret) {
  1015. struct ieee80211_tx_stored_packet *store =
  1016. &local->pending_packet[control->queue];
  1017. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1018. skb = NULL;
  1019. set_bit(IEEE80211_LINK_STATE_PENDING,
  1020. &local->state[control->queue]);
  1021. smp_mb();
  1022. /* When the driver gets out of buffers during sending of
  1023. * fragments and calls ieee80211_stop_queue, there is
  1024. * a small window between IEEE80211_LINK_STATE_XOFF and
  1025. * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
  1026. * gets available in that window (i.e. driver calls
  1027. * ieee80211_wake_queue), we would end up with ieee80211_tx
  1028. * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
  1029. * continuing transmitting here when that situation is
  1030. * possible to have happened. */
  1031. if (!__ieee80211_queue_stopped(local, control->queue)) {
  1032. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1033. &local->state[control->queue]);
  1034. goto retry;
  1035. }
  1036. memcpy(&store->control, control,
  1037. sizeof(struct ieee80211_tx_control));
  1038. store->skb = skb;
  1039. store->extra_frag = tx.u.tx.extra_frag;
  1040. store->num_extra_frag = tx.u.tx.num_extra_frag;
  1041. store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
  1042. store->last_frag_rate = tx.u.tx.last_frag_rate;
  1043. store->last_frag_rate_ctrl_probe =
  1044. !!(tx.flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG);
  1045. }
  1046. return 0;
  1047. drop:
  1048. if (skb)
  1049. dev_kfree_skb(skb);
  1050. for (i = 0; i < tx.u.tx.num_extra_frag; i++)
  1051. if (tx.u.tx.extra_frag[i])
  1052. dev_kfree_skb(tx.u.tx.extra_frag[i]);
  1053. kfree(tx.u.tx.extra_frag);
  1054. return 0;
  1055. }
  1056. /* device xmit handlers */
  1057. int ieee80211_master_start_xmit(struct sk_buff *skb,
  1058. struct net_device *dev)
  1059. {
  1060. struct ieee80211_tx_control control;
  1061. struct ieee80211_tx_packet_data *pkt_data;
  1062. struct net_device *odev = NULL;
  1063. struct ieee80211_sub_if_data *osdata;
  1064. int headroom;
  1065. int ret;
  1066. /*
  1067. * copy control out of the skb so other people can use skb->cb
  1068. */
  1069. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1070. memset(&control, 0, sizeof(struct ieee80211_tx_control));
  1071. if (pkt_data->ifindex)
  1072. odev = dev_get_by_index(pkt_data->ifindex);
  1073. if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
  1074. dev_put(odev);
  1075. odev = NULL;
  1076. }
  1077. if (unlikely(!odev)) {
  1078. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1079. printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
  1080. "originating device\n", dev->name);
  1081. #endif
  1082. dev_kfree_skb(skb);
  1083. return 0;
  1084. }
  1085. osdata = IEEE80211_DEV_TO_SUB_IF(odev);
  1086. headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
  1087. if (skb_headroom(skb) < headroom) {
  1088. if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
  1089. dev_kfree_skb(skb);
  1090. dev_put(odev);
  1091. return 0;
  1092. }
  1093. }
  1094. control.ifindex = odev->ifindex;
  1095. control.type = osdata->type;
  1096. if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
  1097. control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
  1098. if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
  1099. control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  1100. if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
  1101. control.flags |= IEEE80211_TXCTL_REQUEUE;
  1102. control.queue = pkt_data->queue;
  1103. ret = ieee80211_tx(odev, skb, &control,
  1104. control.type == IEEE80211_IF_TYPE_MGMT);
  1105. dev_put(odev);
  1106. return ret;
  1107. }
  1108. int ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1109. struct net_device *dev)
  1110. {
  1111. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1112. struct ieee80211_tx_packet_data *pkt_data;
  1113. struct ieee80211_radiotap_header *prthdr =
  1114. (struct ieee80211_radiotap_header *)skb->data;
  1115. u16 len_rthdr;
  1116. /* check for not even having the fixed radiotap header part */
  1117. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1118. goto fail; /* too short to be possibly valid */
  1119. /* is it a header version we can trust to find length from? */
  1120. if (unlikely(prthdr->it_version))
  1121. goto fail; /* only version 0 is supported */
  1122. /* then there must be a radiotap header with a length we can use */
  1123. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1124. /* does the skb contain enough to deliver on the alleged length? */
  1125. if (unlikely(skb->len < len_rthdr))
  1126. goto fail; /* skb too short for claimed rt header extent */
  1127. skb->dev = local->mdev;
  1128. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1129. memset(pkt_data, 0, sizeof(*pkt_data));
  1130. /* needed because we set skb device to master */
  1131. pkt_data->ifindex = dev->ifindex;
  1132. pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
  1133. /*
  1134. * fix up the pointers accounting for the radiotap
  1135. * header still being in there. We are being given
  1136. * a precooked IEEE80211 header so no need for
  1137. * normal processing
  1138. */
  1139. skb_set_mac_header(skb, len_rthdr);
  1140. /*
  1141. * these are just fixed to the end of the rt area since we
  1142. * don't have any better information and at this point, nobody cares
  1143. */
  1144. skb_set_network_header(skb, len_rthdr);
  1145. skb_set_transport_header(skb, len_rthdr);
  1146. /* pass the radiotap header up to the next stage intact */
  1147. dev_queue_xmit(skb);
  1148. return NETDEV_TX_OK;
  1149. fail:
  1150. dev_kfree_skb(skb);
  1151. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  1152. }
  1153. /**
  1154. * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
  1155. * subinterfaces (wlan#, WDS, and VLAN interfaces)
  1156. * @skb: packet to be sent
  1157. * @dev: incoming interface
  1158. *
  1159. * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
  1160. * not be freed, and caller is responsible for either retrying later or freeing
  1161. * skb).
  1162. *
  1163. * This function takes in an Ethernet header and encapsulates it with suitable
  1164. * IEEE 802.11 header based on which interface the packet is coming in. The
  1165. * encapsulated packet will then be passed to master interface, wlan#.11, for
  1166. * transmission (through low-level driver).
  1167. */
  1168. int ieee80211_subif_start_xmit(struct sk_buff *skb,
  1169. struct net_device *dev)
  1170. {
  1171. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1172. struct ieee80211_tx_packet_data *pkt_data;
  1173. struct ieee80211_sub_if_data *sdata;
  1174. int ret = 1, head_need;
  1175. u16 ethertype, hdrlen, fc;
  1176. struct ieee80211_hdr hdr;
  1177. const u8 *encaps_data;
  1178. int encaps_len, skip_header_bytes;
  1179. int nh_pos, h_pos;
  1180. struct sta_info *sta;
  1181. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1182. if (unlikely(skb->len < ETH_HLEN)) {
  1183. printk(KERN_DEBUG "%s: short skb (len=%d)\n",
  1184. dev->name, skb->len);
  1185. ret = 0;
  1186. goto fail;
  1187. }
  1188. nh_pos = skb_network_header(skb) - skb->data;
  1189. h_pos = skb_transport_header(skb) - skb->data;
  1190. /* convert Ethernet header to proper 802.11 header (based on
  1191. * operation mode) */
  1192. ethertype = (skb->data[12] << 8) | skb->data[13];
  1193. /* TODO: handling for 802.1x authorized/unauthorized port */
  1194. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  1195. switch (sdata->type) {
  1196. case IEEE80211_IF_TYPE_AP:
  1197. case IEEE80211_IF_TYPE_VLAN:
  1198. fc |= IEEE80211_FCTL_FROMDS;
  1199. /* DA BSSID SA */
  1200. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1201. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1202. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1203. hdrlen = 24;
  1204. break;
  1205. case IEEE80211_IF_TYPE_WDS:
  1206. fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
  1207. /* RA TA DA SA */
  1208. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  1209. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1210. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1211. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1212. hdrlen = 30;
  1213. break;
  1214. case IEEE80211_IF_TYPE_STA:
  1215. fc |= IEEE80211_FCTL_TODS;
  1216. /* BSSID SA DA */
  1217. memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
  1218. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1219. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1220. hdrlen = 24;
  1221. break;
  1222. case IEEE80211_IF_TYPE_IBSS:
  1223. /* DA SA BSSID */
  1224. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1225. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1226. memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
  1227. hdrlen = 24;
  1228. break;
  1229. default:
  1230. ret = 0;
  1231. goto fail;
  1232. }
  1233. /* receiver is QoS enabled, use a QoS type frame */
  1234. sta = sta_info_get(local, hdr.addr1);
  1235. if (sta) {
  1236. if (sta->flags & WLAN_STA_WME) {
  1237. fc |= IEEE80211_STYPE_QOS_DATA;
  1238. hdrlen += 2;
  1239. }
  1240. sta_info_put(sta);
  1241. }
  1242. hdr.frame_control = cpu_to_le16(fc);
  1243. hdr.duration_id = 0;
  1244. hdr.seq_ctrl = 0;
  1245. skip_header_bytes = ETH_HLEN;
  1246. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1247. encaps_data = bridge_tunnel_header;
  1248. encaps_len = sizeof(bridge_tunnel_header);
  1249. skip_header_bytes -= 2;
  1250. } else if (ethertype >= 0x600) {
  1251. encaps_data = rfc1042_header;
  1252. encaps_len = sizeof(rfc1042_header);
  1253. skip_header_bytes -= 2;
  1254. } else {
  1255. encaps_data = NULL;
  1256. encaps_len = 0;
  1257. }
  1258. skb_pull(skb, skip_header_bytes);
  1259. nh_pos -= skip_header_bytes;
  1260. h_pos -= skip_header_bytes;
  1261. /* TODO: implement support for fragments so that there is no need to
  1262. * reallocate and copy payload; it might be enough to support one
  1263. * extra fragment that would be copied in the beginning of the frame
  1264. * data.. anyway, it would be nice to include this into skb structure
  1265. * somehow
  1266. *
  1267. * There are few options for this:
  1268. * use skb->cb as an extra space for 802.11 header
  1269. * allocate new buffer if not enough headroom
  1270. * make sure that there is enough headroom in every skb by increasing
  1271. * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
  1272. * alloc_skb() (net/core/skbuff.c)
  1273. */
  1274. head_need = hdrlen + encaps_len + local->tx_headroom;
  1275. head_need -= skb_headroom(skb);
  1276. /* We are going to modify skb data, so make a copy of it if happens to
  1277. * be cloned. This could happen, e.g., with Linux bridge code passing
  1278. * us broadcast frames. */
  1279. if (head_need > 0 || skb_cloned(skb)) {
  1280. #if 0
  1281. printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
  1282. "of headroom\n", dev->name, head_need);
  1283. #endif
  1284. if (skb_cloned(skb))
  1285. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1286. else
  1287. I802_DEBUG_INC(local->tx_expand_skb_head);
  1288. /* Since we have to reallocate the buffer, make sure that there
  1289. * is enough room for possible WEP IV/ICV and TKIP (8 bytes
  1290. * before payload and 12 after). */
  1291. if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
  1292. 12, GFP_ATOMIC)) {
  1293. printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
  1294. "\n", dev->name);
  1295. goto fail;
  1296. }
  1297. }
  1298. if (encaps_data) {
  1299. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1300. nh_pos += encaps_len;
  1301. h_pos += encaps_len;
  1302. }
  1303. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1304. nh_pos += hdrlen;
  1305. h_pos += hdrlen;
  1306. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1307. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1308. pkt_data->ifindex = dev->ifindex;
  1309. if (sdata->type == IEEE80211_IF_TYPE_MGMT)
  1310. pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
  1311. skb->dev = local->mdev;
  1312. sdata->stats.tx_packets++;
  1313. sdata->stats.tx_bytes += skb->len;
  1314. /* Update skb pointers to various headers since this modified frame
  1315. * is going to go through Linux networking code that may potentially
  1316. * need things like pointer to IP header. */
  1317. skb_set_mac_header(skb, 0);
  1318. skb_set_network_header(skb, nh_pos);
  1319. skb_set_transport_header(skb, h_pos);
  1320. dev->trans_start = jiffies;
  1321. dev_queue_xmit(skb);
  1322. return 0;
  1323. fail:
  1324. if (!ret)
  1325. dev_kfree_skb(skb);
  1326. return ret;
  1327. }
  1328. /*
  1329. * This is the transmit routine for the 802.11 type interfaces
  1330. * called by upper layers of the linux networking
  1331. * stack when it has a frame to transmit
  1332. */
  1333. int ieee80211_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1334. {
  1335. struct ieee80211_sub_if_data *sdata;
  1336. struct ieee80211_tx_packet_data *pkt_data;
  1337. struct ieee80211_hdr *hdr;
  1338. u16 fc;
  1339. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1340. if (skb->len < 10) {
  1341. dev_kfree_skb(skb);
  1342. return 0;
  1343. }
  1344. if (skb_headroom(skb) < sdata->local->tx_headroom) {
  1345. if (pskb_expand_head(skb, sdata->local->tx_headroom,
  1346. 0, GFP_ATOMIC)) {
  1347. dev_kfree_skb(skb);
  1348. return 0;
  1349. }
  1350. }
  1351. hdr = (struct ieee80211_hdr *) skb->data;
  1352. fc = le16_to_cpu(hdr->frame_control);
  1353. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  1354. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1355. pkt_data->ifindex = sdata->dev->ifindex;
  1356. if (sdata->type == IEEE80211_IF_TYPE_MGMT)
  1357. pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
  1358. skb->priority = 20; /* use hardcoded priority for mgmt TX queue */
  1359. skb->dev = sdata->local->mdev;
  1360. /*
  1361. * We're using the protocol field of the the frame control header
  1362. * to request TX callback for hostapd. BIT(1) is checked.
  1363. */
  1364. if ((fc & BIT(1)) == BIT(1)) {
  1365. pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
  1366. fc &= ~BIT(1);
  1367. hdr->frame_control = cpu_to_le16(fc);
  1368. }
  1369. if (!(fc & IEEE80211_FCTL_PROTECTED))
  1370. pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
  1371. sdata->stats.tx_packets++;
  1372. sdata->stats.tx_bytes += skb->len;
  1373. dev_queue_xmit(skb);
  1374. return 0;
  1375. }
  1376. /* helper functions for pending packets for when queues are stopped */
  1377. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  1378. {
  1379. int i, j;
  1380. struct ieee80211_tx_stored_packet *store;
  1381. for (i = 0; i < local->hw.queues; i++) {
  1382. if (!__ieee80211_queue_pending(local, i))
  1383. continue;
  1384. store = &local->pending_packet[i];
  1385. kfree_skb(store->skb);
  1386. for (j = 0; j < store->num_extra_frag; j++)
  1387. kfree_skb(store->extra_frag[j]);
  1388. kfree(store->extra_frag);
  1389. clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
  1390. }
  1391. }
  1392. void ieee80211_tx_pending(unsigned long data)
  1393. {
  1394. struct ieee80211_local *local = (struct ieee80211_local *)data;
  1395. struct net_device *dev = local->mdev;
  1396. struct ieee80211_tx_stored_packet *store;
  1397. struct ieee80211_txrx_data tx;
  1398. int i, ret, reschedule = 0;
  1399. netif_tx_lock_bh(dev);
  1400. for (i = 0; i < local->hw.queues; i++) {
  1401. if (__ieee80211_queue_stopped(local, i))
  1402. continue;
  1403. if (!__ieee80211_queue_pending(local, i)) {
  1404. reschedule = 1;
  1405. continue;
  1406. }
  1407. store = &local->pending_packet[i];
  1408. tx.u.tx.control = &store->control;
  1409. tx.u.tx.extra_frag = store->extra_frag;
  1410. tx.u.tx.num_extra_frag = store->num_extra_frag;
  1411. tx.u.tx.last_frag_hwrate = store->last_frag_hwrate;
  1412. tx.u.tx.last_frag_rate = store->last_frag_rate;
  1413. tx.flags = 0;
  1414. if (store->last_frag_rate_ctrl_probe)
  1415. tx.flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  1416. ret = __ieee80211_tx(local, store->skb, &tx);
  1417. if (ret) {
  1418. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1419. store->skb = NULL;
  1420. } else {
  1421. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1422. &local->state[i]);
  1423. reschedule = 1;
  1424. }
  1425. }
  1426. netif_tx_unlock_bh(dev);
  1427. if (reschedule) {
  1428. if (!ieee80211_qdisc_installed(dev)) {
  1429. if (!__ieee80211_queue_stopped(local, 0))
  1430. netif_wake_queue(dev);
  1431. } else
  1432. netif_schedule(dev);
  1433. }
  1434. }
  1435. /* functions for drivers to get certain frames */
  1436. static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
  1437. struct ieee80211_if_ap *bss,
  1438. struct sk_buff *skb)
  1439. {
  1440. u8 *pos, *tim;
  1441. int aid0 = 0;
  1442. int i, have_bits = 0, n1, n2;
  1443. /* Generate bitmap for TIM only if there are any STAs in power save
  1444. * mode. */
  1445. read_lock_bh(&local->sta_lock);
  1446. if (atomic_read(&bss->num_sta_ps) > 0)
  1447. /* in the hope that this is faster than
  1448. * checking byte-for-byte */
  1449. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1450. IEEE80211_MAX_AID+1);
  1451. if (bss->dtim_count == 0)
  1452. bss->dtim_count = bss->dtim_period - 1;
  1453. else
  1454. bss->dtim_count--;
  1455. tim = pos = (u8 *) skb_put(skb, 6);
  1456. *pos++ = WLAN_EID_TIM;
  1457. *pos++ = 4;
  1458. *pos++ = bss->dtim_count;
  1459. *pos++ = bss->dtim_period;
  1460. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1461. aid0 = 1;
  1462. if (have_bits) {
  1463. /* Find largest even number N1 so that bits numbered 1 through
  1464. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1465. * (N2 + 1) x 8 through 2007 are 0. */
  1466. n1 = 0;
  1467. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1468. if (bss->tim[i]) {
  1469. n1 = i & 0xfe;
  1470. break;
  1471. }
  1472. }
  1473. n2 = n1;
  1474. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1475. if (bss->tim[i]) {
  1476. n2 = i;
  1477. break;
  1478. }
  1479. }
  1480. /* Bitmap control */
  1481. *pos++ = n1 | aid0;
  1482. /* Part Virt Bitmap */
  1483. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1484. tim[1] = n2 - n1 + 4;
  1485. skb_put(skb, n2 - n1);
  1486. } else {
  1487. *pos++ = aid0; /* Bitmap control */
  1488. *pos++ = 0; /* Part Virt Bitmap */
  1489. }
  1490. read_unlock_bh(&local->sta_lock);
  1491. }
  1492. struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw, int if_id,
  1493. struct ieee80211_tx_control *control)
  1494. {
  1495. struct ieee80211_local *local = hw_to_local(hw);
  1496. struct sk_buff *skb;
  1497. struct net_device *bdev;
  1498. struct ieee80211_sub_if_data *sdata = NULL;
  1499. struct ieee80211_if_ap *ap = NULL;
  1500. struct ieee80211_rate *rate;
  1501. struct rate_control_extra extra;
  1502. u8 *b_head, *b_tail;
  1503. int bh_len, bt_len;
  1504. bdev = dev_get_by_index(if_id);
  1505. if (bdev) {
  1506. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  1507. ap = &sdata->u.ap;
  1508. dev_put(bdev);
  1509. }
  1510. if (!ap || sdata->type != IEEE80211_IF_TYPE_AP ||
  1511. !ap->beacon_head) {
  1512. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1513. if (net_ratelimit())
  1514. printk(KERN_DEBUG "no beacon data avail for idx=%d "
  1515. "(%s)\n", if_id, bdev ? bdev->name : "N/A");
  1516. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1517. return NULL;
  1518. }
  1519. /* Assume we are generating the normal beacon locally */
  1520. b_head = ap->beacon_head;
  1521. b_tail = ap->beacon_tail;
  1522. bh_len = ap->beacon_head_len;
  1523. bt_len = ap->beacon_tail_len;
  1524. skb = dev_alloc_skb(local->tx_headroom +
  1525. bh_len + bt_len + 256 /* maximum TIM len */);
  1526. if (!skb)
  1527. return NULL;
  1528. skb_reserve(skb, local->tx_headroom);
  1529. memcpy(skb_put(skb, bh_len), b_head, bh_len);
  1530. ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
  1531. ieee80211_beacon_add_tim(local, ap, skb);
  1532. if (b_tail) {
  1533. memcpy(skb_put(skb, bt_len), b_tail, bt_len);
  1534. }
  1535. if (control) {
  1536. memset(&extra, 0, sizeof(extra));
  1537. extra.mode = local->oper_hw_mode;
  1538. rate = rate_control_get_rate(local, local->mdev, skb, &extra);
  1539. if (!rate) {
  1540. if (net_ratelimit()) {
  1541. printk(KERN_DEBUG "%s: ieee80211_beacon_get: no rate "
  1542. "found\n", local->mdev->name);
  1543. }
  1544. dev_kfree_skb(skb);
  1545. return NULL;
  1546. }
  1547. control->tx_rate = (sdata->short_preamble &&
  1548. (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
  1549. rate->val2 : rate->val;
  1550. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  1551. control->power_level = local->hw.conf.power_level;
  1552. control->flags |= IEEE80211_TXCTL_NO_ACK;
  1553. control->retry_limit = 1;
  1554. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  1555. }
  1556. ap->num_beacons++;
  1557. return skb;
  1558. }
  1559. EXPORT_SYMBOL(ieee80211_beacon_get);
  1560. void ieee80211_rts_get(struct ieee80211_hw *hw, int if_id,
  1561. const void *frame, size_t frame_len,
  1562. const struct ieee80211_tx_control *frame_txctl,
  1563. struct ieee80211_rts *rts)
  1564. {
  1565. const struct ieee80211_hdr *hdr = frame;
  1566. u16 fctl;
  1567. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
  1568. rts->frame_control = cpu_to_le16(fctl);
  1569. rts->duration = ieee80211_rts_duration(hw, if_id, frame_len, frame_txctl);
  1570. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  1571. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  1572. }
  1573. EXPORT_SYMBOL(ieee80211_rts_get);
  1574. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, int if_id,
  1575. const void *frame, size_t frame_len,
  1576. const struct ieee80211_tx_control *frame_txctl,
  1577. struct ieee80211_cts *cts)
  1578. {
  1579. const struct ieee80211_hdr *hdr = frame;
  1580. u16 fctl;
  1581. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
  1582. cts->frame_control = cpu_to_le16(fctl);
  1583. cts->duration = ieee80211_ctstoself_duration(hw, if_id, frame_len, frame_txctl);
  1584. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  1585. }
  1586. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  1587. struct sk_buff *
  1588. ieee80211_get_buffered_bc(struct ieee80211_hw *hw, int if_id,
  1589. struct ieee80211_tx_control *control)
  1590. {
  1591. struct ieee80211_local *local = hw_to_local(hw);
  1592. struct sk_buff *skb;
  1593. struct sta_info *sta;
  1594. ieee80211_tx_handler *handler;
  1595. struct ieee80211_txrx_data tx;
  1596. ieee80211_txrx_result res = TXRX_DROP;
  1597. struct net_device *bdev;
  1598. struct ieee80211_sub_if_data *sdata;
  1599. struct ieee80211_if_ap *bss = NULL;
  1600. bdev = dev_get_by_index(if_id);
  1601. if (bdev) {
  1602. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  1603. bss = &sdata->u.ap;
  1604. dev_put(bdev);
  1605. }
  1606. if (!bss || sdata->type != IEEE80211_IF_TYPE_AP || !bss->beacon_head)
  1607. return NULL;
  1608. if (bss->dtim_count != 0)
  1609. return NULL; /* send buffered bc/mc only after DTIM beacon */
  1610. memset(control, 0, sizeof(*control));
  1611. while (1) {
  1612. skb = skb_dequeue(&bss->ps_bc_buf);
  1613. if (!skb)
  1614. return NULL;
  1615. local->total_ps_buffered--;
  1616. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  1617. struct ieee80211_hdr *hdr =
  1618. (struct ieee80211_hdr *) skb->data;
  1619. /* more buffered multicast/broadcast frames ==> set
  1620. * MoreData flag in IEEE 802.11 header to inform PS
  1621. * STAs */
  1622. hdr->frame_control |=
  1623. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1624. }
  1625. if (ieee80211_tx_prepare(&tx, skb, local->mdev, control) == 0)
  1626. break;
  1627. dev_kfree_skb_any(skb);
  1628. }
  1629. sta = tx.sta;
  1630. tx.flags |= IEEE80211_TXRXD_TXPS_BUFFERED;
  1631. for (handler = local->tx_handlers; *handler != NULL; handler++) {
  1632. res = (*handler)(&tx);
  1633. if (res == TXRX_DROP || res == TXRX_QUEUED)
  1634. break;
  1635. }
  1636. dev_put(tx.dev);
  1637. skb = tx.skb; /* handlers are allowed to change skb */
  1638. if (res == TXRX_DROP) {
  1639. I802_DEBUG_INC(local->tx_handlers_drop);
  1640. dev_kfree_skb(skb);
  1641. skb = NULL;
  1642. } else if (res == TXRX_QUEUED) {
  1643. I802_DEBUG_INC(local->tx_handlers_queued);
  1644. skb = NULL;
  1645. }
  1646. if (sta)
  1647. sta_info_put(sta);
  1648. return skb;
  1649. }
  1650. EXPORT_SYMBOL(ieee80211_get_buffered_bc);