skbuff.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  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/config.h>
  40. #include <linux/module.h>
  41. #include <linux/types.h>
  42. #include <linux/kernel.h>
  43. #include <linux/sched.h>
  44. #include <linux/mm.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/in.h>
  47. #include <linux/inet.h>
  48. #include <linux/slab.h>
  49. #include <linux/netdevice.h>
  50. #ifdef CONFIG_NET_CLS_ACT
  51. #include <net/pkt_sched.h>
  52. #endif
  53. #include <linux/string.h>
  54. #include <linux/skbuff.h>
  55. #include <linux/cache.h>
  56. #include <linux/rtnetlink.h>
  57. #include <linux/init.h>
  58. #include <linux/highmem.h>
  59. #include <net/protocol.h>
  60. #include <net/dst.h>
  61. #include <net/sock.h>
  62. #include <net/checksum.h>
  63. #include <net/xfrm.h>
  64. #include <asm/uaccess.h>
  65. #include <asm/system.h>
  66. static kmem_cache_t *skbuff_head_cache __read_mostly;
  67. static kmem_cache_t *skbuff_fclone_cache __read_mostly;
  68. /*
  69. * Keep out-of-line to prevent kernel bloat.
  70. * __builtin_return_address is not used because it is not always
  71. * reliable.
  72. */
  73. /**
  74. * skb_over_panic - private function
  75. * @skb: buffer
  76. * @sz: size
  77. * @here: address
  78. *
  79. * Out of line support code for skb_put(). Not user callable.
  80. */
  81. void skb_over_panic(struct sk_buff *skb, int sz, void *here)
  82. {
  83. printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
  84. "data:%p tail:%p end:%p dev:%s\n",
  85. here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
  86. skb->dev ? skb->dev->name : "<NULL>");
  87. BUG();
  88. }
  89. /**
  90. * skb_under_panic - private function
  91. * @skb: buffer
  92. * @sz: size
  93. * @here: address
  94. *
  95. * Out of line support code for skb_push(). Not user callable.
  96. */
  97. void skb_under_panic(struct sk_buff *skb, int sz, void *here)
  98. {
  99. printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
  100. "data:%p tail:%p end:%p dev:%s\n",
  101. here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
  102. skb->dev ? skb->dev->name : "<NULL>");
  103. BUG();
  104. }
  105. void skb_truesize_bug(struct sk_buff *skb)
  106. {
  107. printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
  108. "len=%u, sizeof(sk_buff)=%Zd\n",
  109. skb->truesize, skb->len, sizeof(struct sk_buff));
  110. }
  111. EXPORT_SYMBOL(skb_truesize_bug);
  112. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  113. * 'private' fields and also do memory statistics to find all the
  114. * [BEEP] leaks.
  115. *
  116. */
  117. /**
  118. * __alloc_skb - allocate a network buffer
  119. * @size: size to allocate
  120. * @gfp_mask: allocation mask
  121. * @fclone: allocate from fclone cache instead of head cache
  122. * and allocate a cloned (child) skb
  123. *
  124. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  125. * tail room of size bytes. The object has a reference count of one.
  126. * The return is the buffer. On a failure the return is %NULL.
  127. *
  128. * Buffers may only be allocated from interrupts using a @gfp_mask of
  129. * %GFP_ATOMIC.
  130. */
  131. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  132. int fclone)
  133. {
  134. kmem_cache_t *cache;
  135. struct skb_shared_info *shinfo;
  136. struct sk_buff *skb;
  137. u8 *data;
  138. cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
  139. /* Get the HEAD */
  140. skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
  141. if (!skb)
  142. goto out;
  143. /* Get the DATA. Size must match skb_add_mtu(). */
  144. size = SKB_DATA_ALIGN(size);
  145. data = ____kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  146. if (!data)
  147. goto nodata;
  148. memset(skb, 0, offsetof(struct sk_buff, truesize));
  149. skb->truesize = size + sizeof(struct sk_buff);
  150. atomic_set(&skb->users, 1);
  151. skb->head = data;
  152. skb->data = data;
  153. skb->tail = data;
  154. skb->end = data + size;
  155. /* make sure we initialize shinfo sequentially */
  156. shinfo = skb_shinfo(skb);
  157. atomic_set(&shinfo->dataref, 1);
  158. shinfo->nr_frags = 0;
  159. shinfo->tso_size = 0;
  160. shinfo->tso_segs = 0;
  161. shinfo->ufo_size = 0;
  162. shinfo->ip6_frag_id = 0;
  163. shinfo->frag_list = NULL;
  164. if (fclone) {
  165. struct sk_buff *child = skb + 1;
  166. atomic_t *fclone_ref = (atomic_t *) (child + 1);
  167. skb->fclone = SKB_FCLONE_ORIG;
  168. atomic_set(fclone_ref, 1);
  169. child->fclone = SKB_FCLONE_UNAVAILABLE;
  170. }
  171. out:
  172. return skb;
  173. nodata:
  174. kmem_cache_free(cache, skb);
  175. skb = NULL;
  176. goto out;
  177. }
  178. /**
  179. * alloc_skb_from_cache - allocate a network buffer
  180. * @cp: kmem_cache from which to allocate the data area
  181. * (object size must be big enough for @size bytes + skb overheads)
  182. * @size: size to allocate
  183. * @gfp_mask: allocation mask
  184. *
  185. * Allocate a new &sk_buff. The returned buffer has no headroom and
  186. * tail room of size bytes. The object has a reference count of one.
  187. * The return is the buffer. On a failure the return is %NULL.
  188. *
  189. * Buffers may only be allocated from interrupts using a @gfp_mask of
  190. * %GFP_ATOMIC.
  191. */
  192. struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
  193. unsigned int size,
  194. gfp_t gfp_mask)
  195. {
  196. struct sk_buff *skb;
  197. u8 *data;
  198. /* Get the HEAD */
  199. skb = kmem_cache_alloc(skbuff_head_cache,
  200. gfp_mask & ~__GFP_DMA);
  201. if (!skb)
  202. goto out;
  203. /* Get the DATA. */
  204. size = SKB_DATA_ALIGN(size);
  205. data = kmem_cache_alloc(cp, gfp_mask);
  206. if (!data)
  207. goto nodata;
  208. memset(skb, 0, offsetof(struct sk_buff, truesize));
  209. skb->truesize = size + sizeof(struct sk_buff);
  210. atomic_set(&skb->users, 1);
  211. skb->head = data;
  212. skb->data = data;
  213. skb->tail = data;
  214. skb->end = data + size;
  215. atomic_set(&(skb_shinfo(skb)->dataref), 1);
  216. skb_shinfo(skb)->nr_frags = 0;
  217. skb_shinfo(skb)->tso_size = 0;
  218. skb_shinfo(skb)->tso_segs = 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. 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)->tso_size = skb_shinfo(old)->tso_size;
  480. skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
  481. }
  482. /**
  483. * skb_copy - create private copy of an sk_buff
  484. * @skb: buffer to copy
  485. * @gfp_mask: allocation priority
  486. *
  487. * Make a copy of both an &sk_buff and its data. This is used when the
  488. * caller wishes to modify the data and needs a private copy of the
  489. * data to alter. Returns %NULL on failure or the pointer to the buffer
  490. * on success. The returned buffer has a reference count of 1.
  491. *
  492. * As by-product this function converts non-linear &sk_buff to linear
  493. * one, so that &sk_buff becomes completely private and caller is allowed
  494. * to modify all the data of returned buffer. This means that this
  495. * function is not recommended for use in circumstances when only
  496. * header is going to be modified. Use pskb_copy() instead.
  497. */
  498. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  499. {
  500. int headerlen = skb->data - skb->head;
  501. /*
  502. * Allocate the copy buffer
  503. */
  504. struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
  505. gfp_mask);
  506. if (!n)
  507. return NULL;
  508. /* Set the data pointer */
  509. skb_reserve(n, headerlen);
  510. /* Set the tail pointer and length */
  511. skb_put(n, skb->len);
  512. n->csum = skb->csum;
  513. n->ip_summed = skb->ip_summed;
  514. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  515. BUG();
  516. copy_skb_header(n, skb);
  517. return n;
  518. }
  519. /**
  520. * pskb_copy - create copy of an sk_buff with private head.
  521. * @skb: buffer to copy
  522. * @gfp_mask: allocation priority
  523. *
  524. * Make a copy of both an &sk_buff and part of its data, located
  525. * in header. Fragmented data remain shared. This is used when
  526. * the caller wishes to modify only header of &sk_buff and needs
  527. * private copy of the header to alter. Returns %NULL on failure
  528. * or the pointer to the buffer on success.
  529. * The returned buffer has a reference count of 1.
  530. */
  531. struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
  532. {
  533. /*
  534. * Allocate the copy buffer
  535. */
  536. struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
  537. if (!n)
  538. goto out;
  539. /* Set the data pointer */
  540. skb_reserve(n, skb->data - skb->head);
  541. /* Set the tail pointer and length */
  542. skb_put(n, skb_headlen(skb));
  543. /* Copy the bytes */
  544. memcpy(n->data, skb->data, n->len);
  545. n->csum = skb->csum;
  546. n->ip_summed = skb->ip_summed;
  547. n->data_len = skb->data_len;
  548. n->len = skb->len;
  549. if (skb_shinfo(skb)->nr_frags) {
  550. int i;
  551. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  552. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  553. get_page(skb_shinfo(n)->frags[i].page);
  554. }
  555. skb_shinfo(n)->nr_frags = i;
  556. }
  557. if (skb_shinfo(skb)->frag_list) {
  558. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  559. skb_clone_fraglist(n);
  560. }
  561. copy_skb_header(n, skb);
  562. out:
  563. return n;
  564. }
  565. /**
  566. * pskb_expand_head - reallocate header of &sk_buff
  567. * @skb: buffer to reallocate
  568. * @nhead: room to add at head
  569. * @ntail: room to add at tail
  570. * @gfp_mask: allocation priority
  571. *
  572. * Expands (or creates identical copy, if &nhead and &ntail are zero)
  573. * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
  574. * reference count of 1. Returns zero in the case of success or error,
  575. * if expansion failed. In the last case, &sk_buff is not changed.
  576. *
  577. * All the pointers pointing into skb header may change and must be
  578. * reloaded after call to this function.
  579. */
  580. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  581. gfp_t gfp_mask)
  582. {
  583. int i;
  584. u8 *data;
  585. int size = nhead + (skb->end - skb->head) + ntail;
  586. long off;
  587. if (skb_shared(skb))
  588. BUG();
  589. size = SKB_DATA_ALIGN(size);
  590. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  591. if (!data)
  592. goto nodata;
  593. /* Copy only real data... and, alas, header. This should be
  594. * optimized for the cases when header is void. */
  595. memcpy(data + nhead, skb->head, skb->tail - skb->head);
  596. memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
  597. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  598. get_page(skb_shinfo(skb)->frags[i].page);
  599. if (skb_shinfo(skb)->frag_list)
  600. skb_clone_fraglist(skb);
  601. skb_release_data(skb);
  602. off = (data + nhead) - skb->head;
  603. skb->head = data;
  604. skb->end = data + size;
  605. skb->data += off;
  606. skb->tail += off;
  607. skb->mac.raw += off;
  608. skb->h.raw += off;
  609. skb->nh.raw += off;
  610. skb->cloned = 0;
  611. skb->nohdr = 0;
  612. atomic_set(&skb_shinfo(skb)->dataref, 1);
  613. return 0;
  614. nodata:
  615. return -ENOMEM;
  616. }
  617. /* Make private copy of skb with writable head and some headroom */
  618. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  619. {
  620. struct sk_buff *skb2;
  621. int delta = headroom - skb_headroom(skb);
  622. if (delta <= 0)
  623. skb2 = pskb_copy(skb, GFP_ATOMIC);
  624. else {
  625. skb2 = skb_clone(skb, GFP_ATOMIC);
  626. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  627. GFP_ATOMIC)) {
  628. kfree_skb(skb2);
  629. skb2 = NULL;
  630. }
  631. }
  632. return skb2;
  633. }
  634. /**
  635. * skb_copy_expand - copy and expand sk_buff
  636. * @skb: buffer to copy
  637. * @newheadroom: new free bytes at head
  638. * @newtailroom: new free bytes at tail
  639. * @gfp_mask: allocation priority
  640. *
  641. * Make a copy of both an &sk_buff and its data and while doing so
  642. * allocate additional space.
  643. *
  644. * This is used when the caller wishes to modify the data and needs a
  645. * private copy of the data to alter as well as more space for new fields.
  646. * Returns %NULL on failure or the pointer to the buffer
  647. * on success. The returned buffer has a reference count of 1.
  648. *
  649. * You must pass %GFP_ATOMIC as the allocation priority if this function
  650. * is called from an interrupt.
  651. *
  652. * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
  653. * only by netfilter in the cases when checksum is recalculated? --ANK
  654. */
  655. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  656. int newheadroom, int newtailroom,
  657. gfp_t gfp_mask)
  658. {
  659. /*
  660. * Allocate the copy buffer
  661. */
  662. struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
  663. gfp_mask);
  664. int head_copy_len, head_copy_off;
  665. if (!n)
  666. return NULL;
  667. skb_reserve(n, newheadroom);
  668. /* Set the tail pointer and length */
  669. skb_put(n, skb->len);
  670. head_copy_len = skb_headroom(skb);
  671. head_copy_off = 0;
  672. if (newheadroom <= head_copy_len)
  673. head_copy_len = newheadroom;
  674. else
  675. head_copy_off = newheadroom - head_copy_len;
  676. /* Copy the linear header and data. */
  677. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  678. skb->len + head_copy_len))
  679. BUG();
  680. copy_skb_header(n, skb);
  681. return n;
  682. }
  683. /**
  684. * skb_pad - zero pad the tail of an skb
  685. * @skb: buffer to pad
  686. * @pad: space to pad
  687. *
  688. * Ensure that a buffer is followed by a padding area that is zero
  689. * filled. Used by network drivers which may DMA or transfer data
  690. * beyond the buffer end onto the wire.
  691. *
  692. * May return NULL in out of memory cases.
  693. */
  694. struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
  695. {
  696. struct sk_buff *nskb;
  697. /* If the skbuff is non linear tailroom is always zero.. */
  698. if (skb_tailroom(skb) >= pad) {
  699. memset(skb->data+skb->len, 0, pad);
  700. return skb;
  701. }
  702. nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
  703. kfree_skb(skb);
  704. if (nskb)
  705. memset(nskb->data+nskb->len, 0, pad);
  706. return nskb;
  707. }
  708. /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
  709. * If realloc==0 and trimming is impossible without change of data,
  710. * it is BUG().
  711. */
  712. int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
  713. {
  714. int offset = skb_headlen(skb);
  715. int nfrags = skb_shinfo(skb)->nr_frags;
  716. int i;
  717. for (i = 0; i < nfrags; i++) {
  718. int end = offset + skb_shinfo(skb)->frags[i].size;
  719. if (end > len) {
  720. if (skb_cloned(skb)) {
  721. BUG_ON(!realloc);
  722. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  723. return -ENOMEM;
  724. }
  725. if (len <= offset) {
  726. put_page(skb_shinfo(skb)->frags[i].page);
  727. skb_shinfo(skb)->nr_frags--;
  728. } else {
  729. skb_shinfo(skb)->frags[i].size = len - offset;
  730. }
  731. }
  732. offset = end;
  733. }
  734. if (offset < len) {
  735. skb->data_len -= skb->len - len;
  736. skb->len = len;
  737. } else {
  738. if (len <= skb_headlen(skb)) {
  739. skb->len = len;
  740. skb->data_len = 0;
  741. skb->tail = skb->data + len;
  742. if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
  743. skb_drop_fraglist(skb);
  744. } else {
  745. skb->data_len -= skb->len - len;
  746. skb->len = len;
  747. }
  748. }
  749. return 0;
  750. }
  751. /**
  752. * __pskb_pull_tail - advance tail of skb header
  753. * @skb: buffer to reallocate
  754. * @delta: number of bytes to advance tail
  755. *
  756. * The function makes a sense only on a fragmented &sk_buff,
  757. * it expands header moving its tail forward and copying necessary
  758. * data from fragmented part.
  759. *
  760. * &sk_buff MUST have reference count of 1.
  761. *
  762. * Returns %NULL (and &sk_buff does not change) if pull failed
  763. * or value of new tail of skb in the case of success.
  764. *
  765. * All the pointers pointing into skb header may change and must be
  766. * reloaded after call to this function.
  767. */
  768. /* Moves tail of skb head forward, copying data from fragmented part,
  769. * when it is necessary.
  770. * 1. It may fail due to malloc failure.
  771. * 2. It may change skb pointers.
  772. *
  773. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  774. */
  775. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  776. {
  777. /* If skb has not enough free space at tail, get new one
  778. * plus 128 bytes for future expansions. If we have enough
  779. * room at tail, reallocate without expansion only if skb is cloned.
  780. */
  781. int i, k, eat = (skb->tail + delta) - skb->end;
  782. if (eat > 0 || skb_cloned(skb)) {
  783. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  784. GFP_ATOMIC))
  785. return NULL;
  786. }
  787. if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
  788. BUG();
  789. /* Optimization: no fragments, no reasons to preestimate
  790. * size of pulled pages. Superb.
  791. */
  792. if (!skb_shinfo(skb)->frag_list)
  793. goto pull_pages;
  794. /* Estimate size of pulled pages. */
  795. eat = delta;
  796. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  797. if (skb_shinfo(skb)->frags[i].size >= eat)
  798. goto pull_pages;
  799. eat -= skb_shinfo(skb)->frags[i].size;
  800. }
  801. /* If we need update frag list, we are in troubles.
  802. * Certainly, it possible to add an offset to skb data,
  803. * but taking into account that pulling is expected to
  804. * be very rare operation, it is worth to fight against
  805. * further bloating skb head and crucify ourselves here instead.
  806. * Pure masohism, indeed. 8)8)
  807. */
  808. if (eat) {
  809. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  810. struct sk_buff *clone = NULL;
  811. struct sk_buff *insp = NULL;
  812. do {
  813. BUG_ON(!list);
  814. if (list->len <= eat) {
  815. /* Eaten as whole. */
  816. eat -= list->len;
  817. list = list->next;
  818. insp = list;
  819. } else {
  820. /* Eaten partially. */
  821. if (skb_shared(list)) {
  822. /* Sucks! We need to fork list. :-( */
  823. clone = skb_clone(list, GFP_ATOMIC);
  824. if (!clone)
  825. return NULL;
  826. insp = list->next;
  827. list = clone;
  828. } else {
  829. /* This may be pulled without
  830. * problems. */
  831. insp = list;
  832. }
  833. if (!pskb_pull(list, eat)) {
  834. if (clone)
  835. kfree_skb(clone);
  836. return NULL;
  837. }
  838. break;
  839. }
  840. } while (eat);
  841. /* Free pulled out fragments. */
  842. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  843. skb_shinfo(skb)->frag_list = list->next;
  844. kfree_skb(list);
  845. }
  846. /* And insert new clone at head. */
  847. if (clone) {
  848. clone->next = list;
  849. skb_shinfo(skb)->frag_list = clone;
  850. }
  851. }
  852. /* Success! Now we may commit changes to skb data. */
  853. pull_pages:
  854. eat = delta;
  855. k = 0;
  856. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  857. if (skb_shinfo(skb)->frags[i].size <= eat) {
  858. put_page(skb_shinfo(skb)->frags[i].page);
  859. eat -= skb_shinfo(skb)->frags[i].size;
  860. } else {
  861. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  862. if (eat) {
  863. skb_shinfo(skb)->frags[k].page_offset += eat;
  864. skb_shinfo(skb)->frags[k].size -= eat;
  865. eat = 0;
  866. }
  867. k++;
  868. }
  869. }
  870. skb_shinfo(skb)->nr_frags = k;
  871. skb->tail += delta;
  872. skb->data_len -= delta;
  873. return skb->tail;
  874. }
  875. /* Copy some data bits from skb to kernel buffer. */
  876. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  877. {
  878. int i, copy;
  879. int start = skb_headlen(skb);
  880. if (offset > (int)skb->len - len)
  881. goto fault;
  882. /* Copy header. */
  883. if ((copy = start - offset) > 0) {
  884. if (copy > len)
  885. copy = len;
  886. memcpy(to, skb->data + offset, copy);
  887. if ((len -= copy) == 0)
  888. return 0;
  889. offset += copy;
  890. to += copy;
  891. }
  892. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  893. int end;
  894. BUG_TRAP(start <= offset + len);
  895. end = start + skb_shinfo(skb)->frags[i].size;
  896. if ((copy = end - offset) > 0) {
  897. u8 *vaddr;
  898. if (copy > len)
  899. copy = len;
  900. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  901. memcpy(to,
  902. vaddr + skb_shinfo(skb)->frags[i].page_offset+
  903. offset - start, copy);
  904. kunmap_skb_frag(vaddr);
  905. if ((len -= copy) == 0)
  906. return 0;
  907. offset += copy;
  908. to += copy;
  909. }
  910. start = end;
  911. }
  912. if (skb_shinfo(skb)->frag_list) {
  913. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  914. for (; list; list = list->next) {
  915. int end;
  916. BUG_TRAP(start <= offset + len);
  917. end = start + list->len;
  918. if ((copy = end - offset) > 0) {
  919. if (copy > len)
  920. copy = len;
  921. if (skb_copy_bits(list, offset - start,
  922. to, copy))
  923. goto fault;
  924. if ((len -= copy) == 0)
  925. return 0;
  926. offset += copy;
  927. to += copy;
  928. }
  929. start = end;
  930. }
  931. }
  932. if (!len)
  933. return 0;
  934. fault:
  935. return -EFAULT;
  936. }
  937. /**
  938. * skb_store_bits - store bits from kernel buffer to skb
  939. * @skb: destination buffer
  940. * @offset: offset in destination
  941. * @from: source buffer
  942. * @len: number of bytes to copy
  943. *
  944. * Copy the specified number of bytes from the source buffer to the
  945. * destination skb. This function handles all the messy bits of
  946. * traversing fragment lists and such.
  947. */
  948. int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
  949. {
  950. int i, copy;
  951. int start = skb_headlen(skb);
  952. if (offset > (int)skb->len - len)
  953. goto fault;
  954. if ((copy = start - offset) > 0) {
  955. if (copy > len)
  956. copy = len;
  957. memcpy(skb->data + offset, from, copy);
  958. if ((len -= copy) == 0)
  959. return 0;
  960. offset += copy;
  961. from += copy;
  962. }
  963. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  964. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  965. int end;
  966. BUG_TRAP(start <= offset + len);
  967. end = start + frag->size;
  968. if ((copy = end - offset) > 0) {
  969. u8 *vaddr;
  970. if (copy > len)
  971. copy = len;
  972. vaddr = kmap_skb_frag(frag);
  973. memcpy(vaddr + frag->page_offset + offset - start,
  974. from, copy);
  975. kunmap_skb_frag(vaddr);
  976. if ((len -= copy) == 0)
  977. return 0;
  978. offset += copy;
  979. from += copy;
  980. }
  981. start = end;
  982. }
  983. if (skb_shinfo(skb)->frag_list) {
  984. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  985. for (; list; list = list->next) {
  986. int end;
  987. BUG_TRAP(start <= offset + len);
  988. end = start + list->len;
  989. if ((copy = end - offset) > 0) {
  990. if (copy > len)
  991. copy = len;
  992. if (skb_store_bits(list, offset - start,
  993. from, copy))
  994. goto fault;
  995. if ((len -= copy) == 0)
  996. return 0;
  997. offset += copy;
  998. from += copy;
  999. }
  1000. start = end;
  1001. }
  1002. }
  1003. if (!len)
  1004. return 0;
  1005. fault:
  1006. return -EFAULT;
  1007. }
  1008. EXPORT_SYMBOL(skb_store_bits);
  1009. /* Checksum skb data. */
  1010. unsigned int skb_checksum(const struct sk_buff *skb, int offset,
  1011. int len, unsigned int csum)
  1012. {
  1013. int start = skb_headlen(skb);
  1014. int i, copy = start - offset;
  1015. int pos = 0;
  1016. /* Checksum header. */
  1017. if (copy > 0) {
  1018. if (copy > len)
  1019. copy = len;
  1020. csum = csum_partial(skb->data + offset, copy, csum);
  1021. if ((len -= copy) == 0)
  1022. return csum;
  1023. offset += copy;
  1024. pos = copy;
  1025. }
  1026. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1027. int end;
  1028. BUG_TRAP(start <= offset + len);
  1029. end = start + skb_shinfo(skb)->frags[i].size;
  1030. if ((copy = end - offset) > 0) {
  1031. unsigned int csum2;
  1032. u8 *vaddr;
  1033. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1034. if (copy > len)
  1035. copy = len;
  1036. vaddr = kmap_skb_frag(frag);
  1037. csum2 = csum_partial(vaddr + frag->page_offset +
  1038. offset - start, copy, 0);
  1039. kunmap_skb_frag(vaddr);
  1040. csum = csum_block_add(csum, csum2, pos);
  1041. if (!(len -= copy))
  1042. return csum;
  1043. offset += copy;
  1044. pos += copy;
  1045. }
  1046. start = end;
  1047. }
  1048. if (skb_shinfo(skb)->frag_list) {
  1049. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1050. for (; list; list = list->next) {
  1051. int end;
  1052. BUG_TRAP(start <= offset + len);
  1053. end = start + list->len;
  1054. if ((copy = end - offset) > 0) {
  1055. unsigned int csum2;
  1056. if (copy > len)
  1057. copy = len;
  1058. csum2 = skb_checksum(list, offset - start,
  1059. copy, 0);
  1060. csum = csum_block_add(csum, csum2, pos);
  1061. if ((len -= copy) == 0)
  1062. return csum;
  1063. offset += copy;
  1064. pos += copy;
  1065. }
  1066. start = end;
  1067. }
  1068. }
  1069. BUG_ON(len);
  1070. return csum;
  1071. }
  1072. /* Both of above in one bottle. */
  1073. unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1074. u8 *to, int len, unsigned int csum)
  1075. {
  1076. int start = skb_headlen(skb);
  1077. int i, copy = start - offset;
  1078. int pos = 0;
  1079. /* Copy header. */
  1080. if (copy > 0) {
  1081. if (copy > len)
  1082. copy = len;
  1083. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1084. copy, csum);
  1085. if ((len -= copy) == 0)
  1086. return csum;
  1087. offset += copy;
  1088. to += copy;
  1089. pos = copy;
  1090. }
  1091. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1092. int end;
  1093. BUG_TRAP(start <= offset + len);
  1094. end = start + skb_shinfo(skb)->frags[i].size;
  1095. if ((copy = end - offset) > 0) {
  1096. unsigned int csum2;
  1097. u8 *vaddr;
  1098. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1099. if (copy > len)
  1100. copy = len;
  1101. vaddr = kmap_skb_frag(frag);
  1102. csum2 = csum_partial_copy_nocheck(vaddr +
  1103. frag->page_offset +
  1104. offset - start, to,
  1105. copy, 0);
  1106. kunmap_skb_frag(vaddr);
  1107. csum = csum_block_add(csum, csum2, pos);
  1108. if (!(len -= copy))
  1109. return csum;
  1110. offset += copy;
  1111. to += copy;
  1112. pos += copy;
  1113. }
  1114. start = end;
  1115. }
  1116. if (skb_shinfo(skb)->frag_list) {
  1117. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1118. for (; list; list = list->next) {
  1119. unsigned int csum2;
  1120. int end;
  1121. BUG_TRAP(start <= offset + len);
  1122. end = start + list->len;
  1123. if ((copy = end - offset) > 0) {
  1124. if (copy > len)
  1125. copy = len;
  1126. csum2 = skb_copy_and_csum_bits(list,
  1127. offset - start,
  1128. to, copy, 0);
  1129. csum = csum_block_add(csum, csum2, pos);
  1130. if ((len -= copy) == 0)
  1131. return csum;
  1132. offset += copy;
  1133. to += copy;
  1134. pos += copy;
  1135. }
  1136. start = end;
  1137. }
  1138. }
  1139. BUG_ON(len);
  1140. return csum;
  1141. }
  1142. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1143. {
  1144. unsigned int csum;
  1145. long csstart;
  1146. if (skb->ip_summed == CHECKSUM_HW)
  1147. csstart = skb->h.raw - skb->data;
  1148. else
  1149. csstart = skb_headlen(skb);
  1150. BUG_ON(csstart > skb_headlen(skb));
  1151. memcpy(to, skb->data, csstart);
  1152. csum = 0;
  1153. if (csstart != skb->len)
  1154. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1155. skb->len - csstart, 0);
  1156. if (skb->ip_summed == CHECKSUM_HW) {
  1157. long csstuff = csstart + skb->csum;
  1158. *((unsigned short *)(to + csstuff)) = csum_fold(csum);
  1159. }
  1160. }
  1161. /**
  1162. * skb_dequeue - remove from the head of the queue
  1163. * @list: list to dequeue from
  1164. *
  1165. * Remove the head of the list. The list lock is taken so the function
  1166. * may be used safely with other locking list functions. The head item is
  1167. * returned or %NULL if the list is empty.
  1168. */
  1169. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1170. {
  1171. unsigned long flags;
  1172. struct sk_buff *result;
  1173. spin_lock_irqsave(&list->lock, flags);
  1174. result = __skb_dequeue(list);
  1175. spin_unlock_irqrestore(&list->lock, flags);
  1176. return result;
  1177. }
  1178. /**
  1179. * skb_dequeue_tail - remove from the tail of the queue
  1180. * @list: list to dequeue from
  1181. *
  1182. * Remove the tail of the list. The list lock is taken so the function
  1183. * may be used safely with other locking list functions. The tail item is
  1184. * returned or %NULL if the list is empty.
  1185. */
  1186. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1187. {
  1188. unsigned long flags;
  1189. struct sk_buff *result;
  1190. spin_lock_irqsave(&list->lock, flags);
  1191. result = __skb_dequeue_tail(list);
  1192. spin_unlock_irqrestore(&list->lock, flags);
  1193. return result;
  1194. }
  1195. /**
  1196. * skb_queue_purge - empty a list
  1197. * @list: list to empty
  1198. *
  1199. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1200. * the list and one reference dropped. This function takes the list
  1201. * lock and is atomic with respect to other list locking functions.
  1202. */
  1203. void skb_queue_purge(struct sk_buff_head *list)
  1204. {
  1205. struct sk_buff *skb;
  1206. while ((skb = skb_dequeue(list)) != NULL)
  1207. kfree_skb(skb);
  1208. }
  1209. /**
  1210. * skb_queue_head - queue a buffer at the list head
  1211. * @list: list to use
  1212. * @newsk: buffer to queue
  1213. *
  1214. * Queue a buffer at the start of the list. This function takes the
  1215. * list lock and can be used safely with other locking &sk_buff functions
  1216. * safely.
  1217. *
  1218. * A buffer cannot be placed on two lists at the same time.
  1219. */
  1220. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1221. {
  1222. unsigned long flags;
  1223. spin_lock_irqsave(&list->lock, flags);
  1224. __skb_queue_head(list, newsk);
  1225. spin_unlock_irqrestore(&list->lock, flags);
  1226. }
  1227. /**
  1228. * skb_queue_tail - queue a buffer at the list tail
  1229. * @list: list to use
  1230. * @newsk: buffer to queue
  1231. *
  1232. * Queue a buffer at the tail of the list. This function takes the
  1233. * list lock and can be used safely with other locking &sk_buff functions
  1234. * safely.
  1235. *
  1236. * A buffer cannot be placed on two lists at the same time.
  1237. */
  1238. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1239. {
  1240. unsigned long flags;
  1241. spin_lock_irqsave(&list->lock, flags);
  1242. __skb_queue_tail(list, newsk);
  1243. spin_unlock_irqrestore(&list->lock, flags);
  1244. }
  1245. /**
  1246. * skb_unlink - remove a buffer from a list
  1247. * @skb: buffer to remove
  1248. * @list: list to use
  1249. *
  1250. * Remove a packet from a list. The list locks are taken and this
  1251. * function is atomic with respect to other list locked calls
  1252. *
  1253. * You must know what list the SKB is on.
  1254. */
  1255. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1256. {
  1257. unsigned long flags;
  1258. spin_lock_irqsave(&list->lock, flags);
  1259. __skb_unlink(skb, list);
  1260. spin_unlock_irqrestore(&list->lock, flags);
  1261. }
  1262. /**
  1263. * skb_append - append a buffer
  1264. * @old: buffer to insert after
  1265. * @newsk: buffer to insert
  1266. * @list: list to use
  1267. *
  1268. * Place a packet after a given packet in a list. The list locks are taken
  1269. * and this function is atomic with respect to other list locked calls.
  1270. * A buffer cannot be placed on two lists at the same time.
  1271. */
  1272. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1273. {
  1274. unsigned long flags;
  1275. spin_lock_irqsave(&list->lock, flags);
  1276. __skb_append(old, newsk, list);
  1277. spin_unlock_irqrestore(&list->lock, flags);
  1278. }
  1279. /**
  1280. * skb_insert - insert a buffer
  1281. * @old: buffer to insert before
  1282. * @newsk: buffer to insert
  1283. * @list: list to use
  1284. *
  1285. * Place a packet before a given packet in a list. The list locks are
  1286. * taken and this function is atomic with respect to other list locked
  1287. * calls.
  1288. *
  1289. * A buffer cannot be placed on two lists at the same time.
  1290. */
  1291. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1292. {
  1293. unsigned long flags;
  1294. spin_lock_irqsave(&list->lock, flags);
  1295. __skb_insert(newsk, old->prev, old, list);
  1296. spin_unlock_irqrestore(&list->lock, flags);
  1297. }
  1298. #if 0
  1299. /*
  1300. * Tune the memory allocator for a new MTU size.
  1301. */
  1302. void skb_add_mtu(int mtu)
  1303. {
  1304. /* Must match allocation in alloc_skb */
  1305. mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
  1306. kmem_add_cache_size(mtu);
  1307. }
  1308. #endif
  1309. static inline void skb_split_inside_header(struct sk_buff *skb,
  1310. struct sk_buff* skb1,
  1311. const u32 len, const int pos)
  1312. {
  1313. int i;
  1314. memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
  1315. /* And move data appendix as is. */
  1316. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1317. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1318. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1319. skb_shinfo(skb)->nr_frags = 0;
  1320. skb1->data_len = skb->data_len;
  1321. skb1->len += skb1->data_len;
  1322. skb->data_len = 0;
  1323. skb->len = len;
  1324. skb->tail = skb->data + len;
  1325. }
  1326. static inline void skb_split_no_header(struct sk_buff *skb,
  1327. struct sk_buff* skb1,
  1328. const u32 len, int pos)
  1329. {
  1330. int i, k = 0;
  1331. const int nfrags = skb_shinfo(skb)->nr_frags;
  1332. skb_shinfo(skb)->nr_frags = 0;
  1333. skb1->len = skb1->data_len = skb->len - len;
  1334. skb->len = len;
  1335. skb->data_len = len - pos;
  1336. for (i = 0; i < nfrags; i++) {
  1337. int size = skb_shinfo(skb)->frags[i].size;
  1338. if (pos + size > len) {
  1339. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1340. if (pos < len) {
  1341. /* Split frag.
  1342. * We have two variants in this case:
  1343. * 1. Move all the frag to the second
  1344. * part, if it is possible. F.e.
  1345. * this approach is mandatory for TUX,
  1346. * where splitting is expensive.
  1347. * 2. Split is accurately. We make this.
  1348. */
  1349. get_page(skb_shinfo(skb)->frags[i].page);
  1350. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1351. skb_shinfo(skb1)->frags[0].size -= len - pos;
  1352. skb_shinfo(skb)->frags[i].size = len - pos;
  1353. skb_shinfo(skb)->nr_frags++;
  1354. }
  1355. k++;
  1356. } else
  1357. skb_shinfo(skb)->nr_frags++;
  1358. pos += size;
  1359. }
  1360. skb_shinfo(skb1)->nr_frags = k;
  1361. }
  1362. /**
  1363. * skb_split - Split fragmented skb to two parts at length len.
  1364. * @skb: the buffer to split
  1365. * @skb1: the buffer to receive the second part
  1366. * @len: new length for skb
  1367. */
  1368. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1369. {
  1370. int pos = skb_headlen(skb);
  1371. if (len < pos) /* Split line is inside header. */
  1372. skb_split_inside_header(skb, skb1, len, pos);
  1373. else /* Second chunk has no header, nothing to copy. */
  1374. skb_split_no_header(skb, skb1, len, pos);
  1375. }
  1376. /**
  1377. * skb_prepare_seq_read - Prepare a sequential read of skb data
  1378. * @skb: the buffer to read
  1379. * @from: lower offset of data to be read
  1380. * @to: upper offset of data to be read
  1381. * @st: state variable
  1382. *
  1383. * Initializes the specified state variable. Must be called before
  1384. * invoking skb_seq_read() for the first time.
  1385. */
  1386. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  1387. unsigned int to, struct skb_seq_state *st)
  1388. {
  1389. st->lower_offset = from;
  1390. st->upper_offset = to;
  1391. st->root_skb = st->cur_skb = skb;
  1392. st->frag_idx = st->stepped_offset = 0;
  1393. st->frag_data = NULL;
  1394. }
  1395. /**
  1396. * skb_seq_read - Sequentially read skb data
  1397. * @consumed: number of bytes consumed by the caller so far
  1398. * @data: destination pointer for data to be returned
  1399. * @st: state variable
  1400. *
  1401. * Reads a block of skb data at &consumed relative to the
  1402. * lower offset specified to skb_prepare_seq_read(). Assigns
  1403. * the head of the data block to &data and returns the length
  1404. * of the block or 0 if the end of the skb data or the upper
  1405. * offset has been reached.
  1406. *
  1407. * The caller is not required to consume all of the data
  1408. * returned, i.e. &consumed is typically set to the number
  1409. * of bytes already consumed and the next call to
  1410. * skb_seq_read() will return the remaining part of the block.
  1411. *
  1412. * Note: The size of each block of data returned can be arbitary,
  1413. * this limitation is the cost for zerocopy seqeuental
  1414. * reads of potentially non linear data.
  1415. *
  1416. * Note: Fragment lists within fragments are not implemented
  1417. * at the moment, state->root_skb could be replaced with
  1418. * a stack for this purpose.
  1419. */
  1420. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  1421. struct skb_seq_state *st)
  1422. {
  1423. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  1424. skb_frag_t *frag;
  1425. if (unlikely(abs_offset >= st->upper_offset))
  1426. return 0;
  1427. next_skb:
  1428. block_limit = skb_headlen(st->cur_skb);
  1429. if (abs_offset < block_limit) {
  1430. *data = st->cur_skb->data + abs_offset;
  1431. return block_limit - abs_offset;
  1432. }
  1433. if (st->frag_idx == 0 && !st->frag_data)
  1434. st->stepped_offset += skb_headlen(st->cur_skb);
  1435. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  1436. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  1437. block_limit = frag->size + st->stepped_offset;
  1438. if (abs_offset < block_limit) {
  1439. if (!st->frag_data)
  1440. st->frag_data = kmap_skb_frag(frag);
  1441. *data = (u8 *) st->frag_data + frag->page_offset +
  1442. (abs_offset - st->stepped_offset);
  1443. return block_limit - abs_offset;
  1444. }
  1445. if (st->frag_data) {
  1446. kunmap_skb_frag(st->frag_data);
  1447. st->frag_data = NULL;
  1448. }
  1449. st->frag_idx++;
  1450. st->stepped_offset += frag->size;
  1451. }
  1452. if (st->cur_skb->next) {
  1453. st->cur_skb = st->cur_skb->next;
  1454. st->frag_idx = 0;
  1455. goto next_skb;
  1456. } else if (st->root_skb == st->cur_skb &&
  1457. skb_shinfo(st->root_skb)->frag_list) {
  1458. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  1459. goto next_skb;
  1460. }
  1461. return 0;
  1462. }
  1463. /**
  1464. * skb_abort_seq_read - Abort a sequential read of skb data
  1465. * @st: state variable
  1466. *
  1467. * Must be called if skb_seq_read() was not called until it
  1468. * returned 0.
  1469. */
  1470. void skb_abort_seq_read(struct skb_seq_state *st)
  1471. {
  1472. if (st->frag_data)
  1473. kunmap_skb_frag(st->frag_data);
  1474. }
  1475. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  1476. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  1477. struct ts_config *conf,
  1478. struct ts_state *state)
  1479. {
  1480. return skb_seq_read(offset, text, TS_SKB_CB(state));
  1481. }
  1482. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  1483. {
  1484. skb_abort_seq_read(TS_SKB_CB(state));
  1485. }
  1486. /**
  1487. * skb_find_text - Find a text pattern in skb data
  1488. * @skb: the buffer to look in
  1489. * @from: search offset
  1490. * @to: search limit
  1491. * @config: textsearch configuration
  1492. * @state: uninitialized textsearch state variable
  1493. *
  1494. * Finds a pattern in the skb data according to the specified
  1495. * textsearch configuration. Use textsearch_next() to retrieve
  1496. * subsequent occurrences of the pattern. Returns the offset
  1497. * to the first occurrence or UINT_MAX if no match was found.
  1498. */
  1499. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  1500. unsigned int to, struct ts_config *config,
  1501. struct ts_state *state)
  1502. {
  1503. config->get_next_block = skb_ts_get_next_block;
  1504. config->finish = skb_ts_finish;
  1505. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  1506. return textsearch_find(config, state);
  1507. }
  1508. /**
  1509. * skb_append_datato_frags: - append the user data to a skb
  1510. * @sk: sock structure
  1511. * @skb: skb structure to be appened with user data.
  1512. * @getfrag: call back function to be used for getting the user data
  1513. * @from: pointer to user message iov
  1514. * @length: length of the iov message
  1515. *
  1516. * Description: This procedure append the user data in the fragment part
  1517. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  1518. */
  1519. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  1520. int (*getfrag)(void *from, char *to, int offset,
  1521. int len, int odd, struct sk_buff *skb),
  1522. void *from, int length)
  1523. {
  1524. int frg_cnt = 0;
  1525. skb_frag_t *frag = NULL;
  1526. struct page *page = NULL;
  1527. int copy, left;
  1528. int offset = 0;
  1529. int ret;
  1530. do {
  1531. /* Return error if we don't have space for new frag */
  1532. frg_cnt = skb_shinfo(skb)->nr_frags;
  1533. if (frg_cnt >= MAX_SKB_FRAGS)
  1534. return -EFAULT;
  1535. /* allocate a new page for next frag */
  1536. page = alloc_pages(sk->sk_allocation, 0);
  1537. /* If alloc_page fails just return failure and caller will
  1538. * free previous allocated pages by doing kfree_skb()
  1539. */
  1540. if (page == NULL)
  1541. return -ENOMEM;
  1542. /* initialize the next frag */
  1543. sk->sk_sndmsg_page = page;
  1544. sk->sk_sndmsg_off = 0;
  1545. skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
  1546. skb->truesize += PAGE_SIZE;
  1547. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  1548. /* get the new initialized frag */
  1549. frg_cnt = skb_shinfo(skb)->nr_frags;
  1550. frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
  1551. /* copy the user data to page */
  1552. left = PAGE_SIZE - frag->page_offset;
  1553. copy = (length > left)? left : length;
  1554. ret = getfrag(from, (page_address(frag->page) +
  1555. frag->page_offset + frag->size),
  1556. offset, copy, 0, skb);
  1557. if (ret < 0)
  1558. return -EFAULT;
  1559. /* copy was successful so update the size parameters */
  1560. sk->sk_sndmsg_off += copy;
  1561. frag->size += copy;
  1562. skb->len += copy;
  1563. skb->data_len += copy;
  1564. offset += copy;
  1565. length -= copy;
  1566. } while (length > 0);
  1567. return 0;
  1568. }
  1569. /**
  1570. * skb_pull_rcsum - pull skb and update receive checksum
  1571. * @skb: buffer to update
  1572. * @start: start of data before pull
  1573. * @len: length of data pulled
  1574. *
  1575. * This function performs an skb_pull on the packet and updates
  1576. * update the CHECKSUM_HW checksum. It should be used on receive
  1577. * path processing instead of skb_pull unless you know that the
  1578. * checksum difference is zero (e.g., a valid IP header) or you
  1579. * are setting ip_summed to CHECKSUM_NONE.
  1580. */
  1581. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  1582. {
  1583. BUG_ON(len > skb->len);
  1584. skb->len -= len;
  1585. BUG_ON(skb->len < skb->data_len);
  1586. skb_postpull_rcsum(skb, skb->data, len);
  1587. return skb->data += len;
  1588. }
  1589. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  1590. void __init skb_init(void)
  1591. {
  1592. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  1593. sizeof(struct sk_buff),
  1594. 0,
  1595. SLAB_HWCACHE_ALIGN,
  1596. NULL, NULL);
  1597. if (!skbuff_head_cache)
  1598. panic("cannot create skbuff cache");
  1599. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  1600. (2*sizeof(struct sk_buff)) +
  1601. sizeof(atomic_t),
  1602. 0,
  1603. SLAB_HWCACHE_ALIGN,
  1604. NULL, NULL);
  1605. if (!skbuff_fclone_cache)
  1606. panic("cannot create skbuff cache");
  1607. }
  1608. EXPORT_SYMBOL(___pskb_trim);
  1609. EXPORT_SYMBOL(__kfree_skb);
  1610. EXPORT_SYMBOL(kfree_skb);
  1611. EXPORT_SYMBOL(__pskb_pull_tail);
  1612. EXPORT_SYMBOL(__alloc_skb);
  1613. EXPORT_SYMBOL(pskb_copy);
  1614. EXPORT_SYMBOL(pskb_expand_head);
  1615. EXPORT_SYMBOL(skb_checksum);
  1616. EXPORT_SYMBOL(skb_clone);
  1617. EXPORT_SYMBOL(skb_clone_fraglist);
  1618. EXPORT_SYMBOL(skb_copy);
  1619. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1620. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1621. EXPORT_SYMBOL(skb_copy_bits);
  1622. EXPORT_SYMBOL(skb_copy_expand);
  1623. EXPORT_SYMBOL(skb_over_panic);
  1624. EXPORT_SYMBOL(skb_pad);
  1625. EXPORT_SYMBOL(skb_realloc_headroom);
  1626. EXPORT_SYMBOL(skb_under_panic);
  1627. EXPORT_SYMBOL(skb_dequeue);
  1628. EXPORT_SYMBOL(skb_dequeue_tail);
  1629. EXPORT_SYMBOL(skb_insert);
  1630. EXPORT_SYMBOL(skb_queue_purge);
  1631. EXPORT_SYMBOL(skb_queue_head);
  1632. EXPORT_SYMBOL(skb_queue_tail);
  1633. EXPORT_SYMBOL(skb_unlink);
  1634. EXPORT_SYMBOL(skb_append);
  1635. EXPORT_SYMBOL(skb_split);
  1636. EXPORT_SYMBOL(skb_prepare_seq_read);
  1637. EXPORT_SYMBOL(skb_seq_read);
  1638. EXPORT_SYMBOL(skb_abort_seq_read);
  1639. EXPORT_SYMBOL(skb_find_text);
  1640. EXPORT_SYMBOL(skb_append_datato_frags);