tx.c 54 KB

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