skbuff.c 42 KB

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