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