tx.c 55 KB

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