skbuff.c 74 KB

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