tx.c 53 KB

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