skbuff.c 41 KB

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