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