skbuff.c 41 KB

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