skbuff.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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;
  67. /*
  68. * Keep out-of-line to prevent kernel bloat.
  69. * __builtin_return_address is not used because it is not always
  70. * reliable.
  71. */
  72. /**
  73. * skb_over_panic - private function
  74. * @skb: buffer
  75. * @sz: size
  76. * @here: address
  77. *
  78. * Out of line support code for skb_put(). Not user callable.
  79. */
  80. void skb_over_panic(struct sk_buff *skb, int sz, void *here)
  81. {
  82. printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
  83. "data:%p tail:%p end:%p dev:%s\n",
  84. here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
  85. skb->dev ? skb->dev->name : "<NULL>");
  86. BUG();
  87. }
  88. /**
  89. * skb_under_panic - private function
  90. * @skb: buffer
  91. * @sz: size
  92. * @here: address
  93. *
  94. * Out of line support code for skb_push(). Not user callable.
  95. */
  96. void skb_under_panic(struct sk_buff *skb, int sz, void *here)
  97. {
  98. printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
  99. "data:%p tail:%p end:%p dev:%s\n",
  100. here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
  101. skb->dev ? skb->dev->name : "<NULL>");
  102. BUG();
  103. }
  104. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  105. * 'private' fields and also do memory statistics to find all the
  106. * [BEEP] leaks.
  107. *
  108. */
  109. /**
  110. * alloc_skb - allocate a network buffer
  111. * @size: size to allocate
  112. * @gfp_mask: allocation mask
  113. *
  114. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  115. * tail room of size bytes. The object has a reference count of one.
  116. * The return is the buffer. On a failure the return is %NULL.
  117. *
  118. * Buffers may only be allocated from interrupts using a @gfp_mask of
  119. * %GFP_ATOMIC.
  120. */
  121. struct sk_buff *alloc_skb(unsigned int size, int gfp_mask)
  122. {
  123. struct sk_buff *skb;
  124. u8 *data;
  125. /* Get the HEAD */
  126. skb = kmem_cache_alloc(skbuff_head_cache,
  127. gfp_mask & ~__GFP_DMA);
  128. if (!skb)
  129. goto out;
  130. /* Get the DATA. Size must match skb_add_mtu(). */
  131. size = SKB_DATA_ALIGN(size);
  132. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  133. if (!data)
  134. goto nodata;
  135. memset(skb, 0, offsetof(struct sk_buff, truesize));
  136. skb->truesize = size + sizeof(struct sk_buff);
  137. atomic_set(&skb->users, 1);
  138. skb->head = data;
  139. skb->data = data;
  140. skb->tail = data;
  141. skb->end = data + size;
  142. atomic_set(&(skb_shinfo(skb)->dataref), 1);
  143. skb_shinfo(skb)->nr_frags = 0;
  144. skb_shinfo(skb)->tso_size = 0;
  145. skb_shinfo(skb)->tso_segs = 0;
  146. skb_shinfo(skb)->frag_list = NULL;
  147. out:
  148. return skb;
  149. nodata:
  150. kmem_cache_free(skbuff_head_cache, skb);
  151. skb = NULL;
  152. goto out;
  153. }
  154. /**
  155. * alloc_skb_from_cache - allocate a network buffer
  156. * @cp: kmem_cache from which to allocate the data area
  157. * (object size must be big enough for @size bytes + skb overheads)
  158. * @size: size to allocate
  159. * @gfp_mask: allocation mask
  160. *
  161. * Allocate a new &sk_buff. The returned buffer has no headroom and
  162. * tail room of size bytes. The object has a reference count of one.
  163. * The return is the buffer. On a failure the return is %NULL.
  164. *
  165. * Buffers may only be allocated from interrupts using a @gfp_mask of
  166. * %GFP_ATOMIC.
  167. */
  168. struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
  169. unsigned int size, int gfp_mask)
  170. {
  171. struct sk_buff *skb;
  172. u8 *data;
  173. /* Get the HEAD */
  174. skb = kmem_cache_alloc(skbuff_head_cache,
  175. gfp_mask & ~__GFP_DMA);
  176. if (!skb)
  177. goto out;
  178. /* Get the DATA. */
  179. size = SKB_DATA_ALIGN(size);
  180. data = kmem_cache_alloc(cp, gfp_mask);
  181. if (!data)
  182. goto nodata;
  183. memset(skb, 0, offsetof(struct sk_buff, truesize));
  184. skb->truesize = size + sizeof(struct sk_buff);
  185. atomic_set(&skb->users, 1);
  186. skb->head = data;
  187. skb->data = data;
  188. skb->tail = data;
  189. skb->end = data + size;
  190. atomic_set(&(skb_shinfo(skb)->dataref), 1);
  191. skb_shinfo(skb)->nr_frags = 0;
  192. skb_shinfo(skb)->tso_size = 0;
  193. skb_shinfo(skb)->tso_segs = 0;
  194. skb_shinfo(skb)->frag_list = NULL;
  195. out:
  196. return skb;
  197. nodata:
  198. kmem_cache_free(skbuff_head_cache, skb);
  199. skb = NULL;
  200. goto out;
  201. }
  202. static void skb_drop_fraglist(struct sk_buff *skb)
  203. {
  204. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  205. skb_shinfo(skb)->frag_list = NULL;
  206. do {
  207. struct sk_buff *this = list;
  208. list = list->next;
  209. kfree_skb(this);
  210. } while (list);
  211. }
  212. static void skb_clone_fraglist(struct sk_buff *skb)
  213. {
  214. struct sk_buff *list;
  215. for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
  216. skb_get(list);
  217. }
  218. void skb_release_data(struct sk_buff *skb)
  219. {
  220. if (!skb->cloned ||
  221. !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  222. &skb_shinfo(skb)->dataref)) {
  223. if (skb_shinfo(skb)->nr_frags) {
  224. int i;
  225. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  226. put_page(skb_shinfo(skb)->frags[i].page);
  227. }
  228. if (skb_shinfo(skb)->frag_list)
  229. skb_drop_fraglist(skb);
  230. kfree(skb->head);
  231. }
  232. }
  233. /*
  234. * Free an skbuff by memory without cleaning the state.
  235. */
  236. void kfree_skbmem(struct sk_buff *skb)
  237. {
  238. skb_release_data(skb);
  239. kmem_cache_free(skbuff_head_cache, skb);
  240. }
  241. /**
  242. * __kfree_skb - private function
  243. * @skb: buffer
  244. *
  245. * Free an sk_buff. Release anything attached to the buffer.
  246. * Clean the state. This is an internal helper function. Users should
  247. * always call kfree_skb
  248. */
  249. void __kfree_skb(struct sk_buff *skb)
  250. {
  251. BUG_ON(skb->list != NULL);
  252. dst_release(skb->dst);
  253. #ifdef CONFIG_XFRM
  254. secpath_put(skb->sp);
  255. #endif
  256. if (skb->destructor) {
  257. WARN_ON(in_irq());
  258. skb->destructor(skb);
  259. }
  260. #ifdef CONFIG_NETFILTER
  261. nf_conntrack_put(skb->nfct);
  262. #ifdef CONFIG_BRIDGE_NETFILTER
  263. nf_bridge_put(skb->nf_bridge);
  264. #endif
  265. #endif
  266. /* XXX: IS this still necessary? - JHS */
  267. #ifdef CONFIG_NET_SCHED
  268. skb->tc_index = 0;
  269. #ifdef CONFIG_NET_CLS_ACT
  270. skb->tc_verd = 0;
  271. skb->tc_classid = 0;
  272. #endif
  273. #endif
  274. kfree_skbmem(skb);
  275. }
  276. /**
  277. * skb_clone - duplicate an sk_buff
  278. * @skb: buffer to clone
  279. * @gfp_mask: allocation priority
  280. *
  281. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  282. * copies share the same packet data but not structure. The new
  283. * buffer has a reference count of 1. If the allocation fails the
  284. * function returns %NULL otherwise the new buffer is returned.
  285. *
  286. * If this function is called from an interrupt gfp_mask() must be
  287. * %GFP_ATOMIC.
  288. */
  289. struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)
  290. {
  291. struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  292. if (!n)
  293. return NULL;
  294. #define C(x) n->x = skb->x
  295. n->next = n->prev = NULL;
  296. n->list = NULL;
  297. n->sk = NULL;
  298. C(stamp);
  299. C(dev);
  300. C(real_dev);
  301. C(h);
  302. C(nh);
  303. C(mac);
  304. C(dst);
  305. dst_clone(skb->dst);
  306. C(sp);
  307. #ifdef CONFIG_INET
  308. secpath_get(skb->sp);
  309. #endif
  310. memcpy(n->cb, skb->cb, sizeof(skb->cb));
  311. C(len);
  312. C(data_len);
  313. C(csum);
  314. C(local_df);
  315. n->cloned = 1;
  316. n->nohdr = 0;
  317. C(pkt_type);
  318. C(ip_summed);
  319. C(priority);
  320. C(protocol);
  321. C(security);
  322. n->destructor = NULL;
  323. #ifdef CONFIG_NETFILTER
  324. C(nfmark);
  325. C(nfcache);
  326. C(nfct);
  327. nf_conntrack_get(skb->nfct);
  328. C(nfctinfo);
  329. #ifdef CONFIG_NETFILTER_DEBUG
  330. C(nf_debug);
  331. #endif
  332. #ifdef CONFIG_BRIDGE_NETFILTER
  333. C(nf_bridge);
  334. nf_bridge_get(skb->nf_bridge);
  335. #endif
  336. #endif /*CONFIG_NETFILTER*/
  337. #if defined(CONFIG_HIPPI)
  338. C(private);
  339. #endif
  340. #ifdef CONFIG_NET_SCHED
  341. C(tc_index);
  342. #ifdef CONFIG_NET_CLS_ACT
  343. n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
  344. n->tc_verd = CLR_TC_OK2MUNGE(skb->tc_verd);
  345. n->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
  346. C(input_dev);
  347. C(tc_classid);
  348. #endif
  349. #endif
  350. C(truesize);
  351. atomic_set(&n->users, 1);
  352. C(head);
  353. C(data);
  354. C(tail);
  355. C(end);
  356. atomic_inc(&(skb_shinfo(skb)->dataref));
  357. skb->cloned = 1;
  358. return n;
  359. }
  360. static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  361. {
  362. /*
  363. * Shift between the two data areas in bytes
  364. */
  365. unsigned long offset = new->data - old->data;
  366. new->list = NULL;
  367. new->sk = NULL;
  368. new->dev = old->dev;
  369. new->real_dev = old->real_dev;
  370. new->priority = old->priority;
  371. new->protocol = old->protocol;
  372. new->dst = dst_clone(old->dst);
  373. #ifdef CONFIG_INET
  374. new->sp = secpath_get(old->sp);
  375. #endif
  376. new->h.raw = old->h.raw + offset;
  377. new->nh.raw = old->nh.raw + offset;
  378. new->mac.raw = old->mac.raw + offset;
  379. memcpy(new->cb, old->cb, sizeof(old->cb));
  380. new->local_df = old->local_df;
  381. new->pkt_type = old->pkt_type;
  382. new->stamp = old->stamp;
  383. new->destructor = NULL;
  384. new->security = old->security;
  385. #ifdef CONFIG_NETFILTER
  386. new->nfmark = old->nfmark;
  387. new->nfcache = old->nfcache;
  388. new->nfct = old->nfct;
  389. nf_conntrack_get(old->nfct);
  390. new->nfctinfo = old->nfctinfo;
  391. #ifdef CONFIG_NETFILTER_DEBUG
  392. new->nf_debug = old->nf_debug;
  393. #endif
  394. #ifdef CONFIG_BRIDGE_NETFILTER
  395. new->nf_bridge = old->nf_bridge;
  396. nf_bridge_get(old->nf_bridge);
  397. #endif
  398. #endif
  399. #ifdef CONFIG_NET_SCHED
  400. #ifdef CONFIG_NET_CLS_ACT
  401. new->tc_verd = old->tc_verd;
  402. #endif
  403. new->tc_index = old->tc_index;
  404. #endif
  405. atomic_set(&new->users, 1);
  406. skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
  407. skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
  408. }
  409. /**
  410. * skb_copy - create private copy of an sk_buff
  411. * @skb: buffer to copy
  412. * @gfp_mask: allocation priority
  413. *
  414. * Make a copy of both an &sk_buff and its data. This is used when the
  415. * caller wishes to modify the data and needs a private copy of the
  416. * data to alter. Returns %NULL on failure or the pointer to the buffer
  417. * on success. The returned buffer has a reference count of 1.
  418. *
  419. * As by-product this function converts non-linear &sk_buff to linear
  420. * one, so that &sk_buff becomes completely private and caller is allowed
  421. * to modify all the data of returned buffer. This means that this
  422. * function is not recommended for use in circumstances when only
  423. * header is going to be modified. Use pskb_copy() instead.
  424. */
  425. struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)
  426. {
  427. int headerlen = skb->data - skb->head;
  428. /*
  429. * Allocate the copy buffer
  430. */
  431. struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
  432. gfp_mask);
  433. if (!n)
  434. return NULL;
  435. /* Set the data pointer */
  436. skb_reserve(n, headerlen);
  437. /* Set the tail pointer and length */
  438. skb_put(n, skb->len);
  439. n->csum = skb->csum;
  440. n->ip_summed = skb->ip_summed;
  441. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  442. BUG();
  443. copy_skb_header(n, skb);
  444. return n;
  445. }
  446. /**
  447. * pskb_copy - create copy of an sk_buff with private head.
  448. * @skb: buffer to copy
  449. * @gfp_mask: allocation priority
  450. *
  451. * Make a copy of both an &sk_buff and part of its data, located
  452. * in header. Fragmented data remain shared. This is used when
  453. * the caller wishes to modify only header of &sk_buff and needs
  454. * private copy of the header to alter. Returns %NULL on failure
  455. * or the pointer to the buffer on success.
  456. * The returned buffer has a reference count of 1.
  457. */
  458. struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask)
  459. {
  460. /*
  461. * Allocate the copy buffer
  462. */
  463. struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
  464. if (!n)
  465. goto out;
  466. /* Set the data pointer */
  467. skb_reserve(n, skb->data - skb->head);
  468. /* Set the tail pointer and length */
  469. skb_put(n, skb_headlen(skb));
  470. /* Copy the bytes */
  471. memcpy(n->data, skb->data, n->len);
  472. n->csum = skb->csum;
  473. n->ip_summed = skb->ip_summed;
  474. n->data_len = skb->data_len;
  475. n->len = skb->len;
  476. if (skb_shinfo(skb)->nr_frags) {
  477. int i;
  478. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  479. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  480. get_page(skb_shinfo(n)->frags[i].page);
  481. }
  482. skb_shinfo(n)->nr_frags = i;
  483. }
  484. if (skb_shinfo(skb)->frag_list) {
  485. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  486. skb_clone_fraglist(n);
  487. }
  488. copy_skb_header(n, skb);
  489. out:
  490. return n;
  491. }
  492. /**
  493. * pskb_expand_head - reallocate header of &sk_buff
  494. * @skb: buffer to reallocate
  495. * @nhead: room to add at head
  496. * @ntail: room to add at tail
  497. * @gfp_mask: allocation priority
  498. *
  499. * Expands (or creates identical copy, if &nhead and &ntail are zero)
  500. * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
  501. * reference count of 1. Returns zero in the case of success or error,
  502. * if expansion failed. In the last case, &sk_buff is not changed.
  503. *
  504. * All the pointers pointing into skb header may change and must be
  505. * reloaded after call to this function.
  506. */
  507. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask)
  508. {
  509. int i;
  510. u8 *data;
  511. int size = nhead + (skb->end - skb->head) + ntail;
  512. long off;
  513. if (skb_shared(skb))
  514. BUG();
  515. size = SKB_DATA_ALIGN(size);
  516. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  517. if (!data)
  518. goto nodata;
  519. /* Copy only real data... and, alas, header. This should be
  520. * optimized for the cases when header is void. */
  521. memcpy(data + nhead, skb->head, skb->tail - skb->head);
  522. memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
  523. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  524. get_page(skb_shinfo(skb)->frags[i].page);
  525. if (skb_shinfo(skb)->frag_list)
  526. skb_clone_fraglist(skb);
  527. skb_release_data(skb);
  528. off = (data + nhead) - skb->head;
  529. skb->head = data;
  530. skb->end = data + size;
  531. skb->data += off;
  532. skb->tail += off;
  533. skb->mac.raw += off;
  534. skb->h.raw += off;
  535. skb->nh.raw += off;
  536. skb->cloned = 0;
  537. skb->nohdr = 0;
  538. atomic_set(&skb_shinfo(skb)->dataref, 1);
  539. return 0;
  540. nodata:
  541. return -ENOMEM;
  542. }
  543. /* Make private copy of skb with writable head and some headroom */
  544. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  545. {
  546. struct sk_buff *skb2;
  547. int delta = headroom - skb_headroom(skb);
  548. if (delta <= 0)
  549. skb2 = pskb_copy(skb, GFP_ATOMIC);
  550. else {
  551. skb2 = skb_clone(skb, GFP_ATOMIC);
  552. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  553. GFP_ATOMIC)) {
  554. kfree_skb(skb2);
  555. skb2 = NULL;
  556. }
  557. }
  558. return skb2;
  559. }
  560. /**
  561. * skb_copy_expand - copy and expand sk_buff
  562. * @skb: buffer to copy
  563. * @newheadroom: new free bytes at head
  564. * @newtailroom: new free bytes at tail
  565. * @gfp_mask: allocation priority
  566. *
  567. * Make a copy of both an &sk_buff and its data and while doing so
  568. * allocate additional space.
  569. *
  570. * This is used when the caller wishes to modify the data and needs a
  571. * private copy of the data to alter as well as more space for new fields.
  572. * Returns %NULL on failure or the pointer to the buffer
  573. * on success. The returned buffer has a reference count of 1.
  574. *
  575. * You must pass %GFP_ATOMIC as the allocation priority if this function
  576. * is called from an interrupt.
  577. *
  578. * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
  579. * only by netfilter in the cases when checksum is recalculated? --ANK
  580. */
  581. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  582. int newheadroom, int newtailroom, int gfp_mask)
  583. {
  584. /*
  585. * Allocate the copy buffer
  586. */
  587. struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
  588. gfp_mask);
  589. int head_copy_len, head_copy_off;
  590. if (!n)
  591. return NULL;
  592. skb_reserve(n, newheadroom);
  593. /* Set the tail pointer and length */
  594. skb_put(n, skb->len);
  595. head_copy_len = skb_headroom(skb);
  596. head_copy_off = 0;
  597. if (newheadroom <= head_copy_len)
  598. head_copy_len = newheadroom;
  599. else
  600. head_copy_off = newheadroom - head_copy_len;
  601. /* Copy the linear header and data. */
  602. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  603. skb->len + head_copy_len))
  604. BUG();
  605. copy_skb_header(n, skb);
  606. return n;
  607. }
  608. /**
  609. * skb_pad - zero pad the tail of an skb
  610. * @skb: buffer to pad
  611. * @pad: space to pad
  612. *
  613. * Ensure that a buffer is followed by a padding area that is zero
  614. * filled. Used by network drivers which may DMA or transfer data
  615. * beyond the buffer end onto the wire.
  616. *
  617. * May return NULL in out of memory cases.
  618. */
  619. struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
  620. {
  621. struct sk_buff *nskb;
  622. /* If the skbuff is non linear tailroom is always zero.. */
  623. if (skb_tailroom(skb) >= pad) {
  624. memset(skb->data+skb->len, 0, pad);
  625. return skb;
  626. }
  627. nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
  628. kfree_skb(skb);
  629. if (nskb)
  630. memset(nskb->data+nskb->len, 0, pad);
  631. return nskb;
  632. }
  633. /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
  634. * If realloc==0 and trimming is impossible without change of data,
  635. * it is BUG().
  636. */
  637. int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
  638. {
  639. int offset = skb_headlen(skb);
  640. int nfrags = skb_shinfo(skb)->nr_frags;
  641. int i;
  642. for (i = 0; i < nfrags; i++) {
  643. int end = offset + skb_shinfo(skb)->frags[i].size;
  644. if (end > len) {
  645. if (skb_cloned(skb)) {
  646. if (!realloc)
  647. BUG();
  648. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  649. return -ENOMEM;
  650. }
  651. if (len <= offset) {
  652. put_page(skb_shinfo(skb)->frags[i].page);
  653. skb_shinfo(skb)->nr_frags--;
  654. } else {
  655. skb_shinfo(skb)->frags[i].size = len - offset;
  656. }
  657. }
  658. offset = end;
  659. }
  660. if (offset < len) {
  661. skb->data_len -= skb->len - len;
  662. skb->len = len;
  663. } else {
  664. if (len <= skb_headlen(skb)) {
  665. skb->len = len;
  666. skb->data_len = 0;
  667. skb->tail = skb->data + len;
  668. if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
  669. skb_drop_fraglist(skb);
  670. } else {
  671. skb->data_len -= skb->len - len;
  672. skb->len = len;
  673. }
  674. }
  675. return 0;
  676. }
  677. /**
  678. * __pskb_pull_tail - advance tail of skb header
  679. * @skb: buffer to reallocate
  680. * @delta: number of bytes to advance tail
  681. *
  682. * The function makes a sense only on a fragmented &sk_buff,
  683. * it expands header moving its tail forward and copying necessary
  684. * data from fragmented part.
  685. *
  686. * &sk_buff MUST have reference count of 1.
  687. *
  688. * Returns %NULL (and &sk_buff does not change) if pull failed
  689. * or value of new tail of skb in the case of success.
  690. *
  691. * All the pointers pointing into skb header may change and must be
  692. * reloaded after call to this function.
  693. */
  694. /* Moves tail of skb head forward, copying data from fragmented part,
  695. * when it is necessary.
  696. * 1. It may fail due to malloc failure.
  697. * 2. It may change skb pointers.
  698. *
  699. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  700. */
  701. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  702. {
  703. /* If skb has not enough free space at tail, get new one
  704. * plus 128 bytes for future expansions. If we have enough
  705. * room at tail, reallocate without expansion only if skb is cloned.
  706. */
  707. int i, k, eat = (skb->tail + delta) - skb->end;
  708. if (eat > 0 || skb_cloned(skb)) {
  709. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  710. GFP_ATOMIC))
  711. return NULL;
  712. }
  713. if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
  714. BUG();
  715. /* Optimization: no fragments, no reasons to preestimate
  716. * size of pulled pages. Superb.
  717. */
  718. if (!skb_shinfo(skb)->frag_list)
  719. goto pull_pages;
  720. /* Estimate size of pulled pages. */
  721. eat = delta;
  722. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  723. if (skb_shinfo(skb)->frags[i].size >= eat)
  724. goto pull_pages;
  725. eat -= skb_shinfo(skb)->frags[i].size;
  726. }
  727. /* If we need update frag list, we are in troubles.
  728. * Certainly, it possible to add an offset to skb data,
  729. * but taking into account that pulling is expected to
  730. * be very rare operation, it is worth to fight against
  731. * further bloating skb head and crucify ourselves here instead.
  732. * Pure masohism, indeed. 8)8)
  733. */
  734. if (eat) {
  735. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  736. struct sk_buff *clone = NULL;
  737. struct sk_buff *insp = NULL;
  738. do {
  739. if (!list)
  740. BUG();
  741. if (list->len <= eat) {
  742. /* Eaten as whole. */
  743. eat -= list->len;
  744. list = list->next;
  745. insp = list;
  746. } else {
  747. /* Eaten partially. */
  748. if (skb_shared(list)) {
  749. /* Sucks! We need to fork list. :-( */
  750. clone = skb_clone(list, GFP_ATOMIC);
  751. if (!clone)
  752. return NULL;
  753. insp = list->next;
  754. list = clone;
  755. } else {
  756. /* This may be pulled without
  757. * problems. */
  758. insp = list;
  759. }
  760. if (!pskb_pull(list, eat)) {
  761. if (clone)
  762. kfree_skb(clone);
  763. return NULL;
  764. }
  765. break;
  766. }
  767. } while (eat);
  768. /* Free pulled out fragments. */
  769. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  770. skb_shinfo(skb)->frag_list = list->next;
  771. kfree_skb(list);
  772. }
  773. /* And insert new clone at head. */
  774. if (clone) {
  775. clone->next = list;
  776. skb_shinfo(skb)->frag_list = clone;
  777. }
  778. }
  779. /* Success! Now we may commit changes to skb data. */
  780. pull_pages:
  781. eat = delta;
  782. k = 0;
  783. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  784. if (skb_shinfo(skb)->frags[i].size <= eat) {
  785. put_page(skb_shinfo(skb)->frags[i].page);
  786. eat -= skb_shinfo(skb)->frags[i].size;
  787. } else {
  788. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  789. if (eat) {
  790. skb_shinfo(skb)->frags[k].page_offset += eat;
  791. skb_shinfo(skb)->frags[k].size -= eat;
  792. eat = 0;
  793. }
  794. k++;
  795. }
  796. }
  797. skb_shinfo(skb)->nr_frags = k;
  798. skb->tail += delta;
  799. skb->data_len -= delta;
  800. return skb->tail;
  801. }
  802. /* Copy some data bits from skb to kernel buffer. */
  803. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  804. {
  805. int i, copy;
  806. int start = skb_headlen(skb);
  807. if (offset > (int)skb->len - len)
  808. goto fault;
  809. /* Copy header. */
  810. if ((copy = start - offset) > 0) {
  811. if (copy > len)
  812. copy = len;
  813. memcpy(to, skb->data + offset, copy);
  814. if ((len -= copy) == 0)
  815. return 0;
  816. offset += copy;
  817. to += copy;
  818. }
  819. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  820. int end;
  821. BUG_TRAP(start <= offset + len);
  822. end = start + skb_shinfo(skb)->frags[i].size;
  823. if ((copy = end - offset) > 0) {
  824. u8 *vaddr;
  825. if (copy > len)
  826. copy = len;
  827. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  828. memcpy(to,
  829. vaddr + skb_shinfo(skb)->frags[i].page_offset+
  830. offset - start, copy);
  831. kunmap_skb_frag(vaddr);
  832. if ((len -= copy) == 0)
  833. return 0;
  834. offset += copy;
  835. to += copy;
  836. }
  837. start = end;
  838. }
  839. if (skb_shinfo(skb)->frag_list) {
  840. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  841. for (; list; list = list->next) {
  842. int end;
  843. BUG_TRAP(start <= offset + len);
  844. end = start + list->len;
  845. if ((copy = end - offset) > 0) {
  846. if (copy > len)
  847. copy = len;
  848. if (skb_copy_bits(list, offset - start,
  849. to, copy))
  850. goto fault;
  851. if ((len -= copy) == 0)
  852. return 0;
  853. offset += copy;
  854. to += copy;
  855. }
  856. start = end;
  857. }
  858. }
  859. if (!len)
  860. return 0;
  861. fault:
  862. return -EFAULT;
  863. }
  864. /**
  865. * skb_store_bits - store bits from kernel buffer to skb
  866. * @skb: destination buffer
  867. * @offset: offset in destination
  868. * @from: source buffer
  869. * @len: number of bytes to copy
  870. *
  871. * Copy the specified number of bytes from the source buffer to the
  872. * destination skb. This function handles all the messy bits of
  873. * traversing fragment lists and such.
  874. */
  875. int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
  876. {
  877. int i, copy;
  878. int start = skb_headlen(skb);
  879. if (offset > (int)skb->len - len)
  880. goto fault;
  881. if ((copy = start - offset) > 0) {
  882. if (copy > len)
  883. copy = len;
  884. memcpy(skb->data + offset, from, copy);
  885. if ((len -= copy) == 0)
  886. return 0;
  887. offset += copy;
  888. from += copy;
  889. }
  890. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  891. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  892. int end;
  893. BUG_TRAP(start <= offset + len);
  894. end = start + frag->size;
  895. if ((copy = end - offset) > 0) {
  896. u8 *vaddr;
  897. if (copy > len)
  898. copy = len;
  899. vaddr = kmap_skb_frag(frag);
  900. memcpy(vaddr + frag->page_offset + offset - start,
  901. from, copy);
  902. kunmap_skb_frag(vaddr);
  903. if ((len -= copy) == 0)
  904. return 0;
  905. offset += copy;
  906. from += copy;
  907. }
  908. start = end;
  909. }
  910. if (skb_shinfo(skb)->frag_list) {
  911. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  912. for (; list; list = list->next) {
  913. int end;
  914. BUG_TRAP(start <= offset + len);
  915. end = start + list->len;
  916. if ((copy = end - offset) > 0) {
  917. if (copy > len)
  918. copy = len;
  919. if (skb_store_bits(list, offset - start,
  920. from, copy))
  921. goto fault;
  922. if ((len -= copy) == 0)
  923. return 0;
  924. offset += copy;
  925. from += copy;
  926. }
  927. start = end;
  928. }
  929. }
  930. if (!len)
  931. return 0;
  932. fault:
  933. return -EFAULT;
  934. }
  935. EXPORT_SYMBOL(skb_store_bits);
  936. /* Checksum skb data. */
  937. unsigned int skb_checksum(const struct sk_buff *skb, int offset,
  938. int len, unsigned int csum)
  939. {
  940. int start = skb_headlen(skb);
  941. int i, copy = start - offset;
  942. int pos = 0;
  943. /* Checksum header. */
  944. if (copy > 0) {
  945. if (copy > len)
  946. copy = len;
  947. csum = csum_partial(skb->data + offset, copy, csum);
  948. if ((len -= copy) == 0)
  949. return csum;
  950. offset += copy;
  951. pos = copy;
  952. }
  953. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  954. int end;
  955. BUG_TRAP(start <= offset + len);
  956. end = start + skb_shinfo(skb)->frags[i].size;
  957. if ((copy = end - offset) > 0) {
  958. unsigned int csum2;
  959. u8 *vaddr;
  960. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  961. if (copy > len)
  962. copy = len;
  963. vaddr = kmap_skb_frag(frag);
  964. csum2 = csum_partial(vaddr + frag->page_offset +
  965. offset - start, copy, 0);
  966. kunmap_skb_frag(vaddr);
  967. csum = csum_block_add(csum, csum2, pos);
  968. if (!(len -= copy))
  969. return csum;
  970. offset += copy;
  971. pos += copy;
  972. }
  973. start = end;
  974. }
  975. if (skb_shinfo(skb)->frag_list) {
  976. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  977. for (; list; list = list->next) {
  978. int end;
  979. BUG_TRAP(start <= offset + len);
  980. end = start + list->len;
  981. if ((copy = end - offset) > 0) {
  982. unsigned int csum2;
  983. if (copy > len)
  984. copy = len;
  985. csum2 = skb_checksum(list, offset - start,
  986. copy, 0);
  987. csum = csum_block_add(csum, csum2, pos);
  988. if ((len -= copy) == 0)
  989. return csum;
  990. offset += copy;
  991. pos += copy;
  992. }
  993. start = end;
  994. }
  995. }
  996. if (len)
  997. BUG();
  998. return csum;
  999. }
  1000. /* Both of above in one bottle. */
  1001. unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1002. u8 *to, int len, unsigned int csum)
  1003. {
  1004. int start = skb_headlen(skb);
  1005. int i, copy = start - offset;
  1006. int pos = 0;
  1007. /* Copy header. */
  1008. if (copy > 0) {
  1009. if (copy > len)
  1010. copy = len;
  1011. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1012. copy, csum);
  1013. if ((len -= copy) == 0)
  1014. return csum;
  1015. offset += copy;
  1016. to += copy;
  1017. pos = copy;
  1018. }
  1019. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1020. int end;
  1021. BUG_TRAP(start <= offset + len);
  1022. end = start + skb_shinfo(skb)->frags[i].size;
  1023. if ((copy = end - offset) > 0) {
  1024. unsigned int csum2;
  1025. u8 *vaddr;
  1026. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1027. if (copy > len)
  1028. copy = len;
  1029. vaddr = kmap_skb_frag(frag);
  1030. csum2 = csum_partial_copy_nocheck(vaddr +
  1031. frag->page_offset +
  1032. offset - start, to,
  1033. copy, 0);
  1034. kunmap_skb_frag(vaddr);
  1035. csum = csum_block_add(csum, csum2, pos);
  1036. if (!(len -= copy))
  1037. return csum;
  1038. offset += copy;
  1039. to += copy;
  1040. pos += copy;
  1041. }
  1042. start = end;
  1043. }
  1044. if (skb_shinfo(skb)->frag_list) {
  1045. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1046. for (; list; list = list->next) {
  1047. unsigned int csum2;
  1048. int end;
  1049. BUG_TRAP(start <= offset + len);
  1050. end = start + list->len;
  1051. if ((copy = end - offset) > 0) {
  1052. if (copy > len)
  1053. copy = len;
  1054. csum2 = skb_copy_and_csum_bits(list,
  1055. offset - start,
  1056. to, copy, 0);
  1057. csum = csum_block_add(csum, csum2, pos);
  1058. if ((len -= copy) == 0)
  1059. return csum;
  1060. offset += copy;
  1061. to += copy;
  1062. pos += copy;
  1063. }
  1064. start = end;
  1065. }
  1066. }
  1067. if (len)
  1068. BUG();
  1069. return csum;
  1070. }
  1071. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1072. {
  1073. unsigned int csum;
  1074. long csstart;
  1075. if (skb->ip_summed == CHECKSUM_HW)
  1076. csstart = skb->h.raw - skb->data;
  1077. else
  1078. csstart = skb_headlen(skb);
  1079. if (csstart > skb_headlen(skb))
  1080. BUG();
  1081. memcpy(to, skb->data, csstart);
  1082. csum = 0;
  1083. if (csstart != skb->len)
  1084. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1085. skb->len - csstart, 0);
  1086. if (skb->ip_summed == CHECKSUM_HW) {
  1087. long csstuff = csstart + skb->csum;
  1088. *((unsigned short *)(to + csstuff)) = csum_fold(csum);
  1089. }
  1090. }
  1091. /**
  1092. * skb_dequeue - remove from the head of the queue
  1093. * @list: list to dequeue from
  1094. *
  1095. * Remove the head of the list. The list lock is taken so the function
  1096. * may be used safely with other locking list functions. The head item is
  1097. * returned or %NULL if the list is empty.
  1098. */
  1099. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1100. {
  1101. unsigned long flags;
  1102. struct sk_buff *result;
  1103. spin_lock_irqsave(&list->lock, flags);
  1104. result = __skb_dequeue(list);
  1105. spin_unlock_irqrestore(&list->lock, flags);
  1106. return result;
  1107. }
  1108. /**
  1109. * skb_dequeue_tail - remove from the tail of the queue
  1110. * @list: list to dequeue from
  1111. *
  1112. * Remove the tail of the list. The list lock is taken so the function
  1113. * may be used safely with other locking list functions. The tail item is
  1114. * returned or %NULL if the list is empty.
  1115. */
  1116. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1117. {
  1118. unsigned long flags;
  1119. struct sk_buff *result;
  1120. spin_lock_irqsave(&list->lock, flags);
  1121. result = __skb_dequeue_tail(list);
  1122. spin_unlock_irqrestore(&list->lock, flags);
  1123. return result;
  1124. }
  1125. /**
  1126. * skb_queue_purge - empty a list
  1127. * @list: list to empty
  1128. *
  1129. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1130. * the list and one reference dropped. This function takes the list
  1131. * lock and is atomic with respect to other list locking functions.
  1132. */
  1133. void skb_queue_purge(struct sk_buff_head *list)
  1134. {
  1135. struct sk_buff *skb;
  1136. while ((skb = skb_dequeue(list)) != NULL)
  1137. kfree_skb(skb);
  1138. }
  1139. /**
  1140. * skb_queue_head - queue a buffer at the list head
  1141. * @list: list to use
  1142. * @newsk: buffer to queue
  1143. *
  1144. * Queue a buffer at the start of the list. This function takes the
  1145. * list lock and can be used safely with other locking &sk_buff functions
  1146. * safely.
  1147. *
  1148. * A buffer cannot be placed on two lists at the same time.
  1149. */
  1150. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1151. {
  1152. unsigned long flags;
  1153. spin_lock_irqsave(&list->lock, flags);
  1154. __skb_queue_head(list, newsk);
  1155. spin_unlock_irqrestore(&list->lock, flags);
  1156. }
  1157. /**
  1158. * skb_queue_tail - queue a buffer at the list tail
  1159. * @list: list to use
  1160. * @newsk: buffer to queue
  1161. *
  1162. * Queue a buffer at the tail of the list. This function takes the
  1163. * list lock and can be used safely with other locking &sk_buff functions
  1164. * safely.
  1165. *
  1166. * A buffer cannot be placed on two lists at the same time.
  1167. */
  1168. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1169. {
  1170. unsigned long flags;
  1171. spin_lock_irqsave(&list->lock, flags);
  1172. __skb_queue_tail(list, newsk);
  1173. spin_unlock_irqrestore(&list->lock, flags);
  1174. }
  1175. /**
  1176. * skb_unlink - remove a buffer from a list
  1177. * @skb: buffer to remove
  1178. *
  1179. * Place a packet after a given packet in a list. The list locks are taken
  1180. * and this function is atomic with respect to other list locked calls
  1181. *
  1182. * Works even without knowing the list it is sitting on, which can be
  1183. * handy at times. It also means that THE LIST MUST EXIST when you
  1184. * unlink. Thus a list must have its contents unlinked before it is
  1185. * destroyed.
  1186. */
  1187. void skb_unlink(struct sk_buff *skb)
  1188. {
  1189. struct sk_buff_head *list = skb->list;
  1190. if (list) {
  1191. unsigned long flags;
  1192. spin_lock_irqsave(&list->lock, flags);
  1193. if (skb->list == list)
  1194. __skb_unlink(skb, skb->list);
  1195. spin_unlock_irqrestore(&list->lock, flags);
  1196. }
  1197. }
  1198. /**
  1199. * skb_append - append a buffer
  1200. * @old: buffer to insert after
  1201. * @newsk: buffer to insert
  1202. *
  1203. * Place a packet after a given packet in a list. The list locks are taken
  1204. * and this function is atomic with respect to other list locked calls.
  1205. * A buffer cannot be placed on two lists at the same time.
  1206. */
  1207. void skb_append(struct sk_buff *old, struct sk_buff *newsk)
  1208. {
  1209. unsigned long flags;
  1210. spin_lock_irqsave(&old->list->lock, flags);
  1211. __skb_append(old, newsk);
  1212. spin_unlock_irqrestore(&old->list->lock, flags);
  1213. }
  1214. /**
  1215. * skb_insert - insert a buffer
  1216. * @old: buffer to insert before
  1217. * @newsk: buffer to insert
  1218. *
  1219. * Place a packet before a given packet in a list. The list locks are taken
  1220. * and this function is atomic with respect to other list locked calls
  1221. * A buffer cannot be placed on two lists at the same time.
  1222. */
  1223. void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
  1224. {
  1225. unsigned long flags;
  1226. spin_lock_irqsave(&old->list->lock, flags);
  1227. __skb_insert(newsk, old->prev, old, old->list);
  1228. spin_unlock_irqrestore(&old->list->lock, flags);
  1229. }
  1230. #if 0
  1231. /*
  1232. * Tune the memory allocator for a new MTU size.
  1233. */
  1234. void skb_add_mtu(int mtu)
  1235. {
  1236. /* Must match allocation in alloc_skb */
  1237. mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
  1238. kmem_add_cache_size(mtu);
  1239. }
  1240. #endif
  1241. static inline void skb_split_inside_header(struct sk_buff *skb,
  1242. struct sk_buff* skb1,
  1243. const u32 len, const int pos)
  1244. {
  1245. int i;
  1246. memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
  1247. /* And move data appendix as is. */
  1248. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1249. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1250. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1251. skb_shinfo(skb)->nr_frags = 0;
  1252. skb1->data_len = skb->data_len;
  1253. skb1->len += skb1->data_len;
  1254. skb->data_len = 0;
  1255. skb->len = len;
  1256. skb->tail = skb->data + len;
  1257. }
  1258. static inline void skb_split_no_header(struct sk_buff *skb,
  1259. struct sk_buff* skb1,
  1260. const u32 len, int pos)
  1261. {
  1262. int i, k = 0;
  1263. const int nfrags = skb_shinfo(skb)->nr_frags;
  1264. skb_shinfo(skb)->nr_frags = 0;
  1265. skb1->len = skb1->data_len = skb->len - len;
  1266. skb->len = len;
  1267. skb->data_len = len - pos;
  1268. for (i = 0; i < nfrags; i++) {
  1269. int size = skb_shinfo(skb)->frags[i].size;
  1270. if (pos + size > len) {
  1271. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1272. if (pos < len) {
  1273. /* Split frag.
  1274. * We have two variants in this case:
  1275. * 1. Move all the frag to the second
  1276. * part, if it is possible. F.e.
  1277. * this approach is mandatory for TUX,
  1278. * where splitting is expensive.
  1279. * 2. Split is accurately. We make this.
  1280. */
  1281. get_page(skb_shinfo(skb)->frags[i].page);
  1282. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1283. skb_shinfo(skb1)->frags[0].size -= len - pos;
  1284. skb_shinfo(skb)->frags[i].size = len - pos;
  1285. skb_shinfo(skb)->nr_frags++;
  1286. }
  1287. k++;
  1288. } else
  1289. skb_shinfo(skb)->nr_frags++;
  1290. pos += size;
  1291. }
  1292. skb_shinfo(skb1)->nr_frags = k;
  1293. }
  1294. /**
  1295. * skb_split - Split fragmented skb to two parts at length len.
  1296. * @skb: the buffer to split
  1297. * @skb1: the buffer to receive the second part
  1298. * @len: new length for skb
  1299. */
  1300. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1301. {
  1302. int pos = skb_headlen(skb);
  1303. if (len < pos) /* Split line is inside header. */
  1304. skb_split_inside_header(skb, skb1, len, pos);
  1305. else /* Second chunk has no header, nothing to copy. */
  1306. skb_split_no_header(skb, skb1, len, pos);
  1307. }
  1308. void __init skb_init(void)
  1309. {
  1310. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  1311. sizeof(struct sk_buff),
  1312. 0,
  1313. SLAB_HWCACHE_ALIGN,
  1314. NULL, NULL);
  1315. if (!skbuff_head_cache)
  1316. panic("cannot create skbuff cache");
  1317. }
  1318. EXPORT_SYMBOL(___pskb_trim);
  1319. EXPORT_SYMBOL(__kfree_skb);
  1320. EXPORT_SYMBOL(__pskb_pull_tail);
  1321. EXPORT_SYMBOL(alloc_skb);
  1322. EXPORT_SYMBOL(pskb_copy);
  1323. EXPORT_SYMBOL(pskb_expand_head);
  1324. EXPORT_SYMBOL(skb_checksum);
  1325. EXPORT_SYMBOL(skb_clone);
  1326. EXPORT_SYMBOL(skb_clone_fraglist);
  1327. EXPORT_SYMBOL(skb_copy);
  1328. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1329. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1330. EXPORT_SYMBOL(skb_copy_bits);
  1331. EXPORT_SYMBOL(skb_copy_expand);
  1332. EXPORT_SYMBOL(skb_over_panic);
  1333. EXPORT_SYMBOL(skb_pad);
  1334. EXPORT_SYMBOL(skb_realloc_headroom);
  1335. EXPORT_SYMBOL(skb_under_panic);
  1336. EXPORT_SYMBOL(skb_dequeue);
  1337. EXPORT_SYMBOL(skb_dequeue_tail);
  1338. EXPORT_SYMBOL(skb_insert);
  1339. EXPORT_SYMBOL(skb_queue_purge);
  1340. EXPORT_SYMBOL(skb_queue_head);
  1341. EXPORT_SYMBOL(skb_queue_tail);
  1342. EXPORT_SYMBOL(skb_unlink);
  1343. EXPORT_SYMBOL(skb_append);
  1344. EXPORT_SYMBOL(skb_split);