tx.c 53 KB

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