tx.c 54 KB

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