skbuff.c 45 KB

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