skbuff.c 42 KB

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