skbuff.c 83 KB

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