tx.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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 sk_buff *skb = tx->skb;
  213. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  214. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  215. u32 sta_flags;
  216. if (unlikely(tx->flags & IEEE80211_TX_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. /*
  306. * broadcast/multicast frame
  307. *
  308. * If any of the associated stations is in power save mode,
  309. * the frame is buffered to be sent after DTIM beacon frame.
  310. * This is done either by the hardware or us.
  311. */
  312. /* not AP/IBSS or ordered frame */
  313. if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
  314. return TX_CONTINUE;
  315. /* no stations in PS mode */
  316. if (!atomic_read(&tx->sdata->bss->num_sta_ps))
  317. return TX_CONTINUE;
  318. /* buffered in mac80211 */
  319. if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
  320. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  321. purge_old_ps_buffers(tx->local);
  322. if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
  323. AP_MAX_BC_BUFFER) {
  324. if (net_ratelimit()) {
  325. printk(KERN_DEBUG "%s: BC TX buffer full - "
  326. "dropping the oldest frame\n",
  327. tx->dev->name);
  328. }
  329. dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
  330. } else
  331. tx->local->total_ps_buffered++;
  332. skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
  333. return TX_QUEUED;
  334. }
  335. /* buffered in hardware */
  336. tx->control->flags |= IEEE80211_TXCTL_SEND_AFTER_DTIM;
  337. return TX_CONTINUE;
  338. }
  339. static ieee80211_tx_result
  340. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
  341. {
  342. struct sta_info *sta = tx->sta;
  343. u32 staflags;
  344. DECLARE_MAC_BUF(mac);
  345. if (unlikely(!sta ||
  346. ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
  347. (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
  348. return TX_CONTINUE;
  349. staflags = get_sta_flags(sta);
  350. if (unlikely((staflags & WLAN_STA_PS) &&
  351. !(staflags & WLAN_STA_PSPOLL))) {
  352. struct ieee80211_tx_packet_data *pkt_data;
  353. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  354. printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
  355. "before %d)\n",
  356. print_mac(mac, sta->addr), sta->aid,
  357. skb_queue_len(&sta->ps_tx_buf));
  358. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  359. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  360. purge_old_ps_buffers(tx->local);
  361. if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
  362. struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
  363. if (net_ratelimit()) {
  364. printk(KERN_DEBUG "%s: STA %s TX "
  365. "buffer full - dropping oldest frame\n",
  366. tx->dev->name, print_mac(mac, sta->addr));
  367. }
  368. dev_kfree_skb(old);
  369. } else
  370. tx->local->total_ps_buffered++;
  371. /* Queue frame to be sent after STA sends an PS Poll frame */
  372. if (skb_queue_empty(&sta->ps_tx_buf))
  373. sta_info_set_tim_bit(sta);
  374. pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
  375. pkt_data->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. u16 fc = tx->fc;
  404. if (unlikely(tx->control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
  405. tx->key = NULL;
  406. else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
  407. tx->key = key;
  408. else if ((key = rcu_dereference(tx->sdata->default_key)))
  409. tx->key = key;
  410. else if (tx->sdata->drop_unencrypted &&
  411. !(tx->control->flags & IEEE80211_TXCTL_EAPOL_FRAME) &&
  412. !(tx->flags & IEEE80211_TX_INJECTED)) {
  413. I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
  414. return TX_DROP;
  415. } else
  416. tx->key = NULL;
  417. if (tx->key) {
  418. u16 ftype, stype;
  419. tx->key->tx_rx_count++;
  420. /* TODO: add threshold stuff again */
  421. switch (tx->key->conf.alg) {
  422. case ALG_WEP:
  423. ftype = fc & IEEE80211_FCTL_FTYPE;
  424. stype = fc & IEEE80211_FCTL_STYPE;
  425. if (ftype == IEEE80211_FTYPE_MGMT &&
  426. stype == IEEE80211_STYPE_AUTH)
  427. break;
  428. case ALG_TKIP:
  429. case ALG_CCMP:
  430. if (!WLAN_FC_DATA_PRESENT(fc))
  431. tx->key = NULL;
  432. break;
  433. }
  434. }
  435. if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  436. tx->control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  437. return TX_CONTINUE;
  438. }
  439. static ieee80211_tx_result
  440. ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
  441. {
  442. struct rate_selection rsel;
  443. struct ieee80211_supported_band *sband;
  444. sband = tx->local->hw.wiphy->bands[tx->channel->band];
  445. if (likely(tx->rate_idx < 0)) {
  446. rate_control_get_rate(tx->dev, sband, tx->skb, &rsel);
  447. tx->rate_idx = rsel.rate_idx;
  448. if (unlikely(rsel.probe_idx >= 0)) {
  449. tx->control->flags |=
  450. IEEE80211_TXCTL_RATE_CTRL_PROBE;
  451. tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
  452. tx->control->alt_retry_rate_idx = tx->rate_idx;
  453. tx->rate_idx = rsel.probe_idx;
  454. } else
  455. tx->control->alt_retry_rate_idx = -1;
  456. if (unlikely(tx->rate_idx < 0))
  457. return TX_DROP;
  458. } else
  459. tx->control->alt_retry_rate_idx = -1;
  460. if (tx->sdata->bss_conf.use_cts_prot &&
  461. (tx->flags & IEEE80211_TX_FRAGMENTED) && (rsel.nonerp_idx >= 0)) {
  462. tx->last_frag_rate_idx = tx->rate_idx;
  463. if (rsel.probe_idx >= 0)
  464. tx->flags &= ~IEEE80211_TX_PROBE_LAST_FRAG;
  465. else
  466. tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
  467. tx->rate_idx = rsel.nonerp_idx;
  468. tx->control->tx_rate_idx = rsel.nonerp_idx;
  469. tx->control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  470. } else {
  471. tx->last_frag_rate_idx = tx->rate_idx;
  472. tx->control->tx_rate_idx = tx->rate_idx;
  473. }
  474. tx->control->tx_rate_idx = tx->rate_idx;
  475. return TX_CONTINUE;
  476. }
  477. static ieee80211_tx_result
  478. ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
  479. {
  480. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  481. u16 fc = le16_to_cpu(hdr->frame_control);
  482. u16 dur;
  483. struct ieee80211_tx_control *control = tx->control;
  484. struct ieee80211_supported_band *sband;
  485. sband = tx->local->hw.wiphy->bands[tx->channel->band];
  486. if (!control->retry_limit) {
  487. if (!is_multicast_ether_addr(hdr->addr1)) {
  488. if (tx->skb->len + FCS_LEN > tx->local->rts_threshold
  489. && tx->local->rts_threshold <
  490. IEEE80211_MAX_RTS_THRESHOLD) {
  491. control->flags |=
  492. IEEE80211_TXCTL_USE_RTS_CTS;
  493. control->flags |=
  494. IEEE80211_TXCTL_LONG_RETRY_LIMIT;
  495. control->retry_limit =
  496. tx->local->long_retry_limit;
  497. } else {
  498. control->retry_limit =
  499. tx->local->short_retry_limit;
  500. }
  501. } else {
  502. control->retry_limit = 1;
  503. }
  504. }
  505. if (tx->flags & IEEE80211_TX_FRAGMENTED) {
  506. /* Do not use multiple retry rates when sending fragmented
  507. * frames.
  508. * TODO: The last fragment could still use multiple retry
  509. * rates. */
  510. control->alt_retry_rate_idx = -1;
  511. }
  512. /* Use CTS protection for unicast frames sent using extended rates if
  513. * there are associated non-ERP stations and RTS/CTS is not configured
  514. * for the frame. */
  515. if ((tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE) &&
  516. (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_ERP_G) &&
  517. (tx->flags & IEEE80211_TX_UNICAST) &&
  518. tx->sdata->bss_conf.use_cts_prot &&
  519. !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
  520. control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
  521. /* Transmit data frames using short preambles if the driver supports
  522. * short preambles at the selected rate and short preambles are
  523. * available on the network at the current point in time. */
  524. if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  525. (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
  526. tx->sdata->bss_conf.use_short_preamble &&
  527. (!tx->sta || test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))) {
  528. tx->control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
  529. }
  530. /* Setup duration field for the first fragment of the frame. Duration
  531. * for remaining fragments will be updated when they are being sent
  532. * to low-level driver in ieee80211_tx(). */
  533. dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
  534. (tx->flags & IEEE80211_TX_FRAGMENTED) ?
  535. tx->extra_frag[0]->len : 0);
  536. hdr->duration_id = cpu_to_le16(dur);
  537. if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
  538. (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
  539. struct ieee80211_supported_band *sband;
  540. struct ieee80211_rate *rate;
  541. s8 baserate = -1;
  542. int idx;
  543. sband = tx->local->hw.wiphy->bands[tx->channel->band];
  544. /* Do not use multiple retry rates when using RTS/CTS */
  545. control->alt_retry_rate_idx = -1;
  546. /* Use min(data rate, max base rate) as CTS/RTS rate */
  547. rate = &sband->bitrates[tx->rate_idx];
  548. for (idx = 0; idx < sband->n_bitrates; idx++) {
  549. if (sband->bitrates[idx].bitrate > rate->bitrate)
  550. continue;
  551. if (tx->sdata->basic_rates & BIT(idx) &&
  552. (baserate < 0 ||
  553. (sband->bitrates[baserate].bitrate
  554. < sband->bitrates[idx].bitrate)))
  555. baserate = idx;
  556. }
  557. if (baserate >= 0)
  558. control->rts_cts_rate_idx = baserate;
  559. else
  560. control->rts_cts_rate_idx = 0;
  561. }
  562. if (tx->sta)
  563. control->aid = tx->sta->aid;
  564. return TX_CONTINUE;
  565. }
  566. static ieee80211_tx_result
  567. ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
  568. {
  569. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  570. size_t hdrlen, per_fragm, num_fragm, payload_len, left;
  571. struct sk_buff **frags, *first, *frag;
  572. int i;
  573. u16 seq;
  574. u8 *pos;
  575. int frag_threshold = tx->local->fragmentation_threshold;
  576. if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
  577. return TX_CONTINUE;
  578. first = tx->skb;
  579. hdrlen = ieee80211_get_hdrlen(tx->fc);
  580. payload_len = first->len - hdrlen;
  581. per_fragm = frag_threshold - hdrlen - FCS_LEN;
  582. num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
  583. frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
  584. if (!frags)
  585. goto fail;
  586. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  587. seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
  588. pos = first->data + hdrlen + per_fragm;
  589. left = payload_len - per_fragm;
  590. for (i = 0; i < num_fragm - 1; i++) {
  591. struct ieee80211_hdr *fhdr;
  592. size_t copylen;
  593. if (left <= 0)
  594. goto fail;
  595. /* reserve enough extra head and tail room for possible
  596. * encryption */
  597. frag = frags[i] =
  598. dev_alloc_skb(tx->local->tx_headroom +
  599. frag_threshold +
  600. IEEE80211_ENCRYPT_HEADROOM +
  601. IEEE80211_ENCRYPT_TAILROOM);
  602. if (!frag)
  603. goto fail;
  604. /* Make sure that all fragments use the same priority so
  605. * that they end up using the same TX queue */
  606. frag->priority = first->priority;
  607. skb_reserve(frag, tx->local->tx_headroom +
  608. IEEE80211_ENCRYPT_HEADROOM);
  609. fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
  610. memcpy(fhdr, first->data, hdrlen);
  611. if (i == num_fragm - 2)
  612. fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
  613. fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
  614. copylen = left > per_fragm ? per_fragm : left;
  615. memcpy(skb_put(frag, copylen), pos, copylen);
  616. pos += copylen;
  617. left -= copylen;
  618. }
  619. skb_trim(first, hdrlen + per_fragm);
  620. tx->num_extra_frag = num_fragm - 1;
  621. tx->extra_frag = frags;
  622. return TX_CONTINUE;
  623. fail:
  624. printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
  625. if (frags) {
  626. for (i = 0; i < num_fragm - 1; i++)
  627. if (frags[i])
  628. dev_kfree_skb(frags[i]);
  629. kfree(frags);
  630. }
  631. I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
  632. return TX_DROP;
  633. }
  634. static ieee80211_tx_result
  635. ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
  636. {
  637. if (!tx->key)
  638. return TX_CONTINUE;
  639. switch (tx->key->conf.alg) {
  640. case ALG_WEP:
  641. return ieee80211_crypto_wep_encrypt(tx);
  642. case ALG_TKIP:
  643. return ieee80211_crypto_tkip_encrypt(tx);
  644. case ALG_CCMP:
  645. return ieee80211_crypto_ccmp_encrypt(tx);
  646. }
  647. /* not reached */
  648. WARN_ON(1);
  649. return TX_DROP;
  650. }
  651. static ieee80211_tx_result
  652. ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
  653. {
  654. struct ieee80211_local *local = tx->local;
  655. struct sk_buff *skb = tx->skb;
  656. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  657. u32 load = 0, hdrtime;
  658. struct ieee80211_rate *rate;
  659. struct ieee80211_supported_band *sband;
  660. sband = tx->local->hw.wiphy->bands[tx->channel->band];
  661. rate = &sband->bitrates[tx->rate_idx];
  662. /* TODO: this could be part of tx_status handling, so that the number
  663. * of retries would be known; TX rate should in that case be stored
  664. * somewhere with the packet */
  665. /* Estimate total channel use caused by this frame */
  666. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  667. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  668. if (tx->channel->band == IEEE80211_BAND_5GHZ ||
  669. (tx->channel->band == IEEE80211_BAND_2GHZ &&
  670. rate->flags & IEEE80211_RATE_ERP_G))
  671. hdrtime = CHAN_UTIL_HDR_SHORT;
  672. else
  673. hdrtime = CHAN_UTIL_HDR_LONG;
  674. load = hdrtime;
  675. if (!is_multicast_ether_addr(hdr->addr1))
  676. load += hdrtime;
  677. if (tx->control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  678. load += 2 * hdrtime;
  679. else if (tx->control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  680. load += hdrtime;
  681. /* TODO: optimise again */
  682. load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
  683. if (tx->extra_frag) {
  684. int i;
  685. for (i = 0; i < tx->num_extra_frag; i++) {
  686. load += 2 * hdrtime;
  687. load += tx->extra_frag[i]->len *
  688. rate->bitrate;
  689. }
  690. }
  691. /* Divide channel_use by 8 to avoid wrapping around the counter */
  692. load >>= CHAN_UTIL_SHIFT;
  693. local->channel_use_raw += load;
  694. if (tx->sta)
  695. tx->sta->channel_use_raw += load;
  696. tx->sdata->channel_use_raw += load;
  697. if (tx->sta) {
  698. tx->sta->tx_packets++;
  699. tx->sta->tx_fragments++;
  700. tx->sta->tx_bytes += tx->skb->len;
  701. if (tx->extra_frag) {
  702. int i;
  703. tx->sta->tx_fragments += tx->num_extra_frag;
  704. for (i = 0; i < tx->num_extra_frag; i++) {
  705. tx->sta->tx_bytes +=
  706. tx->extra_frag[i]->len;
  707. }
  708. }
  709. }
  710. return TX_CONTINUE;
  711. }
  712. typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_tx_data *);
  713. static ieee80211_tx_handler ieee80211_tx_handlers[] =
  714. {
  715. ieee80211_tx_h_check_assoc,
  716. ieee80211_tx_h_sequence,
  717. ieee80211_tx_h_ps_buf,
  718. ieee80211_tx_h_select_key,
  719. ieee80211_tx_h_michael_mic_add,
  720. ieee80211_tx_h_rate_ctrl,
  721. ieee80211_tx_h_misc,
  722. ieee80211_tx_h_fragment,
  723. ieee80211_tx_h_encrypt,
  724. ieee80211_tx_h_stats,
  725. NULL
  726. };
  727. /* actual transmit path */
  728. /*
  729. * deal with packet injection down monitor interface
  730. * with Radiotap Header -- only called for monitor mode interface
  731. */
  732. static ieee80211_tx_result
  733. __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
  734. struct sk_buff *skb)
  735. {
  736. /*
  737. * this is the moment to interpret and discard the radiotap header that
  738. * must be at the start of the packet injected in Monitor mode
  739. *
  740. * Need to take some care with endian-ness since radiotap
  741. * args are little-endian
  742. */
  743. struct ieee80211_radiotap_iterator iterator;
  744. struct ieee80211_radiotap_header *rthdr =
  745. (struct ieee80211_radiotap_header *) skb->data;
  746. struct ieee80211_supported_band *sband;
  747. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
  748. struct ieee80211_tx_control *control = tx->control;
  749. sband = tx->local->hw.wiphy->bands[tx->channel->band];
  750. control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  751. tx->flags |= IEEE80211_TX_INJECTED;
  752. tx->flags &= ~IEEE80211_TX_FRAGMENTED;
  753. /*
  754. * for every radiotap entry that is present
  755. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  756. * entries present, or -EINVAL on error)
  757. */
  758. while (!ret) {
  759. int i, target_rate;
  760. ret = ieee80211_radiotap_iterator_next(&iterator);
  761. if (ret)
  762. continue;
  763. /* see if this argument is something we can use */
  764. switch (iterator.this_arg_index) {
  765. /*
  766. * You must take care when dereferencing iterator.this_arg
  767. * for multibyte types... the pointer is not aligned. Use
  768. * get_unaligned((type *)iterator.this_arg) to dereference
  769. * iterator.this_arg for type "type" safely on all arches.
  770. */
  771. case IEEE80211_RADIOTAP_RATE:
  772. /*
  773. * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
  774. * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
  775. */
  776. target_rate = (*iterator.this_arg) * 5;
  777. for (i = 0; i < sband->n_bitrates; i++) {
  778. struct ieee80211_rate *r;
  779. r = &sband->bitrates[i];
  780. if (r->bitrate == target_rate) {
  781. tx->rate_idx = i;
  782. break;
  783. }
  784. }
  785. break;
  786. case IEEE80211_RADIOTAP_ANTENNA:
  787. /*
  788. * radiotap uses 0 for 1st ant, mac80211 is 1 for
  789. * 1st ant
  790. */
  791. control->antenna_sel_tx = (*iterator.this_arg) + 1;
  792. break;
  793. #if 0
  794. case IEEE80211_RADIOTAP_DBM_TX_POWER:
  795. control->power_level = *iterator.this_arg;
  796. break;
  797. #endif
  798. case IEEE80211_RADIOTAP_FLAGS:
  799. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  800. /*
  801. * this indicates that the skb we have been
  802. * handed has the 32-bit FCS CRC at the end...
  803. * we should react to that by snipping it off
  804. * because it will be recomputed and added
  805. * on transmission
  806. */
  807. if (skb->len < (iterator.max_length + FCS_LEN))
  808. return TX_DROP;
  809. skb_trim(skb, skb->len - FCS_LEN);
  810. }
  811. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
  812. control->flags &=
  813. ~IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  814. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
  815. tx->flags |= IEEE80211_TX_FRAGMENTED;
  816. break;
  817. /*
  818. * Please update the file
  819. * Documentation/networking/mac80211-injection.txt
  820. * when parsing new fields here.
  821. */
  822. default:
  823. break;
  824. }
  825. }
  826. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  827. return TX_DROP;
  828. /*
  829. * remove the radiotap header
  830. * iterator->max_length was sanity-checked against
  831. * skb->len by iterator init
  832. */
  833. skb_pull(skb, iterator.max_length);
  834. return TX_CONTINUE;
  835. }
  836. /*
  837. * initialises @tx
  838. */
  839. static ieee80211_tx_result
  840. __ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
  841. struct sk_buff *skb,
  842. struct net_device *dev,
  843. struct ieee80211_tx_control *control)
  844. {
  845. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  846. struct ieee80211_hdr *hdr;
  847. struct ieee80211_sub_if_data *sdata;
  848. int hdrlen;
  849. memset(tx, 0, sizeof(*tx));
  850. tx->skb = skb;
  851. tx->dev = dev; /* use original interface */
  852. tx->local = local;
  853. tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  854. tx->control = control;
  855. /*
  856. * Set this flag (used below to indicate "automatic fragmentation"),
  857. * it will be cleared/left by radiotap as desired.
  858. */
  859. tx->flags |= IEEE80211_TX_FRAGMENTED;
  860. /* process and remove the injection radiotap header */
  861. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  862. if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
  863. if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
  864. return TX_DROP;
  865. /*
  866. * __ieee80211_parse_tx_radiotap has now removed
  867. * the radiotap header that was present and pre-filled
  868. * 'tx' with tx control information.
  869. */
  870. }
  871. hdr = (struct ieee80211_hdr *) skb->data;
  872. tx->sta = sta_info_get(local, hdr->addr1);
  873. tx->fc = le16_to_cpu(hdr->frame_control);
  874. if (is_multicast_ether_addr(hdr->addr1)) {
  875. tx->flags &= ~IEEE80211_TX_UNICAST;
  876. control->flags |= IEEE80211_TXCTL_NO_ACK;
  877. } else {
  878. tx->flags |= IEEE80211_TX_UNICAST;
  879. control->flags &= ~IEEE80211_TXCTL_NO_ACK;
  880. }
  881. if (tx->flags & IEEE80211_TX_FRAGMENTED) {
  882. if ((tx->flags & IEEE80211_TX_UNICAST) &&
  883. skb->len + FCS_LEN > local->fragmentation_threshold &&
  884. !local->ops->set_frag_threshold)
  885. tx->flags |= IEEE80211_TX_FRAGMENTED;
  886. else
  887. tx->flags &= ~IEEE80211_TX_FRAGMENTED;
  888. }
  889. if (!tx->sta)
  890. control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
  891. else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
  892. control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
  893. hdrlen = ieee80211_get_hdrlen(tx->fc);
  894. if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
  895. u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
  896. tx->ethertype = (pos[0] << 8) | pos[1];
  897. }
  898. control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
  899. return TX_CONTINUE;
  900. }
  901. /*
  902. * NB: @tx is uninitialised when passed in here
  903. */
  904. static int ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
  905. struct sk_buff *skb,
  906. struct net_device *mdev,
  907. struct ieee80211_tx_control *control)
  908. {
  909. struct ieee80211_tx_packet_data *pkt_data;
  910. struct net_device *dev;
  911. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  912. dev = dev_get_by_index(&init_net, pkt_data->ifindex);
  913. if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
  914. dev_put(dev);
  915. dev = NULL;
  916. }
  917. if (unlikely(!dev))
  918. return -ENODEV;
  919. /* initialises tx with control */
  920. __ieee80211_tx_prepare(tx, skb, dev, control);
  921. dev_put(dev);
  922. return 0;
  923. }
  924. static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
  925. struct ieee80211_tx_data *tx)
  926. {
  927. struct ieee80211_tx_control *control = tx->control;
  928. int ret, i;
  929. if (!ieee80211_qdisc_installed(local->mdev) &&
  930. __ieee80211_queue_stopped(local, 0)) {
  931. netif_stop_queue(local->mdev);
  932. return IEEE80211_TX_AGAIN;
  933. }
  934. if (skb) {
  935. ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
  936. "TX to low-level driver", skb);
  937. ret = local->ops->tx(local_to_hw(local), skb, control);
  938. if (ret)
  939. return IEEE80211_TX_AGAIN;
  940. local->mdev->trans_start = jiffies;
  941. ieee80211_led_tx(local, 1);
  942. }
  943. if (tx->extra_frag) {
  944. control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
  945. IEEE80211_TXCTL_USE_CTS_PROTECT |
  946. IEEE80211_TXCTL_CLEAR_PS_FILT |
  947. IEEE80211_TXCTL_FIRST_FRAGMENT);
  948. for (i = 0; i < tx->num_extra_frag; i++) {
  949. if (!tx->extra_frag[i])
  950. continue;
  951. if (__ieee80211_queue_stopped(local, control->queue))
  952. return IEEE80211_TX_FRAG_AGAIN;
  953. if (i == tx->num_extra_frag) {
  954. control->tx_rate_idx = tx->last_frag_rate_idx;
  955. if (tx->flags & IEEE80211_TX_PROBE_LAST_FRAG)
  956. control->flags |=
  957. IEEE80211_TXCTL_RATE_CTRL_PROBE;
  958. else
  959. control->flags &=
  960. ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
  961. }
  962. ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
  963. "TX to low-level driver",
  964. tx->extra_frag[i]);
  965. ret = local->ops->tx(local_to_hw(local),
  966. tx->extra_frag[i],
  967. control);
  968. if (ret)
  969. return IEEE80211_TX_FRAG_AGAIN;
  970. local->mdev->trans_start = jiffies;
  971. ieee80211_led_tx(local, 1);
  972. tx->extra_frag[i] = NULL;
  973. }
  974. kfree(tx->extra_frag);
  975. tx->extra_frag = NULL;
  976. }
  977. return IEEE80211_TX_OK;
  978. }
  979. static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
  980. struct ieee80211_tx_control *control)
  981. {
  982. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  983. struct sta_info *sta;
  984. ieee80211_tx_handler *handler;
  985. struct ieee80211_tx_data tx;
  986. ieee80211_tx_result res = TX_DROP, res_prepare;
  987. int ret, i;
  988. WARN_ON(__ieee80211_queue_pending(local, control->queue));
  989. if (unlikely(skb->len < 10)) {
  990. dev_kfree_skb(skb);
  991. return 0;
  992. }
  993. rcu_read_lock();
  994. /* initialises tx */
  995. res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
  996. if (res_prepare == TX_DROP) {
  997. dev_kfree_skb(skb);
  998. rcu_read_unlock();
  999. return 0;
  1000. }
  1001. sta = tx.sta;
  1002. tx.channel = local->hw.conf.channel;
  1003. control->band = tx.channel->band;
  1004. for (handler = ieee80211_tx_handlers; *handler != NULL;
  1005. handler++) {
  1006. res = (*handler)(&tx);
  1007. if (res != TX_CONTINUE)
  1008. break;
  1009. }
  1010. skb = tx.skb; /* handlers are allowed to change skb */
  1011. if (unlikely(res == TX_DROP)) {
  1012. I802_DEBUG_INC(local->tx_handlers_drop);
  1013. goto drop;
  1014. }
  1015. if (unlikely(res == TX_QUEUED)) {
  1016. I802_DEBUG_INC(local->tx_handlers_queued);
  1017. rcu_read_unlock();
  1018. return 0;
  1019. }
  1020. if (tx.extra_frag) {
  1021. for (i = 0; i < tx.num_extra_frag; i++) {
  1022. int next_len, dur;
  1023. struct ieee80211_hdr *hdr =
  1024. (struct ieee80211_hdr *)
  1025. tx.extra_frag[i]->data;
  1026. if (i + 1 < tx.num_extra_frag) {
  1027. next_len = tx.extra_frag[i + 1]->len;
  1028. } else {
  1029. next_len = 0;
  1030. tx.rate_idx = tx.last_frag_rate_idx;
  1031. }
  1032. dur = ieee80211_duration(&tx, 0, next_len);
  1033. hdr->duration_id = cpu_to_le16(dur);
  1034. }
  1035. }
  1036. retry:
  1037. ret = __ieee80211_tx(local, skb, &tx);
  1038. if (ret) {
  1039. struct ieee80211_tx_stored_packet *store =
  1040. &local->pending_packet[control->queue];
  1041. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1042. skb = NULL;
  1043. set_bit(IEEE80211_LINK_STATE_PENDING,
  1044. &local->state[control->queue]);
  1045. smp_mb();
  1046. /* When the driver gets out of buffers during sending of
  1047. * fragments and calls ieee80211_stop_queue, there is
  1048. * a small window between IEEE80211_LINK_STATE_XOFF and
  1049. * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
  1050. * gets available in that window (i.e. driver calls
  1051. * ieee80211_wake_queue), we would end up with ieee80211_tx
  1052. * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
  1053. * continuing transmitting here when that situation is
  1054. * possible to have happened. */
  1055. if (!__ieee80211_queue_stopped(local, control->queue)) {
  1056. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1057. &local->state[control->queue]);
  1058. goto retry;
  1059. }
  1060. memcpy(&store->control, control,
  1061. sizeof(struct ieee80211_tx_control));
  1062. store->skb = skb;
  1063. store->extra_frag = tx.extra_frag;
  1064. store->num_extra_frag = tx.num_extra_frag;
  1065. store->last_frag_rate_idx = tx.last_frag_rate_idx;
  1066. store->last_frag_rate_ctrl_probe =
  1067. !!(tx.flags & IEEE80211_TX_PROBE_LAST_FRAG);
  1068. }
  1069. rcu_read_unlock();
  1070. return 0;
  1071. drop:
  1072. if (skb)
  1073. dev_kfree_skb(skb);
  1074. for (i = 0; i < tx.num_extra_frag; i++)
  1075. if (tx.extra_frag[i])
  1076. dev_kfree_skb(tx.extra_frag[i]);
  1077. kfree(tx.extra_frag);
  1078. rcu_read_unlock();
  1079. return 0;
  1080. }
  1081. /* device xmit handlers */
  1082. int ieee80211_master_start_xmit(struct sk_buff *skb,
  1083. struct net_device *dev)
  1084. {
  1085. struct ieee80211_tx_control control;
  1086. struct ieee80211_tx_packet_data *pkt_data;
  1087. struct net_device *odev = NULL;
  1088. struct ieee80211_sub_if_data *osdata;
  1089. int headroom;
  1090. int ret;
  1091. /*
  1092. * copy control out of the skb so other people can use skb->cb
  1093. */
  1094. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1095. memset(&control, 0, sizeof(struct ieee80211_tx_control));
  1096. if (pkt_data->ifindex)
  1097. odev = dev_get_by_index(&init_net, pkt_data->ifindex);
  1098. if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
  1099. dev_put(odev);
  1100. odev = NULL;
  1101. }
  1102. if (unlikely(!odev)) {
  1103. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1104. printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
  1105. "originating device\n", dev->name);
  1106. #endif
  1107. dev_kfree_skb(skb);
  1108. return 0;
  1109. }
  1110. osdata = IEEE80211_DEV_TO_SUB_IF(odev);
  1111. headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
  1112. if (skb_headroom(skb) < headroom) {
  1113. if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
  1114. dev_kfree_skb(skb);
  1115. dev_put(odev);
  1116. return 0;
  1117. }
  1118. }
  1119. control.vif = &osdata->vif;
  1120. control.type = osdata->vif.type;
  1121. if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
  1122. control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
  1123. if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
  1124. control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  1125. if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
  1126. control.flags |= IEEE80211_TXCTL_REQUEUE;
  1127. if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
  1128. control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
  1129. if (pkt_data->flags & IEEE80211_TXPD_AMPDU)
  1130. control.flags |= IEEE80211_TXCTL_AMPDU;
  1131. control.queue = pkt_data->queue;
  1132. ret = ieee80211_tx(odev, skb, &control);
  1133. dev_put(odev);
  1134. return ret;
  1135. }
  1136. int ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1137. struct net_device *dev)
  1138. {
  1139. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1140. struct ieee80211_tx_packet_data *pkt_data;
  1141. struct ieee80211_radiotap_header *prthdr =
  1142. (struct ieee80211_radiotap_header *)skb->data;
  1143. u16 len_rthdr;
  1144. /* check for not even having the fixed radiotap header part */
  1145. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1146. goto fail; /* too short to be possibly valid */
  1147. /* is it a header version we can trust to find length from? */
  1148. if (unlikely(prthdr->it_version))
  1149. goto fail; /* only version 0 is supported */
  1150. /* then there must be a radiotap header with a length we can use */
  1151. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1152. /* does the skb contain enough to deliver on the alleged length? */
  1153. if (unlikely(skb->len < len_rthdr))
  1154. goto fail; /* skb too short for claimed rt header extent */
  1155. skb->dev = local->mdev;
  1156. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1157. memset(pkt_data, 0, sizeof(*pkt_data));
  1158. /* needed because we set skb device to master */
  1159. pkt_data->ifindex = dev->ifindex;
  1160. pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
  1161. /* Interfaces should always request a status report */
  1162. pkt_data->flags |= IEEE80211_TXPD_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_packet_data *pkt_data;
  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. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1412. memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
  1413. pkt_data->ifindex = dev->ifindex;
  1414. if (ethertype == ETH_P_PAE)
  1415. pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
  1416. /* Interfaces should always request a status report */
  1417. pkt_data->flags |= IEEE80211_TXPD_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.control = &store->control;
  1468. tx.extra_frag = store->extra_frag;
  1469. tx.num_extra_frag = store->num_extra_frag;
  1470. tx.last_frag_rate_idx = store->last_frag_rate_idx;
  1471. tx.flags = 0;
  1472. if (store->last_frag_rate_ctrl_probe)
  1473. tx.flags |= IEEE80211_TX_PROBE_LAST_FRAG;
  1474. ret = __ieee80211_tx(local, store->skb, &tx);
  1475. if (ret) {
  1476. if (ret == IEEE80211_TX_FRAG_AGAIN)
  1477. store->skb = NULL;
  1478. } else {
  1479. clear_bit(IEEE80211_LINK_STATE_PENDING,
  1480. &local->state[i]);
  1481. reschedule = 1;
  1482. }
  1483. }
  1484. netif_tx_unlock_bh(dev);
  1485. if (reschedule) {
  1486. if (!ieee80211_qdisc_installed(dev)) {
  1487. if (!__ieee80211_queue_stopped(local, 0))
  1488. netif_wake_queue(dev);
  1489. } else
  1490. netif_schedule(dev);
  1491. }
  1492. }
  1493. /* functions for drivers to get certain frames */
  1494. static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
  1495. struct ieee80211_if_ap *bss,
  1496. struct sk_buff *skb,
  1497. struct beacon_data *beacon)
  1498. {
  1499. u8 *pos, *tim;
  1500. int aid0 = 0;
  1501. int i, have_bits = 0, n1, n2;
  1502. /* Generate bitmap for TIM only if there are any STAs in power save
  1503. * mode. */
  1504. if (atomic_read(&bss->num_sta_ps) > 0)
  1505. /* in the hope that this is faster than
  1506. * checking byte-for-byte */
  1507. have_bits = !bitmap_empty((unsigned long*)bss->tim,
  1508. IEEE80211_MAX_AID+1);
  1509. if (bss->dtim_count == 0)
  1510. bss->dtim_count = beacon->dtim_period - 1;
  1511. else
  1512. bss->dtim_count--;
  1513. tim = pos = (u8 *) skb_put(skb, 6);
  1514. *pos++ = WLAN_EID_TIM;
  1515. *pos++ = 4;
  1516. *pos++ = bss->dtim_count;
  1517. *pos++ = beacon->dtim_period;
  1518. if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
  1519. aid0 = 1;
  1520. if (have_bits) {
  1521. /* Find largest even number N1 so that bits numbered 1 through
  1522. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  1523. * (N2 + 1) x 8 through 2007 are 0. */
  1524. n1 = 0;
  1525. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  1526. if (bss->tim[i]) {
  1527. n1 = i & 0xfe;
  1528. break;
  1529. }
  1530. }
  1531. n2 = n1;
  1532. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  1533. if (bss->tim[i]) {
  1534. n2 = i;
  1535. break;
  1536. }
  1537. }
  1538. /* Bitmap control */
  1539. *pos++ = n1 | aid0;
  1540. /* Part Virt Bitmap */
  1541. memcpy(pos, bss->tim + n1, n2 - n1 + 1);
  1542. tim[1] = n2 - n1 + 4;
  1543. skb_put(skb, n2 - n1);
  1544. } else {
  1545. *pos++ = aid0; /* Bitmap control */
  1546. *pos++ = 0; /* Part Virt Bitmap */
  1547. }
  1548. }
  1549. struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
  1550. struct ieee80211_vif *vif,
  1551. struct ieee80211_tx_control *control)
  1552. {
  1553. struct ieee80211_local *local = hw_to_local(hw);
  1554. struct sk_buff *skb;
  1555. struct net_device *bdev;
  1556. struct ieee80211_sub_if_data *sdata = NULL;
  1557. struct ieee80211_if_ap *ap = NULL;
  1558. struct rate_selection rsel;
  1559. struct beacon_data *beacon;
  1560. struct ieee80211_supported_band *sband;
  1561. struct ieee80211_mgmt *mgmt;
  1562. int *num_beacons;
  1563. bool err = true;
  1564. enum ieee80211_band band = local->hw.conf.channel->band;
  1565. u8 *pos;
  1566. sband = local->hw.wiphy->bands[band];
  1567. rcu_read_lock();
  1568. sdata = vif_to_sdata(vif);
  1569. bdev = sdata->dev;
  1570. if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
  1571. ap = &sdata->u.ap;
  1572. beacon = rcu_dereference(ap->beacon);
  1573. if (ap && beacon) {
  1574. /*
  1575. * headroom, head length,
  1576. * tail length and maximum TIM length
  1577. */
  1578. skb = dev_alloc_skb(local->tx_headroom +
  1579. beacon->head_len +
  1580. beacon->tail_len + 256);
  1581. if (!skb)
  1582. goto out;
  1583. skb_reserve(skb, local->tx_headroom);
  1584. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  1585. beacon->head_len);
  1586. ieee80211_include_sequence(sdata,
  1587. (struct ieee80211_hdr *)skb->data);
  1588. /*
  1589. * Not very nice, but we want to allow the driver to call
  1590. * ieee80211_beacon_get() as a response to the set_tim()
  1591. * callback. That, however, is already invoked under the
  1592. * sta_lock to guarantee consistent and race-free update
  1593. * of the tim bitmap in mac80211 and the driver.
  1594. */
  1595. if (local->tim_in_locked_section) {
  1596. ieee80211_beacon_add_tim(local, ap, skb, beacon);
  1597. } else {
  1598. unsigned long flags;
  1599. spin_lock_irqsave(&local->sta_lock, flags);
  1600. ieee80211_beacon_add_tim(local, ap, skb, beacon);
  1601. spin_unlock_irqrestore(&local->sta_lock, flags);
  1602. }
  1603. if (beacon->tail)
  1604. memcpy(skb_put(skb, beacon->tail_len),
  1605. beacon->tail, beacon->tail_len);
  1606. num_beacons = &ap->num_beacons;
  1607. err = false;
  1608. }
  1609. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1610. /* headroom, head length, tail length and maximum TIM length */
  1611. skb = dev_alloc_skb(local->tx_headroom + 400);
  1612. if (!skb)
  1613. goto out;
  1614. skb_reserve(skb, local->hw.extra_tx_headroom);
  1615. mgmt = (struct ieee80211_mgmt *)
  1616. skb_put(skb, 24 + sizeof(mgmt->u.beacon));
  1617. memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  1618. mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
  1619. IEEE80211_STYPE_BEACON);
  1620. memset(mgmt->da, 0xff, ETH_ALEN);
  1621. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  1622. /* BSSID is left zeroed, wildcard value */
  1623. mgmt->u.beacon.beacon_int =
  1624. cpu_to_le16(local->hw.conf.beacon_int);
  1625. mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
  1626. pos = skb_put(skb, 2);
  1627. *pos++ = WLAN_EID_SSID;
  1628. *pos++ = 0x0;
  1629. mesh_mgmt_ies_add(skb, sdata->dev);
  1630. num_beacons = &sdata->u.sta.num_beacons;
  1631. err = false;
  1632. }
  1633. if (err) {
  1634. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1635. if (net_ratelimit())
  1636. printk(KERN_DEBUG "no beacon data avail for %s\n",
  1637. bdev->name);
  1638. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  1639. skb = NULL;
  1640. goto out;
  1641. }
  1642. if (control) {
  1643. control->band = band;
  1644. rate_control_get_rate(local->mdev, sband, skb, &rsel);
  1645. if (unlikely(rsel.rate_idx < 0)) {
  1646. if (net_ratelimit()) {
  1647. printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
  1648. "no rate found\n",
  1649. wiphy_name(local->hw.wiphy));
  1650. }
  1651. dev_kfree_skb(skb);
  1652. skb = NULL;
  1653. goto out;
  1654. }
  1655. control->vif = vif;
  1656. control->tx_rate_idx = rsel.rate_idx;
  1657. if (sdata->bss_conf.use_short_preamble &&
  1658. sband->bitrates[rsel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
  1659. control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
  1660. control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
  1661. control->flags |= IEEE80211_TXCTL_NO_ACK;
  1662. control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
  1663. control->retry_limit = 1;
  1664. control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
  1665. }
  1666. (*num_beacons)++;
  1667. out:
  1668. rcu_read_unlock();
  1669. return skb;
  1670. }
  1671. EXPORT_SYMBOL(ieee80211_beacon_get);
  1672. void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1673. const void *frame, size_t frame_len,
  1674. const struct ieee80211_tx_control *frame_txctl,
  1675. struct ieee80211_rts *rts)
  1676. {
  1677. const struct ieee80211_hdr *hdr = frame;
  1678. u16 fctl;
  1679. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
  1680. rts->frame_control = cpu_to_le16(fctl);
  1681. rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
  1682. frame_txctl);
  1683. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  1684. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  1685. }
  1686. EXPORT_SYMBOL(ieee80211_rts_get);
  1687. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1688. const void *frame, size_t frame_len,
  1689. const struct ieee80211_tx_control *frame_txctl,
  1690. struct ieee80211_cts *cts)
  1691. {
  1692. const struct ieee80211_hdr *hdr = frame;
  1693. u16 fctl;
  1694. fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
  1695. cts->frame_control = cpu_to_le16(fctl);
  1696. cts->duration = ieee80211_ctstoself_duration(hw, vif,
  1697. frame_len, frame_txctl);
  1698. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  1699. }
  1700. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  1701. struct sk_buff *
  1702. ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
  1703. struct ieee80211_vif *vif,
  1704. struct ieee80211_tx_control *control)
  1705. {
  1706. struct ieee80211_local *local = hw_to_local(hw);
  1707. struct sk_buff *skb;
  1708. struct sta_info *sta;
  1709. ieee80211_tx_handler *handler;
  1710. struct ieee80211_tx_data tx;
  1711. ieee80211_tx_result res = TX_DROP;
  1712. struct net_device *bdev;
  1713. struct ieee80211_sub_if_data *sdata;
  1714. struct ieee80211_if_ap *bss = NULL;
  1715. struct beacon_data *beacon;
  1716. sdata = vif_to_sdata(vif);
  1717. bdev = sdata->dev;
  1718. if (!bss)
  1719. return NULL;
  1720. rcu_read_lock();
  1721. beacon = rcu_dereference(bss->beacon);
  1722. if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon ||
  1723. !beacon->head) {
  1724. rcu_read_unlock();
  1725. return NULL;
  1726. }
  1727. if (bss->dtim_count != 0)
  1728. return NULL; /* send buffered bc/mc only after DTIM beacon */
  1729. memset(control, 0, sizeof(*control));
  1730. while (1) {
  1731. skb = skb_dequeue(&bss->ps_bc_buf);
  1732. if (!skb)
  1733. return NULL;
  1734. local->total_ps_buffered--;
  1735. if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
  1736. struct ieee80211_hdr *hdr =
  1737. (struct ieee80211_hdr *) skb->data;
  1738. /* more buffered multicast/broadcast frames ==> set
  1739. * MoreData flag in IEEE 802.11 header to inform PS
  1740. * STAs */
  1741. hdr->frame_control |=
  1742. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1743. }
  1744. if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
  1745. break;
  1746. dev_kfree_skb_any(skb);
  1747. }
  1748. sta = tx.sta;
  1749. tx.flags |= IEEE80211_TX_PS_BUFFERED;
  1750. tx.channel = local->hw.conf.channel;
  1751. control->band = tx.channel->band;
  1752. for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
  1753. res = (*handler)(&tx);
  1754. if (res == TX_DROP || res == TX_QUEUED)
  1755. break;
  1756. }
  1757. skb = tx.skb; /* handlers are allowed to change skb */
  1758. if (res == TX_DROP) {
  1759. I802_DEBUG_INC(local->tx_handlers_drop);
  1760. dev_kfree_skb(skb);
  1761. skb = NULL;
  1762. } else if (res == TX_QUEUED) {
  1763. I802_DEBUG_INC(local->tx_handlers_queued);
  1764. skb = NULL;
  1765. }
  1766. rcu_read_unlock();
  1767. return skb;
  1768. }
  1769. EXPORT_SYMBOL(ieee80211_get_buffered_bc);