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. const struct ieee80211_hdr *hdr;
  388. u16 fc;
  389. hdr = (const struct ieee80211_hdr *) tx->skb->data;
  390. fc = le16_to_cpu(hdr->frame_control);
  391. if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
  392. tx->key = NULL;
  393. else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
  394. tx->key = key;
  395. else if ((key = rcu_dereference(tx->sdata->default_key)))
  396. tx->key = key;
  397. else if (tx->sdata->drop_unencrypted &&
  398. !(tx->u.tx.control->flags & IEEE80211_TXCTL_EAPOL_FRAME) &&
  399. !(tx->flags & IEEE80211_TXRXD_TX_INJECTED)) {
  400. I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
  401. return TXRX_DROP;
  402. } else {
  403. tx->key = NULL;
  404. tx->u.tx.control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  405. }
  406. if (tx->key) {
  407. tx->key->tx_rx_count++;
  408. /* TODO: add threshold stuff again */
  409. }
  410. return TXRX_CONTINUE;
  411. }
  412. static ieee80211_txrx_result
  413. ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
  414. {
  415. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  416. size_t hdrlen, per_fragm, num_fragm, payload_len, left;
  417. struct sk_buff **frags, *first, *frag;
  418. int i;
  419. u16 seq;
  420. u8 *pos;
  421. int frag_threshold = tx->local->fragmentation_threshold;
  422. if (!(tx->flags & IEEE80211_TXRXD_FRAGMENTED))
  423. return TXRX_CONTINUE;
  424. first = tx->skb;
  425. hdrlen = ieee80211_get_hdrlen(tx->fc);
  426. payload_len = first->len - hdrlen;
  427. per_fragm = frag_threshold - hdrlen - FCS_LEN;
  428. num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
  429. frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
  430. if (!frags)
  431. goto fail;
  432. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  433. seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
  434. pos = first->data + hdrlen + per_fragm;
  435. left = payload_len - per_fragm;
  436. for (i = 0; i < num_fragm - 1; i++) {
  437. struct ieee80211_hdr *fhdr;
  438. size_t copylen;
  439. if (left <= 0)
  440. goto fail;
  441. /* reserve enough extra head and tail room for possible
  442. * encryption */
  443. frag = frags[i] =
  444. dev_alloc_skb(tx->local->tx_headroom +
  445. frag_threshold +
  446. IEEE80211_ENCRYPT_HEADROOM +
  447. IEEE80211_ENCRYPT_TAILROOM);
  448. if (!frag)
  449. goto fail;
  450. /* Make sure that all fragments use the same priority so
  451. * that they end up using the same TX queue */
  452. frag->priority = first->priority;
  453. skb_reserve(frag, tx->local->tx_headroom +
  454. IEEE80211_ENCRYPT_HEADROOM);
  455. fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
  456. memcpy(fhdr, first->data, hdrlen);
  457. if (i == num_fragm - 2)
  458. fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
  459. fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
  460. copylen = left > per_fragm ? per_fragm : left;
  461. memcpy(skb_put(frag, copylen), pos, copylen);
  462. pos += copylen;
  463. left -= copylen;
  464. }
  465. skb_trim(first, hdrlen + per_fragm);
  466. tx->u.tx.num_extra_frag = num_fragm - 1;
  467. tx->u.tx.extra_frag = frags;
  468. return TXRX_CONTINUE;
  469. fail:
  470. printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
  471. if (frags) {
  472. for (i = 0; i < num_fragm - 1; i++)
  473. if (frags[i])
  474. dev_kfree_skb(frags[i]);
  475. kfree(frags);
  476. }
  477. I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
  478. return TXRX_DROP;
  479. }
  480. static ieee80211_txrx_result
  481. ieee80211_tx_h_encrypt(struct ieee80211_txrx_data *tx)
  482. {
  483. if (!tx->key)
  484. return TXRX_CONTINUE;
  485. switch (tx->key->conf.alg) {
  486. case ALG_WEP:
  487. return ieee80211_crypto_wep_encrypt(tx);
  488. case ALG_TKIP:
  489. return ieee80211_crypto_tkip_encrypt(tx);
  490. case ALG_CCMP:
  491. return ieee80211_crypto_ccmp_encrypt(tx);
  492. }
  493. /* not reached */
  494. WARN_ON(1);
  495. return TXRX_DROP;
  496. }
  497. static ieee80211_txrx_result
  498. ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
  499. {
  500. struct rate_selection rsel;
  501. if (likely(!tx->u.tx.rate)) {
  502. rate_control_get_rate(tx->dev, tx->u.tx.mode, tx->skb, &rsel);
  503. tx->u.tx.rate = rsel.rate;
  504. if (unlikely(rsel.probe != NULL)) {
  505. tx->u.tx.control->flags |=
  506. IEEE80211_TXCTL_RATE_CTRL_PROBE;
  507. tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  508. tx->u.tx.control->alt_retry_rate = tx->u.tx.rate->val;
  509. tx->u.tx.rate = rsel.probe;
  510. } else
  511. tx->u.tx.control->alt_retry_rate = -1;
  512. if (!tx->u.tx.rate)
  513. return TXRX_DROP;
  514. } else
  515. tx->u.tx.control->alt_retry_rate = -1;
  516. if (tx->u.tx.mode->mode == MODE_IEEE80211G &&
  517. (tx->sdata->flags & IEEE80211_SDATA_USE_PROTECTION) &&
  518. (tx->flags & IEEE80211_TXRXD_FRAGMENTED) && rsel.nonerp) {
  519. tx->u.tx.last_frag_rate = tx->u.tx.rate;
  520. if (rsel.probe)
  521. tx->flags &= ~IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  522. else
  523. tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  524. tx->u.tx.rate = rsel.nonerp;
  525. tx->u.tx.control->rate = rsel.nonerp;
  526. tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  527. } else {
  528. tx->u.tx.last_frag_rate = tx->u.tx.rate;
  529. tx->u.tx.control->rate = tx->u.tx.rate;
  530. }
  531. tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
  532. return TXRX_CONTINUE;
  533. }
  534. static ieee80211_txrx_result
  535. ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
  536. {
  537. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  538. u16 fc = le16_to_cpu(hdr->frame_control);
  539. u16 dur;
  540. struct ieee80211_tx_control *control = tx->u.tx.control;
  541. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  542. if (!control->retry_limit) {
  543. if (!is_multicast_ether_addr(hdr->addr1)) {
  544. if (tx->skb->len + FCS_LEN > tx->local->rts_threshold
  545. && tx->local->rts_threshold <
  546. IEEE80211_MAX_RTS_THRESHOLD) {
  547. control->flags |=
  548. IEEE80211_TXCTL_USE_RTS_CTS;
  549. control->flags |=
  550. IEEE80211_TXCTL_LONG_RETRY_LIMIT;
  551. control->retry_limit =
  552. tx->local->long_retry_limit;
  553. } else {
  554. control->retry_limit =
  555. tx->local->short_retry_limit;
  556. }
  557. } else {
  558. control->retry_limit = 1;
  559. }
  560. }
  561. if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
  562. /* Do not use multiple retry rates when sending fragmented
  563. * frames.
  564. * TODO: The last fragment could still use multiple retry
  565. * rates. */
  566. control->alt_retry_rate = -1;
  567. }
  568. /* Use CTS protection for unicast frames sent using extended rates if
  569. * there are associated non-ERP stations and RTS/CTS is not configured
  570. * for the frame. */
  571. if (mode->mode == MODE_IEEE80211G &&
  572. (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
  573. (tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
  574. (tx->sdata->flags & IEEE80211_SDATA_USE_PROTECTION) &&
  575. !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
  576. control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
  577. /* Transmit data frames using short preambles if the driver supports
  578. * short preambles at the selected rate and short preambles are
  579. * available on the network at the current point in time. */
  580. if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  581. (tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
  582. (tx->sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
  583. (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
  584. tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
  585. }
  586. /* Setup duration field for the first fragment of the frame. Duration
  587. * for remaining fragments will be updated when they are being sent
  588. * to low-level driver in ieee80211_tx(). */
  589. dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
  590. (tx->flags & IEEE80211_TXRXD_FRAGMENTED) ?
  591. tx->u.tx.extra_frag[0]->len : 0);
  592. hdr->duration_id = cpu_to_le16(dur);
  593. if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
  594. (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
  595. struct ieee80211_rate *rate;
  596. /* Do not use multiple retry rates when using RTS/CTS */
  597. control->alt_retry_rate = -1;
  598. /* Use min(data rate, max base rate) as CTS/RTS rate */
  599. rate = tx->u.tx.rate;
  600. while (rate > mode->rates &&
  601. !(rate->flags & IEEE80211_RATE_BASIC))
  602. rate--;
  603. control->rts_cts_rate = rate->val;
  604. control->rts_rate = rate;
  605. }
  606. if (tx->sta) {
  607. tx->sta->tx_packets++;
  608. tx->sta->tx_fragments++;
  609. tx->sta->tx_bytes += tx->skb->len;
  610. if (tx->u.tx.extra_frag) {
  611. int i;
  612. tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
  613. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  614. tx->sta->tx_bytes +=
  615. tx->u.tx.extra_frag[i]->len;
  616. }
  617. }
  618. }
  619. /*
  620. * Tell hardware to not encrypt when we had sw crypto.
  621. * Because we use the same flag to internally indicate that
  622. * no (software) encryption should be done, we have to set it
  623. * after all crypto handlers.
  624. */
  625. if (tx->key && !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  626. tx->u.tx.control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  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_IEEE80211G &&
  645. tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
  646. hdrtime = CHAN_UTIL_HDR_SHORT;
  647. else
  648. hdrtime = CHAN_UTIL_HDR_LONG;
  649. load = hdrtime;
  650. if (!is_multicast_ether_addr(hdr->addr1))
  651. load += hdrtime;
  652. if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  653. load += 2 * hdrtime;
  654. else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  655. load += hdrtime;
  656. load += skb->len * tx->u.tx.rate->rate_inv;
  657. if (tx->u.tx.extra_frag) {
  658. int i;
  659. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  660. load += 2 * hdrtime;
  661. load += tx->u.tx.extra_frag[i]->len *
  662. tx->u.tx.rate->rate;
  663. }
  664. }
  665. /* Divide channel_use by 8 to avoid wrapping around the counter */
  666. load >>= CHAN_UTIL_SHIFT;
  667. local->channel_use_raw += load;
  668. if (tx->sta)
  669. tx->sta->channel_use_raw += load;
  670. tx->sdata->channel_use_raw += load;
  671. return TXRX_CONTINUE;
  672. }
  673. /* TODO: implement register/unregister functions for adding TX/RX handlers
  674. * into ordered list */
  675. ieee80211_tx_handler ieee80211_tx_handlers[] =
  676. {
  677. ieee80211_tx_h_check_assoc,
  678. ieee80211_tx_h_sequence,
  679. ieee80211_tx_h_ps_buf,
  680. ieee80211_tx_h_select_key,
  681. ieee80211_tx_h_michael_mic_add,
  682. ieee80211_tx_h_fragment,
  683. ieee80211_tx_h_encrypt,
  684. ieee80211_tx_h_rate_ctrl,
  685. ieee80211_tx_h_misc,
  686. ieee80211_tx_h_load_stats,
  687. NULL
  688. };
  689. /* actual transmit path */
  690. /*
  691. * deal with packet injection down monitor interface
  692. * with Radiotap Header -- only called for monitor mode interface
  693. */
  694. static ieee80211_txrx_result
  695. __ieee80211_parse_tx_radiotap(struct ieee80211_txrx_data *tx,
  696. struct sk_buff *skb)
  697. {
  698. /*
  699. * this is the moment to interpret and discard the radiotap header that
  700. * must be at the start of the packet injected in Monitor mode
  701. *
  702. * Need to take some care with endian-ness since radiotap
  703. * args are little-endian
  704. */
  705. struct ieee80211_radiotap_iterator iterator;
  706. struct ieee80211_radiotap_header *rthdr =
  707. (struct ieee80211_radiotap_header *) skb->data;
  708. struct ieee80211_hw_mode *mode = tx->local->hw.conf.mode;
  709. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
  710. struct ieee80211_tx_control *control = tx->u.tx.control;
  711. control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  712. tx->flags |= IEEE80211_TXRXD_TX_INJECTED;
  713. tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
  714. /*
  715. * for every radiotap entry that is present
  716. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  717. * entries present, or -EINVAL on error)
  718. */
  719. while (!ret) {
  720. int i, target_rate;
  721. ret = ieee80211_radiotap_iterator_next(&iterator);
  722. if (ret)
  723. continue;
  724. /* see if this argument is something we can use */
  725. switch (iterator.this_arg_index) {
  726. /*
  727. * You must take care when dereferencing iterator.this_arg
  728. * for multibyte types... the pointer is not aligned. Use
  729. * get_unaligned((type *)iterator.this_arg) to dereference
  730. * iterator.this_arg for type "type" safely on all arches.
  731. */
  732. case IEEE80211_RADIOTAP_RATE:
  733. /*
  734. * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
  735. * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
  736. */
  737. target_rate = (*iterator.this_arg) * 5;
  738. for (i = 0; i < mode->num_rates; i++) {
  739. struct ieee80211_rate *r = &mode->rates[i];
  740. if (r->rate == target_rate) {
  741. tx->u.tx.rate = r;
  742. break;
  743. }
  744. }
  745. break;
  746. case IEEE80211_RADIOTAP_ANTENNA:
  747. /*
  748. * radiotap uses 0 for 1st ant, mac80211 is 1 for
  749. * 1st ant
  750. */
  751. control->antenna_sel_tx = (*iterator.this_arg) + 1;
  752. break;
  753. case IEEE80211_RADIOTAP_DBM_TX_POWER:
  754. control->power_level = *iterator.this_arg;
  755. break;
  756. case IEEE80211_RADIOTAP_FLAGS:
  757. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  758. /*
  759. * this indicates that the skb we have been
  760. * handed has the 32-bit FCS CRC at the end...
  761. * we should react to that by snipping it off
  762. * because it will be recomputed and added
  763. * on transmission
  764. */
  765. if (skb->len < (iterator.max_length + FCS_LEN))
  766. return TXRX_DROP;
  767. skb_trim(skb, skb->len - FCS_LEN);
  768. }
  769. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
  770. control->flags &=
  771. ~IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  772. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
  773. tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
  774. break;
  775. /*
  776. * Please update the file
  777. * Documentation/networking/mac80211-injection.txt
  778. * when parsing new fields here.
  779. */
  780. default:
  781. break;
  782. }
  783. }
  784. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  785. return TXRX_DROP;
  786. /*
  787. * remove the radiotap header
  788. * iterator->max_length was sanity-checked against
  789. * skb->len by iterator init
  790. */
  791. skb_pull(skb, iterator.max_length);
  792. return TXRX_CONTINUE;
  793. }
  794. /*
  795. * initialises @tx
  796. */
  797. static ieee80211_txrx_result
  798. __ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  799. struct sk_buff *skb,
  800. struct net_device *dev,
  801. struct ieee80211_tx_control *control)
  802. {
  803. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  804. struct ieee80211_hdr *hdr;
  805. struct ieee80211_sub_if_data *sdata;
  806. ieee80211_txrx_result res = TXRX_CONTINUE;
  807. int hdrlen;
  808. memset(tx, 0, sizeof(*tx));
  809. tx->skb = skb;
  810. tx->dev = dev; /* use original interface */
  811. tx->local = local;
  812. tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  813. tx->u.tx.control = control;
  814. /*
  815. * Set this flag (used below to indicate "automatic fragmentation"),
  816. * it will be cleared/left by radiotap as desired.
  817. */
  818. tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
  819. /* process and remove the injection radiotap header */
  820. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  821. if (unlikely(sdata->type == IEEE80211_IF_TYPE_MNTR)) {
  822. if (__ieee80211_parse_tx_radiotap(tx, skb) == TXRX_DROP)
  823. return TXRX_DROP;
  824. /*
  825. * __ieee80211_parse_tx_radiotap has now removed
  826. * the radiotap header that was present and pre-filled
  827. * 'tx' with tx control information.
  828. */
  829. }
  830. hdr = (struct ieee80211_hdr *) skb->data;
  831. tx->sta = sta_info_get(local, hdr->addr1);
  832. tx->fc = le16_to_cpu(hdr->frame_control);
  833. if (is_multicast_ether_addr(hdr->addr1)) {
  834. tx->flags &= ~IEEE80211_TXRXD_TXUNICAST;
  835. control->flags |= IEEE80211_TXCTL_NO_ACK;
  836. } else {
  837. tx->flags |= IEEE80211_TXRXD_TXUNICAST;
  838. control->flags &= ~IEEE80211_TXCTL_NO_ACK;
  839. }
  840. if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
  841. if ((tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
  842. skb->len + FCS_LEN > local->fragmentation_threshold &&
  843. !local->ops->set_frag_threshold)
  844. tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
  845. else
  846. tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
  847. }
  848. if (!tx->sta)
  849. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  850. else if (tx->sta->clear_dst_mask) {
  851. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  852. tx->sta->clear_dst_mask = 0;
  853. }
  854. hdrlen = ieee80211_get_hdrlen(tx->fc);
  855. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  856. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  857. tx->ethertype = (pos[0] << 8) | pos[1];
  858. }
  859. control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
  860. return res;
  861. }
  862. /* Device in tx->dev has a reference added; use dev_put(tx->dev) when
  863. * finished with it.
  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. return 0;
  885. }
  886. static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
  887. struct ieee80211_txrx_data *tx)
  888. {
  889. struct ieee80211_tx_control *control = tx->u.tx.control;
  890. int ret, i;
  891. if (!ieee80211_qdisc_installed(local->mdev) &&
  892. __ieee80211_queue_stopped(local, 0)) {
  893. netif_stop_queue(local->mdev);
  894. return IEEE80211_TX_AGAIN;
  895. }
  896. if (skb) {
  897. ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
  898. "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->flags & IEEE80211_TXRXD_TXPROBE_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(wiphy_name(local->hw.wiphy),
  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)
  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. /* initialises tx */
  957. res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
  958. if (res_prepare == TXRX_DROP) {
  959. dev_kfree_skb(skb);
  960. return 0;
  961. }
  962. /*
  963. * key references are protected using RCU and this requires that
  964. * we are in a read-site RCU section during receive processing
  965. */
  966. rcu_read_lock();
  967. sta = tx.sta;
  968. tx.u.tx.mode = local->hw.conf.mode;
  969. for (handler = local->tx_handlers; *handler != NULL;
  970. handler++) {
  971. res = (*handler)(&tx);
  972. if (res != TXRX_CONTINUE)
  973. break;
  974. }
  975. skb = tx.skb; /* handlers are allowed to change skb */
  976. if (sta)
  977. sta_info_put(sta);
  978. if (unlikely(res == TXRX_DROP)) {
  979. I802_DEBUG_INC(local->tx_handlers_drop);
  980. goto drop;
  981. }
  982. if (unlikely(res == TXRX_QUEUED)) {
  983. I802_DEBUG_INC(local->tx_handlers_queued);
  984. rcu_read_unlock();
  985. return 0;
  986. }
  987. if (tx.u.tx.extra_frag) {
  988. for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
  989. int next_len, dur;
  990. struct ieee80211_hdr *hdr =
  991. (struct ieee80211_hdr *)
  992. tx.u.tx.extra_frag[i]->data;
  993. if (i + 1 < tx.u.tx.num_extra_frag) {
  994. next_len = tx.u.tx.extra_frag[i + 1]->len;
  995. } else {
  996. next_len = 0;
  997. tx.u.tx.rate = tx.u.tx.last_frag_rate;
  998. tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
  999. }
  1000. dur = ieee80211_duration(&tx, 0, next_len);
  1001. hdr->duration_id = cpu_to_le16(dur);
  1002. }
  1003. }
  1004. retry:
  1005. ret = __ieee80211_tx(local, skb, &tx);
  1006. if (ret) {
  1007. struct ieee80211_tx_stored_packet *store =
  1008. &local->pending_packet[control->queue];
  1009. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1010. skb = NULL;
  1011. set_bit(IEEE80211_LINK_STATE_PENDING,
  1012. &local->state[control->queue]);
  1013. smp_mb();
  1014. /* When the driver gets out of buffers during sending of
  1015. * fragments and calls ieee80211_stop_queue, there is
  1016. * a small window between IEEE80211_LINK_STATE_XOFF and
  1017. * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
  1018. * gets available in that window (i.e. driver calls
  1019. * ieee80211_wake_queue), we would end up with ieee80211_tx
  1020. * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
  1021. * continuing transmitting here when that situation is
  1022. * possible to have happened. */
  1023. if (!__ieee80211_queue_stopped(local, control->queue)) {
  1024. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1025. &local->state[control->queue]);
  1026. goto retry;
  1027. }
  1028. memcpy(&store->control, control,
  1029. sizeof(struct ieee80211_tx_control));
  1030. store->skb = skb;
  1031. store->extra_frag = tx.u.tx.extra_frag;
  1032. store->num_extra_frag = tx.u.tx.num_extra_frag;
  1033. store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
  1034. store->last_frag_rate = tx.u.tx.last_frag_rate;
  1035. store->last_frag_rate_ctrl_probe =
  1036. !!(tx.flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG);
  1037. }
  1038. rcu_read_unlock();
  1039. return 0;
  1040. drop:
  1041. if (skb)
  1042. dev_kfree_skb(skb);
  1043. for (i = 0; i < tx.u.tx.num_extra_frag; i++)
  1044. if (tx.u.tx.extra_frag[i])
  1045. dev_kfree_skb(tx.u.tx.extra_frag[i]);
  1046. kfree(tx.u.tx.extra_frag);
  1047. rcu_read_unlock();
  1048. return 0;
  1049. }
  1050. /* device xmit handlers */
  1051. int ieee80211_master_start_xmit(struct sk_buff *skb,
  1052. struct net_device *dev)
  1053. {
  1054. struct ieee80211_tx_control control;
  1055. struct ieee80211_tx_packet_data *pkt_data;
  1056. struct net_device *odev = NULL;
  1057. struct ieee80211_sub_if_data *osdata;
  1058. int headroom;
  1059. int ret;
  1060. /*
  1061. * copy control out of the skb so other people can use skb->cb
  1062. */
  1063. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1064. memset(&control, 0, sizeof(struct ieee80211_tx_control));
  1065. if (pkt_data->ifindex)
  1066. odev = dev_get_by_index(&init_net, pkt_data->ifindex);
  1067. if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
  1068. dev_put(odev);
  1069. odev = NULL;
  1070. }
  1071. if (unlikely(!odev)) {
  1072. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1073. printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
  1074. "originating device\n", dev->name);
  1075. #endif
  1076. dev_kfree_skb(skb);
  1077. return 0;
  1078. }
  1079. osdata = IEEE80211_DEV_TO_SUB_IF(odev);
  1080. headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
  1081. if (skb_headroom(skb) < headroom) {
  1082. if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
  1083. dev_kfree_skb(skb);
  1084. dev_put(odev);
  1085. return 0;
  1086. }
  1087. }
  1088. control.ifindex = odev->ifindex;
  1089. control.type = osdata->type;
  1090. if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
  1091. control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
  1092. if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
  1093. control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  1094. if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
  1095. control.flags |= IEEE80211_TXCTL_REQUEUE;
  1096. if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
  1097. control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
  1098. control.queue = pkt_data->queue;
  1099. ret = ieee80211_tx(odev, skb, &control);
  1100. dev_put(odev);
  1101. return ret;
  1102. }
  1103. int ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1104. struct net_device *dev)
  1105. {
  1106. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1107. struct ieee80211_tx_packet_data *pkt_data;
  1108. struct ieee80211_radiotap_header *prthdr =
  1109. (struct ieee80211_radiotap_header *)skb->data;
  1110. u16 len_rthdr;
  1111. /* check for not even having the fixed radiotap header part */
  1112. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1113. goto fail; /* too short to be possibly valid */
  1114. /* is it a header version we can trust to find length from? */
  1115. if (unlikely(prthdr->it_version))
  1116. goto fail; /* only version 0 is supported */
  1117. /* then there must be a radiotap header with a length we can use */
  1118. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1119. /* does the skb contain enough to deliver on the alleged length? */
  1120. if (unlikely(skb->len < len_rthdr))
  1121. goto fail; /* skb too short for claimed rt header extent */
  1122. skb->dev = local->mdev;
  1123. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1124. memset(pkt_data, 0, sizeof(*pkt_data));
  1125. /* needed because we set skb device to master */
  1126. pkt_data->ifindex = dev->ifindex;
  1127. pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
  1128. /*
  1129. * fix up the pointers accounting for the radiotap
  1130. * header still being in there. We are being given
  1131. * a precooked IEEE80211 header so no need for
  1132. * normal processing
  1133. */
  1134. skb_set_mac_header(skb, len_rthdr);
  1135. /*
  1136. * these are just fixed to the end of the rt area since we
  1137. * don't have any better information and at this point, nobody cares
  1138. */
  1139. skb_set_network_header(skb, len_rthdr);
  1140. skb_set_transport_header(skb, len_rthdr);
  1141. /* pass the radiotap header up to the next stage intact */
  1142. dev_queue_xmit(skb);
  1143. return NETDEV_TX_OK;
  1144. fail:
  1145. dev_kfree_skb(skb);
  1146. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  1147. }
  1148. /**
  1149. * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
  1150. * subinterfaces (wlan#, WDS, and VLAN interfaces)
  1151. * @skb: packet to be sent
  1152. * @dev: incoming interface
  1153. *
  1154. * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
  1155. * not be freed, and caller is responsible for either retrying later or freeing
  1156. * skb).
  1157. *
  1158. * This function takes in an Ethernet header and encapsulates it with suitable
  1159. * IEEE 802.11 header based on which interface the packet is coming in. The
  1160. * encapsulated packet will then be passed to master interface, wlan#.11, for
  1161. * transmission (through low-level driver).
  1162. */
  1163. int ieee80211_subif_start_xmit(struct sk_buff *skb,
  1164. struct net_device *dev)
  1165. {
  1166. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1167. struct ieee80211_tx_packet_data *pkt_data;
  1168. struct ieee80211_sub_if_data *sdata;
  1169. int ret = 1, head_need;
  1170. u16 ethertype, hdrlen, fc;
  1171. struct ieee80211_hdr hdr;
  1172. const u8 *encaps_data;
  1173. int encaps_len, skip_header_bytes;
  1174. int nh_pos, h_pos;
  1175. struct sta_info *sta;
  1176. u32 sta_flags = 0;
  1177. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1178. if (unlikely(skb->len < ETH_HLEN)) {
  1179. printk(KERN_DEBUG "%s: short skb (len=%d)\n",
  1180. dev->name, skb->len);
  1181. ret = 0;
  1182. goto fail;
  1183. }
  1184. nh_pos = skb_network_header(skb) - skb->data;
  1185. h_pos = skb_transport_header(skb) - skb->data;
  1186. /* convert Ethernet header to proper 802.11 header (based on
  1187. * operation mode) */
  1188. ethertype = (skb->data[12] << 8) | skb->data[13];
  1189. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  1190. switch (sdata->type) {
  1191. case IEEE80211_IF_TYPE_AP:
  1192. case IEEE80211_IF_TYPE_VLAN:
  1193. fc |= IEEE80211_FCTL_FROMDS;
  1194. /* DA BSSID SA */
  1195. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1196. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1197. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1198. hdrlen = 24;
  1199. break;
  1200. case IEEE80211_IF_TYPE_WDS:
  1201. fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
  1202. /* RA TA DA SA */
  1203. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  1204. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1205. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1206. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1207. hdrlen = 30;
  1208. break;
  1209. case IEEE80211_IF_TYPE_STA:
  1210. fc |= IEEE80211_FCTL_TODS;
  1211. /* BSSID SA DA */
  1212. memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
  1213. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1214. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1215. hdrlen = 24;
  1216. break;
  1217. case IEEE80211_IF_TYPE_IBSS:
  1218. /* DA SA BSSID */
  1219. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1220. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1221. memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
  1222. hdrlen = 24;
  1223. break;
  1224. default:
  1225. ret = 0;
  1226. goto fail;
  1227. }
  1228. sta = sta_info_get(local, hdr.addr1);
  1229. if (sta) {
  1230. sta_flags = sta->flags;
  1231. sta_info_put(sta);
  1232. }
  1233. /* receiver is QoS enabled, use a QoS type frame */
  1234. if (sta_flags & WLAN_STA_WME) {
  1235. fc |= IEEE80211_STYPE_QOS_DATA;
  1236. hdrlen += 2;
  1237. }
  1238. /*
  1239. * If port access control is enabled, drop frames to unauthorised
  1240. * stations unless they are EAPOL frames from the local station.
  1241. */
  1242. if (unlikely(sdata->ieee802_1x_pac &&
  1243. !(sta_flags & WLAN_STA_AUTHORIZED) &&
  1244. !(ethertype == ETH_P_PAE &&
  1245. compare_ether_addr(dev->dev_addr,
  1246. skb->data + ETH_ALEN) == 0))) {
  1247. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1248. DECLARE_MAC_BUF(mac);
  1249. if (net_ratelimit())
  1250. printk(KERN_DEBUG "%s: dropped frame to %s"
  1251. " (unauthorized port)\n", dev->name,
  1252. print_mac(mac, hdr.addr1));
  1253. #endif
  1254. I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
  1255. ret = 0;
  1256. goto fail;
  1257. }
  1258. hdr.frame_control = cpu_to_le16(fc);
  1259. hdr.duration_id = 0;
  1260. hdr.seq_ctrl = 0;
  1261. skip_header_bytes = ETH_HLEN;
  1262. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1263. encaps_data = bridge_tunnel_header;
  1264. encaps_len = sizeof(bridge_tunnel_header);
  1265. skip_header_bytes -= 2;
  1266. } else if (ethertype >= 0x600) {
  1267. encaps_data = rfc1042_header;
  1268. encaps_len = sizeof(rfc1042_header);
  1269. skip_header_bytes -= 2;
  1270. } else {
  1271. encaps_data = NULL;
  1272. encaps_len = 0;
  1273. }
  1274. skb_pull(skb, skip_header_bytes);
  1275. nh_pos -= skip_header_bytes;
  1276. h_pos -= skip_header_bytes;
  1277. /* TODO: implement support for fragments so that there is no need to
  1278. * reallocate and copy payload; it might be enough to support one
  1279. * extra fragment that would be copied in the beginning of the frame
  1280. * data.. anyway, it would be nice to include this into skb structure
  1281. * somehow
  1282. *
  1283. * There are few options for this:
  1284. * use skb->cb as an extra space for 802.11 header
  1285. * allocate new buffer if not enough headroom
  1286. * make sure that there is enough headroom in every skb by increasing
  1287. * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
  1288. * alloc_skb() (net/core/skbuff.c)
  1289. */
  1290. head_need = hdrlen + encaps_len + local->tx_headroom;
  1291. head_need -= skb_headroom(skb);
  1292. /* We are going to modify skb data, so make a copy of it if happens to
  1293. * be cloned. This could happen, e.g., with Linux bridge code passing
  1294. * us broadcast frames. */
  1295. if (head_need > 0 || skb_cloned(skb)) {
  1296. #if 0
  1297. printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
  1298. "of headroom\n", dev->name, head_need);
  1299. #endif
  1300. if (skb_cloned(skb))
  1301. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1302. else
  1303. I802_DEBUG_INC(local->tx_expand_skb_head);
  1304. /* Since we have to reallocate the buffer, make sure that there
  1305. * is enough room for possible WEP IV/ICV and TKIP (8 bytes
  1306. * before payload and 12 after). */
  1307. if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
  1308. 12, GFP_ATOMIC)) {
  1309. printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
  1310. "\n", dev->name);
  1311. goto fail;
  1312. }
  1313. }
  1314. if (encaps_data) {
  1315. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1316. nh_pos += encaps_len;
  1317. h_pos += encaps_len;
  1318. }
  1319. if (fc & IEEE80211_STYPE_QOS_DATA) {
  1320. __le16 *qos_control;
  1321. qos_control = (__le16*) skb_push(skb, 2);
  1322. memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
  1323. /*
  1324. * Maybe we could actually set some fields here, for now just
  1325. * initialise to zero to indicate no special operation.
  1326. */
  1327. *qos_control = 0;
  1328. } else
  1329. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1330. nh_pos += hdrlen;
  1331. h_pos += hdrlen;
  1332. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1333. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1334. pkt_data->ifindex = dev->ifindex;
  1335. if (ethertype == ETH_P_PAE)
  1336. pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
  1337. skb->dev = local->mdev;
  1338. dev->stats.tx_packets++;
  1339. dev->stats.tx_bytes += skb->len;
  1340. /* Update skb pointers to various headers since this modified frame
  1341. * is going to go through Linux networking code that may potentially
  1342. * need things like pointer to IP header. */
  1343. skb_set_mac_header(skb, 0);
  1344. skb_set_network_header(skb, nh_pos);
  1345. skb_set_transport_header(skb, h_pos);
  1346. dev->trans_start = jiffies;
  1347. dev_queue_xmit(skb);
  1348. return 0;
  1349. fail:
  1350. if (!ret)
  1351. dev_kfree_skb(skb);
  1352. return ret;
  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.flags = 0;
  1392. if (store->last_frag_rate_ctrl_probe)
  1393. tx.flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
  1394. ret = __ieee80211_tx(local, store->skb, &tx);
  1395. if (ret) {
  1396. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1397. store->skb = NULL;
  1398. } else {
  1399. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1400. &local->state[i]);
  1401. reschedule = 1;
  1402. }
  1403. }
  1404. netif_tx_unlock_bh(dev);
  1405. if (reschedule) {
  1406. if (!ieee80211_qdisc_installed(dev)) {
  1407. if (!__ieee80211_queue_stopped(local, 0))
  1408. netif_wake_queue(dev);
  1409. } else
  1410. netif_schedule(dev);
  1411. }
  1412. }
  1413. /* functions for drivers to get certain frames */
  1414. static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
  1415. struct ieee80211_if_ap *bss,
  1416. struct sk_buff *skb)
  1417. {
  1418. u8 *pos, *tim;
  1419. int aid0 = 0;
  1420. int i, have_bits = 0, n1, n2;
  1421. /* Generate bitmap for TIM only if there are any STAs in power save
  1422. * mode. */
  1423. read_lock_bh(&local->sta_lock);
  1424. if (atomic_read(&bss->num_sta_ps) > 0)
  1425. /* in the hope that this is faster than
  1426. * checking byte-for-byte */
  1427. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1428. IEEE80211_MAX_AID+1);
  1429. if (bss->dtim_count == 0)
  1430. bss->dtim_count = bss->dtim_period - 1;
  1431. else
  1432. bss->dtim_count--;
  1433. tim = pos = (u8 *) skb_put(skb, 6);
  1434. *pos++ = WLAN_EID_TIM;
  1435. *pos++ = 4;
  1436. *pos++ = bss->dtim_count;
  1437. *pos++ = bss->dtim_period;
  1438. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1439. aid0 = 1;
  1440. if (have_bits) {
  1441. /* Find largest even number N1 so that bits numbered 1 through
  1442. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1443. * (N2 + 1) x 8 through 2007 are 0. */
  1444. n1 = 0;
  1445. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1446. if (bss->tim[i]) {
  1447. n1 = i & 0xfe;
  1448. break;
  1449. }
  1450. }
  1451. n2 = n1;
  1452. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1453. if (bss->tim[i]) {
  1454. n2 = i;
  1455. break;
  1456. }
  1457. }
  1458. /* Bitmap control */
  1459. *pos++ = n1 | aid0;
  1460. /* Part Virt Bitmap */
  1461. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1462. tim[1] = n2 - n1 + 4;
  1463. skb_put(skb, n2 - n1);
  1464. } else {
  1465. *pos++ = aid0; /* Bitmap control */
  1466. *pos++ = 0; /* Part Virt Bitmap */
  1467. }
  1468. read_unlock_bh(&local->sta_lock);
  1469. }
  1470. struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw, int if_id,
  1471. struct ieee80211_tx_control *control)
  1472. {
  1473. struct ieee80211_local *local = hw_to_local(hw);
  1474. struct sk_buff *skb;
  1475. struct net_device *bdev;
  1476. struct ieee80211_sub_if_data *sdata = NULL;
  1477. struct ieee80211_if_ap *ap = NULL;
  1478. struct rate_selection rsel;
  1479. u8 *b_head, *b_tail;
  1480. int bh_len, bt_len;
  1481. bdev = dev_get_by_index(&init_net, if_id);
  1482. if (bdev) {
  1483. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  1484. ap = &sdata->u.ap;
  1485. dev_put(bdev);
  1486. }
  1487. if (!ap || sdata->type != IEEE80211_IF_TYPE_AP ||
  1488. !ap->beacon_head) {
  1489. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1490. if (net_ratelimit())
  1491. printk(KERN_DEBUG "no beacon data avail for idx=%d "
  1492. "(%s)\n", if_id, bdev ? bdev->name : "N/A");
  1493. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1494. return NULL;
  1495. }
  1496. /* Assume we are generating the normal beacon locally */
  1497. b_head = ap->beacon_head;
  1498. b_tail = ap->beacon_tail;
  1499. bh_len = ap->beacon_head_len;
  1500. bt_len = ap->beacon_tail_len;
  1501. skb = dev_alloc_skb(local->tx_headroom +
  1502. bh_len + bt_len + 256 /* maximum TIM len */);
  1503. if (!skb)
  1504. return NULL;
  1505. skb_reserve(skb, local->tx_headroom);
  1506. memcpy(skb_put(skb, bh_len), b_head, bh_len);
  1507. ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
  1508. ieee80211_beacon_add_tim(local, ap, skb);
  1509. if (b_tail) {
  1510. memcpy(skb_put(skb, bt_len), b_tail, bt_len);
  1511. }
  1512. if (control) {
  1513. rate_control_get_rate(local->mdev, local->oper_hw_mode, skb,
  1514. &rsel);
  1515. if (!rsel.rate) {
  1516. if (net_ratelimit()) {
  1517. printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
  1518. "no rate found\n",
  1519. wiphy_name(local->hw.wiphy));
  1520. }
  1521. dev_kfree_skb(skb);
  1522. return NULL;
  1523. }
  1524. control->tx_rate =
  1525. ((sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
  1526. (rsel.rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
  1527. rsel.rate->val2 : rsel.rate->val;
  1528. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  1529. control->power_level = local->hw.conf.power_level;
  1530. control->flags |= IEEE80211_TXCTL_NO_ACK;
  1531. control->retry_limit = 1;
  1532. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  1533. }
  1534. ap->num_beacons++;
  1535. return skb;
  1536. }
  1537. EXPORT_SYMBOL(ieee80211_beacon_get);
  1538. void ieee80211_rts_get(struct ieee80211_hw *hw, int if_id,
  1539. const void *frame, size_t frame_len,
  1540. const struct ieee80211_tx_control *frame_txctl,
  1541. struct ieee80211_rts *rts)
  1542. {
  1543. const struct ieee80211_hdr *hdr = frame;
  1544. u16 fctl;
  1545. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
  1546. rts->frame_control = cpu_to_le16(fctl);
  1547. rts->duration = ieee80211_rts_duration(hw, if_id, frame_len, 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, int if_id,
  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, if_id, frame_len, frame_txctl);
  1562. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  1563. }
  1564. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  1565. struct sk_buff *
  1566. ieee80211_get_buffered_bc(struct ieee80211_hw *hw, int if_id,
  1567. struct ieee80211_tx_control *control)
  1568. {
  1569. struct ieee80211_local *local = hw_to_local(hw);
  1570. struct sk_buff *skb;
  1571. struct sta_info *sta;
  1572. ieee80211_tx_handler *handler;
  1573. struct ieee80211_txrx_data tx;
  1574. ieee80211_txrx_result res = TXRX_DROP;
  1575. struct net_device *bdev;
  1576. struct ieee80211_sub_if_data *sdata;
  1577. struct ieee80211_if_ap *bss = NULL;
  1578. bdev = dev_get_by_index(&init_net, if_id);
  1579. if (bdev) {
  1580. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  1581. bss = &sdata->u.ap;
  1582. dev_put(bdev);
  1583. }
  1584. if (!bss || sdata->type != IEEE80211_IF_TYPE_AP || !bss->beacon_head)
  1585. return NULL;
  1586. if (bss->dtim_count != 0)
  1587. return NULL; /* send buffered bc/mc only after DTIM beacon */
  1588. memset(control, 0, sizeof(*control));
  1589. while (1) {
  1590. skb = skb_dequeue(&bss->ps_bc_buf);
  1591. if (!skb)
  1592. return NULL;
  1593. local->total_ps_buffered--;
  1594. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  1595. struct ieee80211_hdr *hdr =
  1596. (struct ieee80211_hdr *) skb->data;
  1597. /* more buffered multicast/broadcast frames ==> set
  1598. * MoreData flag in IEEE 802.11 header to inform PS
  1599. * STAs */
  1600. hdr->frame_control |=
  1601. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1602. }
  1603. if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
  1604. break;
  1605. dev_kfree_skb_any(skb);
  1606. }
  1607. sta = tx.sta;
  1608. tx.flags |= IEEE80211_TXRXD_TXPS_BUFFERED;
  1609. tx.u.tx.mode = local->hw.conf.mode;
  1610. for (handler = local->tx_handlers; *handler != NULL; handler++) {
  1611. res = (*handler)(&tx);
  1612. if (res == TXRX_DROP || res == TXRX_QUEUED)
  1613. break;
  1614. }
  1615. dev_put(tx.dev);
  1616. skb = tx.skb; /* handlers are allowed to change skb */
  1617. if (res == TXRX_DROP) {
  1618. I802_DEBUG_INC(local->tx_handlers_drop);
  1619. dev_kfree_skb(skb);
  1620. skb = NULL;
  1621. } else if (res == TXRX_QUEUED) {
  1622. I802_DEBUG_INC(local->tx_handlers_queued);
  1623. skb = NULL;
  1624. }
  1625. if (sta)
  1626. sta_info_put(sta);
  1627. return skb;
  1628. }
  1629. EXPORT_SYMBOL(ieee80211_get_buffered_bc);