tx.c 57 KB

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