skbuff.c 49 KB

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