tx.c 53 KB

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