skbuff.c 79 KB

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