tx.c 53 KB

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