tx.c 55 KB

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