skbuff.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277
  1. /*
  2. * Routines having to do with the 'struct sk_buff' memory handlers.
  3. *
  4. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
  5. * Florian La Roche <rzsfl@rz.uni-sb.de>
  6. *
  7. * Fixes:
  8. * Alan Cox : Fixed the worst of the load
  9. * balancer bugs.
  10. * Dave Platt : Interrupt stacking fix.
  11. * Richard Kooijman : Timestamp fixes.
  12. * Alan Cox : Changed buffer format.
  13. * Alan Cox : destructor hook for AF_UNIX etc.
  14. * Linus Torvalds : Better skb_clone.
  15. * Alan Cox : Added skb_copy.
  16. * Alan Cox : Added all the changed routines Linus
  17. * only put in the headers
  18. * Ray VanTassle : Fixed --skb->lock in free
  19. * Alan Cox : skb_copy copy arp field
  20. * Andi Kleen : slabified it.
  21. * Robert Olsson : Removed skb_head_pool
  22. *
  23. * NOTE:
  24. * The __skb_ routines should be called with interrupts
  25. * disabled, or you better be *real* sure that the operation is atomic
  26. * with respect to whatever list is being frobbed (e.g. via lock_sock()
  27. * or via disabling bottom half handlers, etc).
  28. *
  29. * This program is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU General Public License
  31. * as published by the Free Software Foundation; either version
  32. * 2 of the License, or (at your option) any later version.
  33. */
  34. /*
  35. * The functions in this file will not compile correctly with gcc 2.4.x
  36. */
  37. #include <linux/module.h>
  38. #include <linux/types.h>
  39. #include <linux/kernel.h>
  40. #include <linux/kmemcheck.h>
  41. #include <linux/mm.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/in.h>
  44. #include <linux/inet.h>
  45. #include <linux/slab.h>
  46. #include <linux/netdevice.h>
  47. #ifdef CONFIG_NET_CLS_ACT
  48. #include <net/pkt_sched.h>
  49. #endif
  50. #include <linux/string.h>
  51. #include <linux/skbuff.h>
  52. #include <linux/splice.h>
  53. #include <linux/cache.h>
  54. #include <linux/rtnetlink.h>
  55. #include <linux/init.h>
  56. #include <linux/scatterlist.h>
  57. #include <linux/errqueue.h>
  58. #include <linux/prefetch.h>
  59. #include <net/protocol.h>
  60. #include <net/dst.h>
  61. #include <net/sock.h>
  62. #include <net/checksum.h>
  63. #include <net/xfrm.h>
  64. #include <asm/uaccess.h>
  65. #include <asm/system.h>
  66. #include <trace/events/skb.h>
  67. #include "kmap_skb.h"
  68. static struct kmem_cache *skbuff_head_cache __read_mostly;
  69. static struct kmem_cache *skbuff_fclone_cache __read_mostly;
  70. static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
  71. struct pipe_buffer *buf)
  72. {
  73. put_page(buf->page);
  74. }
  75. static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
  76. struct pipe_buffer *buf)
  77. {
  78. get_page(buf->page);
  79. }
  80. static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
  81. struct pipe_buffer *buf)
  82. {
  83. return 1;
  84. }
  85. /* Pipe buffer operations for a socket. */
  86. static const struct pipe_buf_operations sock_pipe_buf_ops = {
  87. .can_merge = 0,
  88. .map = generic_pipe_buf_map,
  89. .unmap = generic_pipe_buf_unmap,
  90. .confirm = generic_pipe_buf_confirm,
  91. .release = sock_pipe_buf_release,
  92. .steal = sock_pipe_buf_steal,
  93. .get = sock_pipe_buf_get,
  94. };
  95. /*
  96. * Keep out-of-line to prevent kernel bloat.
  97. * __builtin_return_address is not used because it is not always
  98. * reliable.
  99. */
  100. /**
  101. * skb_over_panic - private function
  102. * @skb: buffer
  103. * @sz: size
  104. * @here: address
  105. *
  106. * Out of line support code for skb_put(). Not user callable.
  107. */
  108. static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
  109. {
  110. printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
  111. "data:%p tail:%#lx end:%#lx dev:%s\n",
  112. here, skb->len, sz, skb->head, skb->data,
  113. (unsigned long)skb->tail, (unsigned long)skb->end,
  114. skb->dev ? skb->dev->name : "<NULL>");
  115. BUG();
  116. }
  117. /**
  118. * skb_under_panic - private function
  119. * @skb: buffer
  120. * @sz: size
  121. * @here: address
  122. *
  123. * Out of line support code for skb_push(). Not user callable.
  124. */
  125. static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
  126. {
  127. printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
  128. "data:%p tail:%#lx end:%#lx dev:%s\n",
  129. here, skb->len, sz, skb->head, skb->data,
  130. (unsigned long)skb->tail, (unsigned long)skb->end,
  131. skb->dev ? skb->dev->name : "<NULL>");
  132. BUG();
  133. }
  134. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  135. * 'private' fields and also do memory statistics to find all the
  136. * [BEEP] leaks.
  137. *
  138. */
  139. /**
  140. * __alloc_skb - allocate a network buffer
  141. * @size: size to allocate
  142. * @gfp_mask: allocation mask
  143. * @fclone: allocate from fclone cache instead of head cache
  144. * and allocate a cloned (child) skb
  145. * @node: numa node to allocate memory on
  146. *
  147. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  148. * tail room of size bytes. The object has a reference count of one.
  149. * The return is the buffer. On a failure the return is %NULL.
  150. *
  151. * Buffers may only be allocated from interrupts using a @gfp_mask of
  152. * %GFP_ATOMIC.
  153. */
  154. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  155. int fclone, int node)
  156. {
  157. struct kmem_cache *cache;
  158. struct skb_shared_info *shinfo;
  159. struct sk_buff *skb;
  160. u8 *data;
  161. cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
  162. /* Get the HEAD */
  163. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
  164. if (!skb)
  165. goto out;
  166. prefetchw(skb);
  167. /* We do our best to align skb_shared_info on a separate cache
  168. * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
  169. * aligned memory blocks, unless SLUB/SLAB debug is enabled.
  170. * Both skb->head and skb_shared_info are cache line aligned.
  171. */
  172. size = SKB_DATA_ALIGN(size);
  173. size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  174. data = kmalloc_node_track_caller(size, gfp_mask, node);
  175. if (!data)
  176. goto nodata;
  177. /* kmalloc(size) might give us more room than requested.
  178. * Put skb_shared_info exactly at the end of allocated zone,
  179. * to allow max possible filling before reallocation.
  180. */
  181. size = SKB_WITH_OVERHEAD(ksize(data));
  182. prefetchw(data + size);
  183. /*
  184. * Only clear those fields we need to clear, not those that we will
  185. * actually initialise below. Hence, don't put any more fields after
  186. * the tail pointer in struct sk_buff!
  187. */
  188. memset(skb, 0, offsetof(struct sk_buff, tail));
  189. /* Account for allocated memory : skb + skb->head */
  190. skb->truesize = SKB_TRUESIZE(size);
  191. atomic_set(&skb->users, 1);
  192. skb->head = data;
  193. skb->data = data;
  194. skb_reset_tail_pointer(skb);
  195. skb->end = skb->tail + size;
  196. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  197. skb->mac_header = ~0U;
  198. #endif
  199. /* make sure we initialize shinfo sequentially */
  200. shinfo = skb_shinfo(skb);
  201. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  202. atomic_set(&shinfo->dataref, 1);
  203. kmemcheck_annotate_variable(shinfo->destructor_arg);
  204. if (fclone) {
  205. struct sk_buff *child = skb + 1;
  206. atomic_t *fclone_ref = (atomic_t *) (child + 1);
  207. kmemcheck_annotate_bitfield(child, flags1);
  208. kmemcheck_annotate_bitfield(child, flags2);
  209. skb->fclone = SKB_FCLONE_ORIG;
  210. atomic_set(fclone_ref, 1);
  211. child->fclone = SKB_FCLONE_UNAVAILABLE;
  212. }
  213. out:
  214. return skb;
  215. nodata:
  216. kmem_cache_free(cache, skb);
  217. skb = NULL;
  218. goto out;
  219. }
  220. EXPORT_SYMBOL(__alloc_skb);
  221. /**
  222. * build_skb - build a network buffer
  223. * @data: data buffer provided by caller
  224. *
  225. * Allocate a new &sk_buff. Caller provides space holding head and
  226. * skb_shared_info. @data must have been allocated by kmalloc()
  227. * The return is the new skb buffer.
  228. * On a failure the return is %NULL, and @data is not freed.
  229. * Notes :
  230. * Before IO, driver allocates only data buffer where NIC put incoming frame
  231. * Driver should add room at head (NET_SKB_PAD) and
  232. * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
  233. * After IO, driver calls build_skb(), to allocate sk_buff and populate it
  234. * before giving packet to stack.
  235. * RX rings only contains data buffers, not full skbs.
  236. */
  237. struct sk_buff *build_skb(void *data)
  238. {
  239. struct skb_shared_info *shinfo;
  240. struct sk_buff *skb;
  241. unsigned int size;
  242. skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
  243. if (!skb)
  244. return NULL;
  245. size = ksize(data) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  246. memset(skb, 0, offsetof(struct sk_buff, tail));
  247. skb->truesize = SKB_TRUESIZE(size);
  248. atomic_set(&skb->users, 1);
  249. skb->head = data;
  250. skb->data = data;
  251. skb_reset_tail_pointer(skb);
  252. skb->end = skb->tail + size;
  253. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  254. skb->mac_header = ~0U;
  255. #endif
  256. /* make sure we initialize shinfo sequentially */
  257. shinfo = skb_shinfo(skb);
  258. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  259. atomic_set(&shinfo->dataref, 1);
  260. kmemcheck_annotate_variable(shinfo->destructor_arg);
  261. return skb;
  262. }
  263. EXPORT_SYMBOL(build_skb);
  264. /**
  265. * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
  266. * @dev: network device to receive on
  267. * @length: length to allocate
  268. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  269. *
  270. * Allocate a new &sk_buff and assign it a usage count of one. The
  271. * buffer has unspecified headroom built in. Users should allocate
  272. * the headroom they think they need without accounting for the
  273. * built in space. The built in space is used for optimisations.
  274. *
  275. * %NULL is returned if there is no free memory.
  276. */
  277. struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  278. unsigned int length, gfp_t gfp_mask)
  279. {
  280. struct sk_buff *skb;
  281. skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, NUMA_NO_NODE);
  282. if (likely(skb)) {
  283. skb_reserve(skb, NET_SKB_PAD);
  284. skb->dev = dev;
  285. }
  286. return skb;
  287. }
  288. EXPORT_SYMBOL(__netdev_alloc_skb);
  289. void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
  290. int size)
  291. {
  292. skb_fill_page_desc(skb, i, page, off, size);
  293. skb->len += size;
  294. skb->data_len += size;
  295. skb->truesize += size;
  296. }
  297. EXPORT_SYMBOL(skb_add_rx_frag);
  298. /**
  299. * dev_alloc_skb - allocate an skbuff for receiving
  300. * @length: length to allocate
  301. *
  302. * Allocate a new &sk_buff and assign it a usage count of one. The
  303. * buffer has unspecified headroom built in. Users should allocate
  304. * the headroom they think they need without accounting for the
  305. * built in space. The built in space is used for optimisations.
  306. *
  307. * %NULL is returned if there is no free memory. Although this function
  308. * allocates memory it can be called from an interrupt.
  309. */
  310. struct sk_buff *dev_alloc_skb(unsigned int length)
  311. {
  312. /*
  313. * There is more code here than it seems:
  314. * __dev_alloc_skb is an inline
  315. */
  316. return __dev_alloc_skb(length, GFP_ATOMIC);
  317. }
  318. EXPORT_SYMBOL(dev_alloc_skb);
  319. static void skb_drop_list(struct sk_buff **listp)
  320. {
  321. struct sk_buff *list = *listp;
  322. *listp = NULL;
  323. do {
  324. struct sk_buff *this = list;
  325. list = list->next;
  326. kfree_skb(this);
  327. } while (list);
  328. }
  329. static inline void skb_drop_fraglist(struct sk_buff *skb)
  330. {
  331. skb_drop_list(&skb_shinfo(skb)->frag_list);
  332. }
  333. static void skb_clone_fraglist(struct sk_buff *skb)
  334. {
  335. struct sk_buff *list;
  336. skb_walk_frags(skb, list)
  337. skb_get(list);
  338. }
  339. static void skb_release_data(struct sk_buff *skb)
  340. {
  341. if (!skb->cloned ||
  342. !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  343. &skb_shinfo(skb)->dataref)) {
  344. if (skb_shinfo(skb)->nr_frags) {
  345. int i;
  346. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  347. skb_frag_unref(skb, i);
  348. }
  349. /*
  350. * If skb buf is from userspace, we need to notify the caller
  351. * the lower device DMA has done;
  352. */
  353. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  354. struct ubuf_info *uarg;
  355. uarg = skb_shinfo(skb)->destructor_arg;
  356. if (uarg->callback)
  357. uarg->callback(uarg);
  358. }
  359. if (skb_has_frag_list(skb))
  360. skb_drop_fraglist(skb);
  361. kfree(skb->head);
  362. }
  363. }
  364. /*
  365. * Free an skbuff by memory without cleaning the state.
  366. */
  367. static void kfree_skbmem(struct sk_buff *skb)
  368. {
  369. struct sk_buff *other;
  370. atomic_t *fclone_ref;
  371. switch (skb->fclone) {
  372. case SKB_FCLONE_UNAVAILABLE:
  373. kmem_cache_free(skbuff_head_cache, skb);
  374. break;
  375. case SKB_FCLONE_ORIG:
  376. fclone_ref = (atomic_t *) (skb + 2);
  377. if (atomic_dec_and_test(fclone_ref))
  378. kmem_cache_free(skbuff_fclone_cache, skb);
  379. break;
  380. case SKB_FCLONE_CLONE:
  381. fclone_ref = (atomic_t *) (skb + 1);
  382. other = skb - 1;
  383. /* The clone portion is available for
  384. * fast-cloning again.
  385. */
  386. skb->fclone = SKB_FCLONE_UNAVAILABLE;
  387. if (atomic_dec_and_test(fclone_ref))
  388. kmem_cache_free(skbuff_fclone_cache, other);
  389. break;
  390. }
  391. }
  392. static void skb_release_head_state(struct sk_buff *skb)
  393. {
  394. skb_dst_drop(skb);
  395. #ifdef CONFIG_XFRM
  396. secpath_put(skb->sp);
  397. #endif
  398. if (skb->destructor) {
  399. WARN_ON(in_irq());
  400. skb->destructor(skb);
  401. }
  402. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  403. nf_conntrack_put(skb->nfct);
  404. #endif
  405. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  406. nf_conntrack_put_reasm(skb->nfct_reasm);
  407. #endif
  408. #ifdef CONFIG_BRIDGE_NETFILTER
  409. nf_bridge_put(skb->nf_bridge);
  410. #endif
  411. /* XXX: IS this still necessary? - JHS */
  412. #ifdef CONFIG_NET_SCHED
  413. skb->tc_index = 0;
  414. #ifdef CONFIG_NET_CLS_ACT
  415. skb->tc_verd = 0;
  416. #endif
  417. #endif
  418. }
  419. /* Free everything but the sk_buff shell. */
  420. static void skb_release_all(struct sk_buff *skb)
  421. {
  422. skb_release_head_state(skb);
  423. skb_release_data(skb);
  424. }
  425. /**
  426. * __kfree_skb - private function
  427. * @skb: buffer
  428. *
  429. * Free an sk_buff. Release anything attached to the buffer.
  430. * Clean the state. This is an internal helper function. Users should
  431. * always call kfree_skb
  432. */
  433. void __kfree_skb(struct sk_buff *skb)
  434. {
  435. skb_release_all(skb);
  436. kfree_skbmem(skb);
  437. }
  438. EXPORT_SYMBOL(__kfree_skb);
  439. /**
  440. * kfree_skb - free an sk_buff
  441. * @skb: buffer to free
  442. *
  443. * Drop a reference to the buffer and free it if the usage count has
  444. * hit zero.
  445. */
  446. void kfree_skb(struct sk_buff *skb)
  447. {
  448. if (unlikely(!skb))
  449. return;
  450. if (likely(atomic_read(&skb->users) == 1))
  451. smp_rmb();
  452. else if (likely(!atomic_dec_and_test(&skb->users)))
  453. return;
  454. trace_kfree_skb(skb, __builtin_return_address(0));
  455. __kfree_skb(skb);
  456. }
  457. EXPORT_SYMBOL(kfree_skb);
  458. /**
  459. * consume_skb - free an skbuff
  460. * @skb: buffer to free
  461. *
  462. * Drop a ref to the buffer and free it if the usage count has hit zero
  463. * Functions identically to kfree_skb, but kfree_skb assumes that the frame
  464. * is being dropped after a failure and notes that
  465. */
  466. void consume_skb(struct sk_buff *skb)
  467. {
  468. if (unlikely(!skb))
  469. return;
  470. if (likely(atomic_read(&skb->users) == 1))
  471. smp_rmb();
  472. else if (likely(!atomic_dec_and_test(&skb->users)))
  473. return;
  474. trace_consume_skb(skb);
  475. __kfree_skb(skb);
  476. }
  477. EXPORT_SYMBOL(consume_skb);
  478. /**
  479. * skb_recycle - clean up an skb for reuse
  480. * @skb: buffer
  481. *
  482. * Recycles the skb to be reused as a receive buffer. This
  483. * function does any necessary reference count dropping, and
  484. * cleans up the skbuff as if it just came from __alloc_skb().
  485. */
  486. void skb_recycle(struct sk_buff *skb)
  487. {
  488. struct skb_shared_info *shinfo;
  489. skb_release_head_state(skb);
  490. shinfo = skb_shinfo(skb);
  491. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  492. atomic_set(&shinfo->dataref, 1);
  493. memset(skb, 0, offsetof(struct sk_buff, tail));
  494. skb->data = skb->head + NET_SKB_PAD;
  495. skb_reset_tail_pointer(skb);
  496. }
  497. EXPORT_SYMBOL(skb_recycle);
  498. /**
  499. * skb_recycle_check - check if skb can be reused for receive
  500. * @skb: buffer
  501. * @skb_size: minimum receive buffer size
  502. *
  503. * Checks that the skb passed in is not shared or cloned, and
  504. * that it is linear and its head portion at least as large as
  505. * skb_size so that it can be recycled as a receive buffer.
  506. * If these conditions are met, this function does any necessary
  507. * reference count dropping and cleans up the skbuff as if it
  508. * just came from __alloc_skb().
  509. */
  510. bool skb_recycle_check(struct sk_buff *skb, int skb_size)
  511. {
  512. if (!skb_is_recycleable(skb, skb_size))
  513. return false;
  514. skb_recycle(skb);
  515. return true;
  516. }
  517. EXPORT_SYMBOL(skb_recycle_check);
  518. static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  519. {
  520. new->tstamp = old->tstamp;
  521. new->dev = old->dev;
  522. new->transport_header = old->transport_header;
  523. new->network_header = old->network_header;
  524. new->mac_header = old->mac_header;
  525. skb_dst_copy(new, old);
  526. new->rxhash = old->rxhash;
  527. new->ooo_okay = old->ooo_okay;
  528. new->l4_rxhash = old->l4_rxhash;
  529. #ifdef CONFIG_XFRM
  530. new->sp = secpath_get(old->sp);
  531. #endif
  532. memcpy(new->cb, old->cb, sizeof(old->cb));
  533. new->csum = old->csum;
  534. new->local_df = old->local_df;
  535. new->pkt_type = old->pkt_type;
  536. new->ip_summed = old->ip_summed;
  537. skb_copy_queue_mapping(new, old);
  538. new->priority = old->priority;
  539. #if IS_ENABLED(CONFIG_IP_VS)
  540. new->ipvs_property = old->ipvs_property;
  541. #endif
  542. new->protocol = old->protocol;
  543. new->mark = old->mark;
  544. new->skb_iif = old->skb_iif;
  545. __nf_copy(new, old);
  546. #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
  547. new->nf_trace = old->nf_trace;
  548. #endif
  549. #ifdef CONFIG_NET_SCHED
  550. new->tc_index = old->tc_index;
  551. #ifdef CONFIG_NET_CLS_ACT
  552. new->tc_verd = old->tc_verd;
  553. #endif
  554. #endif
  555. new->vlan_tci = old->vlan_tci;
  556. skb_copy_secmark(new, old);
  557. }
  558. /*
  559. * You should not add any new code to this function. Add it to
  560. * __copy_skb_header above instead.
  561. */
  562. static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
  563. {
  564. #define C(x) n->x = skb->x
  565. n->next = n->prev = NULL;
  566. n->sk = NULL;
  567. __copy_skb_header(n, skb);
  568. C(len);
  569. C(data_len);
  570. C(mac_len);
  571. n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
  572. n->cloned = 1;
  573. n->nohdr = 0;
  574. n->destructor = NULL;
  575. C(tail);
  576. C(end);
  577. C(head);
  578. C(data);
  579. C(truesize);
  580. atomic_set(&n->users, 1);
  581. atomic_inc(&(skb_shinfo(skb)->dataref));
  582. skb->cloned = 1;
  583. return n;
  584. #undef C
  585. }
  586. /**
  587. * skb_morph - morph one skb into another
  588. * @dst: the skb to receive the contents
  589. * @src: the skb to supply the contents
  590. *
  591. * This is identical to skb_clone except that the target skb is
  592. * supplied by the user.
  593. *
  594. * The target skb is returned upon exit.
  595. */
  596. struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
  597. {
  598. skb_release_all(dst);
  599. return __skb_clone(dst, src);
  600. }
  601. EXPORT_SYMBOL_GPL(skb_morph);
  602. /* skb_copy_ubufs - copy userspace skb frags buffers to kernel
  603. * @skb: the skb to modify
  604. * @gfp_mask: allocation priority
  605. *
  606. * This must be called on SKBTX_DEV_ZEROCOPY skb.
  607. * It will copy all frags into kernel and drop the reference
  608. * to userspace pages.
  609. *
  610. * If this function is called from an interrupt gfp_mask() must be
  611. * %GFP_ATOMIC.
  612. *
  613. * Returns 0 on success or a negative error code on failure
  614. * to allocate kernel memory to copy to.
  615. */
  616. int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
  617. {
  618. int i;
  619. int num_frags = skb_shinfo(skb)->nr_frags;
  620. struct page *page, *head = NULL;
  621. struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
  622. for (i = 0; i < num_frags; i++) {
  623. u8 *vaddr;
  624. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  625. page = alloc_page(GFP_ATOMIC);
  626. if (!page) {
  627. while (head) {
  628. struct page *next = (struct page *)head->private;
  629. put_page(head);
  630. head = next;
  631. }
  632. return -ENOMEM;
  633. }
  634. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  635. memcpy(page_address(page),
  636. vaddr + f->page_offset, skb_frag_size(f));
  637. kunmap_skb_frag(vaddr);
  638. page->private = (unsigned long)head;
  639. head = page;
  640. }
  641. /* skb frags release userspace buffers */
  642. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  643. skb_frag_unref(skb, i);
  644. uarg->callback(uarg);
  645. /* skb frags point to kernel buffers */
  646. for (i = skb_shinfo(skb)->nr_frags; i > 0; i--) {
  647. __skb_fill_page_desc(skb, i-1, head, 0,
  648. skb_shinfo(skb)->frags[i - 1].size);
  649. head = (struct page *)head->private;
  650. }
  651. skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
  652. return 0;
  653. }
  654. /**
  655. * skb_clone - duplicate an sk_buff
  656. * @skb: buffer to clone
  657. * @gfp_mask: allocation priority
  658. *
  659. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  660. * copies share the same packet data but not structure. The new
  661. * buffer has a reference count of 1. If the allocation fails the
  662. * function returns %NULL otherwise the new buffer is returned.
  663. *
  664. * If this function is called from an interrupt gfp_mask() must be
  665. * %GFP_ATOMIC.
  666. */
  667. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  668. {
  669. struct sk_buff *n;
  670. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  671. if (skb_copy_ubufs(skb, gfp_mask))
  672. return NULL;
  673. }
  674. n = skb + 1;
  675. if (skb->fclone == SKB_FCLONE_ORIG &&
  676. n->fclone == SKB_FCLONE_UNAVAILABLE) {
  677. atomic_t *fclone_ref = (atomic_t *) (n + 1);
  678. n->fclone = SKB_FCLONE_CLONE;
  679. atomic_inc(fclone_ref);
  680. } else {
  681. n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  682. if (!n)
  683. return NULL;
  684. kmemcheck_annotate_bitfield(n, flags1);
  685. kmemcheck_annotate_bitfield(n, flags2);
  686. n->fclone = SKB_FCLONE_UNAVAILABLE;
  687. }
  688. return __skb_clone(n, skb);
  689. }
  690. EXPORT_SYMBOL(skb_clone);
  691. static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  692. {
  693. #ifndef NET_SKBUFF_DATA_USES_OFFSET
  694. /*
  695. * Shift between the two data areas in bytes
  696. */
  697. unsigned long offset = new->data - old->data;
  698. #endif
  699. __copy_skb_header(new, old);
  700. #ifndef NET_SKBUFF_DATA_USES_OFFSET
  701. /* {transport,network,mac}_header are relative to skb->head */
  702. new->transport_header += offset;
  703. new->network_header += offset;
  704. if (skb_mac_header_was_set(new))
  705. new->mac_header += offset;
  706. #endif
  707. skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
  708. skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
  709. skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
  710. }
  711. /**
  712. * skb_copy - create private copy of an sk_buff
  713. * @skb: buffer to copy
  714. * @gfp_mask: allocation priority
  715. *
  716. * Make a copy of both an &sk_buff and its data. This is used when the
  717. * caller wishes to modify the data and needs a private copy of the
  718. * data to alter. Returns %NULL on failure or the pointer to the buffer
  719. * on success. The returned buffer has a reference count of 1.
  720. *
  721. * As by-product this function converts non-linear &sk_buff to linear
  722. * one, so that &sk_buff becomes completely private and caller is allowed
  723. * to modify all the data of returned buffer. This means that this
  724. * function is not recommended for use in circumstances when only
  725. * header is going to be modified. Use pskb_copy() instead.
  726. */
  727. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  728. {
  729. int headerlen = skb_headroom(skb);
  730. unsigned int size = (skb_end_pointer(skb) - skb->head) + skb->data_len;
  731. struct sk_buff *n = alloc_skb(size, gfp_mask);
  732. if (!n)
  733. return NULL;
  734. /* Set the data pointer */
  735. skb_reserve(n, headerlen);
  736. /* Set the tail pointer and length */
  737. skb_put(n, skb->len);
  738. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  739. BUG();
  740. copy_skb_header(n, skb);
  741. return n;
  742. }
  743. EXPORT_SYMBOL(skb_copy);
  744. /**
  745. * __pskb_copy - create copy of an sk_buff with private head.
  746. * @skb: buffer to copy
  747. * @headroom: headroom of new skb
  748. * @gfp_mask: allocation priority
  749. *
  750. * Make a copy of both an &sk_buff and part of its data, located
  751. * in header. Fragmented data remain shared. This is used when
  752. * the caller wishes to modify only header of &sk_buff and needs
  753. * private copy of the header to alter. Returns %NULL on failure
  754. * or the pointer to the buffer on success.
  755. * The returned buffer has a reference count of 1.
  756. */
  757. struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
  758. {
  759. unsigned int size = skb_headlen(skb) + headroom;
  760. struct sk_buff *n = alloc_skb(size, gfp_mask);
  761. if (!n)
  762. goto out;
  763. /* Set the data pointer */
  764. skb_reserve(n, headroom);
  765. /* Set the tail pointer and length */
  766. skb_put(n, skb_headlen(skb));
  767. /* Copy the bytes */
  768. skb_copy_from_linear_data(skb, n->data, n->len);
  769. n->truesize += skb->data_len;
  770. n->data_len = skb->data_len;
  771. n->len = skb->len;
  772. if (skb_shinfo(skb)->nr_frags) {
  773. int i;
  774. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  775. if (skb_copy_ubufs(skb, gfp_mask)) {
  776. kfree_skb(n);
  777. n = NULL;
  778. goto out;
  779. }
  780. }
  781. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  782. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  783. skb_frag_ref(skb, i);
  784. }
  785. skb_shinfo(n)->nr_frags = i;
  786. }
  787. if (skb_has_frag_list(skb)) {
  788. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  789. skb_clone_fraglist(n);
  790. }
  791. copy_skb_header(n, skb);
  792. out:
  793. return n;
  794. }
  795. EXPORT_SYMBOL(__pskb_copy);
  796. /**
  797. * pskb_expand_head - reallocate header of &sk_buff
  798. * @skb: buffer to reallocate
  799. * @nhead: room to add at head
  800. * @ntail: room to add at tail
  801. * @gfp_mask: allocation priority
  802. *
  803. * Expands (or creates identical copy, if &nhead and &ntail are zero)
  804. * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
  805. * reference count of 1. Returns zero in the case of success or error,
  806. * if expansion failed. In the last case, &sk_buff is not changed.
  807. *
  808. * All the pointers pointing into skb header may change and must be
  809. * reloaded after call to this function.
  810. */
  811. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  812. gfp_t gfp_mask)
  813. {
  814. int i;
  815. u8 *data;
  816. int size = nhead + (skb_end_pointer(skb) - skb->head) + ntail;
  817. long off;
  818. bool fastpath;
  819. BUG_ON(nhead < 0);
  820. if (skb_shared(skb))
  821. BUG();
  822. size = SKB_DATA_ALIGN(size);
  823. /* Check if we can avoid taking references on fragments if we own
  824. * the last reference on skb->head. (see skb_release_data())
  825. */
  826. if (!skb->cloned)
  827. fastpath = true;
  828. else {
  829. int delta = skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1;
  830. fastpath = atomic_read(&skb_shinfo(skb)->dataref) == delta;
  831. }
  832. if (fastpath &&
  833. size + sizeof(struct skb_shared_info) <= ksize(skb->head)) {
  834. memmove(skb->head + size, skb_shinfo(skb),
  835. offsetof(struct skb_shared_info,
  836. frags[skb_shinfo(skb)->nr_frags]));
  837. memmove(skb->head + nhead, skb->head,
  838. skb_tail_pointer(skb) - skb->head);
  839. off = nhead;
  840. goto adjust_others;
  841. }
  842. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  843. if (!data)
  844. goto nodata;
  845. /* Copy only real data... and, alas, header. This should be
  846. * optimized for the cases when header is void.
  847. */
  848. memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
  849. memcpy((struct skb_shared_info *)(data + size),
  850. skb_shinfo(skb),
  851. offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
  852. if (fastpath) {
  853. kfree(skb->head);
  854. } else {
  855. /* copy this zero copy skb frags */
  856. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  857. if (skb_copy_ubufs(skb, gfp_mask))
  858. goto nofrags;
  859. }
  860. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  861. skb_frag_ref(skb, i);
  862. if (skb_has_frag_list(skb))
  863. skb_clone_fraglist(skb);
  864. skb_release_data(skb);
  865. }
  866. off = (data + nhead) - skb->head;
  867. skb->head = data;
  868. adjust_others:
  869. skb->data += off;
  870. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  871. skb->end = size;
  872. off = nhead;
  873. #else
  874. skb->end = skb->head + size;
  875. #endif
  876. /* {transport,network,mac}_header and tail are relative to skb->head */
  877. skb->tail += off;
  878. skb->transport_header += off;
  879. skb->network_header += off;
  880. if (skb_mac_header_was_set(skb))
  881. skb->mac_header += off;
  882. /* Only adjust this if it actually is csum_start rather than csum */
  883. if (skb->ip_summed == CHECKSUM_PARTIAL)
  884. skb->csum_start += nhead;
  885. skb->cloned = 0;
  886. skb->hdr_len = 0;
  887. skb->nohdr = 0;
  888. atomic_set(&skb_shinfo(skb)->dataref, 1);
  889. return 0;
  890. nofrags:
  891. kfree(data);
  892. nodata:
  893. return -ENOMEM;
  894. }
  895. EXPORT_SYMBOL(pskb_expand_head);
  896. /* Make private copy of skb with writable head and some headroom */
  897. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  898. {
  899. struct sk_buff *skb2;
  900. int delta = headroom - skb_headroom(skb);
  901. if (delta <= 0)
  902. skb2 = pskb_copy(skb, GFP_ATOMIC);
  903. else {
  904. skb2 = skb_clone(skb, GFP_ATOMIC);
  905. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  906. GFP_ATOMIC)) {
  907. kfree_skb(skb2);
  908. skb2 = NULL;
  909. }
  910. }
  911. return skb2;
  912. }
  913. EXPORT_SYMBOL(skb_realloc_headroom);
  914. /**
  915. * skb_copy_expand - copy and expand sk_buff
  916. * @skb: buffer to copy
  917. * @newheadroom: new free bytes at head
  918. * @newtailroom: new free bytes at tail
  919. * @gfp_mask: allocation priority
  920. *
  921. * Make a copy of both an &sk_buff and its data and while doing so
  922. * allocate additional space.
  923. *
  924. * This is used when the caller wishes to modify the data and needs a
  925. * private copy of the data to alter as well as more space for new fields.
  926. * Returns %NULL on failure or the pointer to the buffer
  927. * on success. The returned buffer has a reference count of 1.
  928. *
  929. * You must pass %GFP_ATOMIC as the allocation priority if this function
  930. * is called from an interrupt.
  931. */
  932. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  933. int newheadroom, int newtailroom,
  934. gfp_t gfp_mask)
  935. {
  936. /*
  937. * Allocate the copy buffer
  938. */
  939. struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
  940. gfp_mask);
  941. int oldheadroom = skb_headroom(skb);
  942. int head_copy_len, head_copy_off;
  943. int off;
  944. if (!n)
  945. return NULL;
  946. skb_reserve(n, newheadroom);
  947. /* Set the tail pointer and length */
  948. skb_put(n, skb->len);
  949. head_copy_len = oldheadroom;
  950. head_copy_off = 0;
  951. if (newheadroom <= head_copy_len)
  952. head_copy_len = newheadroom;
  953. else
  954. head_copy_off = newheadroom - head_copy_len;
  955. /* Copy the linear header and data. */
  956. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  957. skb->len + head_copy_len))
  958. BUG();
  959. copy_skb_header(n, skb);
  960. off = newheadroom - oldheadroom;
  961. if (n->ip_summed == CHECKSUM_PARTIAL)
  962. n->csum_start += off;
  963. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  964. n->transport_header += off;
  965. n->network_header += off;
  966. if (skb_mac_header_was_set(skb))
  967. n->mac_header += off;
  968. #endif
  969. return n;
  970. }
  971. EXPORT_SYMBOL(skb_copy_expand);
  972. /**
  973. * skb_pad - zero pad the tail of an skb
  974. * @skb: buffer to pad
  975. * @pad: space to pad
  976. *
  977. * Ensure that a buffer is followed by a padding area that is zero
  978. * filled. Used by network drivers which may DMA or transfer data
  979. * beyond the buffer end onto the wire.
  980. *
  981. * May return error in out of memory cases. The skb is freed on error.
  982. */
  983. int skb_pad(struct sk_buff *skb, int pad)
  984. {
  985. int err;
  986. int ntail;
  987. /* If the skbuff is non linear tailroom is always zero.. */
  988. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  989. memset(skb->data+skb->len, 0, pad);
  990. return 0;
  991. }
  992. ntail = skb->data_len + pad - (skb->end - skb->tail);
  993. if (likely(skb_cloned(skb) || ntail > 0)) {
  994. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  995. if (unlikely(err))
  996. goto free_skb;
  997. }
  998. /* FIXME: The use of this function with non-linear skb's really needs
  999. * to be audited.
  1000. */
  1001. err = skb_linearize(skb);
  1002. if (unlikely(err))
  1003. goto free_skb;
  1004. memset(skb->data + skb->len, 0, pad);
  1005. return 0;
  1006. free_skb:
  1007. kfree_skb(skb);
  1008. return err;
  1009. }
  1010. EXPORT_SYMBOL(skb_pad);
  1011. /**
  1012. * skb_put - add data to a buffer
  1013. * @skb: buffer to use
  1014. * @len: amount of data to add
  1015. *
  1016. * This function extends the used data area of the buffer. If this would
  1017. * exceed the total buffer size the kernel will panic. A pointer to the
  1018. * first byte of the extra data is returned.
  1019. */
  1020. unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
  1021. {
  1022. unsigned char *tmp = skb_tail_pointer(skb);
  1023. SKB_LINEAR_ASSERT(skb);
  1024. skb->tail += len;
  1025. skb->len += len;
  1026. if (unlikely(skb->tail > skb->end))
  1027. skb_over_panic(skb, len, __builtin_return_address(0));
  1028. return tmp;
  1029. }
  1030. EXPORT_SYMBOL(skb_put);
  1031. /**
  1032. * skb_push - add data to the start of a buffer
  1033. * @skb: buffer to use
  1034. * @len: amount of data to add
  1035. *
  1036. * This function extends the used data area of the buffer at the buffer
  1037. * start. If this would exceed the total buffer headroom the kernel will
  1038. * panic. A pointer to the first byte of the extra data is returned.
  1039. */
  1040. unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
  1041. {
  1042. skb->data -= len;
  1043. skb->len += len;
  1044. if (unlikely(skb->data<skb->head))
  1045. skb_under_panic(skb, len, __builtin_return_address(0));
  1046. return skb->data;
  1047. }
  1048. EXPORT_SYMBOL(skb_push);
  1049. /**
  1050. * skb_pull - remove data from the start of a buffer
  1051. * @skb: buffer to use
  1052. * @len: amount of data to remove
  1053. *
  1054. * This function removes data from the start of a buffer, returning
  1055. * the memory to the headroom. A pointer to the next data in the buffer
  1056. * is returned. Once the data has been pulled future pushes will overwrite
  1057. * the old data.
  1058. */
  1059. unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
  1060. {
  1061. return skb_pull_inline(skb, len);
  1062. }
  1063. EXPORT_SYMBOL(skb_pull);
  1064. /**
  1065. * skb_trim - remove end from a buffer
  1066. * @skb: buffer to alter
  1067. * @len: new length
  1068. *
  1069. * Cut the length of a buffer down by removing data from the tail. If
  1070. * the buffer is already under the length specified it is not modified.
  1071. * The skb must be linear.
  1072. */
  1073. void skb_trim(struct sk_buff *skb, unsigned int len)
  1074. {
  1075. if (skb->len > len)
  1076. __skb_trim(skb, len);
  1077. }
  1078. EXPORT_SYMBOL(skb_trim);
  1079. /* Trims skb to length len. It can change skb pointers.
  1080. */
  1081. int ___pskb_trim(struct sk_buff *skb, unsigned int len)
  1082. {
  1083. struct sk_buff **fragp;
  1084. struct sk_buff *frag;
  1085. int offset = skb_headlen(skb);
  1086. int nfrags = skb_shinfo(skb)->nr_frags;
  1087. int i;
  1088. int err;
  1089. if (skb_cloned(skb) &&
  1090. unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
  1091. return err;
  1092. i = 0;
  1093. if (offset >= len)
  1094. goto drop_pages;
  1095. for (; i < nfrags; i++) {
  1096. int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1097. if (end < len) {
  1098. offset = end;
  1099. continue;
  1100. }
  1101. skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
  1102. drop_pages:
  1103. skb_shinfo(skb)->nr_frags = i;
  1104. for (; i < nfrags; i++)
  1105. skb_frag_unref(skb, i);
  1106. if (skb_has_frag_list(skb))
  1107. skb_drop_fraglist(skb);
  1108. goto done;
  1109. }
  1110. for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
  1111. fragp = &frag->next) {
  1112. int end = offset + frag->len;
  1113. if (skb_shared(frag)) {
  1114. struct sk_buff *nfrag;
  1115. nfrag = skb_clone(frag, GFP_ATOMIC);
  1116. if (unlikely(!nfrag))
  1117. return -ENOMEM;
  1118. nfrag->next = frag->next;
  1119. kfree_skb(frag);
  1120. frag = nfrag;
  1121. *fragp = frag;
  1122. }
  1123. if (end < len) {
  1124. offset = end;
  1125. continue;
  1126. }
  1127. if (end > len &&
  1128. unlikely((err = pskb_trim(frag, len - offset))))
  1129. return err;
  1130. if (frag->next)
  1131. skb_drop_list(&frag->next);
  1132. break;
  1133. }
  1134. done:
  1135. if (len > skb_headlen(skb)) {
  1136. skb->data_len -= skb->len - len;
  1137. skb->len = len;
  1138. } else {
  1139. skb->len = len;
  1140. skb->data_len = 0;
  1141. skb_set_tail_pointer(skb, len);
  1142. }
  1143. return 0;
  1144. }
  1145. EXPORT_SYMBOL(___pskb_trim);
  1146. /**
  1147. * __pskb_pull_tail - advance tail of skb header
  1148. * @skb: buffer to reallocate
  1149. * @delta: number of bytes to advance tail
  1150. *
  1151. * The function makes a sense only on a fragmented &sk_buff,
  1152. * it expands header moving its tail forward and copying necessary
  1153. * data from fragmented part.
  1154. *
  1155. * &sk_buff MUST have reference count of 1.
  1156. *
  1157. * Returns %NULL (and &sk_buff does not change) if pull failed
  1158. * or value of new tail of skb in the case of success.
  1159. *
  1160. * All the pointers pointing into skb header may change and must be
  1161. * reloaded after call to this function.
  1162. */
  1163. /* Moves tail of skb head forward, copying data from fragmented part,
  1164. * when it is necessary.
  1165. * 1. It may fail due to malloc failure.
  1166. * 2. It may change skb pointers.
  1167. *
  1168. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  1169. */
  1170. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  1171. {
  1172. /* If skb has not enough free space at tail, get new one
  1173. * plus 128 bytes for future expansions. If we have enough
  1174. * room at tail, reallocate without expansion only if skb is cloned.
  1175. */
  1176. int i, k, eat = (skb->tail + delta) - skb->end;
  1177. if (eat > 0 || skb_cloned(skb)) {
  1178. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  1179. GFP_ATOMIC))
  1180. return NULL;
  1181. }
  1182. if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
  1183. BUG();
  1184. /* Optimization: no fragments, no reasons to preestimate
  1185. * size of pulled pages. Superb.
  1186. */
  1187. if (!skb_has_frag_list(skb))
  1188. goto pull_pages;
  1189. /* Estimate size of pulled pages. */
  1190. eat = delta;
  1191. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1192. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1193. if (size >= eat)
  1194. goto pull_pages;
  1195. eat -= size;
  1196. }
  1197. /* If we need update frag list, we are in troubles.
  1198. * Certainly, it possible to add an offset to skb data,
  1199. * but taking into account that pulling is expected to
  1200. * be very rare operation, it is worth to fight against
  1201. * further bloating skb head and crucify ourselves here instead.
  1202. * Pure masohism, indeed. 8)8)
  1203. */
  1204. if (eat) {
  1205. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1206. struct sk_buff *clone = NULL;
  1207. struct sk_buff *insp = NULL;
  1208. do {
  1209. BUG_ON(!list);
  1210. if (list->len <= eat) {
  1211. /* Eaten as whole. */
  1212. eat -= list->len;
  1213. list = list->next;
  1214. insp = list;
  1215. } else {
  1216. /* Eaten partially. */
  1217. if (skb_shared(list)) {
  1218. /* Sucks! We need to fork list. :-( */
  1219. clone = skb_clone(list, GFP_ATOMIC);
  1220. if (!clone)
  1221. return NULL;
  1222. insp = list->next;
  1223. list = clone;
  1224. } else {
  1225. /* This may be pulled without
  1226. * problems. */
  1227. insp = list;
  1228. }
  1229. if (!pskb_pull(list, eat)) {
  1230. kfree_skb(clone);
  1231. return NULL;
  1232. }
  1233. break;
  1234. }
  1235. } while (eat);
  1236. /* Free pulled out fragments. */
  1237. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  1238. skb_shinfo(skb)->frag_list = list->next;
  1239. kfree_skb(list);
  1240. }
  1241. /* And insert new clone at head. */
  1242. if (clone) {
  1243. clone->next = list;
  1244. skb_shinfo(skb)->frag_list = clone;
  1245. }
  1246. }
  1247. /* Success! Now we may commit changes to skb data. */
  1248. pull_pages:
  1249. eat = delta;
  1250. k = 0;
  1251. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1252. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1253. if (size <= eat) {
  1254. skb_frag_unref(skb, i);
  1255. eat -= size;
  1256. } else {
  1257. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  1258. if (eat) {
  1259. skb_shinfo(skb)->frags[k].page_offset += eat;
  1260. skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
  1261. eat = 0;
  1262. }
  1263. k++;
  1264. }
  1265. }
  1266. skb_shinfo(skb)->nr_frags = k;
  1267. skb->tail += delta;
  1268. skb->data_len -= delta;
  1269. return skb_tail_pointer(skb);
  1270. }
  1271. EXPORT_SYMBOL(__pskb_pull_tail);
  1272. /**
  1273. * skb_copy_bits - copy bits from skb to kernel buffer
  1274. * @skb: source skb
  1275. * @offset: offset in source
  1276. * @to: destination buffer
  1277. * @len: number of bytes to copy
  1278. *
  1279. * Copy the specified number of bytes from the source skb to the
  1280. * destination buffer.
  1281. *
  1282. * CAUTION ! :
  1283. * If its prototype is ever changed,
  1284. * check arch/{*}/net/{*}.S files,
  1285. * since it is called from BPF assembly code.
  1286. */
  1287. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  1288. {
  1289. int start = skb_headlen(skb);
  1290. struct sk_buff *frag_iter;
  1291. int i, copy;
  1292. if (offset > (int)skb->len - len)
  1293. goto fault;
  1294. /* Copy header. */
  1295. if ((copy = start - offset) > 0) {
  1296. if (copy > len)
  1297. copy = len;
  1298. skb_copy_from_linear_data_offset(skb, offset, to, copy);
  1299. if ((len -= copy) == 0)
  1300. return 0;
  1301. offset += copy;
  1302. to += copy;
  1303. }
  1304. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1305. int end;
  1306. WARN_ON(start > offset + len);
  1307. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1308. if ((copy = end - offset) > 0) {
  1309. u8 *vaddr;
  1310. if (copy > len)
  1311. copy = len;
  1312. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  1313. memcpy(to,
  1314. vaddr + skb_shinfo(skb)->frags[i].page_offset+
  1315. offset - start, copy);
  1316. kunmap_skb_frag(vaddr);
  1317. if ((len -= copy) == 0)
  1318. return 0;
  1319. offset += copy;
  1320. to += copy;
  1321. }
  1322. start = end;
  1323. }
  1324. skb_walk_frags(skb, frag_iter) {
  1325. int end;
  1326. WARN_ON(start > offset + len);
  1327. end = start + frag_iter->len;
  1328. if ((copy = end - offset) > 0) {
  1329. if (copy > len)
  1330. copy = len;
  1331. if (skb_copy_bits(frag_iter, offset - start, to, copy))
  1332. goto fault;
  1333. if ((len -= copy) == 0)
  1334. return 0;
  1335. offset += copy;
  1336. to += copy;
  1337. }
  1338. start = end;
  1339. }
  1340. if (!len)
  1341. return 0;
  1342. fault:
  1343. return -EFAULT;
  1344. }
  1345. EXPORT_SYMBOL(skb_copy_bits);
  1346. /*
  1347. * Callback from splice_to_pipe(), if we need to release some pages
  1348. * at the end of the spd in case we error'ed out in filling the pipe.
  1349. */
  1350. static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  1351. {
  1352. put_page(spd->pages[i]);
  1353. }
  1354. static inline struct page *linear_to_page(struct page *page, unsigned int *len,
  1355. unsigned int *offset,
  1356. struct sk_buff *skb, struct sock *sk)
  1357. {
  1358. struct page *p = sk->sk_sndmsg_page;
  1359. unsigned int off;
  1360. if (!p) {
  1361. new_page:
  1362. p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
  1363. if (!p)
  1364. return NULL;
  1365. off = sk->sk_sndmsg_off = 0;
  1366. /* hold one ref to this page until it's full */
  1367. } else {
  1368. unsigned int mlen;
  1369. off = sk->sk_sndmsg_off;
  1370. mlen = PAGE_SIZE - off;
  1371. if (mlen < 64 && mlen < *len) {
  1372. put_page(p);
  1373. goto new_page;
  1374. }
  1375. *len = min_t(unsigned int, *len, mlen);
  1376. }
  1377. memcpy(page_address(p) + off, page_address(page) + *offset, *len);
  1378. sk->sk_sndmsg_off += *len;
  1379. *offset = off;
  1380. get_page(p);
  1381. return p;
  1382. }
  1383. /*
  1384. * Fill page/offset/length into spd, if it can hold more pages.
  1385. */
  1386. static inline int spd_fill_page(struct splice_pipe_desc *spd,
  1387. struct pipe_inode_info *pipe, struct page *page,
  1388. unsigned int *len, unsigned int offset,
  1389. struct sk_buff *skb, int linear,
  1390. struct sock *sk)
  1391. {
  1392. if (unlikely(spd->nr_pages == pipe->buffers))
  1393. return 1;
  1394. if (linear) {
  1395. page = linear_to_page(page, len, &offset, skb, sk);
  1396. if (!page)
  1397. return 1;
  1398. } else
  1399. get_page(page);
  1400. spd->pages[spd->nr_pages] = page;
  1401. spd->partial[spd->nr_pages].len = *len;
  1402. spd->partial[spd->nr_pages].offset = offset;
  1403. spd->nr_pages++;
  1404. return 0;
  1405. }
  1406. static inline void __segment_seek(struct page **page, unsigned int *poff,
  1407. unsigned int *plen, unsigned int off)
  1408. {
  1409. unsigned long n;
  1410. *poff += off;
  1411. n = *poff / PAGE_SIZE;
  1412. if (n)
  1413. *page = nth_page(*page, n);
  1414. *poff = *poff % PAGE_SIZE;
  1415. *plen -= off;
  1416. }
  1417. static inline int __splice_segment(struct page *page, unsigned int poff,
  1418. unsigned int plen, unsigned int *off,
  1419. unsigned int *len, struct sk_buff *skb,
  1420. struct splice_pipe_desc *spd, int linear,
  1421. struct sock *sk,
  1422. struct pipe_inode_info *pipe)
  1423. {
  1424. if (!*len)
  1425. return 1;
  1426. /* skip this segment if already processed */
  1427. if (*off >= plen) {
  1428. *off -= plen;
  1429. return 0;
  1430. }
  1431. /* ignore any bits we already processed */
  1432. if (*off) {
  1433. __segment_seek(&page, &poff, &plen, *off);
  1434. *off = 0;
  1435. }
  1436. do {
  1437. unsigned int flen = min(*len, plen);
  1438. /* the linear region may spread across several pages */
  1439. flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
  1440. if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
  1441. return 1;
  1442. __segment_seek(&page, &poff, &plen, flen);
  1443. *len -= flen;
  1444. } while (*len && plen);
  1445. return 0;
  1446. }
  1447. /*
  1448. * Map linear and fragment data from the skb to spd. It reports failure if the
  1449. * pipe is full or if we already spliced the requested length.
  1450. */
  1451. static int __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
  1452. unsigned int *offset, unsigned int *len,
  1453. struct splice_pipe_desc *spd, struct sock *sk)
  1454. {
  1455. int seg;
  1456. /*
  1457. * map the linear part
  1458. */
  1459. if (__splice_segment(virt_to_page(skb->data),
  1460. (unsigned long) skb->data & (PAGE_SIZE - 1),
  1461. skb_headlen(skb),
  1462. offset, len, skb, spd, 1, sk, pipe))
  1463. return 1;
  1464. /*
  1465. * then map the fragments
  1466. */
  1467. for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
  1468. const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
  1469. if (__splice_segment(skb_frag_page(f),
  1470. f->page_offset, skb_frag_size(f),
  1471. offset, len, skb, spd, 0, sk, pipe))
  1472. return 1;
  1473. }
  1474. return 0;
  1475. }
  1476. /*
  1477. * Map data from the skb to a pipe. Should handle both the linear part,
  1478. * the fragments, and the frag list. It does NOT handle frag lists within
  1479. * the frag list, if such a thing exists. We'd probably need to recurse to
  1480. * handle that cleanly.
  1481. */
  1482. int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
  1483. struct pipe_inode_info *pipe, unsigned int tlen,
  1484. unsigned int flags)
  1485. {
  1486. struct partial_page partial[PIPE_DEF_BUFFERS];
  1487. struct page *pages[PIPE_DEF_BUFFERS];
  1488. struct splice_pipe_desc spd = {
  1489. .pages = pages,
  1490. .partial = partial,
  1491. .flags = flags,
  1492. .ops = &sock_pipe_buf_ops,
  1493. .spd_release = sock_spd_release,
  1494. };
  1495. struct sk_buff *frag_iter;
  1496. struct sock *sk = skb->sk;
  1497. int ret = 0;
  1498. if (splice_grow_spd(pipe, &spd))
  1499. return -ENOMEM;
  1500. /*
  1501. * __skb_splice_bits() only fails if the output has no room left,
  1502. * so no point in going over the frag_list for the error case.
  1503. */
  1504. if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
  1505. goto done;
  1506. else if (!tlen)
  1507. goto done;
  1508. /*
  1509. * now see if we have a frag_list to map
  1510. */
  1511. skb_walk_frags(skb, frag_iter) {
  1512. if (!tlen)
  1513. break;
  1514. if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
  1515. break;
  1516. }
  1517. done:
  1518. if (spd.nr_pages) {
  1519. /*
  1520. * Drop the socket lock, otherwise we have reverse
  1521. * locking dependencies between sk_lock and i_mutex
  1522. * here as compared to sendfile(). We enter here
  1523. * with the socket lock held, and splice_to_pipe() will
  1524. * grab the pipe inode lock. For sendfile() emulation,
  1525. * we call into ->sendpage() with the i_mutex lock held
  1526. * and networking will grab the socket lock.
  1527. */
  1528. release_sock(sk);
  1529. ret = splice_to_pipe(pipe, &spd);
  1530. lock_sock(sk);
  1531. }
  1532. splice_shrink_spd(pipe, &spd);
  1533. return ret;
  1534. }
  1535. /**
  1536. * skb_store_bits - store bits from kernel buffer to skb
  1537. * @skb: destination buffer
  1538. * @offset: offset in destination
  1539. * @from: source buffer
  1540. * @len: number of bytes to copy
  1541. *
  1542. * Copy the specified number of bytes from the source buffer to the
  1543. * destination skb. This function handles all the messy bits of
  1544. * traversing fragment lists and such.
  1545. */
  1546. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  1547. {
  1548. int start = skb_headlen(skb);
  1549. struct sk_buff *frag_iter;
  1550. int i, copy;
  1551. if (offset > (int)skb->len - len)
  1552. goto fault;
  1553. if ((copy = start - offset) > 0) {
  1554. if (copy > len)
  1555. copy = len;
  1556. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  1557. if ((len -= copy) == 0)
  1558. return 0;
  1559. offset += copy;
  1560. from += copy;
  1561. }
  1562. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1563. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1564. int end;
  1565. WARN_ON(start > offset + len);
  1566. end = start + skb_frag_size(frag);
  1567. if ((copy = end - offset) > 0) {
  1568. u8 *vaddr;
  1569. if (copy > len)
  1570. copy = len;
  1571. vaddr = kmap_skb_frag(frag);
  1572. memcpy(vaddr + frag->page_offset + offset - start,
  1573. from, copy);
  1574. kunmap_skb_frag(vaddr);
  1575. if ((len -= copy) == 0)
  1576. return 0;
  1577. offset += copy;
  1578. from += copy;
  1579. }
  1580. start = end;
  1581. }
  1582. skb_walk_frags(skb, frag_iter) {
  1583. int end;
  1584. WARN_ON(start > offset + len);
  1585. end = start + frag_iter->len;
  1586. if ((copy = end - offset) > 0) {
  1587. if (copy > len)
  1588. copy = len;
  1589. if (skb_store_bits(frag_iter, offset - start,
  1590. from, copy))
  1591. goto fault;
  1592. if ((len -= copy) == 0)
  1593. return 0;
  1594. offset += copy;
  1595. from += copy;
  1596. }
  1597. start = end;
  1598. }
  1599. if (!len)
  1600. return 0;
  1601. fault:
  1602. return -EFAULT;
  1603. }
  1604. EXPORT_SYMBOL(skb_store_bits);
  1605. /* Checksum skb data. */
  1606. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1607. int len, __wsum csum)
  1608. {
  1609. int start = skb_headlen(skb);
  1610. int i, copy = start - offset;
  1611. struct sk_buff *frag_iter;
  1612. int pos = 0;
  1613. /* Checksum header. */
  1614. if (copy > 0) {
  1615. if (copy > len)
  1616. copy = len;
  1617. csum = csum_partial(skb->data + offset, copy, csum);
  1618. if ((len -= copy) == 0)
  1619. return csum;
  1620. offset += copy;
  1621. pos = copy;
  1622. }
  1623. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1624. int end;
  1625. WARN_ON(start > offset + len);
  1626. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1627. if ((copy = end - offset) > 0) {
  1628. __wsum csum2;
  1629. u8 *vaddr;
  1630. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1631. if (copy > len)
  1632. copy = len;
  1633. vaddr = kmap_skb_frag(frag);
  1634. csum2 = csum_partial(vaddr + frag->page_offset +
  1635. offset - start, copy, 0);
  1636. kunmap_skb_frag(vaddr);
  1637. csum = csum_block_add(csum, csum2, pos);
  1638. if (!(len -= copy))
  1639. return csum;
  1640. offset += copy;
  1641. pos += copy;
  1642. }
  1643. start = end;
  1644. }
  1645. skb_walk_frags(skb, frag_iter) {
  1646. int end;
  1647. WARN_ON(start > offset + len);
  1648. end = start + frag_iter->len;
  1649. if ((copy = end - offset) > 0) {
  1650. __wsum csum2;
  1651. if (copy > len)
  1652. copy = len;
  1653. csum2 = skb_checksum(frag_iter, offset - start,
  1654. copy, 0);
  1655. csum = csum_block_add(csum, csum2, pos);
  1656. if ((len -= copy) == 0)
  1657. return csum;
  1658. offset += copy;
  1659. pos += copy;
  1660. }
  1661. start = end;
  1662. }
  1663. BUG_ON(len);
  1664. return csum;
  1665. }
  1666. EXPORT_SYMBOL(skb_checksum);
  1667. /* Both of above in one bottle. */
  1668. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1669. u8 *to, int len, __wsum csum)
  1670. {
  1671. int start = skb_headlen(skb);
  1672. int i, copy = start - offset;
  1673. struct sk_buff *frag_iter;
  1674. int pos = 0;
  1675. /* Copy header. */
  1676. if (copy > 0) {
  1677. if (copy > len)
  1678. copy = len;
  1679. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1680. copy, csum);
  1681. if ((len -= copy) == 0)
  1682. return csum;
  1683. offset += copy;
  1684. to += copy;
  1685. pos = copy;
  1686. }
  1687. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1688. int end;
  1689. WARN_ON(start > offset + len);
  1690. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1691. if ((copy = end - offset) > 0) {
  1692. __wsum csum2;
  1693. u8 *vaddr;
  1694. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1695. if (copy > len)
  1696. copy = len;
  1697. vaddr = kmap_skb_frag(frag);
  1698. csum2 = csum_partial_copy_nocheck(vaddr +
  1699. frag->page_offset +
  1700. offset - start, to,
  1701. copy, 0);
  1702. kunmap_skb_frag(vaddr);
  1703. csum = csum_block_add(csum, csum2, pos);
  1704. if (!(len -= copy))
  1705. return csum;
  1706. offset += copy;
  1707. to += copy;
  1708. pos += copy;
  1709. }
  1710. start = end;
  1711. }
  1712. skb_walk_frags(skb, frag_iter) {
  1713. __wsum csum2;
  1714. int end;
  1715. WARN_ON(start > offset + len);
  1716. end = start + frag_iter->len;
  1717. if ((copy = end - offset) > 0) {
  1718. if (copy > len)
  1719. copy = len;
  1720. csum2 = skb_copy_and_csum_bits(frag_iter,
  1721. offset - start,
  1722. to, copy, 0);
  1723. csum = csum_block_add(csum, csum2, pos);
  1724. if ((len -= copy) == 0)
  1725. return csum;
  1726. offset += copy;
  1727. to += copy;
  1728. pos += copy;
  1729. }
  1730. start = end;
  1731. }
  1732. BUG_ON(len);
  1733. return csum;
  1734. }
  1735. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1736. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1737. {
  1738. __wsum csum;
  1739. long csstart;
  1740. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1741. csstart = skb_checksum_start_offset(skb);
  1742. else
  1743. csstart = skb_headlen(skb);
  1744. BUG_ON(csstart > skb_headlen(skb));
  1745. skb_copy_from_linear_data(skb, to, csstart);
  1746. csum = 0;
  1747. if (csstart != skb->len)
  1748. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1749. skb->len - csstart, 0);
  1750. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1751. long csstuff = csstart + skb->csum_offset;
  1752. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  1753. }
  1754. }
  1755. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1756. /**
  1757. * skb_dequeue - remove from the head of the queue
  1758. * @list: list to dequeue from
  1759. *
  1760. * Remove the head of the list. The list lock is taken so the function
  1761. * may be used safely with other locking list functions. The head item is
  1762. * returned or %NULL if the list is empty.
  1763. */
  1764. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1765. {
  1766. unsigned long flags;
  1767. struct sk_buff *result;
  1768. spin_lock_irqsave(&list->lock, flags);
  1769. result = __skb_dequeue(list);
  1770. spin_unlock_irqrestore(&list->lock, flags);
  1771. return result;
  1772. }
  1773. EXPORT_SYMBOL(skb_dequeue);
  1774. /**
  1775. * skb_dequeue_tail - remove from the tail of the queue
  1776. * @list: list to dequeue from
  1777. *
  1778. * Remove the tail of the list. The list lock is taken so the function
  1779. * may be used safely with other locking list functions. The tail item is
  1780. * returned or %NULL if the list is empty.
  1781. */
  1782. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1783. {
  1784. unsigned long flags;
  1785. struct sk_buff *result;
  1786. spin_lock_irqsave(&list->lock, flags);
  1787. result = __skb_dequeue_tail(list);
  1788. spin_unlock_irqrestore(&list->lock, flags);
  1789. return result;
  1790. }
  1791. EXPORT_SYMBOL(skb_dequeue_tail);
  1792. /**
  1793. * skb_queue_purge - empty a list
  1794. * @list: list to empty
  1795. *
  1796. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1797. * the list and one reference dropped. This function takes the list
  1798. * lock and is atomic with respect to other list locking functions.
  1799. */
  1800. void skb_queue_purge(struct sk_buff_head *list)
  1801. {
  1802. struct sk_buff *skb;
  1803. while ((skb = skb_dequeue(list)) != NULL)
  1804. kfree_skb(skb);
  1805. }
  1806. EXPORT_SYMBOL(skb_queue_purge);
  1807. /**
  1808. * skb_queue_head - queue a buffer at the list head
  1809. * @list: list to use
  1810. * @newsk: buffer to queue
  1811. *
  1812. * Queue a buffer at the start of the list. This function takes the
  1813. * list lock and can be used safely with other locking &sk_buff functions
  1814. * safely.
  1815. *
  1816. * A buffer cannot be placed on two lists at the same time.
  1817. */
  1818. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1819. {
  1820. unsigned long flags;
  1821. spin_lock_irqsave(&list->lock, flags);
  1822. __skb_queue_head(list, newsk);
  1823. spin_unlock_irqrestore(&list->lock, flags);
  1824. }
  1825. EXPORT_SYMBOL(skb_queue_head);
  1826. /**
  1827. * skb_queue_tail - queue a buffer at the list tail
  1828. * @list: list to use
  1829. * @newsk: buffer to queue
  1830. *
  1831. * Queue a buffer at the tail of the list. This function takes the
  1832. * list lock and can be used safely with other locking &sk_buff functions
  1833. * safely.
  1834. *
  1835. * A buffer cannot be placed on two lists at the same time.
  1836. */
  1837. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1838. {
  1839. unsigned long flags;
  1840. spin_lock_irqsave(&list->lock, flags);
  1841. __skb_queue_tail(list, newsk);
  1842. spin_unlock_irqrestore(&list->lock, flags);
  1843. }
  1844. EXPORT_SYMBOL(skb_queue_tail);
  1845. /**
  1846. * skb_unlink - remove a buffer from a list
  1847. * @skb: buffer to remove
  1848. * @list: list to use
  1849. *
  1850. * Remove a packet from a list. The list locks are taken and this
  1851. * function is atomic with respect to other list locked calls
  1852. *
  1853. * You must know what list the SKB is on.
  1854. */
  1855. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1856. {
  1857. unsigned long flags;
  1858. spin_lock_irqsave(&list->lock, flags);
  1859. __skb_unlink(skb, list);
  1860. spin_unlock_irqrestore(&list->lock, flags);
  1861. }
  1862. EXPORT_SYMBOL(skb_unlink);
  1863. /**
  1864. * skb_append - append a buffer
  1865. * @old: buffer to insert after
  1866. * @newsk: buffer to insert
  1867. * @list: list to use
  1868. *
  1869. * Place a packet after a given packet in a list. The list locks are taken
  1870. * and this function is atomic with respect to other list locked calls.
  1871. * A buffer cannot be placed on two lists at the same time.
  1872. */
  1873. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1874. {
  1875. unsigned long flags;
  1876. spin_lock_irqsave(&list->lock, flags);
  1877. __skb_queue_after(list, old, newsk);
  1878. spin_unlock_irqrestore(&list->lock, flags);
  1879. }
  1880. EXPORT_SYMBOL(skb_append);
  1881. /**
  1882. * skb_insert - insert a buffer
  1883. * @old: buffer to insert before
  1884. * @newsk: buffer to insert
  1885. * @list: list to use
  1886. *
  1887. * Place a packet before a given packet in a list. The list locks are
  1888. * taken and this function is atomic with respect to other list locked
  1889. * calls.
  1890. *
  1891. * A buffer cannot be placed on two lists at the same time.
  1892. */
  1893. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1894. {
  1895. unsigned long flags;
  1896. spin_lock_irqsave(&list->lock, flags);
  1897. __skb_insert(newsk, old->prev, old, list);
  1898. spin_unlock_irqrestore(&list->lock, flags);
  1899. }
  1900. EXPORT_SYMBOL(skb_insert);
  1901. static inline void skb_split_inside_header(struct sk_buff *skb,
  1902. struct sk_buff* skb1,
  1903. const u32 len, const int pos)
  1904. {
  1905. int i;
  1906. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  1907. pos - len);
  1908. /* And move data appendix as is. */
  1909. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1910. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1911. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1912. skb_shinfo(skb)->nr_frags = 0;
  1913. skb1->data_len = skb->data_len;
  1914. skb1->len += skb1->data_len;
  1915. skb->data_len = 0;
  1916. skb->len = len;
  1917. skb_set_tail_pointer(skb, len);
  1918. }
  1919. static inline void skb_split_no_header(struct sk_buff *skb,
  1920. struct sk_buff* skb1,
  1921. const u32 len, int pos)
  1922. {
  1923. int i, k = 0;
  1924. const int nfrags = skb_shinfo(skb)->nr_frags;
  1925. skb_shinfo(skb)->nr_frags = 0;
  1926. skb1->len = skb1->data_len = skb->len - len;
  1927. skb->len = len;
  1928. skb->data_len = len - pos;
  1929. for (i = 0; i < nfrags; i++) {
  1930. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1931. if (pos + size > len) {
  1932. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1933. if (pos < len) {
  1934. /* Split frag.
  1935. * We have two variants in this case:
  1936. * 1. Move all the frag to the second
  1937. * part, if it is possible. F.e.
  1938. * this approach is mandatory for TUX,
  1939. * where splitting is expensive.
  1940. * 2. Split is accurately. We make this.
  1941. */
  1942. skb_frag_ref(skb, i);
  1943. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1944. skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
  1945. skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
  1946. skb_shinfo(skb)->nr_frags++;
  1947. }
  1948. k++;
  1949. } else
  1950. skb_shinfo(skb)->nr_frags++;
  1951. pos += size;
  1952. }
  1953. skb_shinfo(skb1)->nr_frags = k;
  1954. }
  1955. /**
  1956. * skb_split - Split fragmented skb to two parts at length len.
  1957. * @skb: the buffer to split
  1958. * @skb1: the buffer to receive the second part
  1959. * @len: new length for skb
  1960. */
  1961. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1962. {
  1963. int pos = skb_headlen(skb);
  1964. if (len < pos) /* Split line is inside header. */
  1965. skb_split_inside_header(skb, skb1, len, pos);
  1966. else /* Second chunk has no header, nothing to copy. */
  1967. skb_split_no_header(skb, skb1, len, pos);
  1968. }
  1969. EXPORT_SYMBOL(skb_split);
  1970. /* Shifting from/to a cloned skb is a no-go.
  1971. *
  1972. * Caller cannot keep skb_shinfo related pointers past calling here!
  1973. */
  1974. static int skb_prepare_for_shift(struct sk_buff *skb)
  1975. {
  1976. return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1977. }
  1978. /**
  1979. * skb_shift - Shifts paged data partially from skb to another
  1980. * @tgt: buffer into which tail data gets added
  1981. * @skb: buffer from which the paged data comes from
  1982. * @shiftlen: shift up to this many bytes
  1983. *
  1984. * Attempts to shift up to shiftlen worth of bytes, which may be less than
  1985. * the length of the skb, from skb to tgt. Returns number bytes shifted.
  1986. * It's up to caller to free skb if everything was shifted.
  1987. *
  1988. * If @tgt runs out of frags, the whole operation is aborted.
  1989. *
  1990. * Skb cannot include anything else but paged data while tgt is allowed
  1991. * to have non-paged data as well.
  1992. *
  1993. * TODO: full sized shift could be optimized but that would need
  1994. * specialized skb free'er to handle frags without up-to-date nr_frags.
  1995. */
  1996. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
  1997. {
  1998. int from, to, merge, todo;
  1999. struct skb_frag_struct *fragfrom, *fragto;
  2000. BUG_ON(shiftlen > skb->len);
  2001. BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
  2002. todo = shiftlen;
  2003. from = 0;
  2004. to = skb_shinfo(tgt)->nr_frags;
  2005. fragfrom = &skb_shinfo(skb)->frags[from];
  2006. /* Actual merge is delayed until the point when we know we can
  2007. * commit all, so that we don't have to undo partial changes
  2008. */
  2009. if (!to ||
  2010. !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
  2011. fragfrom->page_offset)) {
  2012. merge = -1;
  2013. } else {
  2014. merge = to - 1;
  2015. todo -= skb_frag_size(fragfrom);
  2016. if (todo < 0) {
  2017. if (skb_prepare_for_shift(skb) ||
  2018. skb_prepare_for_shift(tgt))
  2019. return 0;
  2020. /* All previous frag pointers might be stale! */
  2021. fragfrom = &skb_shinfo(skb)->frags[from];
  2022. fragto = &skb_shinfo(tgt)->frags[merge];
  2023. skb_frag_size_add(fragto, shiftlen);
  2024. skb_frag_size_sub(fragfrom, shiftlen);
  2025. fragfrom->page_offset += shiftlen;
  2026. goto onlymerged;
  2027. }
  2028. from++;
  2029. }
  2030. /* Skip full, not-fitting skb to avoid expensive operations */
  2031. if ((shiftlen == skb->len) &&
  2032. (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
  2033. return 0;
  2034. if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
  2035. return 0;
  2036. while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
  2037. if (to == MAX_SKB_FRAGS)
  2038. return 0;
  2039. fragfrom = &skb_shinfo(skb)->frags[from];
  2040. fragto = &skb_shinfo(tgt)->frags[to];
  2041. if (todo >= skb_frag_size(fragfrom)) {
  2042. *fragto = *fragfrom;
  2043. todo -= skb_frag_size(fragfrom);
  2044. from++;
  2045. to++;
  2046. } else {
  2047. __skb_frag_ref(fragfrom);
  2048. fragto->page = fragfrom->page;
  2049. fragto->page_offset = fragfrom->page_offset;
  2050. skb_frag_size_set(fragto, todo);
  2051. fragfrom->page_offset += todo;
  2052. skb_frag_size_sub(fragfrom, todo);
  2053. todo = 0;
  2054. to++;
  2055. break;
  2056. }
  2057. }
  2058. /* Ready to "commit" this state change to tgt */
  2059. skb_shinfo(tgt)->nr_frags = to;
  2060. if (merge >= 0) {
  2061. fragfrom = &skb_shinfo(skb)->frags[0];
  2062. fragto = &skb_shinfo(tgt)->frags[merge];
  2063. skb_frag_size_add(fragto, skb_frag_size(fragfrom));
  2064. __skb_frag_unref(fragfrom);
  2065. }
  2066. /* Reposition in the original skb */
  2067. to = 0;
  2068. while (from < skb_shinfo(skb)->nr_frags)
  2069. skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
  2070. skb_shinfo(skb)->nr_frags = to;
  2071. BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
  2072. onlymerged:
  2073. /* Most likely the tgt won't ever need its checksum anymore, skb on
  2074. * the other hand might need it if it needs to be resent
  2075. */
  2076. tgt->ip_summed = CHECKSUM_PARTIAL;
  2077. skb->ip_summed = CHECKSUM_PARTIAL;
  2078. /* Yak, is it really working this way? Some helper please? */
  2079. skb->len -= shiftlen;
  2080. skb->data_len -= shiftlen;
  2081. skb->truesize -= shiftlen;
  2082. tgt->len += shiftlen;
  2083. tgt->data_len += shiftlen;
  2084. tgt->truesize += shiftlen;
  2085. return shiftlen;
  2086. }
  2087. /**
  2088. * skb_prepare_seq_read - Prepare a sequential read of skb data
  2089. * @skb: the buffer to read
  2090. * @from: lower offset of data to be read
  2091. * @to: upper offset of data to be read
  2092. * @st: state variable
  2093. *
  2094. * Initializes the specified state variable. Must be called before
  2095. * invoking skb_seq_read() for the first time.
  2096. */
  2097. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  2098. unsigned int to, struct skb_seq_state *st)
  2099. {
  2100. st->lower_offset = from;
  2101. st->upper_offset = to;
  2102. st->root_skb = st->cur_skb = skb;
  2103. st->frag_idx = st->stepped_offset = 0;
  2104. st->frag_data = NULL;
  2105. }
  2106. EXPORT_SYMBOL(skb_prepare_seq_read);
  2107. /**
  2108. * skb_seq_read - Sequentially read skb data
  2109. * @consumed: number of bytes consumed by the caller so far
  2110. * @data: destination pointer for data to be returned
  2111. * @st: state variable
  2112. *
  2113. * Reads a block of skb data at &consumed relative to the
  2114. * lower offset specified to skb_prepare_seq_read(). Assigns
  2115. * the head of the data block to &data and returns the length
  2116. * of the block or 0 if the end of the skb data or the upper
  2117. * offset has been reached.
  2118. *
  2119. * The caller is not required to consume all of the data
  2120. * returned, i.e. &consumed is typically set to the number
  2121. * of bytes already consumed and the next call to
  2122. * skb_seq_read() will return the remaining part of the block.
  2123. *
  2124. * Note 1: The size of each block of data returned can be arbitrary,
  2125. * this limitation is the cost for zerocopy seqeuental
  2126. * reads of potentially non linear data.
  2127. *
  2128. * Note 2: Fragment lists within fragments are not implemented
  2129. * at the moment, state->root_skb could be replaced with
  2130. * a stack for this purpose.
  2131. */
  2132. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  2133. struct skb_seq_state *st)
  2134. {
  2135. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  2136. skb_frag_t *frag;
  2137. if (unlikely(abs_offset >= st->upper_offset))
  2138. return 0;
  2139. next_skb:
  2140. block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
  2141. if (abs_offset < block_limit && !st->frag_data) {
  2142. *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
  2143. return block_limit - abs_offset;
  2144. }
  2145. if (st->frag_idx == 0 && !st->frag_data)
  2146. st->stepped_offset += skb_headlen(st->cur_skb);
  2147. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  2148. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  2149. block_limit = skb_frag_size(frag) + st->stepped_offset;
  2150. if (abs_offset < block_limit) {
  2151. if (!st->frag_data)
  2152. st->frag_data = kmap_skb_frag(frag);
  2153. *data = (u8 *) st->frag_data + frag->page_offset +
  2154. (abs_offset - st->stepped_offset);
  2155. return block_limit - abs_offset;
  2156. }
  2157. if (st->frag_data) {
  2158. kunmap_skb_frag(st->frag_data);
  2159. st->frag_data = NULL;
  2160. }
  2161. st->frag_idx++;
  2162. st->stepped_offset += skb_frag_size(frag);
  2163. }
  2164. if (st->frag_data) {
  2165. kunmap_skb_frag(st->frag_data);
  2166. st->frag_data = NULL;
  2167. }
  2168. if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
  2169. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  2170. st->frag_idx = 0;
  2171. goto next_skb;
  2172. } else if (st->cur_skb->next) {
  2173. st->cur_skb = st->cur_skb->next;
  2174. st->frag_idx = 0;
  2175. goto next_skb;
  2176. }
  2177. return 0;
  2178. }
  2179. EXPORT_SYMBOL(skb_seq_read);
  2180. /**
  2181. * skb_abort_seq_read - Abort a sequential read of skb data
  2182. * @st: state variable
  2183. *
  2184. * Must be called if skb_seq_read() was not called until it
  2185. * returned 0.
  2186. */
  2187. void skb_abort_seq_read(struct skb_seq_state *st)
  2188. {
  2189. if (st->frag_data)
  2190. kunmap_skb_frag(st->frag_data);
  2191. }
  2192. EXPORT_SYMBOL(skb_abort_seq_read);
  2193. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  2194. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  2195. struct ts_config *conf,
  2196. struct ts_state *state)
  2197. {
  2198. return skb_seq_read(offset, text, TS_SKB_CB(state));
  2199. }
  2200. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  2201. {
  2202. skb_abort_seq_read(TS_SKB_CB(state));
  2203. }
  2204. /**
  2205. * skb_find_text - Find a text pattern in skb data
  2206. * @skb: the buffer to look in
  2207. * @from: search offset
  2208. * @to: search limit
  2209. * @config: textsearch configuration
  2210. * @state: uninitialized textsearch state variable
  2211. *
  2212. * Finds a pattern in the skb data according to the specified
  2213. * textsearch configuration. Use textsearch_next() to retrieve
  2214. * subsequent occurrences of the pattern. Returns the offset
  2215. * to the first occurrence or UINT_MAX if no match was found.
  2216. */
  2217. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  2218. unsigned int to, struct ts_config *config,
  2219. struct ts_state *state)
  2220. {
  2221. unsigned int ret;
  2222. config->get_next_block = skb_ts_get_next_block;
  2223. config->finish = skb_ts_finish;
  2224. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  2225. ret = textsearch_find(config, state);
  2226. return (ret <= to - from ? ret : UINT_MAX);
  2227. }
  2228. EXPORT_SYMBOL(skb_find_text);
  2229. /**
  2230. * skb_append_datato_frags: - append the user data to a skb
  2231. * @sk: sock structure
  2232. * @skb: skb structure to be appened with user data.
  2233. * @getfrag: call back function to be used for getting the user data
  2234. * @from: pointer to user message iov
  2235. * @length: length of the iov message
  2236. *
  2237. * Description: This procedure append the user data in the fragment part
  2238. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  2239. */
  2240. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  2241. int (*getfrag)(void *from, char *to, int offset,
  2242. int len, int odd, struct sk_buff *skb),
  2243. void *from, int length)
  2244. {
  2245. int frg_cnt = 0;
  2246. skb_frag_t *frag = NULL;
  2247. struct page *page = NULL;
  2248. int copy, left;
  2249. int offset = 0;
  2250. int ret;
  2251. do {
  2252. /* Return error if we don't have space for new frag */
  2253. frg_cnt = skb_shinfo(skb)->nr_frags;
  2254. if (frg_cnt >= MAX_SKB_FRAGS)
  2255. return -EFAULT;
  2256. /* allocate a new page for next frag */
  2257. page = alloc_pages(sk->sk_allocation, 0);
  2258. /* If alloc_page fails just return failure and caller will
  2259. * free previous allocated pages by doing kfree_skb()
  2260. */
  2261. if (page == NULL)
  2262. return -ENOMEM;
  2263. /* initialize the next frag */
  2264. skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
  2265. skb->truesize += PAGE_SIZE;
  2266. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  2267. /* get the new initialized frag */
  2268. frg_cnt = skb_shinfo(skb)->nr_frags;
  2269. frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
  2270. /* copy the user data to page */
  2271. left = PAGE_SIZE - frag->page_offset;
  2272. copy = (length > left)? left : length;
  2273. ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
  2274. offset, copy, 0, skb);
  2275. if (ret < 0)
  2276. return -EFAULT;
  2277. /* copy was successful so update the size parameters */
  2278. skb_frag_size_add(frag, copy);
  2279. skb->len += copy;
  2280. skb->data_len += copy;
  2281. offset += copy;
  2282. length -= copy;
  2283. } while (length > 0);
  2284. return 0;
  2285. }
  2286. EXPORT_SYMBOL(skb_append_datato_frags);
  2287. /**
  2288. * skb_pull_rcsum - pull skb and update receive checksum
  2289. * @skb: buffer to update
  2290. * @len: length of data pulled
  2291. *
  2292. * This function performs an skb_pull on the packet and updates
  2293. * the CHECKSUM_COMPLETE checksum. It should be used on
  2294. * receive path processing instead of skb_pull unless you know
  2295. * that the checksum difference is zero (e.g., a valid IP header)
  2296. * or you are setting ip_summed to CHECKSUM_NONE.
  2297. */
  2298. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  2299. {
  2300. BUG_ON(len > skb->len);
  2301. skb->len -= len;
  2302. BUG_ON(skb->len < skb->data_len);
  2303. skb_postpull_rcsum(skb, skb->data, len);
  2304. return skb->data += len;
  2305. }
  2306. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  2307. /**
  2308. * skb_segment - Perform protocol segmentation on skb.
  2309. * @skb: buffer to segment
  2310. * @features: features for the output path (see dev->features)
  2311. *
  2312. * This function performs segmentation on the given skb. It returns
  2313. * a pointer to the first in a list of new skbs for the segments.
  2314. * In case of error it returns ERR_PTR(err).
  2315. */
  2316. struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
  2317. {
  2318. struct sk_buff *segs = NULL;
  2319. struct sk_buff *tail = NULL;
  2320. struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
  2321. unsigned int mss = skb_shinfo(skb)->gso_size;
  2322. unsigned int doffset = skb->data - skb_mac_header(skb);
  2323. unsigned int offset = doffset;
  2324. unsigned int headroom;
  2325. unsigned int len;
  2326. int sg = !!(features & NETIF_F_SG);
  2327. int nfrags = skb_shinfo(skb)->nr_frags;
  2328. int err = -ENOMEM;
  2329. int i = 0;
  2330. int pos;
  2331. __skb_push(skb, doffset);
  2332. headroom = skb_headroom(skb);
  2333. pos = skb_headlen(skb);
  2334. do {
  2335. struct sk_buff *nskb;
  2336. skb_frag_t *frag;
  2337. int hsize;
  2338. int size;
  2339. len = skb->len - offset;
  2340. if (len > mss)
  2341. len = mss;
  2342. hsize = skb_headlen(skb) - offset;
  2343. if (hsize < 0)
  2344. hsize = 0;
  2345. if (hsize > len || !sg)
  2346. hsize = len;
  2347. if (!hsize && i >= nfrags) {
  2348. BUG_ON(fskb->len != len);
  2349. pos += len;
  2350. nskb = skb_clone(fskb, GFP_ATOMIC);
  2351. fskb = fskb->next;
  2352. if (unlikely(!nskb))
  2353. goto err;
  2354. hsize = skb_end_pointer(nskb) - nskb->head;
  2355. if (skb_cow_head(nskb, doffset + headroom)) {
  2356. kfree_skb(nskb);
  2357. goto err;
  2358. }
  2359. nskb->truesize += skb_end_pointer(nskb) - nskb->head -
  2360. hsize;
  2361. skb_release_head_state(nskb);
  2362. __skb_push(nskb, doffset);
  2363. } else {
  2364. nskb = alloc_skb(hsize + doffset + headroom,
  2365. GFP_ATOMIC);
  2366. if (unlikely(!nskb))
  2367. goto err;
  2368. skb_reserve(nskb, headroom);
  2369. __skb_put(nskb, doffset);
  2370. }
  2371. if (segs)
  2372. tail->next = nskb;
  2373. else
  2374. segs = nskb;
  2375. tail = nskb;
  2376. __copy_skb_header(nskb, skb);
  2377. nskb->mac_len = skb->mac_len;
  2378. /* nskb and skb might have different headroom */
  2379. if (nskb->ip_summed == CHECKSUM_PARTIAL)
  2380. nskb->csum_start += skb_headroom(nskb) - headroom;
  2381. skb_reset_mac_header(nskb);
  2382. skb_set_network_header(nskb, skb->mac_len);
  2383. nskb->transport_header = (nskb->network_header +
  2384. skb_network_header_len(skb));
  2385. skb_copy_from_linear_data(skb, nskb->data, doffset);
  2386. if (fskb != skb_shinfo(skb)->frag_list)
  2387. continue;
  2388. if (!sg) {
  2389. nskb->ip_summed = CHECKSUM_NONE;
  2390. nskb->csum = skb_copy_and_csum_bits(skb, offset,
  2391. skb_put(nskb, len),
  2392. len, 0);
  2393. continue;
  2394. }
  2395. frag = skb_shinfo(nskb)->frags;
  2396. skb_copy_from_linear_data_offset(skb, offset,
  2397. skb_put(nskb, hsize), hsize);
  2398. while (pos < offset + len && i < nfrags) {
  2399. *frag = skb_shinfo(skb)->frags[i];
  2400. __skb_frag_ref(frag);
  2401. size = skb_frag_size(frag);
  2402. if (pos < offset) {
  2403. frag->page_offset += offset - pos;
  2404. skb_frag_size_sub(frag, offset - pos);
  2405. }
  2406. skb_shinfo(nskb)->nr_frags++;
  2407. if (pos + size <= offset + len) {
  2408. i++;
  2409. pos += size;
  2410. } else {
  2411. skb_frag_size_sub(frag, pos + size - (offset + len));
  2412. goto skip_fraglist;
  2413. }
  2414. frag++;
  2415. }
  2416. if (pos < offset + len) {
  2417. struct sk_buff *fskb2 = fskb;
  2418. BUG_ON(pos + fskb->len != offset + len);
  2419. pos += fskb->len;
  2420. fskb = fskb->next;
  2421. if (fskb2->next) {
  2422. fskb2 = skb_clone(fskb2, GFP_ATOMIC);
  2423. if (!fskb2)
  2424. goto err;
  2425. } else
  2426. skb_get(fskb2);
  2427. SKB_FRAG_ASSERT(nskb);
  2428. skb_shinfo(nskb)->frag_list = fskb2;
  2429. }
  2430. skip_fraglist:
  2431. nskb->data_len = len - hsize;
  2432. nskb->len += nskb->data_len;
  2433. nskb->truesize += nskb->data_len;
  2434. } while ((offset += len) < skb->len);
  2435. return segs;
  2436. err:
  2437. while ((skb = segs)) {
  2438. segs = skb->next;
  2439. kfree_skb(skb);
  2440. }
  2441. return ERR_PTR(err);
  2442. }
  2443. EXPORT_SYMBOL_GPL(skb_segment);
  2444. int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  2445. {
  2446. struct sk_buff *p = *head;
  2447. struct sk_buff *nskb;
  2448. struct skb_shared_info *skbinfo = skb_shinfo(skb);
  2449. struct skb_shared_info *pinfo = skb_shinfo(p);
  2450. unsigned int headroom;
  2451. unsigned int len = skb_gro_len(skb);
  2452. unsigned int offset = skb_gro_offset(skb);
  2453. unsigned int headlen = skb_headlen(skb);
  2454. if (p->len + len >= 65536)
  2455. return -E2BIG;
  2456. if (pinfo->frag_list)
  2457. goto merge;
  2458. else if (headlen <= offset) {
  2459. skb_frag_t *frag;
  2460. skb_frag_t *frag2;
  2461. int i = skbinfo->nr_frags;
  2462. int nr_frags = pinfo->nr_frags + i;
  2463. offset -= headlen;
  2464. if (nr_frags > MAX_SKB_FRAGS)
  2465. return -E2BIG;
  2466. pinfo->nr_frags = nr_frags;
  2467. skbinfo->nr_frags = 0;
  2468. frag = pinfo->frags + nr_frags;
  2469. frag2 = skbinfo->frags + i;
  2470. do {
  2471. *--frag = *--frag2;
  2472. } while (--i);
  2473. frag->page_offset += offset;
  2474. skb_frag_size_sub(frag, offset);
  2475. skb->truesize -= skb->data_len;
  2476. skb->len -= skb->data_len;
  2477. skb->data_len = 0;
  2478. NAPI_GRO_CB(skb)->free = 1;
  2479. goto done;
  2480. } else if (skb_gro_len(p) != pinfo->gso_size)
  2481. return -E2BIG;
  2482. headroom = skb_headroom(p);
  2483. nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
  2484. if (unlikely(!nskb))
  2485. return -ENOMEM;
  2486. __copy_skb_header(nskb, p);
  2487. nskb->mac_len = p->mac_len;
  2488. skb_reserve(nskb, headroom);
  2489. __skb_put(nskb, skb_gro_offset(p));
  2490. skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
  2491. skb_set_network_header(nskb, skb_network_offset(p));
  2492. skb_set_transport_header(nskb, skb_transport_offset(p));
  2493. __skb_pull(p, skb_gro_offset(p));
  2494. memcpy(skb_mac_header(nskb), skb_mac_header(p),
  2495. p->data - skb_mac_header(p));
  2496. *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
  2497. skb_shinfo(nskb)->frag_list = p;
  2498. skb_shinfo(nskb)->gso_size = pinfo->gso_size;
  2499. pinfo->gso_size = 0;
  2500. skb_header_release(p);
  2501. nskb->prev = p;
  2502. nskb->data_len += p->len;
  2503. nskb->truesize += p->len;
  2504. nskb->len += p->len;
  2505. *head = nskb;
  2506. nskb->next = p->next;
  2507. p->next = NULL;
  2508. p = nskb;
  2509. merge:
  2510. if (offset > headlen) {
  2511. unsigned int eat = offset - headlen;
  2512. skbinfo->frags[0].page_offset += eat;
  2513. skb_frag_size_sub(&skbinfo->frags[0], eat);
  2514. skb->data_len -= eat;
  2515. skb->len -= eat;
  2516. offset = headlen;
  2517. }
  2518. __skb_pull(skb, offset);
  2519. p->prev->next = skb;
  2520. p->prev = skb;
  2521. skb_header_release(skb);
  2522. done:
  2523. NAPI_GRO_CB(p)->count++;
  2524. p->data_len += len;
  2525. p->truesize += len;
  2526. p->len += len;
  2527. NAPI_GRO_CB(skb)->same_flow = 1;
  2528. return 0;
  2529. }
  2530. EXPORT_SYMBOL_GPL(skb_gro_receive);
  2531. void __init skb_init(void)
  2532. {
  2533. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  2534. sizeof(struct sk_buff),
  2535. 0,
  2536. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2537. NULL);
  2538. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  2539. (2*sizeof(struct sk_buff)) +
  2540. sizeof(atomic_t),
  2541. 0,
  2542. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2543. NULL);
  2544. }
  2545. /**
  2546. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  2547. * @skb: Socket buffer containing the buffers to be mapped
  2548. * @sg: The scatter-gather list to map into
  2549. * @offset: The offset into the buffer's contents to start mapping
  2550. * @len: Length of buffer space to be mapped
  2551. *
  2552. * Fill the specified scatter-gather list with mappings/pointers into a
  2553. * region of the buffer space attached to a socket buffer.
  2554. */
  2555. static int
  2556. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2557. {
  2558. int start = skb_headlen(skb);
  2559. int i, copy = start - offset;
  2560. struct sk_buff *frag_iter;
  2561. int elt = 0;
  2562. if (copy > 0) {
  2563. if (copy > len)
  2564. copy = len;
  2565. sg_set_buf(sg, skb->data + offset, copy);
  2566. elt++;
  2567. if ((len -= copy) == 0)
  2568. return elt;
  2569. offset += copy;
  2570. }
  2571. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2572. int end;
  2573. WARN_ON(start > offset + len);
  2574. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2575. if ((copy = end - offset) > 0) {
  2576. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2577. if (copy > len)
  2578. copy = len;
  2579. sg_set_page(&sg[elt], skb_frag_page(frag), copy,
  2580. frag->page_offset+offset-start);
  2581. elt++;
  2582. if (!(len -= copy))
  2583. return elt;
  2584. offset += copy;
  2585. }
  2586. start = end;
  2587. }
  2588. skb_walk_frags(skb, frag_iter) {
  2589. int end;
  2590. WARN_ON(start > offset + len);
  2591. end = start + frag_iter->len;
  2592. if ((copy = end - offset) > 0) {
  2593. if (copy > len)
  2594. copy = len;
  2595. elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  2596. copy);
  2597. if ((len -= copy) == 0)
  2598. return elt;
  2599. offset += copy;
  2600. }
  2601. start = end;
  2602. }
  2603. BUG_ON(len);
  2604. return elt;
  2605. }
  2606. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2607. {
  2608. int nsg = __skb_to_sgvec(skb, sg, offset, len);
  2609. sg_mark_end(&sg[nsg - 1]);
  2610. return nsg;
  2611. }
  2612. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  2613. /**
  2614. * skb_cow_data - Check that a socket buffer's data buffers are writable
  2615. * @skb: The socket buffer to check.
  2616. * @tailbits: Amount of trailing space to be added
  2617. * @trailer: Returned pointer to the skb where the @tailbits space begins
  2618. *
  2619. * Make sure that the data buffers attached to a socket buffer are
  2620. * writable. If they are not, private copies are made of the data buffers
  2621. * and the socket buffer is set to use these instead.
  2622. *
  2623. * If @tailbits is given, make sure that there is space to write @tailbits
  2624. * bytes of data beyond current end of socket buffer. @trailer will be
  2625. * set to point to the skb in which this space begins.
  2626. *
  2627. * The number of scatterlist elements required to completely map the
  2628. * COW'd and extended socket buffer will be returned.
  2629. */
  2630. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  2631. {
  2632. int copyflag;
  2633. int elt;
  2634. struct sk_buff *skb1, **skb_p;
  2635. /* If skb is cloned or its head is paged, reallocate
  2636. * head pulling out all the pages (pages are considered not writable
  2637. * at the moment even if they are anonymous).
  2638. */
  2639. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  2640. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  2641. return -ENOMEM;
  2642. /* Easy case. Most of packets will go this way. */
  2643. if (!skb_has_frag_list(skb)) {
  2644. /* A little of trouble, not enough of space for trailer.
  2645. * This should not happen, when stack is tuned to generate
  2646. * good frames. OK, on miss we reallocate and reserve even more
  2647. * space, 128 bytes is fair. */
  2648. if (skb_tailroom(skb) < tailbits &&
  2649. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  2650. return -ENOMEM;
  2651. /* Voila! */
  2652. *trailer = skb;
  2653. return 1;
  2654. }
  2655. /* Misery. We are in troubles, going to mincer fragments... */
  2656. elt = 1;
  2657. skb_p = &skb_shinfo(skb)->frag_list;
  2658. copyflag = 0;
  2659. while ((skb1 = *skb_p) != NULL) {
  2660. int ntail = 0;
  2661. /* The fragment is partially pulled by someone,
  2662. * this can happen on input. Copy it and everything
  2663. * after it. */
  2664. if (skb_shared(skb1))
  2665. copyflag = 1;
  2666. /* If the skb is the last, worry about trailer. */
  2667. if (skb1->next == NULL && tailbits) {
  2668. if (skb_shinfo(skb1)->nr_frags ||
  2669. skb_has_frag_list(skb1) ||
  2670. skb_tailroom(skb1) < tailbits)
  2671. ntail = tailbits + 128;
  2672. }
  2673. if (copyflag ||
  2674. skb_cloned(skb1) ||
  2675. ntail ||
  2676. skb_shinfo(skb1)->nr_frags ||
  2677. skb_has_frag_list(skb1)) {
  2678. struct sk_buff *skb2;
  2679. /* Fuck, we are miserable poor guys... */
  2680. if (ntail == 0)
  2681. skb2 = skb_copy(skb1, GFP_ATOMIC);
  2682. else
  2683. skb2 = skb_copy_expand(skb1,
  2684. skb_headroom(skb1),
  2685. ntail,
  2686. GFP_ATOMIC);
  2687. if (unlikely(skb2 == NULL))
  2688. return -ENOMEM;
  2689. if (skb1->sk)
  2690. skb_set_owner_w(skb2, skb1->sk);
  2691. /* Looking around. Are we still alive?
  2692. * OK, link new skb, drop old one */
  2693. skb2->next = skb1->next;
  2694. *skb_p = skb2;
  2695. kfree_skb(skb1);
  2696. skb1 = skb2;
  2697. }
  2698. elt++;
  2699. *trailer = skb1;
  2700. skb_p = &skb1->next;
  2701. }
  2702. return elt;
  2703. }
  2704. EXPORT_SYMBOL_GPL(skb_cow_data);
  2705. static void sock_rmem_free(struct sk_buff *skb)
  2706. {
  2707. struct sock *sk = skb->sk;
  2708. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  2709. }
  2710. /*
  2711. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  2712. */
  2713. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  2714. {
  2715. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  2716. (unsigned)sk->sk_rcvbuf)
  2717. return -ENOMEM;
  2718. skb_orphan(skb);
  2719. skb->sk = sk;
  2720. skb->destructor = sock_rmem_free;
  2721. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  2722. /* before exiting rcu section, make sure dst is refcounted */
  2723. skb_dst_force(skb);
  2724. skb_queue_tail(&sk->sk_error_queue, skb);
  2725. if (!sock_flag(sk, SOCK_DEAD))
  2726. sk->sk_data_ready(sk, skb->len);
  2727. return 0;
  2728. }
  2729. EXPORT_SYMBOL(sock_queue_err_skb);
  2730. void skb_tstamp_tx(struct sk_buff *orig_skb,
  2731. struct skb_shared_hwtstamps *hwtstamps)
  2732. {
  2733. struct sock *sk = orig_skb->sk;
  2734. struct sock_exterr_skb *serr;
  2735. struct sk_buff *skb;
  2736. int err;
  2737. if (!sk)
  2738. return;
  2739. skb = skb_clone(orig_skb, GFP_ATOMIC);
  2740. if (!skb)
  2741. return;
  2742. if (hwtstamps) {
  2743. *skb_hwtstamps(skb) =
  2744. *hwtstamps;
  2745. } else {
  2746. /*
  2747. * no hardware time stamps available,
  2748. * so keep the shared tx_flags and only
  2749. * store software time stamp
  2750. */
  2751. skb->tstamp = ktime_get_real();
  2752. }
  2753. serr = SKB_EXT_ERR(skb);
  2754. memset(serr, 0, sizeof(*serr));
  2755. serr->ee.ee_errno = ENOMSG;
  2756. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  2757. err = sock_queue_err_skb(sk, skb);
  2758. if (err)
  2759. kfree_skb(skb);
  2760. }
  2761. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  2762. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  2763. {
  2764. struct sock *sk = skb->sk;
  2765. struct sock_exterr_skb *serr;
  2766. int err;
  2767. skb->wifi_acked_valid = 1;
  2768. skb->wifi_acked = acked;
  2769. serr = SKB_EXT_ERR(skb);
  2770. memset(serr, 0, sizeof(*serr));
  2771. serr->ee.ee_errno = ENOMSG;
  2772. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  2773. err = sock_queue_err_skb(sk, skb);
  2774. if (err)
  2775. kfree_skb(skb);
  2776. }
  2777. EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
  2778. /**
  2779. * skb_partial_csum_set - set up and verify partial csum values for packet
  2780. * @skb: the skb to set
  2781. * @start: the number of bytes after skb->data to start checksumming.
  2782. * @off: the offset from start to place the checksum.
  2783. *
  2784. * For untrusted partially-checksummed packets, we need to make sure the values
  2785. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  2786. *
  2787. * This function checks and sets those values and skb->ip_summed: if this
  2788. * returns false you should drop the packet.
  2789. */
  2790. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  2791. {
  2792. if (unlikely(start > skb_headlen(skb)) ||
  2793. unlikely((int)start + off > skb_headlen(skb) - 2)) {
  2794. if (net_ratelimit())
  2795. printk(KERN_WARNING
  2796. "bad partial csum: csum=%u/%u len=%u\n",
  2797. start, off, skb_headlen(skb));
  2798. return false;
  2799. }
  2800. skb->ip_summed = CHECKSUM_PARTIAL;
  2801. skb->csum_start = skb_headroom(skb) + start;
  2802. skb->csum_offset = off;
  2803. return true;
  2804. }
  2805. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  2806. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  2807. {
  2808. if (net_ratelimit())
  2809. pr_warning("%s: received packets cannot be forwarded"
  2810. " while LRO is enabled\n", skb->dev->name);
  2811. }
  2812. EXPORT_SYMBOL(__skb_warn_lro_forwarding);