skbuff.c 78 KB

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