tx.c 58 KB

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