tx.c 56 KB

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