tx.c 56 KB

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