tx.c 54 KB

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