skbuff.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280
  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 <trace/events/skb.h>
  66. #include "kmap_skb.h"
  67. static struct kmem_cache *skbuff_head_cache __read_mostly;
  68. static struct kmem_cache *skbuff_fclone_cache __read_mostly;
  69. static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
  70. struct pipe_buffer *buf)
  71. {
  72. put_page(buf->page);
  73. }
  74. static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
  75. struct pipe_buffer *buf)
  76. {
  77. get_page(buf->page);
  78. }
  79. static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
  80. struct pipe_buffer *buf)
  81. {
  82. return 1;
  83. }
  84. /* Pipe buffer operations for a socket. */
  85. static const struct pipe_buf_operations sock_pipe_buf_ops = {
  86. .can_merge = 0,
  87. .map = generic_pipe_buf_map,
  88. .unmap = generic_pipe_buf_unmap,
  89. .confirm = generic_pipe_buf_confirm,
  90. .release = sock_pipe_buf_release,
  91. .steal = sock_pipe_buf_steal,
  92. .get = sock_pipe_buf_get,
  93. };
  94. /*
  95. * Keep out-of-line to prevent kernel bloat.
  96. * __builtin_return_address is not used because it is not always
  97. * reliable.
  98. */
  99. /**
  100. * skb_over_panic - private function
  101. * @skb: buffer
  102. * @sz: size
  103. * @here: address
  104. *
  105. * Out of line support code for skb_put(). Not user callable.
  106. */
  107. static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
  108. {
  109. printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
  110. "data:%p tail:%#lx end:%#lx dev:%s\n",
  111. here, skb->len, sz, skb->head, skb->data,
  112. (unsigned long)skb->tail, (unsigned long)skb->end,
  113. skb->dev ? skb->dev->name : "<NULL>");
  114. BUG();
  115. }
  116. /**
  117. * skb_under_panic - private function
  118. * @skb: buffer
  119. * @sz: size
  120. * @here: address
  121. *
  122. * Out of line support code for skb_push(). Not user callable.
  123. */
  124. static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
  125. {
  126. printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
  127. "data:%p tail:%#lx end:%#lx dev:%s\n",
  128. here, skb->len, sz, skb->head, skb->data,
  129. (unsigned long)skb->tail, (unsigned long)skb->end,
  130. skb->dev ? skb->dev->name : "<NULL>");
  131. BUG();
  132. }
  133. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  134. * 'private' fields and also do memory statistics to find all the
  135. * [BEEP] leaks.
  136. *
  137. */
  138. /**
  139. * __alloc_skb - allocate a network buffer
  140. * @size: size to allocate
  141. * @gfp_mask: allocation mask
  142. * @fclone: allocate from fclone cache instead of head cache
  143. * and allocate a cloned (child) skb
  144. * @node: numa node to allocate memory on
  145. *
  146. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  147. * tail room of size bytes. The object has a reference count of one.
  148. * The return is the buffer. On a failure the return is %NULL.
  149. *
  150. * Buffers may only be allocated from interrupts using a @gfp_mask of
  151. * %GFP_ATOMIC.
  152. */
  153. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  154. int fclone, int node)
  155. {
  156. struct kmem_cache *cache;
  157. struct skb_shared_info *shinfo;
  158. struct sk_buff *skb;
  159. u8 *data;
  160. cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
  161. /* Get the HEAD */
  162. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
  163. if (!skb)
  164. goto out;
  165. prefetchw(skb);
  166. /* We do our best to align skb_shared_info on a separate cache
  167. * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
  168. * aligned memory blocks, unless SLUB/SLAB debug is enabled.
  169. * Both skb->head and skb_shared_info are cache line aligned.
  170. */
  171. size = SKB_DATA_ALIGN(size);
  172. size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  173. data = kmalloc_node_track_caller(size, gfp_mask, node);
  174. if (!data)
  175. goto nodata;
  176. /* kmalloc(size) might give us more room than requested.
  177. * Put skb_shared_info exactly at the end of allocated zone,
  178. * to allow max possible filling before reallocation.
  179. */
  180. size = SKB_WITH_OVERHEAD(ksize(data));
  181. prefetchw(data + size);
  182. /*
  183. * Only clear those fields we need to clear, not those that we will
  184. * actually initialise below. Hence, don't put any more fields after
  185. * the tail pointer in struct sk_buff!
  186. */
  187. memset(skb, 0, offsetof(struct sk_buff, tail));
  188. /* Account for allocated memory : skb + skb->head */
  189. skb->truesize = SKB_TRUESIZE(size);
  190. atomic_set(&skb->users, 1);
  191. skb->head = data;
  192. skb->data = data;
  193. skb_reset_tail_pointer(skb);
  194. skb->end = skb->tail + size;
  195. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  196. skb->mac_header = ~0U;
  197. #endif
  198. /* make sure we initialize shinfo sequentially */
  199. shinfo = skb_shinfo(skb);
  200. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  201. atomic_set(&shinfo->dataref, 1);
  202. kmemcheck_annotate_variable(shinfo->destructor_arg);
  203. if (fclone) {
  204. struct sk_buff *child = skb + 1;
  205. atomic_t *fclone_ref = (atomic_t *) (child + 1);
  206. kmemcheck_annotate_bitfield(child, flags1);
  207. kmemcheck_annotate_bitfield(child, flags2);
  208. skb->fclone = SKB_FCLONE_ORIG;
  209. atomic_set(fclone_ref, 1);
  210. child->fclone = SKB_FCLONE_UNAVAILABLE;
  211. }
  212. out:
  213. return skb;
  214. nodata:
  215. kmem_cache_free(cache, skb);
  216. skb = NULL;
  217. goto out;
  218. }
  219. EXPORT_SYMBOL(__alloc_skb);
  220. /**
  221. * build_skb - build a network buffer
  222. * @data: data buffer provided by caller
  223. *
  224. * Allocate a new &sk_buff. Caller provides space holding head and
  225. * skb_shared_info. @data must have been allocated by kmalloc()
  226. * The return is the new skb buffer.
  227. * On a failure the return is %NULL, and @data is not freed.
  228. * Notes :
  229. * Before IO, driver allocates only data buffer where NIC put incoming frame
  230. * Driver should add room at head (NET_SKB_PAD) and
  231. * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
  232. * After IO, driver calls build_skb(), to allocate sk_buff and populate it
  233. * before giving packet to stack.
  234. * RX rings only contains data buffers, not full skbs.
  235. */
  236. struct sk_buff *build_skb(void *data)
  237. {
  238. struct skb_shared_info *shinfo;
  239. struct sk_buff *skb;
  240. unsigned int size;
  241. skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
  242. if (!skb)
  243. return NULL;
  244. size = ksize(data) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  245. memset(skb, 0, offsetof(struct sk_buff, tail));
  246. skb->truesize = SKB_TRUESIZE(size);
  247. atomic_set(&skb->users, 1);
  248. skb->head = data;
  249. skb->data = data;
  250. skb_reset_tail_pointer(skb);
  251. skb->end = skb->tail + size;
  252. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  253. skb->mac_header = ~0U;
  254. #endif
  255. /* make sure we initialize shinfo sequentially */
  256. shinfo = skb_shinfo(skb);
  257. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  258. atomic_set(&shinfo->dataref, 1);
  259. kmemcheck_annotate_variable(shinfo->destructor_arg);
  260. return skb;
  261. }
  262. EXPORT_SYMBOL(build_skb);
  263. /**
  264. * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
  265. * @dev: network device to receive on
  266. * @length: length to allocate
  267. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  268. *
  269. * Allocate a new &sk_buff and assign it a usage count of one. The
  270. * buffer has unspecified headroom built in. Users should allocate
  271. * the headroom they think they need without accounting for the
  272. * built in space. The built in space is used for optimisations.
  273. *
  274. * %NULL is returned if there is no free memory.
  275. */
  276. struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  277. unsigned int length, gfp_t gfp_mask)
  278. {
  279. struct sk_buff *skb;
  280. skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, NUMA_NO_NODE);
  281. if (likely(skb)) {
  282. skb_reserve(skb, NET_SKB_PAD);
  283. skb->dev = dev;
  284. }
  285. return skb;
  286. }
  287. EXPORT_SYMBOL(__netdev_alloc_skb);
  288. void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
  289. int size, unsigned int truesize)
  290. {
  291. skb_fill_page_desc(skb, i, page, off, size);
  292. skb->len += size;
  293. skb->data_len += size;
  294. skb->truesize += truesize;
  295. }
  296. EXPORT_SYMBOL(skb_add_rx_frag);
  297. /**
  298. * dev_alloc_skb - allocate an skbuff for receiving
  299. * @length: length to allocate
  300. *
  301. * Allocate a new &sk_buff and assign it a usage count of one. The
  302. * buffer has unspecified headroom built in. Users should allocate
  303. * the headroom they think they need without accounting for the
  304. * built in space. The built in space is used for optimisations.
  305. *
  306. * %NULL is returned if there is no free memory. Although this function
  307. * allocates memory it can be called from an interrupt.
  308. */
  309. struct sk_buff *dev_alloc_skb(unsigned int length)
  310. {
  311. /*
  312. * There is more code here than it seems:
  313. * __dev_alloc_skb is an inline
  314. */
  315. return __dev_alloc_skb(length, GFP_ATOMIC);
  316. }
  317. EXPORT_SYMBOL(dev_alloc_skb);
  318. static void skb_drop_list(struct sk_buff **listp)
  319. {
  320. struct sk_buff *list = *listp;
  321. *listp = NULL;
  322. do {
  323. struct sk_buff *this = list;
  324. list = list->next;
  325. kfree_skb(this);
  326. } while (list);
  327. }
  328. static inline void skb_drop_fraglist(struct sk_buff *skb)
  329. {
  330. skb_drop_list(&skb_shinfo(skb)->frag_list);
  331. }
  332. static void skb_clone_fraglist(struct sk_buff *skb)
  333. {
  334. struct sk_buff *list;
  335. skb_walk_frags(skb, list)
  336. skb_get(list);
  337. }
  338. static void skb_release_data(struct sk_buff *skb)
  339. {
  340. if (!skb->cloned ||
  341. !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  342. &skb_shinfo(skb)->dataref)) {
  343. if (skb_shinfo(skb)->nr_frags) {
  344. int i;
  345. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  346. skb_frag_unref(skb, i);
  347. }
  348. /*
  349. * If skb buf is from userspace, we need to notify the caller
  350. * the lower device DMA has done;
  351. */
  352. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  353. struct ubuf_info *uarg;
  354. uarg = skb_shinfo(skb)->destructor_arg;
  355. if (uarg->callback)
  356. uarg->callback(uarg);
  357. }
  358. if (skb_has_frag_list(skb))
  359. skb_drop_fraglist(skb);
  360. kfree(skb->head);
  361. }
  362. }
  363. /*
  364. * Free an skbuff by memory without cleaning the state.
  365. */
  366. static void kfree_skbmem(struct sk_buff *skb)
  367. {
  368. struct sk_buff *other;
  369. atomic_t *fclone_ref;
  370. switch (skb->fclone) {
  371. case SKB_FCLONE_UNAVAILABLE:
  372. kmem_cache_free(skbuff_head_cache, skb);
  373. break;
  374. case SKB_FCLONE_ORIG:
  375. fclone_ref = (atomic_t *) (skb + 2);
  376. if (atomic_dec_and_test(fclone_ref))
  377. kmem_cache_free(skbuff_fclone_cache, skb);
  378. break;
  379. case SKB_FCLONE_CLONE:
  380. fclone_ref = (atomic_t *) (skb + 1);
  381. other = skb - 1;
  382. /* The clone portion is available for
  383. * fast-cloning again.
  384. */
  385. skb->fclone = SKB_FCLONE_UNAVAILABLE;
  386. if (atomic_dec_and_test(fclone_ref))
  387. kmem_cache_free(skbuff_fclone_cache, other);
  388. break;
  389. }
  390. }
  391. static void skb_release_head_state(struct sk_buff *skb)
  392. {
  393. skb_dst_drop(skb);
  394. #ifdef CONFIG_XFRM
  395. secpath_put(skb->sp);
  396. #endif
  397. if (skb->destructor) {
  398. WARN_ON(in_irq());
  399. skb->destructor(skb);
  400. }
  401. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  402. nf_conntrack_put(skb->nfct);
  403. #endif
  404. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  405. nf_conntrack_put_reasm(skb->nfct_reasm);
  406. #endif
  407. #ifdef CONFIG_BRIDGE_NETFILTER
  408. nf_bridge_put(skb->nf_bridge);
  409. #endif
  410. /* XXX: IS this still necessary? - JHS */
  411. #ifdef CONFIG_NET_SCHED
  412. skb->tc_index = 0;
  413. #ifdef CONFIG_NET_CLS_ACT
  414. skb->tc_verd = 0;
  415. #endif
  416. #endif
  417. }
  418. /* Free everything but the sk_buff shell. */
  419. static void skb_release_all(struct sk_buff *skb)
  420. {
  421. skb_release_head_state(skb);
  422. skb_release_data(skb);
  423. }
  424. /**
  425. * __kfree_skb - private function
  426. * @skb: buffer
  427. *
  428. * Free an sk_buff. Release anything attached to the buffer.
  429. * Clean the state. This is an internal helper function. Users should
  430. * always call kfree_skb
  431. */
  432. void __kfree_skb(struct sk_buff *skb)
  433. {
  434. skb_release_all(skb);
  435. kfree_skbmem(skb);
  436. }
  437. EXPORT_SYMBOL(__kfree_skb);
  438. /**
  439. * kfree_skb - free an sk_buff
  440. * @skb: buffer to free
  441. *
  442. * Drop a reference to the buffer and free it if the usage count has
  443. * hit zero.
  444. */
  445. void kfree_skb(struct sk_buff *skb)
  446. {
  447. if (unlikely(!skb))
  448. return;
  449. if (likely(atomic_read(&skb->users) == 1))
  450. smp_rmb();
  451. else if (likely(!atomic_dec_and_test(&skb->users)))
  452. return;
  453. trace_kfree_skb(skb, __builtin_return_address(0));
  454. __kfree_skb(skb);
  455. }
  456. EXPORT_SYMBOL(kfree_skb);
  457. /**
  458. * consume_skb - free an skbuff
  459. * @skb: buffer to free
  460. *
  461. * Drop a ref to the buffer and free it if the usage count has hit zero
  462. * Functions identically to kfree_skb, but kfree_skb assumes that the frame
  463. * is being dropped after a failure and notes that
  464. */
  465. void consume_skb(struct sk_buff *skb)
  466. {
  467. if (unlikely(!skb))
  468. return;
  469. if (likely(atomic_read(&skb->users) == 1))
  470. smp_rmb();
  471. else if (likely(!atomic_dec_and_test(&skb->users)))
  472. return;
  473. trace_consume_skb(skb);
  474. __kfree_skb(skb);
  475. }
  476. EXPORT_SYMBOL(consume_skb);
  477. /**
  478. * skb_recycle - clean up an skb for reuse
  479. * @skb: buffer
  480. *
  481. * Recycles the skb to be reused as a receive buffer. This
  482. * function does any necessary reference count dropping, and
  483. * cleans up the skbuff as if it just came from __alloc_skb().
  484. */
  485. void skb_recycle(struct sk_buff *skb)
  486. {
  487. struct skb_shared_info *shinfo;
  488. skb_release_head_state(skb);
  489. shinfo = skb_shinfo(skb);
  490. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  491. atomic_set(&shinfo->dataref, 1);
  492. memset(skb, 0, offsetof(struct sk_buff, tail));
  493. skb->data = skb->head + NET_SKB_PAD;
  494. skb_reset_tail_pointer(skb);
  495. }
  496. EXPORT_SYMBOL(skb_recycle);
  497. /**
  498. * skb_recycle_check - check if skb can be reused for receive
  499. * @skb: buffer
  500. * @skb_size: minimum receive buffer size
  501. *
  502. * Checks that the skb passed in is not shared or cloned, and
  503. * that it is linear and its head portion at least as large as
  504. * skb_size so that it can be recycled as a receive buffer.
  505. * If these conditions are met, this function does any necessary
  506. * reference count dropping and cleans up the skbuff as if it
  507. * just came from __alloc_skb().
  508. */
  509. bool skb_recycle_check(struct sk_buff *skb, int skb_size)
  510. {
  511. if (!skb_is_recycleable(skb, skb_size))
  512. return false;
  513. skb_recycle(skb);
  514. return true;
  515. }
  516. EXPORT_SYMBOL(skb_recycle_check);
  517. static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  518. {
  519. new->tstamp = old->tstamp;
  520. new->dev = old->dev;
  521. new->transport_header = old->transport_header;
  522. new->network_header = old->network_header;
  523. new->mac_header = old->mac_header;
  524. skb_dst_copy(new, old);
  525. new->rxhash = old->rxhash;
  526. new->ooo_okay = old->ooo_okay;
  527. new->l4_rxhash = old->l4_rxhash;
  528. new->no_fcs = old->no_fcs;
  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->truesize;
  2504. nskb->len += p->len;
  2505. *head = nskb;
  2506. nskb->next = p->next;
  2507. p->next = NULL;
  2508. p = nskb;
  2509. merge:
  2510. p->truesize += skb->truesize - len;
  2511. if (offset > headlen) {
  2512. unsigned int eat = offset - headlen;
  2513. skbinfo->frags[0].page_offset += eat;
  2514. skb_frag_size_sub(&skbinfo->frags[0], eat);
  2515. skb->data_len -= eat;
  2516. skb->len -= eat;
  2517. offset = headlen;
  2518. }
  2519. __skb_pull(skb, offset);
  2520. p->prev->next = skb;
  2521. p->prev = skb;
  2522. skb_header_release(skb);
  2523. done:
  2524. NAPI_GRO_CB(p)->count++;
  2525. p->data_len += len;
  2526. p->truesize += len;
  2527. p->len += len;
  2528. NAPI_GRO_CB(skb)->same_flow = 1;
  2529. return 0;
  2530. }
  2531. EXPORT_SYMBOL_GPL(skb_gro_receive);
  2532. void __init skb_init(void)
  2533. {
  2534. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  2535. sizeof(struct sk_buff),
  2536. 0,
  2537. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2538. NULL);
  2539. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  2540. (2*sizeof(struct sk_buff)) +
  2541. sizeof(atomic_t),
  2542. 0,
  2543. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2544. NULL);
  2545. }
  2546. /**
  2547. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  2548. * @skb: Socket buffer containing the buffers to be mapped
  2549. * @sg: The scatter-gather list to map into
  2550. * @offset: The offset into the buffer's contents to start mapping
  2551. * @len: Length of buffer space to be mapped
  2552. *
  2553. * Fill the specified scatter-gather list with mappings/pointers into a
  2554. * region of the buffer space attached to a socket buffer.
  2555. */
  2556. static int
  2557. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2558. {
  2559. int start = skb_headlen(skb);
  2560. int i, copy = start - offset;
  2561. struct sk_buff *frag_iter;
  2562. int elt = 0;
  2563. if (copy > 0) {
  2564. if (copy > len)
  2565. copy = len;
  2566. sg_set_buf(sg, skb->data + offset, copy);
  2567. elt++;
  2568. if ((len -= copy) == 0)
  2569. return elt;
  2570. offset += copy;
  2571. }
  2572. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2573. int end;
  2574. WARN_ON(start > offset + len);
  2575. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2576. if ((copy = end - offset) > 0) {
  2577. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2578. if (copy > len)
  2579. copy = len;
  2580. sg_set_page(&sg[elt], skb_frag_page(frag), copy,
  2581. frag->page_offset+offset-start);
  2582. elt++;
  2583. if (!(len -= copy))
  2584. return elt;
  2585. offset += copy;
  2586. }
  2587. start = end;
  2588. }
  2589. skb_walk_frags(skb, frag_iter) {
  2590. int end;
  2591. WARN_ON(start > offset + len);
  2592. end = start + frag_iter->len;
  2593. if ((copy = end - offset) > 0) {
  2594. if (copy > len)
  2595. copy = len;
  2596. elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  2597. copy);
  2598. if ((len -= copy) == 0)
  2599. return elt;
  2600. offset += copy;
  2601. }
  2602. start = end;
  2603. }
  2604. BUG_ON(len);
  2605. return elt;
  2606. }
  2607. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2608. {
  2609. int nsg = __skb_to_sgvec(skb, sg, offset, len);
  2610. sg_mark_end(&sg[nsg - 1]);
  2611. return nsg;
  2612. }
  2613. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  2614. /**
  2615. * skb_cow_data - Check that a socket buffer's data buffers are writable
  2616. * @skb: The socket buffer to check.
  2617. * @tailbits: Amount of trailing space to be added
  2618. * @trailer: Returned pointer to the skb where the @tailbits space begins
  2619. *
  2620. * Make sure that the data buffers attached to a socket buffer are
  2621. * writable. If they are not, private copies are made of the data buffers
  2622. * and the socket buffer is set to use these instead.
  2623. *
  2624. * If @tailbits is given, make sure that there is space to write @tailbits
  2625. * bytes of data beyond current end of socket buffer. @trailer will be
  2626. * set to point to the skb in which this space begins.
  2627. *
  2628. * The number of scatterlist elements required to completely map the
  2629. * COW'd and extended socket buffer will be returned.
  2630. */
  2631. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  2632. {
  2633. int copyflag;
  2634. int elt;
  2635. struct sk_buff *skb1, **skb_p;
  2636. /* If skb is cloned or its head is paged, reallocate
  2637. * head pulling out all the pages (pages are considered not writable
  2638. * at the moment even if they are anonymous).
  2639. */
  2640. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  2641. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  2642. return -ENOMEM;
  2643. /* Easy case. Most of packets will go this way. */
  2644. if (!skb_has_frag_list(skb)) {
  2645. /* A little of trouble, not enough of space for trailer.
  2646. * This should not happen, when stack is tuned to generate
  2647. * good frames. OK, on miss we reallocate and reserve even more
  2648. * space, 128 bytes is fair. */
  2649. if (skb_tailroom(skb) < tailbits &&
  2650. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  2651. return -ENOMEM;
  2652. /* Voila! */
  2653. *trailer = skb;
  2654. return 1;
  2655. }
  2656. /* Misery. We are in troubles, going to mincer fragments... */
  2657. elt = 1;
  2658. skb_p = &skb_shinfo(skb)->frag_list;
  2659. copyflag = 0;
  2660. while ((skb1 = *skb_p) != NULL) {
  2661. int ntail = 0;
  2662. /* The fragment is partially pulled by someone,
  2663. * this can happen on input. Copy it and everything
  2664. * after it. */
  2665. if (skb_shared(skb1))
  2666. copyflag = 1;
  2667. /* If the skb is the last, worry about trailer. */
  2668. if (skb1->next == NULL && tailbits) {
  2669. if (skb_shinfo(skb1)->nr_frags ||
  2670. skb_has_frag_list(skb1) ||
  2671. skb_tailroom(skb1) < tailbits)
  2672. ntail = tailbits + 128;
  2673. }
  2674. if (copyflag ||
  2675. skb_cloned(skb1) ||
  2676. ntail ||
  2677. skb_shinfo(skb1)->nr_frags ||
  2678. skb_has_frag_list(skb1)) {
  2679. struct sk_buff *skb2;
  2680. /* Fuck, we are miserable poor guys... */
  2681. if (ntail == 0)
  2682. skb2 = skb_copy(skb1, GFP_ATOMIC);
  2683. else
  2684. skb2 = skb_copy_expand(skb1,
  2685. skb_headroom(skb1),
  2686. ntail,
  2687. GFP_ATOMIC);
  2688. if (unlikely(skb2 == NULL))
  2689. return -ENOMEM;
  2690. if (skb1->sk)
  2691. skb_set_owner_w(skb2, skb1->sk);
  2692. /* Looking around. Are we still alive?
  2693. * OK, link new skb, drop old one */
  2694. skb2->next = skb1->next;
  2695. *skb_p = skb2;
  2696. kfree_skb(skb1);
  2697. skb1 = skb2;
  2698. }
  2699. elt++;
  2700. *trailer = skb1;
  2701. skb_p = &skb1->next;
  2702. }
  2703. return elt;
  2704. }
  2705. EXPORT_SYMBOL_GPL(skb_cow_data);
  2706. static void sock_rmem_free(struct sk_buff *skb)
  2707. {
  2708. struct sock *sk = skb->sk;
  2709. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  2710. }
  2711. /*
  2712. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  2713. */
  2714. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  2715. {
  2716. int len = skb->len;
  2717. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  2718. (unsigned)sk->sk_rcvbuf)
  2719. return -ENOMEM;
  2720. skb_orphan(skb);
  2721. skb->sk = sk;
  2722. skb->destructor = sock_rmem_free;
  2723. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  2724. /* before exiting rcu section, make sure dst is refcounted */
  2725. skb_dst_force(skb);
  2726. skb_queue_tail(&sk->sk_error_queue, skb);
  2727. if (!sock_flag(sk, SOCK_DEAD))
  2728. sk->sk_data_ready(sk, len);
  2729. return 0;
  2730. }
  2731. EXPORT_SYMBOL(sock_queue_err_skb);
  2732. void skb_tstamp_tx(struct sk_buff *orig_skb,
  2733. struct skb_shared_hwtstamps *hwtstamps)
  2734. {
  2735. struct sock *sk = orig_skb->sk;
  2736. struct sock_exterr_skb *serr;
  2737. struct sk_buff *skb;
  2738. int err;
  2739. if (!sk)
  2740. return;
  2741. skb = skb_clone(orig_skb, GFP_ATOMIC);
  2742. if (!skb)
  2743. return;
  2744. if (hwtstamps) {
  2745. *skb_hwtstamps(skb) =
  2746. *hwtstamps;
  2747. } else {
  2748. /*
  2749. * no hardware time stamps available,
  2750. * so keep the shared tx_flags and only
  2751. * store software time stamp
  2752. */
  2753. skb->tstamp = ktime_get_real();
  2754. }
  2755. serr = SKB_EXT_ERR(skb);
  2756. memset(serr, 0, sizeof(*serr));
  2757. serr->ee.ee_errno = ENOMSG;
  2758. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  2759. err = sock_queue_err_skb(sk, skb);
  2760. if (err)
  2761. kfree_skb(skb);
  2762. }
  2763. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  2764. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  2765. {
  2766. struct sock *sk = skb->sk;
  2767. struct sock_exterr_skb *serr;
  2768. int err;
  2769. skb->wifi_acked_valid = 1;
  2770. skb->wifi_acked = acked;
  2771. serr = SKB_EXT_ERR(skb);
  2772. memset(serr, 0, sizeof(*serr));
  2773. serr->ee.ee_errno = ENOMSG;
  2774. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  2775. err = sock_queue_err_skb(sk, skb);
  2776. if (err)
  2777. kfree_skb(skb);
  2778. }
  2779. EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
  2780. /**
  2781. * skb_partial_csum_set - set up and verify partial csum values for packet
  2782. * @skb: the skb to set
  2783. * @start: the number of bytes after skb->data to start checksumming.
  2784. * @off: the offset from start to place the checksum.
  2785. *
  2786. * For untrusted partially-checksummed packets, we need to make sure the values
  2787. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  2788. *
  2789. * This function checks and sets those values and skb->ip_summed: if this
  2790. * returns false you should drop the packet.
  2791. */
  2792. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  2793. {
  2794. if (unlikely(start > skb_headlen(skb)) ||
  2795. unlikely((int)start + off > skb_headlen(skb) - 2)) {
  2796. if (net_ratelimit())
  2797. printk(KERN_WARNING
  2798. "bad partial csum: csum=%u/%u len=%u\n",
  2799. start, off, skb_headlen(skb));
  2800. return false;
  2801. }
  2802. skb->ip_summed = CHECKSUM_PARTIAL;
  2803. skb->csum_start = skb_headroom(skb) + start;
  2804. skb->csum_offset = off;
  2805. return true;
  2806. }
  2807. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  2808. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  2809. {
  2810. if (net_ratelimit())
  2811. pr_warning("%s: received packets cannot be forwarded"
  2812. " while LRO is enabled\n", skb->dev->name);
  2813. }
  2814. EXPORT_SYMBOL(__skb_warn_lro_forwarding);