tx.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  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->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->short_preamble);
  165. }
  166. return dur;
  167. }
  168. static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
  169. int queue)
  170. {
  171. return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  172. }
  173. static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
  174. int queue)
  175. {
  176. return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
  177. }
  178. static int inline is_ieee80211_device(struct net_device *dev,
  179. struct net_device *master)
  180. {
  181. return (wdev_priv(dev->ieee80211_ptr) ==
  182. wdev_priv(master->ieee80211_ptr));
  183. }
  184. /* tx handlers */
  185. static ieee80211_txrx_result
  186. ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
  187. {
  188. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  189. struct sk_buff *skb = tx->skb;
  190. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  191. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  192. u32 sta_flags;
  193. if (unlikely(tx->local->sta_scanning != 0) &&
  194. ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  195. (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
  196. return TXRX_DROP;
  197. if (tx->u.tx.ps_buffered)
  198. return TXRX_CONTINUE;
  199. sta_flags = tx->sta ? tx->sta->flags : 0;
  200. if (likely(tx->u.tx.unicast)) {
  201. if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
  202. tx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
  203. (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
  204. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  205. printk(KERN_DEBUG "%s: dropped data frame to not "
  206. "associated station " MAC_FMT "\n",
  207. tx->dev->name, MAC_ARG(hdr->addr1));
  208. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  209. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  210. return TXRX_DROP;
  211. }
  212. } else {
  213. if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  214. tx->local->num_sta == 0 &&
  215. !tx->local->allow_broadcast_always &&
  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->u.tx.ps_buffered))
  367. return TXRX_CONTINUE;
  368. if (tx->u.tx.unicast)
  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->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 = (payload_len + per_fragm - 1) / 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->u.tx.probe_last_frag = 1;
  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->use_protection && tx->fragmented &&
  539. extra.nonerp) {
  540. tx->u.tx.last_frag_rate = tx->u.tx.rate;
  541. tx->u.tx.probe_last_frag = extra.probe ? 1 : 0;
  542. tx->u.tx.rate = extra.nonerp;
  543. tx->u.tx.control->rate = extra.nonerp;
  544. tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  545. } else {
  546. tx->u.tx.last_frag_rate = tx->u.tx.rate;
  547. tx->u.tx.control->rate = tx->u.tx.rate;
  548. }
  549. tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
  550. return TXRX_CONTINUE;
  551. }
  552. static ieee80211_txrx_result
  553. ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
  554. {
  555. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  556. u16 fc = le16_to_cpu(hdr->frame_control);
  557. u16 dur;
  558. struct ieee80211_tx_control *control = tx->u.tx.control;
  559. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  560. if (!is_multicast_ether_addr(hdr->addr1)) {
  561. if (tx->skb->len + FCS_LEN > tx->local->rts_threshold &&
  562. tx->local->rts_threshold < IEEE80211_MAX_RTS_THRESHOLD) {
  563. control->flags |= IEEE80211_TXCTL_USE_RTS_CTS;
  564. control->flags |= IEEE80211_TXCTL_LONG_RETRY_LIMIT;
  565. control->retry_limit =
  566. tx->local->long_retry_limit;
  567. } else {
  568. control->retry_limit =
  569. tx->local->short_retry_limit;
  570. }
  571. } else {
  572. control->retry_limit = 1;
  573. }
  574. if (tx->fragmented) {
  575. /* Do not use multiple retry rates when sending fragmented
  576. * frames.
  577. * TODO: The last fragment could still use multiple retry
  578. * rates. */
  579. control->alt_retry_rate = -1;
  580. }
  581. /* Use CTS protection for unicast frames sent using extended rates if
  582. * there are associated non-ERP stations and RTS/CTS is not configured
  583. * for the frame. */
  584. if (mode->mode == MODE_IEEE80211G &&
  585. (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
  586. tx->u.tx.unicast && tx->sdata->use_protection &&
  587. !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
  588. control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
  589. /* Transmit data frames using short preambles if the driver supports
  590. * short preambles at the selected rate and short preambles are
  591. * available on the network at the current point in time. */
  592. if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  593. (tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
  594. tx->sdata->short_preamble &&
  595. (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
  596. tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
  597. }
  598. /* Setup duration field for the first fragment of the frame. Duration
  599. * for remaining fragments will be updated when they are being sent
  600. * to low-level driver in ieee80211_tx(). */
  601. dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
  602. tx->fragmented ? tx->u.tx.extra_frag[0]->len :
  603. 0);
  604. hdr->duration_id = cpu_to_le16(dur);
  605. if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
  606. (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
  607. struct ieee80211_rate *rate;
  608. /* Do not use multiple retry rates when using RTS/CTS */
  609. control->alt_retry_rate = -1;
  610. /* Use min(data rate, max base rate) as CTS/RTS rate */
  611. rate = tx->u.tx.rate;
  612. while (rate > mode->rates &&
  613. !(rate->flags & IEEE80211_RATE_BASIC))
  614. rate--;
  615. control->rts_cts_rate = rate->val;
  616. control->rts_rate = rate;
  617. }
  618. if (tx->sta) {
  619. tx->sta->tx_packets++;
  620. tx->sta->tx_fragments++;
  621. tx->sta->tx_bytes += tx->skb->len;
  622. if (tx->u.tx.extra_frag) {
  623. int i;
  624. tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
  625. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  626. tx->sta->tx_bytes +=
  627. tx->u.tx.extra_frag[i]->len;
  628. }
  629. }
  630. }
  631. return TXRX_CONTINUE;
  632. }
  633. static ieee80211_txrx_result
  634. ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
  635. {
  636. struct ieee80211_local *local = tx->local;
  637. struct ieee80211_hw_mode *mode = tx->u.tx.mode;
  638. struct sk_buff *skb = tx->skb;
  639. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  640. u32 load = 0, hdrtime;
  641. /* TODO: this could be part of tx_status handling, so that the number
  642. * of retries would be known; TX rate should in that case be stored
  643. * somewhere with the packet */
  644. /* Estimate total channel use caused by this frame */
  645. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  646. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  647. if (mode->mode == MODE_IEEE80211A ||
  648. mode->mode == MODE_ATHEROS_TURBO ||
  649. mode->mode == MODE_ATHEROS_TURBOG ||
  650. (mode->mode == MODE_IEEE80211G &&
  651. tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
  652. hdrtime = CHAN_UTIL_HDR_SHORT;
  653. else
  654. hdrtime = CHAN_UTIL_HDR_LONG;
  655. load = hdrtime;
  656. if (!is_multicast_ether_addr(hdr->addr1))
  657. load += hdrtime;
  658. if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  659. load += 2 * hdrtime;
  660. else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  661. load += hdrtime;
  662. load += skb->len * tx->u.tx.rate->rate_inv;
  663. if (tx->u.tx.extra_frag) {
  664. int i;
  665. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  666. load += 2 * hdrtime;
  667. load += tx->u.tx.extra_frag[i]->len *
  668. tx->u.tx.rate->rate;
  669. }
  670. }
  671. /* Divide channel_use by 8 to avoid wrapping around the counter */
  672. load >>= CHAN_UTIL_SHIFT;
  673. local->channel_use_raw += load;
  674. if (tx->sta)
  675. tx->sta->channel_use_raw += load;
  676. tx->sdata->channel_use_raw += load;
  677. return TXRX_CONTINUE;
  678. }
  679. /* TODO: implement register/unregister functions for adding TX/RX handlers
  680. * into ordered list */
  681. ieee80211_tx_handler ieee80211_tx_handlers[] =
  682. {
  683. ieee80211_tx_h_check_assoc,
  684. ieee80211_tx_h_sequence,
  685. ieee80211_tx_h_ps_buf,
  686. ieee80211_tx_h_select_key,
  687. ieee80211_tx_h_michael_mic_add,
  688. ieee80211_tx_h_fragment,
  689. ieee80211_tx_h_tkip_encrypt,
  690. ieee80211_tx_h_ccmp_encrypt,
  691. ieee80211_tx_h_wep_encrypt,
  692. ieee80211_tx_h_rate_ctrl,
  693. ieee80211_tx_h_misc,
  694. ieee80211_tx_h_load_stats,
  695. NULL
  696. };
  697. /* actual transmit path */
  698. /*
  699. * deal with packet injection down monitor interface
  700. * with Radiotap Header -- only called for monitor mode interface
  701. */
  702. static ieee80211_txrx_result
  703. __ieee80211_parse_tx_radiotap(
  704. struct ieee80211_txrx_data *tx,
  705. struct sk_buff *skb, struct ieee80211_tx_control *control)
  706. {
  707. /*
  708. * this is the moment to interpret and discard the radiotap header that
  709. * must be at the start of the packet injected in Monitor mode
  710. *
  711. * Need to take some care with endian-ness since radiotap
  712. * args are little-endian
  713. */
  714. struct ieee80211_radiotap_iterator iterator;
  715. struct ieee80211_radiotap_header *rthdr =
  716. (struct ieee80211_radiotap_header *) skb->data;
  717. struct ieee80211_hw_mode *mode = tx->local->hw.conf.mode;
  718. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
  719. /*
  720. * default control situation for all injected packets
  721. * FIXME: this does not suit all usage cases, expand to allow control
  722. */
  723. control->retry_limit = 1; /* no retry */
  724. control->key_idx = -1; /* no encryption key */
  725. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  726. IEEE80211_TXCTL_USE_CTS_PROTECT);
  727. control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT |
  728. IEEE80211_TXCTL_NO_ACK;
  729. control->antenna_sel_tx = 0; /* default to default antenna */
  730. /*
  731. * for every radiotap entry that is present
  732. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  733. * entries present, or -EINVAL on error)
  734. */
  735. while (!ret) {
  736. int i, target_rate;
  737. ret = ieee80211_radiotap_iterator_next(&iterator);
  738. if (ret)
  739. continue;
  740. /* see if this argument is something we can use */
  741. switch (iterator.this_arg_index) {
  742. /*
  743. * You must take care when dereferencing iterator.this_arg
  744. * for multibyte types... the pointer is not aligned. Use
  745. * get_unaligned((type *)iterator.this_arg) to dereference
  746. * iterator.this_arg for type "type" safely on all arches.
  747. */
  748. case IEEE80211_RADIOTAP_RATE:
  749. /*
  750. * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
  751. * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
  752. */
  753. target_rate = (*iterator.this_arg) * 5;
  754. for (i = 0; i < mode->num_rates; i++) {
  755. struct ieee80211_rate *r = &mode->rates[i];
  756. if (r->rate > target_rate)
  757. continue;
  758. control->rate = r;
  759. if (r->flags & IEEE80211_RATE_PREAMBLE2)
  760. control->tx_rate = r->val2;
  761. else
  762. control->tx_rate = r->val;
  763. /* end on exact match */
  764. if (r->rate == target_rate)
  765. i = mode->num_rates;
  766. }
  767. break;
  768. case IEEE80211_RADIOTAP_ANTENNA:
  769. /*
  770. * radiotap uses 0 for 1st ant, mac80211 is 1 for
  771. * 1st ant
  772. */
  773. control->antenna_sel_tx = (*iterator.this_arg) + 1;
  774. break;
  775. case IEEE80211_RADIOTAP_DBM_TX_POWER:
  776. control->power_level = *iterator.this_arg;
  777. break;
  778. case IEEE80211_RADIOTAP_FLAGS:
  779. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  780. /*
  781. * this indicates that the skb we have been
  782. * handed has the 32-bit FCS CRC at the end...
  783. * we should react to that by snipping it off
  784. * because it will be recomputed and added
  785. * on transmission
  786. */
  787. if (skb->len < (iterator.max_length + FCS_LEN))
  788. return TXRX_DROP;
  789. skb_trim(skb, skb->len - FCS_LEN);
  790. }
  791. break;
  792. default:
  793. break;
  794. }
  795. }
  796. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  797. return TXRX_DROP;
  798. /*
  799. * remove the radiotap header
  800. * iterator->max_length was sanity-checked against
  801. * skb->len by iterator init
  802. */
  803. skb_pull(skb, iterator.max_length);
  804. return TXRX_CONTINUE;
  805. }
  806. static ieee80211_txrx_result inline
  807. __ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  808. struct sk_buff *skb,
  809. struct net_device *dev,
  810. struct ieee80211_tx_control *control)
  811. {
  812. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  813. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  814. struct ieee80211_sub_if_data *sdata;
  815. ieee80211_txrx_result res = TXRX_CONTINUE;
  816. int hdrlen;
  817. memset(tx, 0, sizeof(*tx));
  818. tx->skb = skb;
  819. tx->dev = dev; /* use original interface */
  820. tx->local = local;
  821. tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  822. tx->sta = sta_info_get(local, hdr->addr1);
  823. tx->fc = le16_to_cpu(hdr->frame_control);
  824. /*
  825. * set defaults for things that can be set by
  826. * injected radiotap headers
  827. */
  828. control->power_level = local->hw.conf.power_level;
  829. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  830. if (local->sta_antenna_sel != STA_ANTENNA_SEL_AUTO && tx->sta)
  831. control->antenna_sel_tx = tx->sta->antenna_sel_tx;
  832. /* process and remove the injection radiotap header */
  833. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  834. if (unlikely(sdata->type == IEEE80211_IF_TYPE_MNTR)) {
  835. if (__ieee80211_parse_tx_radiotap(tx, skb, control) ==
  836. TXRX_DROP) {
  837. return TXRX_DROP;
  838. }
  839. /*
  840. * we removed the radiotap header after this point,
  841. * we filled control with what we could use
  842. * set to the actual ieee header now
  843. */
  844. hdr = (struct ieee80211_hdr *) skb->data;
  845. res = TXRX_QUEUED; /* indication it was monitor packet */
  846. }
  847. tx->u.tx.control = control;
  848. tx->u.tx.unicast = !is_multicast_ether_addr(hdr->addr1);
  849. if (is_multicast_ether_addr(hdr->addr1))
  850. control->flags |= IEEE80211_TXCTL_NO_ACK;
  851. else
  852. control->flags &= ~IEEE80211_TXCTL_NO_ACK;
  853. tx->fragmented = local->fragmentation_threshold <
  854. IEEE80211_MAX_FRAG_THRESHOLD && tx->u.tx.unicast &&
  855. skb->len + FCS_LEN > local->fragmentation_threshold &&
  856. (!local->ops->set_frag_threshold);
  857. if (!tx->sta)
  858. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  859. else if (tx->sta->clear_dst_mask) {
  860. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  861. tx->sta->clear_dst_mask = 0;
  862. }
  863. hdrlen = ieee80211_get_hdrlen(tx->fc);
  864. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  865. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  866. tx->ethertype = (pos[0] << 8) | pos[1];
  867. }
  868. control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
  869. return res;
  870. }
  871. /* Device in tx->dev has a reference added; use dev_put(tx->dev) when
  872. * finished with it. */
  873. static int inline ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
  874. struct sk_buff *skb,
  875. struct net_device *mdev,
  876. struct ieee80211_tx_control *control)
  877. {
  878. struct ieee80211_tx_packet_data *pkt_data;
  879. struct net_device *dev;
  880. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  881. dev = dev_get_by_index(pkt_data->ifindex);
  882. if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
  883. dev_put(dev);
  884. dev = NULL;
  885. }
  886. if (unlikely(!dev))
  887. return -ENODEV;
  888. __ieee80211_tx_prepare(tx, skb, dev, control);
  889. return 0;
  890. }
  891. static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
  892. struct ieee80211_txrx_data *tx)
  893. {
  894. struct ieee80211_tx_control *control = tx->u.tx.control;
  895. int ret, i;
  896. if (!ieee80211_qdisc_installed(local->mdev) &&
  897. __ieee80211_queue_stopped(local, 0)) {
  898. netif_stop_queue(local->mdev);
  899. return IEEE80211_TX_AGAIN;
  900. }
  901. if (skb) {
  902. ieee80211_dump_frame(local->mdev->name, "TX to low-level driver", skb);
  903. ret = local->ops->tx(local_to_hw(local), skb, control);
  904. if (ret)
  905. return IEEE80211_TX_AGAIN;
  906. local->mdev->trans_start = jiffies;
  907. ieee80211_led_tx(local, 1);
  908. }
  909. if (tx->u.tx.extra_frag) {
  910. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  911. IEEE80211_TXCTL_USE_CTS_PROTECT |
  912. IEEE80211_TXCTL_CLEAR_DST_MASK |
  913. IEEE80211_TXCTL_FIRST_FRAGMENT);
  914. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  915. if (!tx->u.tx.extra_frag[i])
  916. continue;
  917. if (__ieee80211_queue_stopped(local, control->queue))
  918. return IEEE80211_TX_FRAG_AGAIN;
  919. if (i == tx->u.tx.num_extra_frag) {
  920. control->tx_rate = tx->u.tx.last_frag_hwrate;
  921. control->rate = tx->u.tx.last_frag_rate;
  922. if (tx->u.tx.probe_last_frag)
  923. control->flags |=
  924. IEEE80211_TXCTL_RATE_CTRL_PROBE;
  925. else
  926. control->flags &=
  927. ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  928. }
  929. ieee80211_dump_frame(local->mdev->name,
  930. "TX to low-level driver",
  931. tx->u.tx.extra_frag[i]);
  932. ret = local->ops->tx(local_to_hw(local),
  933. tx->u.tx.extra_frag[i],
  934. control);
  935. if (ret)
  936. return IEEE80211_TX_FRAG_AGAIN;
  937. local->mdev->trans_start = jiffies;
  938. ieee80211_led_tx(local, 1);
  939. tx->u.tx.extra_frag[i] = NULL;
  940. }
  941. kfree(tx->u.tx.extra_frag);
  942. tx->u.tx.extra_frag = NULL;
  943. }
  944. return IEEE80211_TX_OK;
  945. }
  946. static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
  947. struct ieee80211_tx_control *control, int mgmt)
  948. {
  949. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  950. struct sta_info *sta;
  951. ieee80211_tx_handler *handler;
  952. struct ieee80211_txrx_data tx;
  953. ieee80211_txrx_result res = TXRX_DROP, res_prepare;
  954. int ret, i;
  955. WARN_ON(__ieee80211_queue_pending(local, control->queue));
  956. if (unlikely(skb->len < 10)) {
  957. dev_kfree_skb(skb);
  958. return 0;
  959. }
  960. res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
  961. if (res_prepare == TXRX_DROP) {
  962. dev_kfree_skb(skb);
  963. return 0;
  964. }
  965. sta = tx.sta;
  966. tx.u.tx.mgmt_interface = mgmt;
  967. tx.u.tx.mode = local->hw.conf.mode;
  968. if (res_prepare == TXRX_QUEUED) { /* if it was an injected packet */
  969. res = TXRX_CONTINUE;
  970. } else {
  971. for (handler = local->tx_handlers; *handler != NULL;
  972. handler++) {
  973. res = (*handler)(&tx);
  974. if (res != TXRX_CONTINUE)
  975. break;
  976. }
  977. }
  978. skb = tx.skb; /* handlers are allowed to change skb */
  979. if (sta)
  980. sta_info_put(sta);
  981. if (unlikely(res == TXRX_DROP)) {
  982. I802_DEBUG_INC(local->tx_handlers_drop);
  983. goto drop;
  984. }
  985. if (unlikely(res == TXRX_QUEUED)) {
  986. I802_DEBUG_INC(local->tx_handlers_queued);
  987. return 0;
  988. }
  989. if (tx.u.tx.extra_frag) {
  990. for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
  991. int next_len, dur;
  992. struct ieee80211_hdr *hdr =
  993. (struct ieee80211_hdr *)
  994. tx.u.tx.extra_frag[i]->data;
  995. if (i + 1 < tx.u.tx.num_extra_frag) {
  996. next_len = tx.u.tx.extra_frag[i + 1]->len;
  997. } else {
  998. next_len = 0;
  999. tx.u.tx.rate = tx.u.tx.last_frag_rate;
  1000. tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
  1001. }
  1002. dur = ieee80211_duration(&tx, 0, next_len);
  1003. hdr->duration_id = cpu_to_le16(dur);
  1004. }
  1005. }
  1006. retry:
  1007. ret = __ieee80211_tx(local, skb, &tx);
  1008. if (ret) {
  1009. struct ieee80211_tx_stored_packet *store =
  1010. &local->pending_packet[control->queue];
  1011. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1012. skb = NULL;
  1013. set_bit(IEEE80211_LINK_STATE_PENDING,
  1014. &local->state[control->queue]);
  1015. smp_mb();
  1016. /* When the driver gets out of buffers during sending of
  1017. * fragments and calls ieee80211_stop_queue, there is
  1018. * a small window between IEEE80211_LINK_STATE_XOFF and
  1019. * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
  1020. * gets available in that window (i.e. driver calls
  1021. * ieee80211_wake_queue), we would end up with ieee80211_tx
  1022. * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
  1023. * continuing transmitting here when that situation is
  1024. * possible to have happened. */
  1025. if (!__ieee80211_queue_stopped(local, control->queue)) {
  1026. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1027. &local->state[control->queue]);
  1028. goto retry;
  1029. }
  1030. memcpy(&store->control, control,
  1031. sizeof(struct ieee80211_tx_control));
  1032. store->skb = skb;
  1033. store->extra_frag = tx.u.tx.extra_frag;
  1034. store->num_extra_frag = tx.u.tx.num_extra_frag;
  1035. store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
  1036. store->last_frag_rate = tx.u.tx.last_frag_rate;
  1037. store->last_frag_rate_ctrl_probe = tx.u.tx.probe_last_frag;
  1038. }
  1039. return 0;
  1040. drop:
  1041. if (skb)
  1042. dev_kfree_skb(skb);
  1043. for (i = 0; i < tx.u.tx.num_extra_frag; i++)
  1044. if (tx.u.tx.extra_frag[i])
  1045. dev_kfree_skb(tx.u.tx.extra_frag[i]);
  1046. kfree(tx.u.tx.extra_frag);
  1047. 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(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->req_tx_status)
  1090. control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
  1091. if (pkt_data->do_not_encrypt)
  1092. control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  1093. if (pkt_data->requeue)
  1094. control.flags |= IEEE80211_TXCTL_REQUEUE;
  1095. control.queue = pkt_data->queue;
  1096. ret = ieee80211_tx(odev, skb, &control,
  1097. control.type == IEEE80211_IF_TYPE_MGMT);
  1098. dev_put(odev);
  1099. return ret;
  1100. }
  1101. int ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1102. struct net_device *dev)
  1103. {
  1104. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1105. struct ieee80211_tx_packet_data *pkt_data;
  1106. struct ieee80211_radiotap_header *prthdr =
  1107. (struct ieee80211_radiotap_header *)skb->data;
  1108. u16 len;
  1109. /*
  1110. * there must be a radiotap header at the
  1111. * start in this case
  1112. */
  1113. if (unlikely(prthdr->it_version)) {
  1114. /* only version 0 is supported */
  1115. dev_kfree_skb(skb);
  1116. return NETDEV_TX_OK;
  1117. }
  1118. skb->dev = local->mdev;
  1119. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1120. memset(pkt_data, 0, sizeof(*pkt_data));
  1121. pkt_data->ifindex = dev->ifindex;
  1122. pkt_data->mgmt_iface = 0;
  1123. pkt_data->do_not_encrypt = 1;
  1124. /* above needed because we set skb device to master */
  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. len = le16_to_cpu(get_unaligned(&prthdr->it_len));
  1132. skb_set_mac_header(skb, len);
  1133. skb_set_network_header(skb, len + sizeof(struct ieee80211_hdr));
  1134. skb_set_transport_header(skb, len + sizeof(struct ieee80211_hdr));
  1135. /*
  1136. * pass the radiotap header up to
  1137. * the next stage intact
  1138. */
  1139. dev_queue_xmit(skb);
  1140. return NETDEV_TX_OK;
  1141. }
  1142. /**
  1143. * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
  1144. * subinterfaces (wlan#, WDS, and VLAN interfaces)
  1145. * @skb: packet to be sent
  1146. * @dev: incoming interface
  1147. *
  1148. * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
  1149. * not be freed, and caller is responsible for either retrying later or freeing
  1150. * skb).
  1151. *
  1152. * This function takes in an Ethernet header and encapsulates it with suitable
  1153. * IEEE 802.11 header based on which interface the packet is coming in. The
  1154. * encapsulated packet will then be passed to master interface, wlan#.11, for
  1155. * transmission (through low-level driver).
  1156. */
  1157. int ieee80211_subif_start_xmit(struct sk_buff *skb,
  1158. struct net_device *dev)
  1159. {
  1160. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1161. struct ieee80211_tx_packet_data *pkt_data;
  1162. struct ieee80211_sub_if_data *sdata;
  1163. int ret = 1, head_need;
  1164. u16 ethertype, hdrlen, fc;
  1165. struct ieee80211_hdr hdr;
  1166. const u8 *encaps_data;
  1167. int encaps_len, skip_header_bytes;
  1168. int nh_pos, h_pos, no_encrypt = 0;
  1169. struct sta_info *sta;
  1170. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1171. if (unlikely(skb->len < ETH_HLEN)) {
  1172. printk(KERN_DEBUG "%s: short skb (len=%d)\n",
  1173. dev->name, skb->len);
  1174. ret = 0;
  1175. goto fail;
  1176. }
  1177. nh_pos = skb_network_header(skb) - skb->data;
  1178. h_pos = skb_transport_header(skb) - skb->data;
  1179. /* convert Ethernet header to proper 802.11 header (based on
  1180. * operation mode) */
  1181. ethertype = (skb->data[12] << 8) | skb->data[13];
  1182. /* TODO: handling for 802.1x authorized/unauthorized port */
  1183. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  1184. if (likely(sdata->type == IEEE80211_IF_TYPE_AP ||
  1185. sdata->type == IEEE80211_IF_TYPE_VLAN)) {
  1186. fc |= IEEE80211_FCTL_FROMDS;
  1187. /* DA BSSID SA */
  1188. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1189. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1190. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1191. hdrlen = 24;
  1192. } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
  1193. fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
  1194. /* RA TA DA SA */
  1195. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  1196. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1197. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1198. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1199. hdrlen = 30;
  1200. } else if (sdata->type == IEEE80211_IF_TYPE_STA) {
  1201. fc |= IEEE80211_FCTL_TODS;
  1202. /* BSSID SA DA */
  1203. memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
  1204. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1205. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1206. hdrlen = 24;
  1207. } else if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
  1208. /* DA SA BSSID */
  1209. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1210. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1211. memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
  1212. hdrlen = 24;
  1213. } else {
  1214. ret = 0;
  1215. goto fail;
  1216. }
  1217. /* receiver is QoS enabled, use a QoS type frame */
  1218. sta = sta_info_get(local, hdr.addr1);
  1219. if (sta) {
  1220. if (sta->flags & WLAN_STA_WME) {
  1221. fc |= IEEE80211_STYPE_QOS_DATA;
  1222. hdrlen += 2;
  1223. }
  1224. sta_info_put(sta);
  1225. }
  1226. hdr.frame_control = cpu_to_le16(fc);
  1227. hdr.duration_id = 0;
  1228. hdr.seq_ctrl = 0;
  1229. skip_header_bytes = ETH_HLEN;
  1230. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1231. encaps_data = bridge_tunnel_header;
  1232. encaps_len = sizeof(bridge_tunnel_header);
  1233. skip_header_bytes -= 2;
  1234. } else if (ethertype >= 0x600) {
  1235. encaps_data = rfc1042_header;
  1236. encaps_len = sizeof(rfc1042_header);
  1237. skip_header_bytes -= 2;
  1238. } else {
  1239. encaps_data = NULL;
  1240. encaps_len = 0;
  1241. }
  1242. skb_pull(skb, skip_header_bytes);
  1243. nh_pos -= skip_header_bytes;
  1244. h_pos -= skip_header_bytes;
  1245. /* TODO: implement support for fragments so that there is no need to
  1246. * reallocate and copy payload; it might be enough to support one
  1247. * extra fragment that would be copied in the beginning of the frame
  1248. * data.. anyway, it would be nice to include this into skb structure
  1249. * somehow
  1250. *
  1251. * There are few options for this:
  1252. * use skb->cb as an extra space for 802.11 header
  1253. * allocate new buffer if not enough headroom
  1254. * make sure that there is enough headroom in every skb by increasing
  1255. * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
  1256. * alloc_skb() (net/core/skbuff.c)
  1257. */
  1258. head_need = hdrlen + encaps_len + local->tx_headroom;
  1259. head_need -= skb_headroom(skb);
  1260. /* We are going to modify skb data, so make a copy of it if happens to
  1261. * be cloned. This could happen, e.g., with Linux bridge code passing
  1262. * us broadcast frames. */
  1263. if (head_need > 0 || skb_cloned(skb)) {
  1264. #if 0
  1265. printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
  1266. "of headroom\n", dev->name, head_need);
  1267. #endif
  1268. if (skb_cloned(skb))
  1269. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1270. else
  1271. I802_DEBUG_INC(local->tx_expand_skb_head);
  1272. /* Since we have to reallocate the buffer, make sure that there
  1273. * is enough room for possible WEP IV/ICV and TKIP (8 bytes
  1274. * before payload and 12 after). */
  1275. if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
  1276. 12, GFP_ATOMIC)) {
  1277. printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
  1278. "\n", dev->name);
  1279. goto fail;
  1280. }
  1281. }
  1282. if (encaps_data) {
  1283. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1284. nh_pos += encaps_len;
  1285. h_pos += encaps_len;
  1286. }
  1287. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1288. nh_pos += hdrlen;
  1289. h_pos += hdrlen;
  1290. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1291. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1292. pkt_data->ifindex = dev->ifindex;
  1293. pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
  1294. pkt_data->do_not_encrypt = no_encrypt;
  1295. skb->dev = local->mdev;
  1296. sdata->stats.tx_packets++;
  1297. sdata->stats.tx_bytes += skb->len;
  1298. /* Update skb pointers to various headers since this modified frame
  1299. * is going to go through Linux networking code that may potentially
  1300. * need things like pointer to IP header. */
  1301. skb_set_mac_header(skb, 0);
  1302. skb_set_network_header(skb, nh_pos);
  1303. skb_set_transport_header(skb, h_pos);
  1304. dev->trans_start = jiffies;
  1305. dev_queue_xmit(skb);
  1306. return 0;
  1307. fail:
  1308. if (!ret)
  1309. dev_kfree_skb(skb);
  1310. return ret;
  1311. }
  1312. /*
  1313. * This is the transmit routine for the 802.11 type interfaces
  1314. * called by upper layers of the linux networking
  1315. * stack when it has a frame to transmit
  1316. */
  1317. int ieee80211_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1318. {
  1319. struct ieee80211_sub_if_data *sdata;
  1320. struct ieee80211_tx_packet_data *pkt_data;
  1321. struct ieee80211_hdr *hdr;
  1322. u16 fc;
  1323. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1324. if (skb->len < 10) {
  1325. dev_kfree_skb(skb);
  1326. return 0;
  1327. }
  1328. if (skb_headroom(skb) < sdata->local->tx_headroom) {
  1329. if (pskb_expand_head(skb, sdata->local->tx_headroom,
  1330. 0, GFP_ATOMIC)) {
  1331. dev_kfree_skb(skb);
  1332. return 0;
  1333. }
  1334. }
  1335. hdr = (struct ieee80211_hdr *) skb->data;
  1336. fc = le16_to_cpu(hdr->frame_control);
  1337. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  1338. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1339. pkt_data->ifindex = sdata->dev->ifindex;
  1340. pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
  1341. skb->priority = 20; /* use hardcoded priority for mgmt TX queue */
  1342. skb->dev = sdata->local->mdev;
  1343. /*
  1344. * We're using the protocol field of the the frame control header
  1345. * to request TX callback for hostapd. BIT(1) is checked.
  1346. */
  1347. if ((fc & BIT(1)) == BIT(1)) {
  1348. pkt_data->req_tx_status = 1;
  1349. fc &= ~BIT(1);
  1350. hdr->frame_control = cpu_to_le16(fc);
  1351. }
  1352. pkt_data->do_not_encrypt = !(fc & IEEE80211_FCTL_PROTECTED);
  1353. sdata->stats.tx_packets++;
  1354. sdata->stats.tx_bytes += skb->len;
  1355. dev_queue_xmit(skb);
  1356. return 0;
  1357. }
  1358. /* helper functions for pending packets for when queues are stopped */
  1359. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  1360. {
  1361. int i, j;
  1362. struct ieee80211_tx_stored_packet *store;
  1363. for (i = 0; i < local->hw.queues; i++) {
  1364. if (!__ieee80211_queue_pending(local, i))
  1365. continue;
  1366. store = &local->pending_packet[i];
  1367. kfree_skb(store->skb);
  1368. for (j = 0; j < store->num_extra_frag; j++)
  1369. kfree_skb(store->extra_frag[j]);
  1370. kfree(store->extra_frag);
  1371. clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
  1372. }
  1373. }
  1374. void ieee80211_tx_pending(unsigned long data)
  1375. {
  1376. struct ieee80211_local *local = (struct ieee80211_local *)data;
  1377. struct net_device *dev = local->mdev;
  1378. struct ieee80211_tx_stored_packet *store;
  1379. struct ieee80211_txrx_data tx;
  1380. int i, ret, reschedule = 0;
  1381. netif_tx_lock_bh(dev);
  1382. for (i = 0; i < local->hw.queues; i++) {
  1383. if (__ieee80211_queue_stopped(local, i))
  1384. continue;
  1385. if (!__ieee80211_queue_pending(local, i)) {
  1386. reschedule = 1;
  1387. continue;
  1388. }
  1389. store = &local->pending_packet[i];
  1390. tx.u.tx.control = &store->control;
  1391. tx.u.tx.extra_frag = store->extra_frag;
  1392. tx.u.tx.num_extra_frag = store->num_extra_frag;
  1393. tx.u.tx.last_frag_hwrate = store->last_frag_hwrate;
  1394. tx.u.tx.last_frag_rate = store->last_frag_rate;
  1395. tx.u.tx.probe_last_frag = store->last_frag_rate_ctrl_probe;
  1396. ret = __ieee80211_tx(local, store->skb, &tx);
  1397. if (ret) {
  1398. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1399. store->skb = NULL;
  1400. } else {
  1401. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1402. &local->state[i]);
  1403. reschedule = 1;
  1404. }
  1405. }
  1406. netif_tx_unlock_bh(dev);
  1407. if (reschedule) {
  1408. if (!ieee80211_qdisc_installed(dev)) {
  1409. if (!__ieee80211_queue_stopped(local, 0))
  1410. netif_wake_queue(dev);
  1411. } else
  1412. netif_schedule(dev);
  1413. }
  1414. }
  1415. /* functions for drivers to get certain frames */
  1416. static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
  1417. struct ieee80211_if_ap *bss,
  1418. struct sk_buff *skb)
  1419. {
  1420. u8 *pos, *tim;
  1421. int aid0 = 0;
  1422. int i, have_bits = 0, n1, n2;
  1423. /* Generate bitmap for TIM only if there are any STAs in power save
  1424. * mode. */
  1425. read_lock_bh(&local->sta_lock);
  1426. if (atomic_read(&bss->num_sta_ps) > 0)
  1427. /* in the hope that this is faster than
  1428. * checking byte-for-byte */
  1429. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1430. IEEE80211_MAX_AID+1);
  1431. if (bss->dtim_count == 0)
  1432. bss->dtim_count = bss->dtim_period - 1;
  1433. else
  1434. bss->dtim_count--;
  1435. tim = pos = (u8 *) skb_put(skb, 6);
  1436. *pos++ = WLAN_EID_TIM;
  1437. *pos++ = 4;
  1438. *pos++ = bss->dtim_count;
  1439. *pos++ = bss->dtim_period;
  1440. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1441. aid0 = 1;
  1442. if (have_bits) {
  1443. /* Find largest even number N1 so that bits numbered 1 through
  1444. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1445. * (N2 + 1) x 8 through 2007 are 0. */
  1446. n1 = 0;
  1447. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1448. if (bss->tim[i]) {
  1449. n1 = i & 0xfe;
  1450. break;
  1451. }
  1452. }
  1453. n2 = n1;
  1454. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1455. if (bss->tim[i]) {
  1456. n2 = i;
  1457. break;
  1458. }
  1459. }
  1460. /* Bitmap control */
  1461. *pos++ = n1 | aid0;
  1462. /* Part Virt Bitmap */
  1463. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1464. tim[1] = n2 - n1 + 4;
  1465. skb_put(skb, n2 - n1);
  1466. } else {
  1467. *pos++ = aid0; /* Bitmap control */
  1468. *pos++ = 0; /* Part Virt Bitmap */
  1469. }
  1470. read_unlock_bh(&local->sta_lock);
  1471. }
  1472. struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw, int if_id,
  1473. struct ieee80211_tx_control *control)
  1474. {
  1475. struct ieee80211_local *local = hw_to_local(hw);
  1476. struct sk_buff *skb;
  1477. struct net_device *bdev;
  1478. struct ieee80211_sub_if_data *sdata = NULL;
  1479. struct ieee80211_if_ap *ap = NULL;
  1480. struct ieee80211_rate *rate;
  1481. struct rate_control_extra extra;
  1482. u8 *b_head, *b_tail;
  1483. int bh_len, bt_len;
  1484. bdev = dev_get_by_index(if_id);
  1485. if (bdev) {
  1486. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  1487. ap = &sdata->u.ap;
  1488. dev_put(bdev);
  1489. }
  1490. if (!ap || sdata->type != IEEE80211_IF_TYPE_AP ||
  1491. !ap->beacon_head) {
  1492. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1493. if (net_ratelimit())
  1494. printk(KERN_DEBUG "no beacon data avail for idx=%d "
  1495. "(%s)\n", if_id, bdev ? bdev->name : "N/A");
  1496. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1497. return NULL;
  1498. }
  1499. /* Assume we are generating the normal beacon locally */
  1500. b_head = ap->beacon_head;
  1501. b_tail = ap->beacon_tail;
  1502. bh_len = ap->beacon_head_len;
  1503. bt_len = ap->beacon_tail_len;
  1504. skb = dev_alloc_skb(local->tx_headroom +
  1505. bh_len + bt_len + 256 /* maximum TIM len */);
  1506. if (!skb)
  1507. return NULL;
  1508. skb_reserve(skb, local->tx_headroom);
  1509. memcpy(skb_put(skb, bh_len), b_head, bh_len);
  1510. ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
  1511. ieee80211_beacon_add_tim(local, ap, skb);
  1512. if (b_tail) {
  1513. memcpy(skb_put(skb, bt_len), b_tail, bt_len);
  1514. }
  1515. if (control) {
  1516. memset(&extra, 0, sizeof(extra));
  1517. extra.mode = local->oper_hw_mode;
  1518. rate = rate_control_get_rate(local, local->mdev, skb, &extra);
  1519. if (!rate) {
  1520. if (net_ratelimit()) {
  1521. printk(KERN_DEBUG "%s: ieee80211_beacon_get: no rate "
  1522. "found\n", local->mdev->name);
  1523. }
  1524. dev_kfree_skb(skb);
  1525. return NULL;
  1526. }
  1527. control->tx_rate = (sdata->short_preamble &&
  1528. (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
  1529. rate->val2 : rate->val;
  1530. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  1531. control->power_level = local->hw.conf.power_level;
  1532. control->flags |= IEEE80211_TXCTL_NO_ACK;
  1533. control->retry_limit = 1;
  1534. control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
  1535. }
  1536. ap->num_beacons++;
  1537. return skb;
  1538. }
  1539. EXPORT_SYMBOL(ieee80211_beacon_get);
  1540. void ieee80211_rts_get(struct ieee80211_hw *hw, int if_id,
  1541. const void *frame, size_t frame_len,
  1542. const struct ieee80211_tx_control *frame_txctl,
  1543. struct ieee80211_rts *rts)
  1544. {
  1545. const struct ieee80211_hdr *hdr = frame;
  1546. u16 fctl;
  1547. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
  1548. rts->frame_control = cpu_to_le16(fctl);
  1549. rts->duration = ieee80211_rts_duration(hw, if_id, frame_len, frame_txctl);
  1550. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  1551. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  1552. }
  1553. EXPORT_SYMBOL(ieee80211_rts_get);
  1554. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, int if_id,
  1555. const void *frame, size_t frame_len,
  1556. const struct ieee80211_tx_control *frame_txctl,
  1557. struct ieee80211_cts *cts)
  1558. {
  1559. const struct ieee80211_hdr *hdr = frame;
  1560. u16 fctl;
  1561. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
  1562. cts->frame_control = cpu_to_le16(fctl);
  1563. cts->duration = ieee80211_ctstoself_duration(hw, if_id, frame_len, frame_txctl);
  1564. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  1565. }
  1566. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  1567. struct sk_buff *
  1568. ieee80211_get_buffered_bc(struct ieee80211_hw *hw, int if_id,
  1569. struct ieee80211_tx_control *control)
  1570. {
  1571. struct ieee80211_local *local = hw_to_local(hw);
  1572. struct sk_buff *skb;
  1573. struct sta_info *sta;
  1574. ieee80211_tx_handler *handler;
  1575. struct ieee80211_txrx_data tx;
  1576. ieee80211_txrx_result res = TXRX_DROP;
  1577. struct net_device *bdev;
  1578. struct ieee80211_sub_if_data *sdata;
  1579. struct ieee80211_if_ap *bss = NULL;
  1580. bdev = dev_get_by_index(if_id);
  1581. if (bdev) {
  1582. sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
  1583. bss = &sdata->u.ap;
  1584. dev_put(bdev);
  1585. }
  1586. if (!bss || sdata->type != IEEE80211_IF_TYPE_AP || !bss->beacon_head)
  1587. return NULL;
  1588. if (bss->dtim_count != 0)
  1589. return NULL; /* send buffered bc/mc only after DTIM beacon */
  1590. memset(control, 0, sizeof(*control));
  1591. while (1) {
  1592. skb = skb_dequeue(&bss->ps_bc_buf);
  1593. if (!skb)
  1594. return NULL;
  1595. local->total_ps_buffered--;
  1596. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  1597. struct ieee80211_hdr *hdr =
  1598. (struct ieee80211_hdr *) skb->data;
  1599. /* more buffered multicast/broadcast frames ==> set
  1600. * MoreData flag in IEEE 802.11 header to inform PS
  1601. * STAs */
  1602. hdr->frame_control |=
  1603. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1604. }
  1605. if (ieee80211_tx_prepare(&tx, skb, local->mdev, control) == 0)
  1606. break;
  1607. dev_kfree_skb_any(skb);
  1608. }
  1609. sta = tx.sta;
  1610. tx.u.tx.ps_buffered = 1;
  1611. for (handler = local->tx_handlers; *handler != NULL; handler++) {
  1612. res = (*handler)(&tx);
  1613. if (res == TXRX_DROP || res == TXRX_QUEUED)
  1614. break;
  1615. }
  1616. dev_put(tx.dev);
  1617. skb = tx.skb; /* handlers are allowed to change skb */
  1618. if (res == TXRX_DROP) {
  1619. I802_DEBUG_INC(local->tx_handlers_drop);
  1620. dev_kfree_skb(skb);
  1621. skb = NULL;
  1622. } else if (res == TXRX_QUEUED) {
  1623. I802_DEBUG_INC(local->tx_handlers_queued);
  1624. skb = NULL;
  1625. }
  1626. if (sta)
  1627. sta_info_put(sta);
  1628. return skb;
  1629. }
  1630. EXPORT_SYMBOL(ieee80211_get_buffered_bc);