skbuff.c 72 KB

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