skbuff.c 46 KB

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