skbuff.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. /*
  2. * Routines having to do with the 'struct sk_buff' memory handlers.
  3. *
  4. * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
  5. * Florian La Roche <rzsfl@rz.uni-sb.de>
  6. *
  7. * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
  8. *
  9. * Fixes:
  10. * Alan Cox : Fixed the worst of the load
  11. * balancer bugs.
  12. * Dave Platt : Interrupt stacking fix.
  13. * Richard Kooijman : Timestamp fixes.
  14. * Alan Cox : Changed buffer format.
  15. * Alan Cox : destructor hook for AF_UNIX etc.
  16. * Linus Torvalds : Better skb_clone.
  17. * Alan Cox : Added skb_copy.
  18. * Alan Cox : Added all the changed routines Linus
  19. * only put in the headers
  20. * Ray VanTassle : Fixed --skb->lock in free
  21. * Alan Cox : skb_copy copy arp field
  22. * Andi Kleen : slabified it.
  23. * Robert Olsson : Removed skb_head_pool
  24. *
  25. * NOTE:
  26. * The __skb_ routines should be called with interrupts
  27. * disabled, or you better be *real* sure that the operation is atomic
  28. * with respect to whatever list is being frobbed (e.g. via lock_sock()
  29. * or via disabling bottom half handlers, etc).
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version
  34. * 2 of the License, or (at your option) any later version.
  35. */
  36. /*
  37. * The functions in this file will not compile correctly with gcc 2.4.x
  38. */
  39. #include <linux/module.h>
  40. #include <linux/types.h>
  41. #include <linux/kernel.h>
  42. #include <linux/mm.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/in.h>
  45. #include <linux/inet.h>
  46. #include <linux/slab.h>
  47. #include <linux/netdevice.h>
  48. #ifdef CONFIG_NET_CLS_ACT
  49. #include <net/pkt_sched.h>
  50. #endif
  51. #include <linux/string.h>
  52. #include <linux/skbuff.h>
  53. #include <linux/cache.h>
  54. #include <linux/rtnetlink.h>
  55. #include <linux/init.h>
  56. #include <net/protocol.h>
  57. #include <net/dst.h>
  58. #include <net/sock.h>
  59. #include <net/checksum.h>
  60. #include <net/xfrm.h>
  61. #include <asm/uaccess.h>
  62. #include <asm/system.h>
  63. #include "kmap_skb.h"
  64. static struct kmem_cache *skbuff_head_cache __read_mostly;
  65. static struct kmem_cache *skbuff_fclone_cache __read_mostly;
  66. /*
  67. * Keep out-of-line to prevent kernel bloat.
  68. * __builtin_return_address is not used because it is not always
  69. * reliable.
  70. */
  71. /**
  72. * skb_over_panic - private function
  73. * @skb: buffer
  74. * @sz: size
  75. * @here: address
  76. *
  77. * Out of line support code for skb_put(). Not user callable.
  78. */
  79. void skb_over_panic(struct sk_buff *skb, int sz, void *here)
  80. {
  81. printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
  82. "data:%p tail:%p end:%p dev:%s\n",
  83. here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
  84. skb->dev ? skb->dev->name : "<NULL>");
  85. BUG();
  86. }
  87. /**
  88. * skb_under_panic - private function
  89. * @skb: buffer
  90. * @sz: size
  91. * @here: address
  92. *
  93. * Out of line support code for skb_push(). Not user callable.
  94. */
  95. void skb_under_panic(struct sk_buff *skb, int sz, void *here)
  96. {
  97. printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
  98. "data:%p tail:%p end:%p dev:%s\n",
  99. here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
  100. skb->dev ? skb->dev->name : "<NULL>");
  101. BUG();
  102. }
  103. void skb_truesize_bug(struct sk_buff *skb)
  104. {
  105. printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
  106. "len=%u, sizeof(sk_buff)=%Zd\n",
  107. skb->truesize, skb->len, sizeof(struct sk_buff));
  108. }
  109. EXPORT_SYMBOL(skb_truesize_bug);
  110. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  111. * 'private' fields and also do memory statistics to find all the
  112. * [BEEP] leaks.
  113. *
  114. */
  115. /**
  116. * __alloc_skb - allocate a network buffer
  117. * @size: size to allocate
  118. * @gfp_mask: allocation mask
  119. * @fclone: allocate from fclone cache instead of head cache
  120. * and allocate a cloned (child) skb
  121. * @node: numa node to allocate memory on
  122. *
  123. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  124. * tail room of size bytes. The object has a reference count of one.
  125. * The return is the buffer. On a failure the return is %NULL.
  126. *
  127. * Buffers may only be allocated from interrupts using a @gfp_mask of
  128. * %GFP_ATOMIC.
  129. */
  130. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  131. int fclone, int node)
  132. {
  133. struct kmem_cache *cache;
  134. struct skb_shared_info *shinfo;
  135. struct sk_buff *skb;
  136. u8 *data;
  137. cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
  138. /* Get the HEAD */
  139. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
  140. if (!skb)
  141. goto out;
  142. /* Get the DATA. Size must match skb_add_mtu(). */
  143. size = SKB_DATA_ALIGN(size);
  144. data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
  145. gfp_mask, node);
  146. if (!data)
  147. goto nodata;
  148. memset(skb, 0, offsetof(struct sk_buff, truesize));
  149. skb->truesize = size + sizeof(struct sk_buff);
  150. atomic_set(&skb->users, 1);
  151. skb->head = data;
  152. skb->data = data;
  153. skb->tail = data;
  154. skb->end = data + size;
  155. /* make sure we initialize shinfo sequentially */
  156. shinfo = skb_shinfo(skb);
  157. atomic_set(&shinfo->dataref, 1);
  158. shinfo->nr_frags = 0;
  159. shinfo->gso_size = 0;
  160. shinfo->gso_segs = 0;
  161. shinfo->gso_type = 0;
  162. shinfo->ip6_frag_id = 0;
  163. shinfo->frag_list = NULL;
  164. if (fclone) {
  165. struct sk_buff *child = skb + 1;
  166. atomic_t *fclone_ref = (atomic_t *) (child + 1);
  167. skb->fclone = SKB_FCLONE_ORIG;
  168. atomic_set(fclone_ref, 1);
  169. child->fclone = SKB_FCLONE_UNAVAILABLE;
  170. }
  171. out:
  172. return skb;
  173. nodata:
  174. kmem_cache_free(cache, skb);
  175. skb = NULL;
  176. goto out;
  177. }
  178. /**
  179. * alloc_skb_from_cache - allocate a network buffer
  180. * @cp: kmem_cache from which to allocate the data area
  181. * (object size must be big enough for @size bytes + skb overheads)
  182. * @size: size to allocate
  183. * @gfp_mask: allocation mask
  184. *
  185. * Allocate a new &sk_buff. The returned buffer has no headroom and
  186. * tail room of size bytes. The object has a reference count of one.
  187. * The return is the buffer. On a failure the return is %NULL.
  188. *
  189. * Buffers may only be allocated from interrupts using a @gfp_mask of
  190. * %GFP_ATOMIC.
  191. */
  192. struct sk_buff *alloc_skb_from_cache(struct kmem_cache *cp,
  193. unsigned int size,
  194. gfp_t gfp_mask)
  195. {
  196. struct sk_buff *skb;
  197. u8 *data;
  198. /* Get the HEAD */
  199. skb = kmem_cache_alloc(skbuff_head_cache,
  200. gfp_mask & ~__GFP_DMA);
  201. if (!skb)
  202. goto out;
  203. /* Get the DATA. */
  204. size = SKB_DATA_ALIGN(size);
  205. data = kmem_cache_alloc(cp, gfp_mask);
  206. if (!data)
  207. goto nodata;
  208. memset(skb, 0, offsetof(struct sk_buff, truesize));
  209. skb->truesize = size + sizeof(struct sk_buff);
  210. atomic_set(&skb->users, 1);
  211. skb->head = data;
  212. skb->data = data;
  213. skb->tail = data;
  214. skb->end = data + size;
  215. atomic_set(&(skb_shinfo(skb)->dataref), 1);
  216. skb_shinfo(skb)->nr_frags = 0;
  217. skb_shinfo(skb)->gso_size = 0;
  218. skb_shinfo(skb)->gso_segs = 0;
  219. skb_shinfo(skb)->gso_type = 0;
  220. skb_shinfo(skb)->frag_list = NULL;
  221. out:
  222. return skb;
  223. nodata:
  224. kmem_cache_free(skbuff_head_cache, skb);
  225. skb = NULL;
  226. goto out;
  227. }
  228. /**
  229. * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
  230. * @dev: network device to receive on
  231. * @length: length to allocate
  232. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  233. *
  234. * Allocate a new &sk_buff and assign it a usage count of one. The
  235. * buffer has unspecified headroom built in. Users should allocate
  236. * the headroom they think they need without accounting for the
  237. * built in space. The built in space is used for optimisations.
  238. *
  239. * %NULL is returned if there is no free memory.
  240. */
  241. struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  242. unsigned int length, gfp_t gfp_mask)
  243. {
  244. int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
  245. struct sk_buff *skb;
  246. skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
  247. if (likely(skb)) {
  248. skb_reserve(skb, NET_SKB_PAD);
  249. skb->dev = dev;
  250. }
  251. return skb;
  252. }
  253. static void skb_drop_list(struct sk_buff **listp)
  254. {
  255. struct sk_buff *list = *listp;
  256. *listp = NULL;
  257. do {
  258. struct sk_buff *this = list;
  259. list = list->next;
  260. kfree_skb(this);
  261. } while (list);
  262. }
  263. static inline void skb_drop_fraglist(struct sk_buff *skb)
  264. {
  265. skb_drop_list(&skb_shinfo(skb)->frag_list);
  266. }
  267. static void skb_clone_fraglist(struct sk_buff *skb)
  268. {
  269. struct sk_buff *list;
  270. for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
  271. skb_get(list);
  272. }
  273. static void skb_release_data(struct sk_buff *skb)
  274. {
  275. if (!skb->cloned ||
  276. !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  277. &skb_shinfo(skb)->dataref)) {
  278. if (skb_shinfo(skb)->nr_frags) {
  279. int i;
  280. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  281. put_page(skb_shinfo(skb)->frags[i].page);
  282. }
  283. if (skb_shinfo(skb)->frag_list)
  284. skb_drop_fraglist(skb);
  285. kfree(skb->head);
  286. }
  287. }
  288. /*
  289. * Free an skbuff by memory without cleaning the state.
  290. */
  291. void kfree_skbmem(struct sk_buff *skb)
  292. {
  293. struct sk_buff *other;
  294. atomic_t *fclone_ref;
  295. skb_release_data(skb);
  296. switch (skb->fclone) {
  297. case SKB_FCLONE_UNAVAILABLE:
  298. kmem_cache_free(skbuff_head_cache, skb);
  299. break;
  300. case SKB_FCLONE_ORIG:
  301. fclone_ref = (atomic_t *) (skb + 2);
  302. if (atomic_dec_and_test(fclone_ref))
  303. kmem_cache_free(skbuff_fclone_cache, skb);
  304. break;
  305. case SKB_FCLONE_CLONE:
  306. fclone_ref = (atomic_t *) (skb + 1);
  307. other = skb - 1;
  308. /* The clone portion is available for
  309. * fast-cloning again.
  310. */
  311. skb->fclone = SKB_FCLONE_UNAVAILABLE;
  312. if (atomic_dec_and_test(fclone_ref))
  313. kmem_cache_free(skbuff_fclone_cache, other);
  314. break;
  315. };
  316. }
  317. /**
  318. * __kfree_skb - private function
  319. * @skb: buffer
  320. *
  321. * Free an sk_buff. Release anything attached to the buffer.
  322. * Clean the state. This is an internal helper function. Users should
  323. * always call kfree_skb
  324. */
  325. void __kfree_skb(struct sk_buff *skb)
  326. {
  327. dst_release(skb->dst);
  328. #ifdef CONFIG_XFRM
  329. secpath_put(skb->sp);
  330. #endif
  331. if (skb->destructor) {
  332. WARN_ON(in_irq());
  333. skb->destructor(skb);
  334. }
  335. #ifdef CONFIG_NETFILTER
  336. nf_conntrack_put(skb->nfct);
  337. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  338. nf_conntrack_put_reasm(skb->nfct_reasm);
  339. #endif
  340. #ifdef CONFIG_BRIDGE_NETFILTER
  341. nf_bridge_put(skb->nf_bridge);
  342. #endif
  343. #endif
  344. /* XXX: IS this still necessary? - JHS */
  345. #ifdef CONFIG_NET_SCHED
  346. skb->tc_index = 0;
  347. #ifdef CONFIG_NET_CLS_ACT
  348. skb->tc_verd = 0;
  349. #endif
  350. #endif
  351. kfree_skbmem(skb);
  352. }
  353. /**
  354. * kfree_skb - free an sk_buff
  355. * @skb: buffer to free
  356. *
  357. * Drop a reference to the buffer and free it if the usage count has
  358. * hit zero.
  359. */
  360. void kfree_skb(struct sk_buff *skb)
  361. {
  362. if (unlikely(!skb))
  363. return;
  364. if (likely(atomic_read(&skb->users) == 1))
  365. smp_rmb();
  366. else if (likely(!atomic_dec_and_test(&skb->users)))
  367. return;
  368. __kfree_skb(skb);
  369. }
  370. /**
  371. * skb_clone - duplicate an sk_buff
  372. * @skb: buffer to clone
  373. * @gfp_mask: allocation priority
  374. *
  375. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  376. * copies share the same packet data but not structure. The new
  377. * buffer has a reference count of 1. If the allocation fails the
  378. * function returns %NULL otherwise the new buffer is returned.
  379. *
  380. * If this function is called from an interrupt gfp_mask() must be
  381. * %GFP_ATOMIC.
  382. */
  383. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  384. {
  385. struct sk_buff *n;
  386. n = skb + 1;
  387. if (skb->fclone == SKB_FCLONE_ORIG &&
  388. n->fclone == SKB_FCLONE_UNAVAILABLE) {
  389. atomic_t *fclone_ref = (atomic_t *) (n + 1);
  390. n->fclone = SKB_FCLONE_CLONE;
  391. atomic_inc(fclone_ref);
  392. } else {
  393. n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  394. if (!n)
  395. return NULL;
  396. n->fclone = SKB_FCLONE_UNAVAILABLE;
  397. }
  398. #define C(x) n->x = skb->x
  399. n->next = n->prev = NULL;
  400. n->sk = NULL;
  401. C(tstamp);
  402. C(dev);
  403. C(h);
  404. C(nh);
  405. C(mac);
  406. C(dst);
  407. dst_clone(skb->dst);
  408. C(sp);
  409. #ifdef CONFIG_INET
  410. secpath_get(skb->sp);
  411. #endif
  412. memcpy(n->cb, skb->cb, sizeof(skb->cb));
  413. C(len);
  414. C(data_len);
  415. C(csum);
  416. C(local_df);
  417. n->cloned = 1;
  418. n->nohdr = 0;
  419. C(pkt_type);
  420. C(ip_summed);
  421. C(priority);
  422. #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
  423. C(ipvs_property);
  424. #endif
  425. C(protocol);
  426. n->destructor = NULL;
  427. C(mark);
  428. #ifdef CONFIG_NETFILTER
  429. C(nfct);
  430. nf_conntrack_get(skb->nfct);
  431. C(nfctinfo);
  432. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  433. C(nfct_reasm);
  434. nf_conntrack_get_reasm(skb->nfct_reasm);
  435. #endif
  436. #ifdef CONFIG_BRIDGE_NETFILTER
  437. C(nf_bridge);
  438. nf_bridge_get(skb->nf_bridge);
  439. #endif
  440. #endif /*CONFIG_NETFILTER*/
  441. #ifdef CONFIG_NET_SCHED
  442. C(tc_index);
  443. #ifdef CONFIG_NET_CLS_ACT
  444. n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
  445. n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
  446. n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
  447. C(input_dev);
  448. #endif
  449. skb_copy_secmark(n, skb);
  450. #endif
  451. C(truesize);
  452. atomic_set(&n->users, 1);
  453. C(head);
  454. C(data);
  455. C(tail);
  456. C(end);
  457. atomic_inc(&(skb_shinfo(skb)->dataref));
  458. skb->cloned = 1;
  459. return n;
  460. }
  461. static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  462. {
  463. /*
  464. * Shift between the two data areas in bytes
  465. */
  466. unsigned long offset = new->data - old->data;
  467. new->sk = NULL;
  468. new->dev = old->dev;
  469. new->priority = old->priority;
  470. new->protocol = old->protocol;
  471. new->dst = dst_clone(old->dst);
  472. #ifdef CONFIG_INET
  473. new->sp = secpath_get(old->sp);
  474. #endif
  475. new->h.raw = old->h.raw + offset;
  476. new->nh.raw = old->nh.raw + offset;
  477. new->mac.raw = old->mac.raw + offset;
  478. memcpy(new->cb, old->cb, sizeof(old->cb));
  479. new->local_df = old->local_df;
  480. new->fclone = SKB_FCLONE_UNAVAILABLE;
  481. new->pkt_type = old->pkt_type;
  482. new->tstamp = old->tstamp;
  483. new->destructor = NULL;
  484. new->mark = old->mark;
  485. #ifdef CONFIG_NETFILTER
  486. new->nfct = old->nfct;
  487. nf_conntrack_get(old->nfct);
  488. new->nfctinfo = old->nfctinfo;
  489. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  490. new->nfct_reasm = old->nfct_reasm;
  491. nf_conntrack_get_reasm(old->nfct_reasm);
  492. #endif
  493. #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
  494. new->ipvs_property = old->ipvs_property;
  495. #endif
  496. #ifdef CONFIG_BRIDGE_NETFILTER
  497. new->nf_bridge = old->nf_bridge;
  498. nf_bridge_get(old->nf_bridge);
  499. #endif
  500. #endif
  501. #ifdef CONFIG_NET_SCHED
  502. #ifdef CONFIG_NET_CLS_ACT
  503. new->tc_verd = old->tc_verd;
  504. #endif
  505. new->tc_index = old->tc_index;
  506. #endif
  507. skb_copy_secmark(new, old);
  508. atomic_set(&new->users, 1);
  509. skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
  510. skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
  511. skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
  512. }
  513. /**
  514. * skb_copy - create private copy of an sk_buff
  515. * @skb: buffer to copy
  516. * @gfp_mask: allocation priority
  517. *
  518. * Make a copy of both an &sk_buff and its data. This is used when the
  519. * caller wishes to modify the data and needs a private copy of the
  520. * data to alter. Returns %NULL on failure or the pointer to the buffer
  521. * on success. The returned buffer has a reference count of 1.
  522. *
  523. * As by-product this function converts non-linear &sk_buff to linear
  524. * one, so that &sk_buff becomes completely private and caller is allowed
  525. * to modify all the data of returned buffer. This means that this
  526. * function is not recommended for use in circumstances when only
  527. * header is going to be modified. Use pskb_copy() instead.
  528. */
  529. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  530. {
  531. int headerlen = skb->data - skb->head;
  532. /*
  533. * Allocate the copy buffer
  534. */
  535. struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
  536. gfp_mask);
  537. if (!n)
  538. return NULL;
  539. /* Set the data pointer */
  540. skb_reserve(n, headerlen);
  541. /* Set the tail pointer and length */
  542. skb_put(n, skb->len);
  543. n->csum = skb->csum;
  544. n->ip_summed = skb->ip_summed;
  545. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  546. BUG();
  547. copy_skb_header(n, skb);
  548. return n;
  549. }
  550. /**
  551. * pskb_copy - create copy of an sk_buff with private head.
  552. * @skb: buffer to copy
  553. * @gfp_mask: allocation priority
  554. *
  555. * Make a copy of both an &sk_buff and part of its data, located
  556. * in header. Fragmented data remain shared. This is used when
  557. * the caller wishes to modify only header of &sk_buff and needs
  558. * private copy of the header to alter. Returns %NULL on failure
  559. * or the pointer to the buffer on success.
  560. * The returned buffer has a reference count of 1.
  561. */
  562. struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
  563. {
  564. /*
  565. * Allocate the copy buffer
  566. */
  567. struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
  568. if (!n)
  569. goto out;
  570. /* Set the data pointer */
  571. skb_reserve(n, skb->data - skb->head);
  572. /* Set the tail pointer and length */
  573. skb_put(n, skb_headlen(skb));
  574. /* Copy the bytes */
  575. memcpy(n->data, skb->data, n->len);
  576. n->csum = skb->csum;
  577. n->ip_summed = skb->ip_summed;
  578. n->truesize += skb->data_len;
  579. n->data_len = skb->data_len;
  580. n->len = skb->len;
  581. if (skb_shinfo(skb)->nr_frags) {
  582. int i;
  583. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  584. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  585. get_page(skb_shinfo(n)->frags[i].page);
  586. }
  587. skb_shinfo(n)->nr_frags = i;
  588. }
  589. if (skb_shinfo(skb)->frag_list) {
  590. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  591. skb_clone_fraglist(n);
  592. }
  593. copy_skb_header(n, skb);
  594. out:
  595. return n;
  596. }
  597. /**
  598. * pskb_expand_head - reallocate header of &sk_buff
  599. * @skb: buffer to reallocate
  600. * @nhead: room to add at head
  601. * @ntail: room to add at tail
  602. * @gfp_mask: allocation priority
  603. *
  604. * Expands (or creates identical copy, if &nhead and &ntail are zero)
  605. * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
  606. * reference count of 1. Returns zero in the case of success or error,
  607. * if expansion failed. In the last case, &sk_buff is not changed.
  608. *
  609. * All the pointers pointing into skb header may change and must be
  610. * reloaded after call to this function.
  611. */
  612. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  613. gfp_t gfp_mask)
  614. {
  615. int i;
  616. u8 *data;
  617. int size = nhead + (skb->end - skb->head) + ntail;
  618. long off;
  619. if (skb_shared(skb))
  620. BUG();
  621. size = SKB_DATA_ALIGN(size);
  622. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  623. if (!data)
  624. goto nodata;
  625. /* Copy only real data... and, alas, header. This should be
  626. * optimized for the cases when header is void. */
  627. memcpy(data + nhead, skb->head, skb->tail - skb->head);
  628. memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
  629. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  630. get_page(skb_shinfo(skb)->frags[i].page);
  631. if (skb_shinfo(skb)->frag_list)
  632. skb_clone_fraglist(skb);
  633. skb_release_data(skb);
  634. off = (data + nhead) - skb->head;
  635. skb->head = data;
  636. skb->end = data + size;
  637. skb->data += off;
  638. skb->tail += off;
  639. skb->mac.raw += off;
  640. skb->h.raw += off;
  641. skb->nh.raw += off;
  642. skb->cloned = 0;
  643. skb->nohdr = 0;
  644. atomic_set(&skb_shinfo(skb)->dataref, 1);
  645. return 0;
  646. nodata:
  647. return -ENOMEM;
  648. }
  649. /* Make private copy of skb with writable head and some headroom */
  650. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  651. {
  652. struct sk_buff *skb2;
  653. int delta = headroom - skb_headroom(skb);
  654. if (delta <= 0)
  655. skb2 = pskb_copy(skb, GFP_ATOMIC);
  656. else {
  657. skb2 = skb_clone(skb, GFP_ATOMIC);
  658. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  659. GFP_ATOMIC)) {
  660. kfree_skb(skb2);
  661. skb2 = NULL;
  662. }
  663. }
  664. return skb2;
  665. }
  666. /**
  667. * skb_copy_expand - copy and expand sk_buff
  668. * @skb: buffer to copy
  669. * @newheadroom: new free bytes at head
  670. * @newtailroom: new free bytes at tail
  671. * @gfp_mask: allocation priority
  672. *
  673. * Make a copy of both an &sk_buff and its data and while doing so
  674. * allocate additional space.
  675. *
  676. * This is used when the caller wishes to modify the data and needs a
  677. * private copy of the data to alter as well as more space for new fields.
  678. * Returns %NULL on failure or the pointer to the buffer
  679. * on success. The returned buffer has a reference count of 1.
  680. *
  681. * You must pass %GFP_ATOMIC as the allocation priority if this function
  682. * is called from an interrupt.
  683. *
  684. * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
  685. * only by netfilter in the cases when checksum is recalculated? --ANK
  686. */
  687. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  688. int newheadroom, int newtailroom,
  689. gfp_t gfp_mask)
  690. {
  691. /*
  692. * Allocate the copy buffer
  693. */
  694. struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
  695. gfp_mask);
  696. int head_copy_len, head_copy_off;
  697. if (!n)
  698. return NULL;
  699. skb_reserve(n, newheadroom);
  700. /* Set the tail pointer and length */
  701. skb_put(n, skb->len);
  702. head_copy_len = skb_headroom(skb);
  703. head_copy_off = 0;
  704. if (newheadroom <= head_copy_len)
  705. head_copy_len = newheadroom;
  706. else
  707. head_copy_off = newheadroom - head_copy_len;
  708. /* Copy the linear header and data. */
  709. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  710. skb->len + head_copy_len))
  711. BUG();
  712. copy_skb_header(n, skb);
  713. return n;
  714. }
  715. /**
  716. * skb_pad - zero pad the tail of an skb
  717. * @skb: buffer to pad
  718. * @pad: space to pad
  719. *
  720. * Ensure that a buffer is followed by a padding area that is zero
  721. * filled. Used by network drivers which may DMA or transfer data
  722. * beyond the buffer end onto the wire.
  723. *
  724. * May return error in out of memory cases. The skb is freed on error.
  725. */
  726. int skb_pad(struct sk_buff *skb, int pad)
  727. {
  728. int err;
  729. int ntail;
  730. /* If the skbuff is non linear tailroom is always zero.. */
  731. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  732. memset(skb->data+skb->len, 0, pad);
  733. return 0;
  734. }
  735. ntail = skb->data_len + pad - (skb->end - skb->tail);
  736. if (likely(skb_cloned(skb) || ntail > 0)) {
  737. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  738. if (unlikely(err))
  739. goto free_skb;
  740. }
  741. /* FIXME: The use of this function with non-linear skb's really needs
  742. * to be audited.
  743. */
  744. err = skb_linearize(skb);
  745. if (unlikely(err))
  746. goto free_skb;
  747. memset(skb->data + skb->len, 0, pad);
  748. return 0;
  749. free_skb:
  750. kfree_skb(skb);
  751. return err;
  752. }
  753. /* Trims skb to length len. It can change skb pointers.
  754. */
  755. int ___pskb_trim(struct sk_buff *skb, unsigned int len)
  756. {
  757. struct sk_buff **fragp;
  758. struct sk_buff *frag;
  759. int offset = skb_headlen(skb);
  760. int nfrags = skb_shinfo(skb)->nr_frags;
  761. int i;
  762. int err;
  763. if (skb_cloned(skb) &&
  764. unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
  765. return err;
  766. i = 0;
  767. if (offset >= len)
  768. goto drop_pages;
  769. for (; i < nfrags; i++) {
  770. int end = offset + skb_shinfo(skb)->frags[i].size;
  771. if (end < len) {
  772. offset = end;
  773. continue;
  774. }
  775. skb_shinfo(skb)->frags[i++].size = len - offset;
  776. drop_pages:
  777. skb_shinfo(skb)->nr_frags = i;
  778. for (; i < nfrags; i++)
  779. put_page(skb_shinfo(skb)->frags[i].page);
  780. if (skb_shinfo(skb)->frag_list)
  781. skb_drop_fraglist(skb);
  782. goto done;
  783. }
  784. for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
  785. fragp = &frag->next) {
  786. int end = offset + frag->len;
  787. if (skb_shared(frag)) {
  788. struct sk_buff *nfrag;
  789. nfrag = skb_clone(frag, GFP_ATOMIC);
  790. if (unlikely(!nfrag))
  791. return -ENOMEM;
  792. nfrag->next = frag->next;
  793. kfree_skb(frag);
  794. frag = nfrag;
  795. *fragp = frag;
  796. }
  797. if (end < len) {
  798. offset = end;
  799. continue;
  800. }
  801. if (end > len &&
  802. unlikely((err = pskb_trim(frag, len - offset))))
  803. return err;
  804. if (frag->next)
  805. skb_drop_list(&frag->next);
  806. break;
  807. }
  808. done:
  809. if (len > skb_headlen(skb)) {
  810. skb->data_len -= skb->len - len;
  811. skb->len = len;
  812. } else {
  813. skb->len = len;
  814. skb->data_len = 0;
  815. skb->tail = skb->data + len;
  816. }
  817. return 0;
  818. }
  819. /**
  820. * __pskb_pull_tail - advance tail of skb header
  821. * @skb: buffer to reallocate
  822. * @delta: number of bytes to advance tail
  823. *
  824. * The function makes a sense only on a fragmented &sk_buff,
  825. * it expands header moving its tail forward and copying necessary
  826. * data from fragmented part.
  827. *
  828. * &sk_buff MUST have reference count of 1.
  829. *
  830. * Returns %NULL (and &sk_buff does not change) if pull failed
  831. * or value of new tail of skb in the case of success.
  832. *
  833. * All the pointers pointing into skb header may change and must be
  834. * reloaded after call to this function.
  835. */
  836. /* Moves tail of skb head forward, copying data from fragmented part,
  837. * when it is necessary.
  838. * 1. It may fail due to malloc failure.
  839. * 2. It may change skb pointers.
  840. *
  841. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  842. */
  843. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  844. {
  845. /* If skb has not enough free space at tail, get new one
  846. * plus 128 bytes for future expansions. If we have enough
  847. * room at tail, reallocate without expansion only if skb is cloned.
  848. */
  849. int i, k, eat = (skb->tail + delta) - skb->end;
  850. if (eat > 0 || skb_cloned(skb)) {
  851. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  852. GFP_ATOMIC))
  853. return NULL;
  854. }
  855. if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
  856. BUG();
  857. /* Optimization: no fragments, no reasons to preestimate
  858. * size of pulled pages. Superb.
  859. */
  860. if (!skb_shinfo(skb)->frag_list)
  861. goto pull_pages;
  862. /* Estimate size of pulled pages. */
  863. eat = delta;
  864. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  865. if (skb_shinfo(skb)->frags[i].size >= eat)
  866. goto pull_pages;
  867. eat -= skb_shinfo(skb)->frags[i].size;
  868. }
  869. /* If we need update frag list, we are in troubles.
  870. * Certainly, it possible to add an offset to skb data,
  871. * but taking into account that pulling is expected to
  872. * be very rare operation, it is worth to fight against
  873. * further bloating skb head and crucify ourselves here instead.
  874. * Pure masohism, indeed. 8)8)
  875. */
  876. if (eat) {
  877. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  878. struct sk_buff *clone = NULL;
  879. struct sk_buff *insp = NULL;
  880. do {
  881. BUG_ON(!list);
  882. if (list->len <= eat) {
  883. /* Eaten as whole. */
  884. eat -= list->len;
  885. list = list->next;
  886. insp = list;
  887. } else {
  888. /* Eaten partially. */
  889. if (skb_shared(list)) {
  890. /* Sucks! We need to fork list. :-( */
  891. clone = skb_clone(list, GFP_ATOMIC);
  892. if (!clone)
  893. return NULL;
  894. insp = list->next;
  895. list = clone;
  896. } else {
  897. /* This may be pulled without
  898. * problems. */
  899. insp = list;
  900. }
  901. if (!pskb_pull(list, eat)) {
  902. if (clone)
  903. kfree_skb(clone);
  904. return NULL;
  905. }
  906. break;
  907. }
  908. } while (eat);
  909. /* Free pulled out fragments. */
  910. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  911. skb_shinfo(skb)->frag_list = list->next;
  912. kfree_skb(list);
  913. }
  914. /* And insert new clone at head. */
  915. if (clone) {
  916. clone->next = list;
  917. skb_shinfo(skb)->frag_list = clone;
  918. }
  919. }
  920. /* Success! Now we may commit changes to skb data. */
  921. pull_pages:
  922. eat = delta;
  923. k = 0;
  924. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  925. if (skb_shinfo(skb)->frags[i].size <= eat) {
  926. put_page(skb_shinfo(skb)->frags[i].page);
  927. eat -= skb_shinfo(skb)->frags[i].size;
  928. } else {
  929. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  930. if (eat) {
  931. skb_shinfo(skb)->frags[k].page_offset += eat;
  932. skb_shinfo(skb)->frags[k].size -= eat;
  933. eat = 0;
  934. }
  935. k++;
  936. }
  937. }
  938. skb_shinfo(skb)->nr_frags = k;
  939. skb->tail += delta;
  940. skb->data_len -= delta;
  941. return skb->tail;
  942. }
  943. /* Copy some data bits from skb to kernel buffer. */
  944. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  945. {
  946. int i, copy;
  947. int start = skb_headlen(skb);
  948. if (offset > (int)skb->len - len)
  949. goto fault;
  950. /* Copy header. */
  951. if ((copy = start - offset) > 0) {
  952. if (copy > len)
  953. copy = len;
  954. memcpy(to, skb->data + offset, copy);
  955. if ((len -= copy) == 0)
  956. return 0;
  957. offset += copy;
  958. to += copy;
  959. }
  960. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  961. int end;
  962. BUG_TRAP(start <= offset + len);
  963. end = start + skb_shinfo(skb)->frags[i].size;
  964. if ((copy = end - offset) > 0) {
  965. u8 *vaddr;
  966. if (copy > len)
  967. copy = len;
  968. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  969. memcpy(to,
  970. vaddr + skb_shinfo(skb)->frags[i].page_offset+
  971. offset - start, copy);
  972. kunmap_skb_frag(vaddr);
  973. if ((len -= copy) == 0)
  974. return 0;
  975. offset += copy;
  976. to += copy;
  977. }
  978. start = end;
  979. }
  980. if (skb_shinfo(skb)->frag_list) {
  981. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  982. for (; list; list = list->next) {
  983. int end;
  984. BUG_TRAP(start <= offset + len);
  985. end = start + list->len;
  986. if ((copy = end - offset) > 0) {
  987. if (copy > len)
  988. copy = len;
  989. if (skb_copy_bits(list, offset - start,
  990. to, copy))
  991. goto fault;
  992. if ((len -= copy) == 0)
  993. return 0;
  994. offset += copy;
  995. to += copy;
  996. }
  997. start = end;
  998. }
  999. }
  1000. if (!len)
  1001. return 0;
  1002. fault:
  1003. return -EFAULT;
  1004. }
  1005. /**
  1006. * skb_store_bits - store bits from kernel buffer to skb
  1007. * @skb: destination buffer
  1008. * @offset: offset in destination
  1009. * @from: source buffer
  1010. * @len: number of bytes to copy
  1011. *
  1012. * Copy the specified number of bytes from the source buffer to the
  1013. * destination skb. This function handles all the messy bits of
  1014. * traversing fragment lists and such.
  1015. */
  1016. int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
  1017. {
  1018. int i, copy;
  1019. int start = skb_headlen(skb);
  1020. if (offset > (int)skb->len - len)
  1021. goto fault;
  1022. if ((copy = start - offset) > 0) {
  1023. if (copy > len)
  1024. copy = len;
  1025. memcpy(skb->data + offset, from, copy);
  1026. if ((len -= copy) == 0)
  1027. return 0;
  1028. offset += copy;
  1029. from += copy;
  1030. }
  1031. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1032. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1033. int end;
  1034. BUG_TRAP(start <= offset + len);
  1035. end = start + frag->size;
  1036. if ((copy = end - offset) > 0) {
  1037. u8 *vaddr;
  1038. if (copy > len)
  1039. copy = len;
  1040. vaddr = kmap_skb_frag(frag);
  1041. memcpy(vaddr + frag->page_offset + offset - start,
  1042. from, copy);
  1043. kunmap_skb_frag(vaddr);
  1044. if ((len -= copy) == 0)
  1045. return 0;
  1046. offset += copy;
  1047. from += copy;
  1048. }
  1049. start = end;
  1050. }
  1051. if (skb_shinfo(skb)->frag_list) {
  1052. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1053. for (; list; list = list->next) {
  1054. int end;
  1055. BUG_TRAP(start <= offset + len);
  1056. end = start + list->len;
  1057. if ((copy = end - offset) > 0) {
  1058. if (copy > len)
  1059. copy = len;
  1060. if (skb_store_bits(list, offset - start,
  1061. from, copy))
  1062. goto fault;
  1063. if ((len -= copy) == 0)
  1064. return 0;
  1065. offset += copy;
  1066. from += copy;
  1067. }
  1068. start = end;
  1069. }
  1070. }
  1071. if (!len)
  1072. return 0;
  1073. fault:
  1074. return -EFAULT;
  1075. }
  1076. EXPORT_SYMBOL(skb_store_bits);
  1077. /* Checksum skb data. */
  1078. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1079. int len, __wsum csum)
  1080. {
  1081. int start = skb_headlen(skb);
  1082. int i, copy = start - offset;
  1083. int pos = 0;
  1084. /* Checksum header. */
  1085. if (copy > 0) {
  1086. if (copy > len)
  1087. copy = len;
  1088. csum = csum_partial(skb->data + offset, copy, csum);
  1089. if ((len -= copy) == 0)
  1090. return csum;
  1091. offset += copy;
  1092. pos = copy;
  1093. }
  1094. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1095. int end;
  1096. BUG_TRAP(start <= offset + len);
  1097. end = start + skb_shinfo(skb)->frags[i].size;
  1098. if ((copy = end - offset) > 0) {
  1099. __wsum csum2;
  1100. u8 *vaddr;
  1101. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1102. if (copy > len)
  1103. copy = len;
  1104. vaddr = kmap_skb_frag(frag);
  1105. csum2 = csum_partial(vaddr + frag->page_offset +
  1106. offset - start, copy, 0);
  1107. kunmap_skb_frag(vaddr);
  1108. csum = csum_block_add(csum, csum2, pos);
  1109. if (!(len -= copy))
  1110. return csum;
  1111. offset += copy;
  1112. pos += copy;
  1113. }
  1114. start = end;
  1115. }
  1116. if (skb_shinfo(skb)->frag_list) {
  1117. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1118. for (; list; list = list->next) {
  1119. int end;
  1120. BUG_TRAP(start <= offset + len);
  1121. end = start + list->len;
  1122. if ((copy = end - offset) > 0) {
  1123. __wsum csum2;
  1124. if (copy > len)
  1125. copy = len;
  1126. csum2 = skb_checksum(list, offset - start,
  1127. copy, 0);
  1128. csum = csum_block_add(csum, csum2, pos);
  1129. if ((len -= copy) == 0)
  1130. return csum;
  1131. offset += copy;
  1132. pos += copy;
  1133. }
  1134. start = end;
  1135. }
  1136. }
  1137. BUG_ON(len);
  1138. return csum;
  1139. }
  1140. /* Both of above in one bottle. */
  1141. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1142. u8 *to, int len, __wsum csum)
  1143. {
  1144. int start = skb_headlen(skb);
  1145. int i, copy = start - offset;
  1146. int pos = 0;
  1147. /* Copy header. */
  1148. if (copy > 0) {
  1149. if (copy > len)
  1150. copy = len;
  1151. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1152. copy, csum);
  1153. if ((len -= copy) == 0)
  1154. return csum;
  1155. offset += copy;
  1156. to += copy;
  1157. pos = copy;
  1158. }
  1159. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1160. int end;
  1161. BUG_TRAP(start <= offset + len);
  1162. end = start + skb_shinfo(skb)->frags[i].size;
  1163. if ((copy = end - offset) > 0) {
  1164. __wsum csum2;
  1165. u8 *vaddr;
  1166. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1167. if (copy > len)
  1168. copy = len;
  1169. vaddr = kmap_skb_frag(frag);
  1170. csum2 = csum_partial_copy_nocheck(vaddr +
  1171. frag->page_offset +
  1172. offset - start, to,
  1173. copy, 0);
  1174. kunmap_skb_frag(vaddr);
  1175. csum = csum_block_add(csum, csum2, pos);
  1176. if (!(len -= copy))
  1177. return csum;
  1178. offset += copy;
  1179. to += copy;
  1180. pos += copy;
  1181. }
  1182. start = end;
  1183. }
  1184. if (skb_shinfo(skb)->frag_list) {
  1185. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1186. for (; list; list = list->next) {
  1187. __wsum csum2;
  1188. int end;
  1189. BUG_TRAP(start <= offset + len);
  1190. end = start + list->len;
  1191. if ((copy = end - offset) > 0) {
  1192. if (copy > len)
  1193. copy = len;
  1194. csum2 = skb_copy_and_csum_bits(list,
  1195. offset - start,
  1196. to, copy, 0);
  1197. csum = csum_block_add(csum, csum2, pos);
  1198. if ((len -= copy) == 0)
  1199. return csum;
  1200. offset += copy;
  1201. to += copy;
  1202. pos += copy;
  1203. }
  1204. start = end;
  1205. }
  1206. }
  1207. BUG_ON(len);
  1208. return csum;
  1209. }
  1210. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1211. {
  1212. __wsum csum;
  1213. long csstart;
  1214. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1215. csstart = skb->h.raw - skb->data;
  1216. else
  1217. csstart = skb_headlen(skb);
  1218. BUG_ON(csstart > skb_headlen(skb));
  1219. memcpy(to, skb->data, csstart);
  1220. csum = 0;
  1221. if (csstart != skb->len)
  1222. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1223. skb->len - csstart, 0);
  1224. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1225. long csstuff = csstart + skb->csum_offset;
  1226. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  1227. }
  1228. }
  1229. /**
  1230. * skb_dequeue - remove from the head of the queue
  1231. * @list: list to dequeue from
  1232. *
  1233. * Remove the head of the list. The list lock is taken so the function
  1234. * may be used safely with other locking list functions. The head item is
  1235. * returned or %NULL if the list is empty.
  1236. */
  1237. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1238. {
  1239. unsigned long flags;
  1240. struct sk_buff *result;
  1241. spin_lock_irqsave(&list->lock, flags);
  1242. result = __skb_dequeue(list);
  1243. spin_unlock_irqrestore(&list->lock, flags);
  1244. return result;
  1245. }
  1246. /**
  1247. * skb_dequeue_tail - remove from the tail of the queue
  1248. * @list: list to dequeue from
  1249. *
  1250. * Remove the tail of the list. The list lock is taken so the function
  1251. * may be used safely with other locking list functions. The tail item is
  1252. * returned or %NULL if the list is empty.
  1253. */
  1254. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1255. {
  1256. unsigned long flags;
  1257. struct sk_buff *result;
  1258. spin_lock_irqsave(&list->lock, flags);
  1259. result = __skb_dequeue_tail(list);
  1260. spin_unlock_irqrestore(&list->lock, flags);
  1261. return result;
  1262. }
  1263. /**
  1264. * skb_queue_purge - empty a list
  1265. * @list: list to empty
  1266. *
  1267. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1268. * the list and one reference dropped. This function takes the list
  1269. * lock and is atomic with respect to other list locking functions.
  1270. */
  1271. void skb_queue_purge(struct sk_buff_head *list)
  1272. {
  1273. struct sk_buff *skb;
  1274. while ((skb = skb_dequeue(list)) != NULL)
  1275. kfree_skb(skb);
  1276. }
  1277. /**
  1278. * skb_queue_head - queue a buffer at the list head
  1279. * @list: list to use
  1280. * @newsk: buffer to queue
  1281. *
  1282. * Queue a buffer at the start of the list. This function takes the
  1283. * list lock and can be used safely with other locking &sk_buff functions
  1284. * safely.
  1285. *
  1286. * A buffer cannot be placed on two lists at the same time.
  1287. */
  1288. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1289. {
  1290. unsigned long flags;
  1291. spin_lock_irqsave(&list->lock, flags);
  1292. __skb_queue_head(list, newsk);
  1293. spin_unlock_irqrestore(&list->lock, flags);
  1294. }
  1295. /**
  1296. * skb_queue_tail - queue a buffer at the list tail
  1297. * @list: list to use
  1298. * @newsk: buffer to queue
  1299. *
  1300. * Queue a buffer at the tail of the list. This function takes the
  1301. * list lock and can be used safely with other locking &sk_buff functions
  1302. * safely.
  1303. *
  1304. * A buffer cannot be placed on two lists at the same time.
  1305. */
  1306. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1307. {
  1308. unsigned long flags;
  1309. spin_lock_irqsave(&list->lock, flags);
  1310. __skb_queue_tail(list, newsk);
  1311. spin_unlock_irqrestore(&list->lock, flags);
  1312. }
  1313. /**
  1314. * skb_unlink - remove a buffer from a list
  1315. * @skb: buffer to remove
  1316. * @list: list to use
  1317. *
  1318. * Remove a packet from a list. The list locks are taken and this
  1319. * function is atomic with respect to other list locked calls
  1320. *
  1321. * You must know what list the SKB is on.
  1322. */
  1323. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1324. {
  1325. unsigned long flags;
  1326. spin_lock_irqsave(&list->lock, flags);
  1327. __skb_unlink(skb, list);
  1328. spin_unlock_irqrestore(&list->lock, flags);
  1329. }
  1330. /**
  1331. * skb_append - append a buffer
  1332. * @old: buffer to insert after
  1333. * @newsk: buffer to insert
  1334. * @list: list to use
  1335. *
  1336. * Place a packet after a given packet in a list. The list locks are taken
  1337. * and this function is atomic with respect to other list locked calls.
  1338. * A buffer cannot be placed on two lists at the same time.
  1339. */
  1340. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1341. {
  1342. unsigned long flags;
  1343. spin_lock_irqsave(&list->lock, flags);
  1344. __skb_append(old, newsk, list);
  1345. spin_unlock_irqrestore(&list->lock, flags);
  1346. }
  1347. /**
  1348. * skb_insert - insert a buffer
  1349. * @old: buffer to insert before
  1350. * @newsk: buffer to insert
  1351. * @list: list to use
  1352. *
  1353. * Place a packet before a given packet in a list. The list locks are
  1354. * taken and this function is atomic with respect to other list locked
  1355. * calls.
  1356. *
  1357. * A buffer cannot be placed on two lists at the same time.
  1358. */
  1359. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1360. {
  1361. unsigned long flags;
  1362. spin_lock_irqsave(&list->lock, flags);
  1363. __skb_insert(newsk, old->prev, old, list);
  1364. spin_unlock_irqrestore(&list->lock, flags);
  1365. }
  1366. #if 0
  1367. /*
  1368. * Tune the memory allocator for a new MTU size.
  1369. */
  1370. void skb_add_mtu(int mtu)
  1371. {
  1372. /* Must match allocation in alloc_skb */
  1373. mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
  1374. kmem_add_cache_size(mtu);
  1375. }
  1376. #endif
  1377. static inline void skb_split_inside_header(struct sk_buff *skb,
  1378. struct sk_buff* skb1,
  1379. const u32 len, const int pos)
  1380. {
  1381. int i;
  1382. memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
  1383. /* And move data appendix as is. */
  1384. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1385. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1386. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1387. skb_shinfo(skb)->nr_frags = 0;
  1388. skb1->data_len = skb->data_len;
  1389. skb1->len += skb1->data_len;
  1390. skb->data_len = 0;
  1391. skb->len = len;
  1392. skb->tail = skb->data + len;
  1393. }
  1394. static inline void skb_split_no_header(struct sk_buff *skb,
  1395. struct sk_buff* skb1,
  1396. const u32 len, int pos)
  1397. {
  1398. int i, k = 0;
  1399. const int nfrags = skb_shinfo(skb)->nr_frags;
  1400. skb_shinfo(skb)->nr_frags = 0;
  1401. skb1->len = skb1->data_len = skb->len - len;
  1402. skb->len = len;
  1403. skb->data_len = len - pos;
  1404. for (i = 0; i < nfrags; i++) {
  1405. int size = skb_shinfo(skb)->frags[i].size;
  1406. if (pos + size > len) {
  1407. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1408. if (pos < len) {
  1409. /* Split frag.
  1410. * We have two variants in this case:
  1411. * 1. Move all the frag to the second
  1412. * part, if it is possible. F.e.
  1413. * this approach is mandatory for TUX,
  1414. * where splitting is expensive.
  1415. * 2. Split is accurately. We make this.
  1416. */
  1417. get_page(skb_shinfo(skb)->frags[i].page);
  1418. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1419. skb_shinfo(skb1)->frags[0].size -= len - pos;
  1420. skb_shinfo(skb)->frags[i].size = len - pos;
  1421. skb_shinfo(skb)->nr_frags++;
  1422. }
  1423. k++;
  1424. } else
  1425. skb_shinfo(skb)->nr_frags++;
  1426. pos += size;
  1427. }
  1428. skb_shinfo(skb1)->nr_frags = k;
  1429. }
  1430. /**
  1431. * skb_split - Split fragmented skb to two parts at length len.
  1432. * @skb: the buffer to split
  1433. * @skb1: the buffer to receive the second part
  1434. * @len: new length for skb
  1435. */
  1436. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1437. {
  1438. int pos = skb_headlen(skb);
  1439. if (len < pos) /* Split line is inside header. */
  1440. skb_split_inside_header(skb, skb1, len, pos);
  1441. else /* Second chunk has no header, nothing to copy. */
  1442. skb_split_no_header(skb, skb1, len, pos);
  1443. }
  1444. /**
  1445. * skb_prepare_seq_read - Prepare a sequential read of skb data
  1446. * @skb: the buffer to read
  1447. * @from: lower offset of data to be read
  1448. * @to: upper offset of data to be read
  1449. * @st: state variable
  1450. *
  1451. * Initializes the specified state variable. Must be called before
  1452. * invoking skb_seq_read() for the first time.
  1453. */
  1454. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  1455. unsigned int to, struct skb_seq_state *st)
  1456. {
  1457. st->lower_offset = from;
  1458. st->upper_offset = to;
  1459. st->root_skb = st->cur_skb = skb;
  1460. st->frag_idx = st->stepped_offset = 0;
  1461. st->frag_data = NULL;
  1462. }
  1463. /**
  1464. * skb_seq_read - Sequentially read skb data
  1465. * @consumed: number of bytes consumed by the caller so far
  1466. * @data: destination pointer for data to be returned
  1467. * @st: state variable
  1468. *
  1469. * Reads a block of skb data at &consumed relative to the
  1470. * lower offset specified to skb_prepare_seq_read(). Assigns
  1471. * the head of the data block to &data and returns the length
  1472. * of the block or 0 if the end of the skb data or the upper
  1473. * offset has been reached.
  1474. *
  1475. * The caller is not required to consume all of the data
  1476. * returned, i.e. &consumed is typically set to the number
  1477. * of bytes already consumed and the next call to
  1478. * skb_seq_read() will return the remaining part of the block.
  1479. *
  1480. * Note: The size of each block of data returned can be arbitary,
  1481. * this limitation is the cost for zerocopy seqeuental
  1482. * reads of potentially non linear data.
  1483. *
  1484. * Note: Fragment lists within fragments are not implemented
  1485. * at the moment, state->root_skb could be replaced with
  1486. * a stack for this purpose.
  1487. */
  1488. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  1489. struct skb_seq_state *st)
  1490. {
  1491. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  1492. skb_frag_t *frag;
  1493. if (unlikely(abs_offset >= st->upper_offset))
  1494. return 0;
  1495. next_skb:
  1496. block_limit = skb_headlen(st->cur_skb);
  1497. if (abs_offset < block_limit) {
  1498. *data = st->cur_skb->data + abs_offset;
  1499. return block_limit - abs_offset;
  1500. }
  1501. if (st->frag_idx == 0 && !st->frag_data)
  1502. st->stepped_offset += skb_headlen(st->cur_skb);
  1503. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  1504. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  1505. block_limit = frag->size + st->stepped_offset;
  1506. if (abs_offset < block_limit) {
  1507. if (!st->frag_data)
  1508. st->frag_data = kmap_skb_frag(frag);
  1509. *data = (u8 *) st->frag_data + frag->page_offset +
  1510. (abs_offset - st->stepped_offset);
  1511. return block_limit - abs_offset;
  1512. }
  1513. if (st->frag_data) {
  1514. kunmap_skb_frag(st->frag_data);
  1515. st->frag_data = NULL;
  1516. }
  1517. st->frag_idx++;
  1518. st->stepped_offset += frag->size;
  1519. }
  1520. if (st->cur_skb->next) {
  1521. st->cur_skb = st->cur_skb->next;
  1522. st->frag_idx = 0;
  1523. goto next_skb;
  1524. } else if (st->root_skb == st->cur_skb &&
  1525. skb_shinfo(st->root_skb)->frag_list) {
  1526. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  1527. goto next_skb;
  1528. }
  1529. return 0;
  1530. }
  1531. /**
  1532. * skb_abort_seq_read - Abort a sequential read of skb data
  1533. * @st: state variable
  1534. *
  1535. * Must be called if skb_seq_read() was not called until it
  1536. * returned 0.
  1537. */
  1538. void skb_abort_seq_read(struct skb_seq_state *st)
  1539. {
  1540. if (st->frag_data)
  1541. kunmap_skb_frag(st->frag_data);
  1542. }
  1543. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  1544. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  1545. struct ts_config *conf,
  1546. struct ts_state *state)
  1547. {
  1548. return skb_seq_read(offset, text, TS_SKB_CB(state));
  1549. }
  1550. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  1551. {
  1552. skb_abort_seq_read(TS_SKB_CB(state));
  1553. }
  1554. /**
  1555. * skb_find_text - Find a text pattern in skb data
  1556. * @skb: the buffer to look in
  1557. * @from: search offset
  1558. * @to: search limit
  1559. * @config: textsearch configuration
  1560. * @state: uninitialized textsearch state variable
  1561. *
  1562. * Finds a pattern in the skb data according to the specified
  1563. * textsearch configuration. Use textsearch_next() to retrieve
  1564. * subsequent occurrences of the pattern. Returns the offset
  1565. * to the first occurrence or UINT_MAX if no match was found.
  1566. */
  1567. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  1568. unsigned int to, struct ts_config *config,
  1569. struct ts_state *state)
  1570. {
  1571. unsigned int ret;
  1572. config->get_next_block = skb_ts_get_next_block;
  1573. config->finish = skb_ts_finish;
  1574. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  1575. ret = textsearch_find(config, state);
  1576. return (ret <= to - from ? ret : UINT_MAX);
  1577. }
  1578. /**
  1579. * skb_append_datato_frags: - append the user data to a skb
  1580. * @sk: sock structure
  1581. * @skb: skb structure to be appened with user data.
  1582. * @getfrag: call back function to be used for getting the user data
  1583. * @from: pointer to user message iov
  1584. * @length: length of the iov message
  1585. *
  1586. * Description: This procedure append the user data in the fragment part
  1587. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  1588. */
  1589. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  1590. int (*getfrag)(void *from, char *to, int offset,
  1591. int len, int odd, struct sk_buff *skb),
  1592. void *from, int length)
  1593. {
  1594. int frg_cnt = 0;
  1595. skb_frag_t *frag = NULL;
  1596. struct page *page = NULL;
  1597. int copy, left;
  1598. int offset = 0;
  1599. int ret;
  1600. do {
  1601. /* Return error if we don't have space for new frag */
  1602. frg_cnt = skb_shinfo(skb)->nr_frags;
  1603. if (frg_cnt >= MAX_SKB_FRAGS)
  1604. return -EFAULT;
  1605. /* allocate a new page for next frag */
  1606. page = alloc_pages(sk->sk_allocation, 0);
  1607. /* If alloc_page fails just return failure and caller will
  1608. * free previous allocated pages by doing kfree_skb()
  1609. */
  1610. if (page == NULL)
  1611. return -ENOMEM;
  1612. /* initialize the next frag */
  1613. sk->sk_sndmsg_page = page;
  1614. sk->sk_sndmsg_off = 0;
  1615. skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
  1616. skb->truesize += PAGE_SIZE;
  1617. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  1618. /* get the new initialized frag */
  1619. frg_cnt = skb_shinfo(skb)->nr_frags;
  1620. frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
  1621. /* copy the user data to page */
  1622. left = PAGE_SIZE - frag->page_offset;
  1623. copy = (length > left)? left : length;
  1624. ret = getfrag(from, (page_address(frag->page) +
  1625. frag->page_offset + frag->size),
  1626. offset, copy, 0, skb);
  1627. if (ret < 0)
  1628. return -EFAULT;
  1629. /* copy was successful so update the size parameters */
  1630. sk->sk_sndmsg_off += copy;
  1631. frag->size += copy;
  1632. skb->len += copy;
  1633. skb->data_len += copy;
  1634. offset += copy;
  1635. length -= copy;
  1636. } while (length > 0);
  1637. return 0;
  1638. }
  1639. /**
  1640. * skb_pull_rcsum - pull skb and update receive checksum
  1641. * @skb: buffer to update
  1642. * @start: start of data before pull
  1643. * @len: length of data pulled
  1644. *
  1645. * This function performs an skb_pull on the packet and updates
  1646. * update the CHECKSUM_COMPLETE checksum. It should be used on
  1647. * receive path processing instead of skb_pull unless you know
  1648. * that the checksum difference is zero (e.g., a valid IP header)
  1649. * or you are setting ip_summed to CHECKSUM_NONE.
  1650. */
  1651. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  1652. {
  1653. BUG_ON(len > skb->len);
  1654. skb->len -= len;
  1655. BUG_ON(skb->len < skb->data_len);
  1656. skb_postpull_rcsum(skb, skb->data, len);
  1657. return skb->data += len;
  1658. }
  1659. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  1660. /**
  1661. * skb_segment - Perform protocol segmentation on skb.
  1662. * @skb: buffer to segment
  1663. * @features: features for the output path (see dev->features)
  1664. *
  1665. * This function performs segmentation on the given skb. It returns
  1666. * the segment at the given position. It returns NULL if there are
  1667. * no more segments to generate, or when an error is encountered.
  1668. */
  1669. struct sk_buff *skb_segment(struct sk_buff *skb, int features)
  1670. {
  1671. struct sk_buff *segs = NULL;
  1672. struct sk_buff *tail = NULL;
  1673. unsigned int mss = skb_shinfo(skb)->gso_size;
  1674. unsigned int doffset = skb->data - skb->mac.raw;
  1675. unsigned int offset = doffset;
  1676. unsigned int headroom;
  1677. unsigned int len;
  1678. int sg = features & NETIF_F_SG;
  1679. int nfrags = skb_shinfo(skb)->nr_frags;
  1680. int err = -ENOMEM;
  1681. int i = 0;
  1682. int pos;
  1683. __skb_push(skb, doffset);
  1684. headroom = skb_headroom(skb);
  1685. pos = skb_headlen(skb);
  1686. do {
  1687. struct sk_buff *nskb;
  1688. skb_frag_t *frag;
  1689. int hsize;
  1690. int k;
  1691. int size;
  1692. len = skb->len - offset;
  1693. if (len > mss)
  1694. len = mss;
  1695. hsize = skb_headlen(skb) - offset;
  1696. if (hsize < 0)
  1697. hsize = 0;
  1698. if (hsize > len || !sg)
  1699. hsize = len;
  1700. nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
  1701. if (unlikely(!nskb))
  1702. goto err;
  1703. if (segs)
  1704. tail->next = nskb;
  1705. else
  1706. segs = nskb;
  1707. tail = nskb;
  1708. nskb->dev = skb->dev;
  1709. nskb->priority = skb->priority;
  1710. nskb->protocol = skb->protocol;
  1711. nskb->dst = dst_clone(skb->dst);
  1712. memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
  1713. nskb->pkt_type = skb->pkt_type;
  1714. nskb->mac_len = skb->mac_len;
  1715. skb_reserve(nskb, headroom);
  1716. nskb->mac.raw = nskb->data;
  1717. nskb->nh.raw = nskb->data + skb->mac_len;
  1718. nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
  1719. memcpy(skb_put(nskb, doffset), skb->data, doffset);
  1720. if (!sg) {
  1721. nskb->csum = skb_copy_and_csum_bits(skb, offset,
  1722. skb_put(nskb, len),
  1723. len, 0);
  1724. continue;
  1725. }
  1726. frag = skb_shinfo(nskb)->frags;
  1727. k = 0;
  1728. nskb->ip_summed = CHECKSUM_PARTIAL;
  1729. nskb->csum = skb->csum;
  1730. memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
  1731. while (pos < offset + len) {
  1732. BUG_ON(i >= nfrags);
  1733. *frag = skb_shinfo(skb)->frags[i];
  1734. get_page(frag->page);
  1735. size = frag->size;
  1736. if (pos < offset) {
  1737. frag->page_offset += offset - pos;
  1738. frag->size -= offset - pos;
  1739. }
  1740. k++;
  1741. if (pos + size <= offset + len) {
  1742. i++;
  1743. pos += size;
  1744. } else {
  1745. frag->size -= pos + size - (offset + len);
  1746. break;
  1747. }
  1748. frag++;
  1749. }
  1750. skb_shinfo(nskb)->nr_frags = k;
  1751. nskb->data_len = len - hsize;
  1752. nskb->len += nskb->data_len;
  1753. nskb->truesize += nskb->data_len;
  1754. } while ((offset += len) < skb->len);
  1755. return segs;
  1756. err:
  1757. while ((skb = segs)) {
  1758. segs = skb->next;
  1759. kfree_skb(skb);
  1760. }
  1761. return ERR_PTR(err);
  1762. }
  1763. EXPORT_SYMBOL_GPL(skb_segment);
  1764. void __init skb_init(void)
  1765. {
  1766. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  1767. sizeof(struct sk_buff),
  1768. 0,
  1769. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1770. NULL, NULL);
  1771. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  1772. (2*sizeof(struct sk_buff)) +
  1773. sizeof(atomic_t),
  1774. 0,
  1775. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1776. NULL, NULL);
  1777. }
  1778. EXPORT_SYMBOL(___pskb_trim);
  1779. EXPORT_SYMBOL(__kfree_skb);
  1780. EXPORT_SYMBOL(kfree_skb);
  1781. EXPORT_SYMBOL(__pskb_pull_tail);
  1782. EXPORT_SYMBOL(__alloc_skb);
  1783. EXPORT_SYMBOL(__netdev_alloc_skb);
  1784. EXPORT_SYMBOL(pskb_copy);
  1785. EXPORT_SYMBOL(pskb_expand_head);
  1786. EXPORT_SYMBOL(skb_checksum);
  1787. EXPORT_SYMBOL(skb_clone);
  1788. EXPORT_SYMBOL(skb_clone_fraglist);
  1789. EXPORT_SYMBOL(skb_copy);
  1790. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1791. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1792. EXPORT_SYMBOL(skb_copy_bits);
  1793. EXPORT_SYMBOL(skb_copy_expand);
  1794. EXPORT_SYMBOL(skb_over_panic);
  1795. EXPORT_SYMBOL(skb_pad);
  1796. EXPORT_SYMBOL(skb_realloc_headroom);
  1797. EXPORT_SYMBOL(skb_under_panic);
  1798. EXPORT_SYMBOL(skb_dequeue);
  1799. EXPORT_SYMBOL(skb_dequeue_tail);
  1800. EXPORT_SYMBOL(skb_insert);
  1801. EXPORT_SYMBOL(skb_queue_purge);
  1802. EXPORT_SYMBOL(skb_queue_head);
  1803. EXPORT_SYMBOL(skb_queue_tail);
  1804. EXPORT_SYMBOL(skb_unlink);
  1805. EXPORT_SYMBOL(skb_append);
  1806. EXPORT_SYMBOL(skb_split);
  1807. EXPORT_SYMBOL(skb_prepare_seq_read);
  1808. EXPORT_SYMBOL(skb_seq_read);
  1809. EXPORT_SYMBOL(skb_abort_seq_read);
  1810. EXPORT_SYMBOL(skb_find_text);
  1811. EXPORT_SYMBOL(skb_append_datato_frags);