skbuff.c 37 KB

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