tx.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  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 = 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. control->aid = tx->sta->aid;
  644. tx->sta->tx_packets++;
  645. tx->sta->tx_fragments++;
  646. tx->sta->tx_bytes += tx->skb->len;
  647. if (tx->extra_frag) {
  648. int i;
  649. tx->sta->tx_fragments += tx->num_extra_frag;
  650. for (i = 0; i < tx->num_extra_frag; i++) {
  651. tx->sta->tx_bytes +=
  652. tx->extra_frag[i]->len;
  653. }
  654. }
  655. }
  656. return TX_CONTINUE;
  657. }
  658. static ieee80211_tx_result
  659. ieee80211_tx_h_load_stats(struct ieee80211_tx_data *tx)
  660. {
  661. struct ieee80211_local *local = tx->local;
  662. struct sk_buff *skb = tx->skb;
  663. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  664. u32 load = 0, hdrtime;
  665. struct ieee80211_rate *rate = tx->rate;
  666. /* TODO: this could be part of tx_status handling, so that the number
  667. * of retries would be known; TX rate should in that case be stored
  668. * somewhere with the packet */
  669. /* Estimate total channel use caused by this frame */
  670. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  671. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  672. if (tx->channel->band == IEEE80211_BAND_5GHZ ||
  673. (tx->channel->band == IEEE80211_BAND_2GHZ &&
  674. rate->flags & IEEE80211_RATE_ERP_G))
  675. hdrtime = CHAN_UTIL_HDR_SHORT;
  676. else
  677. hdrtime = CHAN_UTIL_HDR_LONG;
  678. load = hdrtime;
  679. if (!is_multicast_ether_addr(hdr->addr1))
  680. load += hdrtime;
  681. if (tx->control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  682. load += 2 * hdrtime;
  683. else if (tx->control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  684. load += hdrtime;
  685. /* TODO: optimise again */
  686. load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
  687. if (tx->extra_frag) {
  688. int i;
  689. for (i = 0; i < tx->num_extra_frag; i++) {
  690. load += 2 * hdrtime;
  691. load += tx->extra_frag[i]->len *
  692. tx->rate->bitrate;
  693. }
  694. }
  695. /* Divide channel_use by 8 to avoid wrapping around the counter */
  696. load >>= CHAN_UTIL_SHIFT;
  697. local->channel_use_raw += load;
  698. if (tx->sta)
  699. tx->sta->channel_use_raw += load;
  700. tx->sdata->channel_use_raw += load;
  701. return TX_CONTINUE;
  702. }
  703. typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_tx_data *);
  704. static ieee80211_tx_handler ieee80211_tx_handlers[] =
  705. {
  706. ieee80211_tx_h_check_assoc,
  707. ieee80211_tx_h_sequence,
  708. ieee80211_tx_h_ps_buf,
  709. ieee80211_tx_h_select_key,
  710. ieee80211_tx_h_michael_mic_add,
  711. ieee80211_tx_h_fragment,
  712. ieee80211_tx_h_encrypt,
  713. ieee80211_tx_h_rate_ctrl,
  714. ieee80211_tx_h_misc,
  715. ieee80211_tx_h_load_stats,
  716. NULL
  717. };
  718. /* actual transmit path */
  719. /*
  720. * deal with packet injection down monitor interface
  721. * with Radiotap Header -- only called for monitor mode interface
  722. */
  723. static ieee80211_tx_result
  724. __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
  725. struct sk_buff *skb)
  726. {
  727. /*
  728. * this is the moment to interpret and discard the radiotap header that
  729. * must be at the start of the packet injected in Monitor mode
  730. *
  731. * Need to take some care with endian-ness since radiotap
  732. * args are little-endian
  733. */
  734. struct ieee80211_radiotap_iterator iterator;
  735. struct ieee80211_radiotap_header *rthdr =
  736. (struct ieee80211_radiotap_header *) skb->data;
  737. struct ieee80211_supported_band *sband;
  738. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
  739. struct ieee80211_tx_control *control = tx->control;
  740. sband = tx->local->hw.wiphy->bands[tx->local->hw.conf.channel->band];
  741. control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  742. tx->flags |= IEEE80211_TX_INJECTED;
  743. tx->flags &= ~IEEE80211_TX_FRAGMENTED;
  744. /*
  745. * for every radiotap entry that is present
  746. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  747. * entries present, or -EINVAL on error)
  748. */
  749. while (!ret) {
  750. int i, target_rate;
  751. ret = ieee80211_radiotap_iterator_next(&iterator);
  752. if (ret)
  753. continue;
  754. /* see if this argument is something we can use */
  755. switch (iterator.this_arg_index) {
  756. /*
  757. * You must take care when dereferencing iterator.this_arg
  758. * for multibyte types... the pointer is not aligned. Use
  759. * get_unaligned((type *)iterator.this_arg) to dereference
  760. * iterator.this_arg for type "type" safely on all arches.
  761. */
  762. case IEEE80211_RADIOTAP_RATE:
  763. /*
  764. * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
  765. * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
  766. */
  767. target_rate = (*iterator.this_arg) * 5;
  768. for (i = 0; i < sband->n_bitrates; i++) {
  769. struct ieee80211_rate *r;
  770. r = &sband->bitrates[i];
  771. if (r->bitrate == target_rate) {
  772. tx->rate = r;
  773. break;
  774. }
  775. }
  776. break;
  777. case IEEE80211_RADIOTAP_ANTENNA:
  778. /*
  779. * radiotap uses 0 for 1st ant, mac80211 is 1 for
  780. * 1st ant
  781. */
  782. control->antenna_sel_tx = (*iterator.this_arg) + 1;
  783. break;
  784. #if 0
  785. case IEEE80211_RADIOTAP_DBM_TX_POWER:
  786. control->power_level = *iterator.this_arg;
  787. break;
  788. #endif
  789. case IEEE80211_RADIOTAP_FLAGS:
  790. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  791. /*
  792. * this indicates that the skb we have been
  793. * handed has the 32-bit FCS CRC at the end...
  794. * we should react to that by snipping it off
  795. * because it will be recomputed and added
  796. * on transmission
  797. */
  798. if (skb->len < (iterator.max_length + FCS_LEN))
  799. return TX_DROP;
  800. skb_trim(skb, skb->len - FCS_LEN);
  801. }
  802. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
  803. control->flags &=
  804. ~IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  805. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
  806. tx->flags |= IEEE80211_TX_FRAGMENTED;
  807. break;
  808. /*
  809. * Please update the file
  810. * Documentation/networking/mac80211-injection.txt
  811. * when parsing new fields here.
  812. */
  813. default:
  814. break;
  815. }
  816. }
  817. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  818. return TX_DROP;
  819. /*
  820. * remove the radiotap header
  821. * iterator->max_length was sanity-checked against
  822. * skb->len by iterator init
  823. */
  824. skb_pull(skb, iterator.max_length);
  825. return TX_CONTINUE;
  826. }
  827. /*
  828. * initialises @tx
  829. */
  830. static ieee80211_tx_result
  831. __ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
  832. struct sk_buff *skb,
  833. struct net_device *dev,
  834. struct ieee80211_tx_control *control)
  835. {
  836. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  837. struct ieee80211_hdr *hdr;
  838. struct ieee80211_sub_if_data *sdata;
  839. int hdrlen;
  840. memset(tx, 0, sizeof(*tx));
  841. tx->skb = skb;
  842. tx->dev = dev; /* use original interface */
  843. tx->local = local;
  844. tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  845. tx->control = control;
  846. /*
  847. * Set this flag (used below to indicate "automatic fragmentation"),
  848. * it will be cleared/left by radiotap as desired.
  849. */
  850. tx->flags |= IEEE80211_TX_FRAGMENTED;
  851. /* process and remove the injection radiotap header */
  852. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  853. if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
  854. if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
  855. return TX_DROP;
  856. /*
  857. * __ieee80211_parse_tx_radiotap has now removed
  858. * the radiotap header that was present and pre-filled
  859. * 'tx' with tx control information.
  860. */
  861. }
  862. hdr = (struct ieee80211_hdr *) skb->data;
  863. tx->sta = sta_info_get(local, hdr->addr1);
  864. tx->fc = le16_to_cpu(hdr->frame_control);
  865. if (is_multicast_ether_addr(hdr->addr1)) {
  866. tx->flags &= ~IEEE80211_TX_UNICAST;
  867. control->flags |= IEEE80211_TXCTL_NO_ACK;
  868. } else {
  869. tx->flags |= IEEE80211_TX_UNICAST;
  870. control->flags &= ~IEEE80211_TXCTL_NO_ACK;
  871. }
  872. if (tx->flags & IEEE80211_TX_FRAGMENTED) {
  873. if ((tx->flags & IEEE80211_TX_UNICAST) &&
  874. skb->len + FCS_LEN > local->fragmentation_threshold &&
  875. !local->ops->set_frag_threshold)
  876. tx->flags |= IEEE80211_TX_FRAGMENTED;
  877. else
  878. tx->flags &= ~IEEE80211_TX_FRAGMENTED;
  879. }
  880. if (!tx->sta)
  881. control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
  882. else if (tx->sta->flags & WLAN_STA_CLEAR_PS_FILT) {
  883. control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
  884. tx->sta->flags &= ~WLAN_STA_CLEAR_PS_FILT;
  885. }
  886. hdrlen = ieee80211_get_hdrlen(tx->fc);
  887. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  888. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  889. tx->ethertype = (pos[0] << 8) | pos[1];
  890. }
  891. control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
  892. return TX_CONTINUE;
  893. }
  894. /*
  895. * NB: @tx is uninitialised when passed in here
  896. */
  897. static int ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
  898. struct sk_buff *skb,
  899. struct net_device *mdev,
  900. struct ieee80211_tx_control *control)
  901. {
  902. struct ieee80211_tx_packet_data *pkt_data;
  903. struct net_device *dev;
  904. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  905. dev = dev_get_by_index(&init_net, pkt_data->ifindex);
  906. if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
  907. dev_put(dev);
  908. dev = NULL;
  909. }
  910. if (unlikely(!dev))
  911. return -ENODEV;
  912. /* initialises tx with control */
  913. __ieee80211_tx_prepare(tx, skb, dev, control);
  914. dev_put(dev);
  915. return 0;
  916. }
  917. static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
  918. struct ieee80211_tx_data *tx)
  919. {
  920. struct ieee80211_tx_control *control = tx->control;
  921. int ret, i;
  922. if (!ieee80211_qdisc_installed(local->mdev) &&
  923. __ieee80211_queue_stopped(local, 0)) {
  924. netif_stop_queue(local->mdev);
  925. return IEEE80211_TX_AGAIN;
  926. }
  927. if (skb) {
  928. ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
  929. "TX to low-level driver", skb);
  930. ret = local->ops->tx(local_to_hw(local), skb, control);
  931. if (ret)
  932. return IEEE80211_TX_AGAIN;
  933. local->mdev->trans_start = jiffies;
  934. ieee80211_led_tx(local, 1);
  935. }
  936. if (tx->extra_frag) {
  937. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  938. IEEE80211_TXCTL_USE_CTS_PROTECT |
  939. IEEE80211_TXCTL_CLEAR_PS_FILT |
  940. IEEE80211_TXCTL_FIRST_FRAGMENT);
  941. for (i = 0; i < tx->num_extra_frag; i++) {
  942. if (!tx->extra_frag[i])
  943. continue;
  944. if (__ieee80211_queue_stopped(local, control->queue))
  945. return IEEE80211_TX_FRAG_AGAIN;
  946. if (i == tx->num_extra_frag) {
  947. control->tx_rate = tx->last_frag_rate;
  948. if (tx->flags & IEEE80211_TX_PROBE_LAST_FRAG)
  949. control->flags |=
  950. IEEE80211_TXCTL_RATE_CTRL_PROBE;
  951. else
  952. control->flags &=
  953. ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  954. }
  955. ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
  956. "TX to low-level driver",
  957. tx->extra_frag[i]);
  958. ret = local->ops->tx(local_to_hw(local),
  959. tx->extra_frag[i],
  960. control);
  961. if (ret)
  962. return IEEE80211_TX_FRAG_AGAIN;
  963. local->mdev->trans_start = jiffies;
  964. ieee80211_led_tx(local, 1);
  965. tx->extra_frag[i] = NULL;
  966. }
  967. kfree(tx->extra_frag);
  968. tx->extra_frag = NULL;
  969. }
  970. return IEEE80211_TX_OK;
  971. }
  972. static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
  973. struct ieee80211_tx_control *control)
  974. {
  975. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  976. struct sta_info *sta;
  977. ieee80211_tx_handler *handler;
  978. struct ieee80211_tx_data tx;
  979. ieee80211_tx_result res = TX_DROP, res_prepare;
  980. int ret, i, retries = 0;
  981. WARN_ON(__ieee80211_queue_pending(local, control->queue));
  982. if (unlikely(skb->len < 10)) {
  983. dev_kfree_skb(skb);
  984. return 0;
  985. }
  986. rcu_read_lock();
  987. /* initialises tx */
  988. res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
  989. if (res_prepare == TX_DROP) {
  990. dev_kfree_skb(skb);
  991. rcu_read_unlock();
  992. return 0;
  993. }
  994. sta = tx.sta;
  995. tx.channel = local->hw.conf.channel;
  996. for (handler = ieee80211_tx_handlers; *handler != NULL;
  997. handler++) {
  998. res = (*handler)(&tx);
  999. if (res != TX_CONTINUE)
  1000. break;
  1001. }
  1002. skb = tx.skb; /* handlers are allowed to change skb */
  1003. if (unlikely(res == TX_DROP)) {
  1004. I802_DEBUG_INC(local->tx_handlers_drop);
  1005. goto drop;
  1006. }
  1007. if (unlikely(res == TX_QUEUED)) {
  1008. I802_DEBUG_INC(local->tx_handlers_queued);
  1009. rcu_read_unlock();
  1010. return 0;
  1011. }
  1012. if (tx.extra_frag) {
  1013. for (i = 0; i < tx.num_extra_frag; i++) {
  1014. int next_len, dur;
  1015. struct ieee80211_hdr *hdr =
  1016. (struct ieee80211_hdr *)
  1017. tx.extra_frag[i]->data;
  1018. if (i + 1 < tx.num_extra_frag) {
  1019. next_len = tx.extra_frag[i + 1]->len;
  1020. } else {
  1021. next_len = 0;
  1022. tx.rate = tx.last_frag_rate;
  1023. }
  1024. dur = ieee80211_duration(&tx, 0, next_len);
  1025. hdr->duration_id = cpu_to_le16(dur);
  1026. }
  1027. }
  1028. retry:
  1029. ret = __ieee80211_tx(local, skb, &tx);
  1030. if (ret) {
  1031. struct ieee80211_tx_stored_packet *store =
  1032. &local->pending_packet[control->queue];
  1033. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1034. skb = NULL;
  1035. set_bit(IEEE80211_LINK_STATE_PENDING,
  1036. &local->state[control->queue]);
  1037. smp_mb();
  1038. /* When the driver gets out of buffers during sending of
  1039. * fragments and calls ieee80211_stop_queue, there is
  1040. * a small window between IEEE80211_LINK_STATE_XOFF and
  1041. * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
  1042. * gets available in that window (i.e. driver calls
  1043. * ieee80211_wake_queue), we would end up with ieee80211_tx
  1044. * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
  1045. * continuing transmitting here when that situation is
  1046. * possible to have happened. */
  1047. if (!__ieee80211_queue_stopped(local, control->queue)) {
  1048. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1049. &local->state[control->queue]);
  1050. retries++;
  1051. /*
  1052. * Driver bug, it's rejecting packets but
  1053. * not stopping queues.
  1054. */
  1055. if (WARN_ON_ONCE(retries > 5))
  1056. goto drop;
  1057. goto retry;
  1058. }
  1059. memcpy(&store->control, control,
  1060. sizeof(struct ieee80211_tx_control));
  1061. store->skb = skb;
  1062. store->extra_frag = tx.extra_frag;
  1063. store->num_extra_frag = tx.num_extra_frag;
  1064. store->last_frag_rate = tx.last_frag_rate;
  1065. store->last_frag_rate_ctrl_probe =
  1066. !!(tx.flags & IEEE80211_TX_PROBE_LAST_FRAG);
  1067. }
  1068. rcu_read_unlock();
  1069. return 0;
  1070. drop:
  1071. if (skb)
  1072. dev_kfree_skb(skb);
  1073. for (i = 0; i < tx.num_extra_frag; i++)
  1074. if (tx.extra_frag[i])
  1075. dev_kfree_skb(tx.extra_frag[i]);
  1076. kfree(tx.extra_frag);
  1077. rcu_read_unlock();
  1078. return 0;
  1079. }
  1080. /* device xmit handlers */
  1081. int ieee80211_master_start_xmit(struct sk_buff *skb,
  1082. struct net_device *dev)
  1083. {
  1084. struct ieee80211_tx_control control;
  1085. struct ieee80211_tx_packet_data *pkt_data;
  1086. struct net_device *odev = NULL;
  1087. struct ieee80211_sub_if_data *osdata;
  1088. int headroom;
  1089. int ret;
  1090. /*
  1091. * copy control out of the skb so other people can use skb->cb
  1092. */
  1093. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1094. memset(&control, 0, sizeof(struct ieee80211_tx_control));
  1095. if (pkt_data->ifindex)
  1096. odev = dev_get_by_index(&init_net, pkt_data->ifindex);
  1097. if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
  1098. dev_put(odev);
  1099. odev = NULL;
  1100. }
  1101. if (unlikely(!odev)) {
  1102. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1103. printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
  1104. "originating device\n", dev->name);
  1105. #endif
  1106. dev_kfree_skb(skb);
  1107. return 0;
  1108. }
  1109. osdata = IEEE80211_DEV_TO_SUB_IF(odev);
  1110. headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
  1111. if (skb_headroom(skb) < headroom) {
  1112. if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
  1113. dev_kfree_skb(skb);
  1114. dev_put(odev);
  1115. return 0;
  1116. }
  1117. }
  1118. control.vif = &osdata->vif;
  1119. control.type = osdata->vif.type;
  1120. if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
  1121. control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
  1122. if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
  1123. control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  1124. if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
  1125. control.flags |= IEEE80211_TXCTL_REQUEUE;
  1126. if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
  1127. control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
  1128. if (pkt_data->flags & IEEE80211_TXPD_AMPDU)
  1129. control.flags |= IEEE80211_TXCTL_AMPDU;
  1130. control.queue = pkt_data->queue;
  1131. ret = ieee80211_tx(odev, skb, &control);
  1132. dev_put(odev);
  1133. return ret;
  1134. }
  1135. int ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1136. struct net_device *dev)
  1137. {
  1138. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1139. struct ieee80211_tx_packet_data *pkt_data;
  1140. struct ieee80211_radiotap_header *prthdr =
  1141. (struct ieee80211_radiotap_header *)skb->data;
  1142. u16 len_rthdr;
  1143. /* check for not even having the fixed radiotap header part */
  1144. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1145. goto fail; /* too short to be possibly valid */
  1146. /* is it a header version we can trust to find length from? */
  1147. if (unlikely(prthdr->it_version))
  1148. goto fail; /* only version 0 is supported */
  1149. /* then there must be a radiotap header with a length we can use */
  1150. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1151. /* does the skb contain enough to deliver on the alleged length? */
  1152. if (unlikely(skb->len < len_rthdr))
  1153. goto fail; /* skb too short for claimed rt header extent */
  1154. skb->dev = local->mdev;
  1155. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1156. memset(pkt_data, 0, sizeof(*pkt_data));
  1157. /* needed because we set skb device to master */
  1158. pkt_data->ifindex = dev->ifindex;
  1159. pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
  1160. /*
  1161. * fix up the pointers accounting for the radiotap
  1162. * header still being in there. We are being given
  1163. * a precooked IEEE80211 header so no need for
  1164. * normal processing
  1165. */
  1166. skb_set_mac_header(skb, len_rthdr);
  1167. /*
  1168. * these are just fixed to the end of the rt area since we
  1169. * don't have any better information and at this point, nobody cares
  1170. */
  1171. skb_set_network_header(skb, len_rthdr);
  1172. skb_set_transport_header(skb, len_rthdr);
  1173. /* pass the radiotap header up to the next stage intact */
  1174. dev_queue_xmit(skb);
  1175. return NETDEV_TX_OK;
  1176. fail:
  1177. dev_kfree_skb(skb);
  1178. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  1179. }
  1180. /**
  1181. * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
  1182. * subinterfaces (wlan#, WDS, and VLAN interfaces)
  1183. * @skb: packet to be sent
  1184. * @dev: incoming interface
  1185. *
  1186. * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
  1187. * not be freed, and caller is responsible for either retrying later or freeing
  1188. * skb).
  1189. *
  1190. * This function takes in an Ethernet header and encapsulates it with suitable
  1191. * IEEE 802.11 header based on which interface the packet is coming in. The
  1192. * encapsulated packet will then be passed to master interface, wlan#.11, for
  1193. * transmission (through low-level driver).
  1194. */
  1195. int ieee80211_subif_start_xmit(struct sk_buff *skb,
  1196. struct net_device *dev)
  1197. {
  1198. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1199. struct ieee80211_tx_packet_data *pkt_data;
  1200. struct ieee80211_sub_if_data *sdata;
  1201. int ret = 1, head_need;
  1202. u16 ethertype, hdrlen, meshhdrlen = 0, fc;
  1203. struct ieee80211_hdr hdr;
  1204. struct ieee80211s_hdr mesh_hdr;
  1205. const u8 *encaps_data;
  1206. int encaps_len, skip_header_bytes;
  1207. int nh_pos, h_pos;
  1208. struct sta_info *sta;
  1209. u32 sta_flags = 0;
  1210. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1211. if (unlikely(skb->len < ETH_HLEN)) {
  1212. printk(KERN_DEBUG "%s: short skb (len=%d)\n",
  1213. dev->name, skb->len);
  1214. ret = 0;
  1215. goto fail;
  1216. }
  1217. nh_pos = skb_network_header(skb) - skb->data;
  1218. h_pos = skb_transport_header(skb) - skb->data;
  1219. /* convert Ethernet header to proper 802.11 header (based on
  1220. * operation mode) */
  1221. ethertype = (skb->data[12] << 8) | skb->data[13];
  1222. fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
  1223. switch (sdata->vif.type) {
  1224. case IEEE80211_IF_TYPE_AP:
  1225. case IEEE80211_IF_TYPE_VLAN:
  1226. fc |= IEEE80211_FCTL_FROMDS;
  1227. /* DA BSSID SA */
  1228. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1229. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1230. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  1231. hdrlen = 24;
  1232. break;
  1233. case IEEE80211_IF_TYPE_WDS:
  1234. fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
  1235. /* RA TA DA SA */
  1236. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  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. hdrlen = 30;
  1241. break;
  1242. #ifdef CONFIG_MAC80211_MESH
  1243. case IEEE80211_IF_TYPE_MESH_POINT:
  1244. fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
  1245. /* RA TA DA SA */
  1246. if (is_multicast_ether_addr(skb->data))
  1247. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1248. else if (mesh_nexthop_lookup(hdr.addr1, skb, dev))
  1249. return 0;
  1250. memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
  1251. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1252. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  1253. if (skb->pkt_type == PACKET_OTHERHOST) {
  1254. /* Forwarded frame, keep mesh ttl and seqnum */
  1255. struct ieee80211s_hdr *prev_meshhdr;
  1256. prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
  1257. meshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
  1258. memcpy(&mesh_hdr, prev_meshhdr, meshhdrlen);
  1259. sdata->u.sta.mshstats.fwded_frames++;
  1260. } else {
  1261. if (!sdata->u.sta.mshcfg.dot11MeshTTL) {
  1262. /* Do not send frames with mesh_ttl == 0 */
  1263. sdata->u.sta.mshstats.dropped_frames_ttl++;
  1264. ret = 0;
  1265. goto fail;
  1266. }
  1267. meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
  1268. sdata);
  1269. }
  1270. hdrlen = 30;
  1271. break;
  1272. #endif
  1273. case IEEE80211_IF_TYPE_STA:
  1274. fc |= IEEE80211_FCTL_TODS;
  1275. /* BSSID SA DA */
  1276. memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
  1277. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1278. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  1279. hdrlen = 24;
  1280. break;
  1281. case IEEE80211_IF_TYPE_IBSS:
  1282. /* DA SA BSSID */
  1283. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  1284. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  1285. memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
  1286. hdrlen = 24;
  1287. break;
  1288. default:
  1289. ret = 0;
  1290. goto fail;
  1291. }
  1292. /*
  1293. * There's no need to try to look up the destination
  1294. * if it is a multicast address (which can only happen
  1295. * in AP mode)
  1296. */
  1297. if (!is_multicast_ether_addr(hdr.addr1)) {
  1298. rcu_read_lock();
  1299. sta = sta_info_get(local, hdr.addr1);
  1300. if (sta)
  1301. sta_flags = sta->flags;
  1302. rcu_read_unlock();
  1303. }
  1304. /* receiver is QoS enabled, use a QoS type frame */
  1305. if (sta_flags & WLAN_STA_WME) {
  1306. fc |= IEEE80211_STYPE_QOS_DATA;
  1307. hdrlen += 2;
  1308. }
  1309. /*
  1310. * Drop unicast frames to unauthorised stations unless they are
  1311. * EAPOL frames from the local station.
  1312. */
  1313. if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
  1314. !(sta_flags & WLAN_STA_AUTHORIZED) &&
  1315. !(ethertype == ETH_P_PAE &&
  1316. compare_ether_addr(dev->dev_addr,
  1317. skb->data + ETH_ALEN) == 0))) {
  1318. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1319. DECLARE_MAC_BUF(mac);
  1320. if (net_ratelimit())
  1321. printk(KERN_DEBUG "%s: dropped frame to %s"
  1322. " (unauthorized port)\n", dev->name,
  1323. print_mac(mac, hdr.addr1));
  1324. #endif
  1325. I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
  1326. ret = 0;
  1327. goto fail;
  1328. }
  1329. hdr.frame_control = cpu_to_le16(fc);
  1330. hdr.duration_id = 0;
  1331. hdr.seq_ctrl = 0;
  1332. skip_header_bytes = ETH_HLEN;
  1333. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  1334. encaps_data = bridge_tunnel_header;
  1335. encaps_len = sizeof(bridge_tunnel_header);
  1336. skip_header_bytes -= 2;
  1337. } else if (ethertype >= 0x600) {
  1338. encaps_data = rfc1042_header;
  1339. encaps_len = sizeof(rfc1042_header);
  1340. skip_header_bytes -= 2;
  1341. } else {
  1342. encaps_data = NULL;
  1343. encaps_len = 0;
  1344. }
  1345. skb_pull(skb, skip_header_bytes);
  1346. nh_pos -= skip_header_bytes;
  1347. h_pos -= skip_header_bytes;
  1348. /* TODO: implement support for fragments so that there is no need to
  1349. * reallocate and copy payload; it might be enough to support one
  1350. * extra fragment that would be copied in the beginning of the frame
  1351. * data.. anyway, it would be nice to include this into skb structure
  1352. * somehow
  1353. *
  1354. * There are few options for this:
  1355. * use skb->cb as an extra space for 802.11 header
  1356. * allocate new buffer if not enough headroom
  1357. * make sure that there is enough headroom in every skb by increasing
  1358. * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
  1359. * alloc_skb() (net/core/skbuff.c)
  1360. */
  1361. head_need = hdrlen + encaps_len + meshhdrlen + local->tx_headroom;
  1362. head_need -= skb_headroom(skb);
  1363. /* We are going to modify skb data, so make a copy of it if happens to
  1364. * be cloned. This could happen, e.g., with Linux bridge code passing
  1365. * us broadcast frames. */
  1366. if (head_need > 0 || skb_cloned(skb)) {
  1367. #if 0
  1368. printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
  1369. "of headroom\n", dev->name, head_need);
  1370. #endif
  1371. if (skb_cloned(skb))
  1372. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1373. else
  1374. I802_DEBUG_INC(local->tx_expand_skb_head);
  1375. /* Since we have to reallocate the buffer, make sure that there
  1376. * is enough room for possible WEP IV/ICV and TKIP (8 bytes
  1377. * before payload and 12 after). */
  1378. if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
  1379. 12, GFP_ATOMIC)) {
  1380. printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
  1381. "\n", dev->name);
  1382. goto fail;
  1383. }
  1384. }
  1385. if (encaps_data) {
  1386. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  1387. nh_pos += encaps_len;
  1388. h_pos += encaps_len;
  1389. }
  1390. if (meshhdrlen > 0) {
  1391. memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
  1392. nh_pos += meshhdrlen;
  1393. h_pos += meshhdrlen;
  1394. }
  1395. if (fc & IEEE80211_STYPE_QOS_DATA) {
  1396. __le16 *qos_control;
  1397. qos_control = (__le16*) skb_push(skb, 2);
  1398. memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
  1399. /*
  1400. * Maybe we could actually set some fields here, for now just
  1401. * initialise to zero to indicate no special operation.
  1402. */
  1403. *qos_control = 0;
  1404. } else
  1405. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  1406. nh_pos += hdrlen;
  1407. h_pos += hdrlen;
  1408. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1409. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1410. pkt_data->ifindex = dev->ifindex;
  1411. if (ethertype == ETH_P_PAE)
  1412. pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
  1413. skb->dev = local->mdev;
  1414. dev->stats.tx_packets++;
  1415. dev->stats.tx_bytes += skb->len;
  1416. /* Update skb pointers to various headers since this modified frame
  1417. * is going to go through Linux networking code that may potentially
  1418. * need things like pointer to IP header. */
  1419. skb_set_mac_header(skb, 0);
  1420. skb_set_network_header(skb, nh_pos);
  1421. skb_set_transport_header(skb, h_pos);
  1422. dev->trans_start = jiffies;
  1423. dev_queue_xmit(skb);
  1424. return 0;
  1425. fail:
  1426. if (!ret)
  1427. dev_kfree_skb(skb);
  1428. return ret;
  1429. }
  1430. /* helper functions for pending packets for when queues are stopped */
  1431. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  1432. {
  1433. int i, j;
  1434. struct ieee80211_tx_stored_packet *store;
  1435. for (i = 0; i < local->hw.queues; i++) {
  1436. if (!__ieee80211_queue_pending(local, i))
  1437. continue;
  1438. store = &local->pending_packet[i];
  1439. kfree_skb(store->skb);
  1440. for (j = 0; j < store->num_extra_frag; j++)
  1441. kfree_skb(store->extra_frag[j]);
  1442. kfree(store->extra_frag);
  1443. clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
  1444. }
  1445. }
  1446. void ieee80211_tx_pending(unsigned long data)
  1447. {
  1448. struct ieee80211_local *local = (struct ieee80211_local *)data;
  1449. struct net_device *dev = local->mdev;
  1450. struct ieee80211_tx_stored_packet *store;
  1451. struct ieee80211_tx_data tx;
  1452. int i, ret, reschedule = 0;
  1453. netif_tx_lock_bh(dev);
  1454. for (i = 0; i < local->hw.queues; i++) {
  1455. if (__ieee80211_queue_stopped(local, i))
  1456. continue;
  1457. if (!__ieee80211_queue_pending(local, i)) {
  1458. reschedule = 1;
  1459. continue;
  1460. }
  1461. store = &local->pending_packet[i];
  1462. tx.control = &store->control;
  1463. tx.extra_frag = store->extra_frag;
  1464. tx.num_extra_frag = store->num_extra_frag;
  1465. tx.last_frag_rate = store->last_frag_rate;
  1466. tx.flags = 0;
  1467. if (store->last_frag_rate_ctrl_probe)
  1468. tx.flags |= IEEE80211_TX_PROBE_LAST_FRAG;
  1469. ret = __ieee80211_tx(local, store->skb, &tx);
  1470. if (ret) {
  1471. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1472. store->skb = NULL;
  1473. } else {
  1474. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1475. &local->state[i]);
  1476. reschedule = 1;
  1477. }
  1478. }
  1479. netif_tx_unlock_bh(dev);
  1480. if (reschedule) {
  1481. if (!ieee80211_qdisc_installed(dev)) {
  1482. if (!__ieee80211_queue_stopped(local, 0))
  1483. netif_wake_queue(dev);
  1484. } else
  1485. netif_schedule(dev);
  1486. }
  1487. }
  1488. /* functions for drivers to get certain frames */
  1489. static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
  1490. struct ieee80211_if_ap *bss,
  1491. struct sk_buff *skb,
  1492. struct beacon_data *beacon)
  1493. {
  1494. u8 *pos, *tim;
  1495. int aid0 = 0;
  1496. int i, have_bits = 0, n1, n2;
  1497. /* Generate bitmap for TIM only if there are any STAs in power save
  1498. * mode. */
  1499. if (atomic_read(&bss->num_sta_ps) > 0)
  1500. /* in the hope that this is faster than
  1501. * checking byte-for-byte */
  1502. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1503. IEEE80211_MAX_AID+1);
  1504. if (bss->dtim_count == 0)
  1505. bss->dtim_count = beacon->dtim_period - 1;
  1506. else
  1507. bss->dtim_count--;
  1508. tim = pos = (u8 *) skb_put(skb, 6);
  1509. *pos++ = WLAN_EID_TIM;
  1510. *pos++ = 4;
  1511. *pos++ = bss->dtim_count;
  1512. *pos++ = beacon->dtim_period;
  1513. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1514. aid0 = 1;
  1515. if (have_bits) {
  1516. /* Find largest even number N1 so that bits numbered 1 through
  1517. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1518. * (N2 + 1) x 8 through 2007 are 0. */
  1519. n1 = 0;
  1520. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1521. if (bss->tim[i]) {
  1522. n1 = i & 0xfe;
  1523. break;
  1524. }
  1525. }
  1526. n2 = n1;
  1527. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1528. if (bss->tim[i]) {
  1529. n2 = i;
  1530. break;
  1531. }
  1532. }
  1533. /* Bitmap control */
  1534. *pos++ = n1 | aid0;
  1535. /* Part Virt Bitmap */
  1536. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1537. tim[1] = n2 - n1 + 4;
  1538. skb_put(skb, n2 - n1);
  1539. } else {
  1540. *pos++ = aid0; /* Bitmap control */
  1541. *pos++ = 0; /* Part Virt Bitmap */
  1542. }
  1543. }
  1544. struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
  1545. struct ieee80211_vif *vif,
  1546. struct ieee80211_tx_control *control)
  1547. {
  1548. struct ieee80211_local *local = hw_to_local(hw);
  1549. struct sk_buff *skb;
  1550. struct net_device *bdev;
  1551. struct ieee80211_sub_if_data *sdata = NULL;
  1552. struct ieee80211_if_ap *ap = NULL;
  1553. struct rate_selection rsel;
  1554. struct beacon_data *beacon;
  1555. struct ieee80211_supported_band *sband;
  1556. struct ieee80211_mgmt *mgmt;
  1557. int *num_beacons;
  1558. bool err = true;
  1559. u8 *pos;
  1560. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1561. rcu_read_lock();
  1562. sdata = vif_to_sdata(vif);
  1563. bdev = sdata->dev;
  1564. if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
  1565. ap = &sdata->u.ap;
  1566. beacon = rcu_dereference(ap->beacon);
  1567. if (ap && beacon) {
  1568. /*
  1569. * headroom, head length,
  1570. * tail length and maximum TIM length
  1571. */
  1572. skb = dev_alloc_skb(local->tx_headroom +
  1573. beacon->head_len +
  1574. beacon->tail_len + 256);
  1575. if (!skb)
  1576. goto out;
  1577. skb_reserve(skb, local->tx_headroom);
  1578. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  1579. beacon->head_len);
  1580. ieee80211_include_sequence(sdata,
  1581. (struct ieee80211_hdr *)skb->data);
  1582. /*
  1583. * Not very nice, but we want to allow the driver to call
  1584. * ieee80211_beacon_get() as a response to the set_tim()
  1585. * callback. That, however, is already invoked under the
  1586. * sta_lock to guarantee consistent and race-free update
  1587. * of the tim bitmap in mac80211 and the driver.
  1588. */
  1589. if (local->tim_in_locked_section) {
  1590. ieee80211_beacon_add_tim(local, ap, skb, beacon);
  1591. } else {
  1592. unsigned long flags;
  1593. spin_lock_irqsave(&local->sta_lock, flags);
  1594. ieee80211_beacon_add_tim(local, ap, skb, beacon);
  1595. spin_unlock_irqrestore(&local->sta_lock, flags);
  1596. }
  1597. if (beacon->tail)
  1598. memcpy(skb_put(skb, beacon->tail_len),
  1599. beacon->tail, beacon->tail_len);
  1600. num_beacons = &ap->num_beacons;
  1601. err = false;
  1602. }
  1603. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1604. /* headroom, head length, tail length and maximum TIM length */
  1605. skb = dev_alloc_skb(local->tx_headroom + 400);
  1606. if (!skb)
  1607. goto out;
  1608. skb_reserve(skb, local->hw.extra_tx_headroom);
  1609. mgmt = (struct ieee80211_mgmt *)
  1610. skb_put(skb, 24 + sizeof(mgmt->u.beacon));
  1611. memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  1612. mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
  1613. IEEE80211_STYPE_BEACON);
  1614. memset(mgmt->da, 0xff, ETH_ALEN);
  1615. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  1616. /* BSSID is left zeroed, wildcard value */
  1617. mgmt->u.beacon.beacon_int =
  1618. cpu_to_le16(local->hw.conf.beacon_int);
  1619. mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
  1620. pos = skb_put(skb, 2);
  1621. *pos++ = WLAN_EID_SSID;
  1622. *pos++ = 0x0;
  1623. mesh_mgmt_ies_add(skb, sdata->dev);
  1624. num_beacons = &sdata->u.sta.num_beacons;
  1625. err = false;
  1626. }
  1627. if (err) {
  1628. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1629. if (net_ratelimit())
  1630. printk(KERN_DEBUG "no beacon data avail for %s\n",
  1631. bdev->name);
  1632. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1633. skb = NULL;
  1634. goto out;
  1635. }
  1636. if (control) {
  1637. rate_control_get_rate(local->mdev, sband, skb, &rsel);
  1638. if (!rsel.rate) {
  1639. if (net_ratelimit()) {
  1640. printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
  1641. "no rate found\n",
  1642. wiphy_name(local->hw.wiphy));
  1643. }
  1644. dev_kfree_skb(skb);
  1645. skb = NULL;
  1646. goto out;
  1647. }
  1648. control->vif = vif;
  1649. control->tx_rate = rsel.rate;
  1650. if (sdata->bss_conf.use_short_preamble &&
  1651. rsel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
  1652. control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
  1653. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  1654. control->flags |= IEEE80211_TXCTL_NO_ACK;
  1655. control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  1656. control->retry_limit = 1;
  1657. control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
  1658. }
  1659. (*num_beacons)++;
  1660. out:
  1661. rcu_read_unlock();
  1662. return skb;
  1663. }
  1664. EXPORT_SYMBOL(ieee80211_beacon_get);
  1665. void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1666. const void *frame, size_t frame_len,
  1667. const struct ieee80211_tx_control *frame_txctl,
  1668. struct ieee80211_rts *rts)
  1669. {
  1670. const struct ieee80211_hdr *hdr = frame;
  1671. u16 fctl;
  1672. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
  1673. rts->frame_control = cpu_to_le16(fctl);
  1674. rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
  1675. frame_txctl);
  1676. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  1677. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  1678. }
  1679. EXPORT_SYMBOL(ieee80211_rts_get);
  1680. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1681. const void *frame, size_t frame_len,
  1682. const struct ieee80211_tx_control *frame_txctl,
  1683. struct ieee80211_cts *cts)
  1684. {
  1685. const struct ieee80211_hdr *hdr = frame;
  1686. u16 fctl;
  1687. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
  1688. cts->frame_control = cpu_to_le16(fctl);
  1689. cts->duration = ieee80211_ctstoself_duration(hw, vif,
  1690. frame_len, frame_txctl);
  1691. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  1692. }
  1693. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  1694. struct sk_buff *
  1695. ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
  1696. struct ieee80211_vif *vif,
  1697. struct ieee80211_tx_control *control)
  1698. {
  1699. struct ieee80211_local *local = hw_to_local(hw);
  1700. struct sk_buff *skb;
  1701. struct sta_info *sta;
  1702. ieee80211_tx_handler *handler;
  1703. struct ieee80211_tx_data tx;
  1704. ieee80211_tx_result res = TX_DROP;
  1705. struct net_device *bdev;
  1706. struct ieee80211_sub_if_data *sdata;
  1707. struct ieee80211_if_ap *bss = NULL;
  1708. struct beacon_data *beacon;
  1709. sdata = vif_to_sdata(vif);
  1710. bdev = sdata->dev;
  1711. if (!bss)
  1712. return NULL;
  1713. rcu_read_lock();
  1714. beacon = rcu_dereference(bss->beacon);
  1715. if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon ||
  1716. !beacon->head) {
  1717. rcu_read_unlock();
  1718. return NULL;
  1719. }
  1720. if (bss->dtim_count != 0)
  1721. return NULL; /* send buffered bc/mc only after DTIM beacon */
  1722. memset(control, 0, sizeof(*control));
  1723. while (1) {
  1724. skb = skb_dequeue(&bss->ps_bc_buf);
  1725. if (!skb)
  1726. return NULL;
  1727. local->total_ps_buffered--;
  1728. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  1729. struct ieee80211_hdr *hdr =
  1730. (struct ieee80211_hdr *) skb->data;
  1731. /* more buffered multicast/broadcast frames ==> set
  1732. * MoreData flag in IEEE 802.11 header to inform PS
  1733. * STAs */
  1734. hdr->frame_control |=
  1735. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1736. }
  1737. if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
  1738. break;
  1739. dev_kfree_skb_any(skb);
  1740. }
  1741. sta = tx.sta;
  1742. tx.flags |= IEEE80211_TX_PS_BUFFERED;
  1743. tx.channel = local->hw.conf.channel;
  1744. for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
  1745. res = (*handler)(&tx);
  1746. if (res == TX_DROP || res == TX_QUEUED)
  1747. break;
  1748. }
  1749. skb = tx.skb; /* handlers are allowed to change skb */
  1750. if (res == TX_DROP) {
  1751. I802_DEBUG_INC(local->tx_handlers_drop);
  1752. dev_kfree_skb(skb);
  1753. skb = NULL;
  1754. } else if (res == TX_QUEUED) {
  1755. I802_DEBUG_INC(local->tx_handlers_queued);
  1756. skb = NULL;
  1757. }
  1758. rcu_read_unlock();
  1759. return skb;
  1760. }
  1761. EXPORT_SYMBOL(ieee80211_get_buffered_bc);