skbuff.c 74 KB

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