skbuff.c 41 KB

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