tx.c 53 KB

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