skbuff.c 73 KB

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