skbuff.c 74 KB

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