skbuff.c 78 KB

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