tx.c 56 KB

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