skbuff.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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_INFO "skput:over: %p:%d put:%d dev:%s",
  83. here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
  84. BUG();
  85. }
  86. /**
  87. * skb_under_panic - private function
  88. * @skb: buffer
  89. * @sz: size
  90. * @here: address
  91. *
  92. * Out of line support code for skb_push(). Not user callable.
  93. */
  94. void skb_under_panic(struct sk_buff *skb, int sz, void *here)
  95. {
  96. printk(KERN_INFO "skput:under: %p:%d put:%d dev:%s",
  97. here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
  98. BUG();
  99. }
  100. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  101. * 'private' fields and also do memory statistics to find all the
  102. * [BEEP] leaks.
  103. *
  104. */
  105. /**
  106. * alloc_skb - allocate a network buffer
  107. * @size: size to allocate
  108. * @gfp_mask: allocation mask
  109. *
  110. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  111. * tail room of size bytes. The object has a reference count of one.
  112. * The return is the buffer. On a failure the return is %NULL.
  113. *
  114. * Buffers may only be allocated from interrupts using a @gfp_mask of
  115. * %GFP_ATOMIC.
  116. */
  117. struct sk_buff *alloc_skb(unsigned int size, int gfp_mask)
  118. {
  119. struct sk_buff *skb;
  120. u8 *data;
  121. /* Get the HEAD */
  122. skb = kmem_cache_alloc(skbuff_head_cache,
  123. gfp_mask & ~__GFP_DMA);
  124. if (!skb)
  125. goto out;
  126. /* Get the DATA. Size must match skb_add_mtu(). */
  127. size = SKB_DATA_ALIGN(size);
  128. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  129. if (!data)
  130. goto nodata;
  131. memset(skb, 0, offsetof(struct sk_buff, truesize));
  132. skb->truesize = size + sizeof(struct sk_buff);
  133. atomic_set(&skb->users, 1);
  134. skb->head = data;
  135. skb->data = data;
  136. skb->tail = data;
  137. skb->end = data + size;
  138. atomic_set(&(skb_shinfo(skb)->dataref), 1);
  139. skb_shinfo(skb)->nr_frags = 0;
  140. skb_shinfo(skb)->tso_size = 0;
  141. skb_shinfo(skb)->tso_segs = 0;
  142. skb_shinfo(skb)->frag_list = NULL;
  143. out:
  144. return skb;
  145. nodata:
  146. kmem_cache_free(skbuff_head_cache, skb);
  147. skb = NULL;
  148. goto out;
  149. }
  150. /**
  151. * alloc_skb_from_cache - allocate a network buffer
  152. * @cp: kmem_cache from which to allocate the data area
  153. * (object size must be big enough for @size bytes + skb overheads)
  154. * @size: size to allocate
  155. * @gfp_mask: allocation mask
  156. *
  157. * Allocate a new &sk_buff. The returned buffer has no headroom and
  158. * tail room of size bytes. The object has a reference count of one.
  159. * The return is the buffer. On a failure the return is %NULL.
  160. *
  161. * Buffers may only be allocated from interrupts using a @gfp_mask of
  162. * %GFP_ATOMIC.
  163. */
  164. struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
  165. unsigned int size, int gfp_mask)
  166. {
  167. struct sk_buff *skb;
  168. u8 *data;
  169. /* Get the HEAD */
  170. skb = kmem_cache_alloc(skbuff_head_cache,
  171. gfp_mask & ~__GFP_DMA);
  172. if (!skb)
  173. goto out;
  174. /* Get the DATA. */
  175. size = SKB_DATA_ALIGN(size);
  176. data = kmem_cache_alloc(cp, gfp_mask);
  177. if (!data)
  178. goto nodata;
  179. memset(skb, 0, offsetof(struct sk_buff, truesize));
  180. skb->truesize = size + sizeof(struct sk_buff);
  181. atomic_set(&skb->users, 1);
  182. skb->head = data;
  183. skb->data = data;
  184. skb->tail = data;
  185. skb->end = data + size;
  186. atomic_set(&(skb_shinfo(skb)->dataref), 1);
  187. skb_shinfo(skb)->nr_frags = 0;
  188. skb_shinfo(skb)->tso_size = 0;
  189. skb_shinfo(skb)->tso_segs = 0;
  190. skb_shinfo(skb)->frag_list = NULL;
  191. out:
  192. return skb;
  193. nodata:
  194. kmem_cache_free(skbuff_head_cache, skb);
  195. skb = NULL;
  196. goto out;
  197. }
  198. static void skb_drop_fraglist(struct sk_buff *skb)
  199. {
  200. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  201. skb_shinfo(skb)->frag_list = NULL;
  202. do {
  203. struct sk_buff *this = list;
  204. list = list->next;
  205. kfree_skb(this);
  206. } while (list);
  207. }
  208. static void skb_clone_fraglist(struct sk_buff *skb)
  209. {
  210. struct sk_buff *list;
  211. for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
  212. skb_get(list);
  213. }
  214. void skb_release_data(struct sk_buff *skb)
  215. {
  216. if (!skb->cloned ||
  217. !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  218. &skb_shinfo(skb)->dataref)) {
  219. if (skb_shinfo(skb)->nr_frags) {
  220. int i;
  221. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  222. put_page(skb_shinfo(skb)->frags[i].page);
  223. }
  224. if (skb_shinfo(skb)->frag_list)
  225. skb_drop_fraglist(skb);
  226. kfree(skb->head);
  227. }
  228. }
  229. /*
  230. * Free an skbuff by memory without cleaning the state.
  231. */
  232. void kfree_skbmem(struct sk_buff *skb)
  233. {
  234. skb_release_data(skb);
  235. kmem_cache_free(skbuff_head_cache, skb);
  236. }
  237. /**
  238. * __kfree_skb - private function
  239. * @skb: buffer
  240. *
  241. * Free an sk_buff. Release anything attached to the buffer.
  242. * Clean the state. This is an internal helper function. Users should
  243. * always call kfree_skb
  244. */
  245. void __kfree_skb(struct sk_buff *skb)
  246. {
  247. BUG_ON(skb->list != NULL);
  248. dst_release(skb->dst);
  249. #ifdef CONFIG_XFRM
  250. secpath_put(skb->sp);
  251. #endif
  252. if (skb->destructor) {
  253. WARN_ON(in_irq());
  254. skb->destructor(skb);
  255. }
  256. #ifdef CONFIG_NETFILTER
  257. nf_conntrack_put(skb->nfct);
  258. #ifdef CONFIG_BRIDGE_NETFILTER
  259. nf_bridge_put(skb->nf_bridge);
  260. #endif
  261. #endif
  262. /* XXX: IS this still necessary? - JHS */
  263. #ifdef CONFIG_NET_SCHED
  264. skb->tc_index = 0;
  265. #ifdef CONFIG_NET_CLS_ACT
  266. skb->tc_verd = 0;
  267. skb->tc_classid = 0;
  268. #endif
  269. #endif
  270. kfree_skbmem(skb);
  271. }
  272. /**
  273. * skb_clone - duplicate an sk_buff
  274. * @skb: buffer to clone
  275. * @gfp_mask: allocation priority
  276. *
  277. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  278. * copies share the same packet data but not structure. The new
  279. * buffer has a reference count of 1. If the allocation fails the
  280. * function returns %NULL otherwise the new buffer is returned.
  281. *
  282. * If this function is called from an interrupt gfp_mask() must be
  283. * %GFP_ATOMIC.
  284. */
  285. struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)
  286. {
  287. struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  288. if (!n)
  289. return NULL;
  290. #define C(x) n->x = skb->x
  291. n->next = n->prev = NULL;
  292. n->list = NULL;
  293. n->sk = NULL;
  294. C(stamp);
  295. C(dev);
  296. C(real_dev);
  297. C(h);
  298. C(nh);
  299. C(mac);
  300. C(dst);
  301. dst_clone(skb->dst);
  302. C(sp);
  303. #ifdef CONFIG_INET
  304. secpath_get(skb->sp);
  305. #endif
  306. memcpy(n->cb, skb->cb, sizeof(skb->cb));
  307. C(len);
  308. C(data_len);
  309. C(csum);
  310. C(local_df);
  311. n->cloned = 1;
  312. n->nohdr = 0;
  313. C(pkt_type);
  314. C(ip_summed);
  315. C(priority);
  316. C(protocol);
  317. C(security);
  318. n->destructor = NULL;
  319. #ifdef CONFIG_NETFILTER
  320. C(nfmark);
  321. C(nfcache);
  322. C(nfct);
  323. nf_conntrack_get(skb->nfct);
  324. C(nfctinfo);
  325. #ifdef CONFIG_NETFILTER_DEBUG
  326. C(nf_debug);
  327. #endif
  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(skb->tc_verd);
  341. n->tc_verd = CLR_TC_MUNGED(skb->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. new->security = old->security;
  381. #ifdef CONFIG_NETFILTER
  382. new->nfmark = old->nfmark;
  383. new->nfcache = old->nfcache;
  384. new->nfct = old->nfct;
  385. nf_conntrack_get(old->nfct);
  386. new->nfctinfo = old->nfctinfo;
  387. #ifdef CONFIG_NETFILTER_DEBUG
  388. new->nf_debug = old->nf_debug;
  389. #endif
  390. #ifdef CONFIG_BRIDGE_NETFILTER
  391. new->nf_bridge = old->nf_bridge;
  392. nf_bridge_get(old->nf_bridge);
  393. #endif
  394. #endif
  395. #ifdef CONFIG_NET_SCHED
  396. #ifdef CONFIG_NET_CLS_ACT
  397. new->tc_verd = old->tc_verd;
  398. #endif
  399. new->tc_index = old->tc_index;
  400. #endif
  401. atomic_set(&new->users, 1);
  402. skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
  403. skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
  404. }
  405. /**
  406. * skb_copy - create private copy of an sk_buff
  407. * @skb: buffer to copy
  408. * @gfp_mask: allocation priority
  409. *
  410. * Make a copy of both an &sk_buff and its data. This is used when the
  411. * caller wishes to modify the data and needs a private copy of the
  412. * data to alter. Returns %NULL on failure or the pointer to the buffer
  413. * on success. The returned buffer has a reference count of 1.
  414. *
  415. * As by-product this function converts non-linear &sk_buff to linear
  416. * one, so that &sk_buff becomes completely private and caller is allowed
  417. * to modify all the data of returned buffer. This means that this
  418. * function is not recommended for use in circumstances when only
  419. * header is going to be modified. Use pskb_copy() instead.
  420. */
  421. struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)
  422. {
  423. int headerlen = skb->data - skb->head;
  424. /*
  425. * Allocate the copy buffer
  426. */
  427. struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
  428. gfp_mask);
  429. if (!n)
  430. return NULL;
  431. /* Set the data pointer */
  432. skb_reserve(n, headerlen);
  433. /* Set the tail pointer and length */
  434. skb_put(n, skb->len);
  435. n->csum = skb->csum;
  436. n->ip_summed = skb->ip_summed;
  437. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  438. BUG();
  439. copy_skb_header(n, skb);
  440. return n;
  441. }
  442. /**
  443. * pskb_copy - create copy of an sk_buff with private head.
  444. * @skb: buffer to copy
  445. * @gfp_mask: allocation priority
  446. *
  447. * Make a copy of both an &sk_buff and part of its data, located
  448. * in header. Fragmented data remain shared. This is used when
  449. * the caller wishes to modify only header of &sk_buff and needs
  450. * private copy of the header to alter. Returns %NULL on failure
  451. * or the pointer to the buffer on success.
  452. * The returned buffer has a reference count of 1.
  453. */
  454. struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask)
  455. {
  456. /*
  457. * Allocate the copy buffer
  458. */
  459. struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
  460. if (!n)
  461. goto out;
  462. /* Set the data pointer */
  463. skb_reserve(n, skb->data - skb->head);
  464. /* Set the tail pointer and length */
  465. skb_put(n, skb_headlen(skb));
  466. /* Copy the bytes */
  467. memcpy(n->data, skb->data, n->len);
  468. n->csum = skb->csum;
  469. n->ip_summed = skb->ip_summed;
  470. n->data_len = skb->data_len;
  471. n->len = skb->len;
  472. if (skb_shinfo(skb)->nr_frags) {
  473. int i;
  474. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  475. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  476. get_page(skb_shinfo(n)->frags[i].page);
  477. }
  478. skb_shinfo(n)->nr_frags = i;
  479. }
  480. if (skb_shinfo(skb)->frag_list) {
  481. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  482. skb_clone_fraglist(n);
  483. }
  484. copy_skb_header(n, skb);
  485. out:
  486. return n;
  487. }
  488. /**
  489. * pskb_expand_head - reallocate header of &sk_buff
  490. * @skb: buffer to reallocate
  491. * @nhead: room to add at head
  492. * @ntail: room to add at tail
  493. * @gfp_mask: allocation priority
  494. *
  495. * Expands (or creates identical copy, if &nhead and &ntail are zero)
  496. * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
  497. * reference count of 1. Returns zero in the case of success or error,
  498. * if expansion failed. In the last case, &sk_buff is not changed.
  499. *
  500. * All the pointers pointing into skb header may change and must be
  501. * reloaded after call to this function.
  502. */
  503. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask)
  504. {
  505. int i;
  506. u8 *data;
  507. int size = nhead + (skb->end - skb->head) + ntail;
  508. long off;
  509. if (skb_shared(skb))
  510. BUG();
  511. size = SKB_DATA_ALIGN(size);
  512. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  513. if (!data)
  514. goto nodata;
  515. /* Copy only real data... and, alas, header. This should be
  516. * optimized for the cases when header is void. */
  517. memcpy(data + nhead, skb->head, skb->tail - skb->head);
  518. memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
  519. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  520. get_page(skb_shinfo(skb)->frags[i].page);
  521. if (skb_shinfo(skb)->frag_list)
  522. skb_clone_fraglist(skb);
  523. skb_release_data(skb);
  524. off = (data + nhead) - skb->head;
  525. skb->head = data;
  526. skb->end = data + size;
  527. skb->data += off;
  528. skb->tail += off;
  529. skb->mac.raw += off;
  530. skb->h.raw += off;
  531. skb->nh.raw += off;
  532. skb->cloned = 0;
  533. skb->nohdr = 0;
  534. atomic_set(&skb_shinfo(skb)->dataref, 1);
  535. return 0;
  536. nodata:
  537. return -ENOMEM;
  538. }
  539. /* Make private copy of skb with writable head and some headroom */
  540. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  541. {
  542. struct sk_buff *skb2;
  543. int delta = headroom - skb_headroom(skb);
  544. if (delta <= 0)
  545. skb2 = pskb_copy(skb, GFP_ATOMIC);
  546. else {
  547. skb2 = skb_clone(skb, GFP_ATOMIC);
  548. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  549. GFP_ATOMIC)) {
  550. kfree_skb(skb2);
  551. skb2 = NULL;
  552. }
  553. }
  554. return skb2;
  555. }
  556. /**
  557. * skb_copy_expand - copy and expand sk_buff
  558. * @skb: buffer to copy
  559. * @newheadroom: new free bytes at head
  560. * @newtailroom: new free bytes at tail
  561. * @gfp_mask: allocation priority
  562. *
  563. * Make a copy of both an &sk_buff and its data and while doing so
  564. * allocate additional space.
  565. *
  566. * This is used when the caller wishes to modify the data and needs a
  567. * private copy of the data to alter as well as more space for new fields.
  568. * Returns %NULL on failure or the pointer to the buffer
  569. * on success. The returned buffer has a reference count of 1.
  570. *
  571. * You must pass %GFP_ATOMIC as the allocation priority if this function
  572. * is called from an interrupt.
  573. *
  574. * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
  575. * only by netfilter in the cases when checksum is recalculated? --ANK
  576. */
  577. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  578. int newheadroom, int newtailroom, int gfp_mask)
  579. {
  580. /*
  581. * Allocate the copy buffer
  582. */
  583. struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
  584. gfp_mask);
  585. int head_copy_len, head_copy_off;
  586. if (!n)
  587. return NULL;
  588. skb_reserve(n, newheadroom);
  589. /* Set the tail pointer and length */
  590. skb_put(n, skb->len);
  591. head_copy_len = skb_headroom(skb);
  592. head_copy_off = 0;
  593. if (newheadroom <= head_copy_len)
  594. head_copy_len = newheadroom;
  595. else
  596. head_copy_off = newheadroom - head_copy_len;
  597. /* Copy the linear header and data. */
  598. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  599. skb->len + head_copy_len))
  600. BUG();
  601. copy_skb_header(n, skb);
  602. return n;
  603. }
  604. /**
  605. * skb_pad - zero pad the tail of an skb
  606. * @skb: buffer to pad
  607. * @pad: space to pad
  608. *
  609. * Ensure that a buffer is followed by a padding area that is zero
  610. * filled. Used by network drivers which may DMA or transfer data
  611. * beyond the buffer end onto the wire.
  612. *
  613. * May return NULL in out of memory cases.
  614. */
  615. struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
  616. {
  617. struct sk_buff *nskb;
  618. /* If the skbuff is non linear tailroom is always zero.. */
  619. if (skb_tailroom(skb) >= pad) {
  620. memset(skb->data+skb->len, 0, pad);
  621. return skb;
  622. }
  623. nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
  624. kfree_skb(skb);
  625. if (nskb)
  626. memset(nskb->data+nskb->len, 0, pad);
  627. return nskb;
  628. }
  629. /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
  630. * If realloc==0 and trimming is impossible without change of data,
  631. * it is BUG().
  632. */
  633. int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
  634. {
  635. int offset = skb_headlen(skb);
  636. int nfrags = skb_shinfo(skb)->nr_frags;
  637. int i;
  638. for (i = 0; i < nfrags; i++) {
  639. int end = offset + skb_shinfo(skb)->frags[i].size;
  640. if (end > len) {
  641. if (skb_cloned(skb)) {
  642. if (!realloc)
  643. BUG();
  644. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  645. return -ENOMEM;
  646. }
  647. if (len <= offset) {
  648. put_page(skb_shinfo(skb)->frags[i].page);
  649. skb_shinfo(skb)->nr_frags--;
  650. } else {
  651. skb_shinfo(skb)->frags[i].size = len - offset;
  652. }
  653. }
  654. offset = end;
  655. }
  656. if (offset < len) {
  657. skb->data_len -= skb->len - len;
  658. skb->len = len;
  659. } else {
  660. if (len <= skb_headlen(skb)) {
  661. skb->len = len;
  662. skb->data_len = 0;
  663. skb->tail = skb->data + len;
  664. if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
  665. skb_drop_fraglist(skb);
  666. } else {
  667. skb->data_len -= skb->len - len;
  668. skb->len = len;
  669. }
  670. }
  671. return 0;
  672. }
  673. /**
  674. * __pskb_pull_tail - advance tail of skb header
  675. * @skb: buffer to reallocate
  676. * @delta: number of bytes to advance tail
  677. *
  678. * The function makes a sense only on a fragmented &sk_buff,
  679. * it expands header moving its tail forward and copying necessary
  680. * data from fragmented part.
  681. *
  682. * &sk_buff MUST have reference count of 1.
  683. *
  684. * Returns %NULL (and &sk_buff does not change) if pull failed
  685. * or value of new tail of skb in the case of success.
  686. *
  687. * All the pointers pointing into skb header may change and must be
  688. * reloaded after call to this function.
  689. */
  690. /* Moves tail of skb head forward, copying data from fragmented part,
  691. * when it is necessary.
  692. * 1. It may fail due to malloc failure.
  693. * 2. It may change skb pointers.
  694. *
  695. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  696. */
  697. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  698. {
  699. /* If skb has not enough free space at tail, get new one
  700. * plus 128 bytes for future expansions. If we have enough
  701. * room at tail, reallocate without expansion only if skb is cloned.
  702. */
  703. int i, k, eat = (skb->tail + delta) - skb->end;
  704. if (eat > 0 || skb_cloned(skb)) {
  705. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  706. GFP_ATOMIC))
  707. return NULL;
  708. }
  709. if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
  710. BUG();
  711. /* Optimization: no fragments, no reasons to preestimate
  712. * size of pulled pages. Superb.
  713. */
  714. if (!skb_shinfo(skb)->frag_list)
  715. goto pull_pages;
  716. /* Estimate size of pulled pages. */
  717. eat = delta;
  718. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  719. if (skb_shinfo(skb)->frags[i].size >= eat)
  720. goto pull_pages;
  721. eat -= skb_shinfo(skb)->frags[i].size;
  722. }
  723. /* If we need update frag list, we are in troubles.
  724. * Certainly, it possible to add an offset to skb data,
  725. * but taking into account that pulling is expected to
  726. * be very rare operation, it is worth to fight against
  727. * further bloating skb head and crucify ourselves here instead.
  728. * Pure masohism, indeed. 8)8)
  729. */
  730. if (eat) {
  731. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  732. struct sk_buff *clone = NULL;
  733. struct sk_buff *insp = NULL;
  734. do {
  735. if (!list)
  736. BUG();
  737. if (list->len <= eat) {
  738. /* Eaten as whole. */
  739. eat -= list->len;
  740. list = list->next;
  741. insp = list;
  742. } else {
  743. /* Eaten partially. */
  744. if (skb_shared(list)) {
  745. /* Sucks! We need to fork list. :-( */
  746. clone = skb_clone(list, GFP_ATOMIC);
  747. if (!clone)
  748. return NULL;
  749. insp = list->next;
  750. list = clone;
  751. } else {
  752. /* This may be pulled without
  753. * problems. */
  754. insp = list;
  755. }
  756. if (!pskb_pull(list, eat)) {
  757. if (clone)
  758. kfree_skb(clone);
  759. return NULL;
  760. }
  761. break;
  762. }
  763. } while (eat);
  764. /* Free pulled out fragments. */
  765. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  766. skb_shinfo(skb)->frag_list = list->next;
  767. kfree_skb(list);
  768. }
  769. /* And insert new clone at head. */
  770. if (clone) {
  771. clone->next = list;
  772. skb_shinfo(skb)->frag_list = clone;
  773. }
  774. }
  775. /* Success! Now we may commit changes to skb data. */
  776. pull_pages:
  777. eat = delta;
  778. k = 0;
  779. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  780. if (skb_shinfo(skb)->frags[i].size <= eat) {
  781. put_page(skb_shinfo(skb)->frags[i].page);
  782. eat -= skb_shinfo(skb)->frags[i].size;
  783. } else {
  784. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  785. if (eat) {
  786. skb_shinfo(skb)->frags[k].page_offset += eat;
  787. skb_shinfo(skb)->frags[k].size -= eat;
  788. eat = 0;
  789. }
  790. k++;
  791. }
  792. }
  793. skb_shinfo(skb)->nr_frags = k;
  794. skb->tail += delta;
  795. skb->data_len -= delta;
  796. return skb->tail;
  797. }
  798. /* Copy some data bits from skb to kernel buffer. */
  799. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  800. {
  801. int i, copy;
  802. int start = skb_headlen(skb);
  803. if (offset > (int)skb->len - len)
  804. goto fault;
  805. /* Copy header. */
  806. if ((copy = start - offset) > 0) {
  807. if (copy > len)
  808. copy = len;
  809. memcpy(to, skb->data + offset, copy);
  810. if ((len -= copy) == 0)
  811. return 0;
  812. offset += copy;
  813. to += copy;
  814. }
  815. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  816. int end;
  817. BUG_TRAP(start <= offset + len);
  818. end = start + skb_shinfo(skb)->frags[i].size;
  819. if ((copy = end - offset) > 0) {
  820. u8 *vaddr;
  821. if (copy > len)
  822. copy = len;
  823. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  824. memcpy(to,
  825. vaddr + skb_shinfo(skb)->frags[i].page_offset+
  826. offset - start, copy);
  827. kunmap_skb_frag(vaddr);
  828. if ((len -= copy) == 0)
  829. return 0;
  830. offset += copy;
  831. to += copy;
  832. }
  833. start = end;
  834. }
  835. if (skb_shinfo(skb)->frag_list) {
  836. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  837. for (; list; list = list->next) {
  838. int end;
  839. BUG_TRAP(start <= offset + len);
  840. end = start + list->len;
  841. if ((copy = end - offset) > 0) {
  842. if (copy > len)
  843. copy = len;
  844. if (skb_copy_bits(list, offset - start,
  845. to, copy))
  846. goto fault;
  847. if ((len -= copy) == 0)
  848. return 0;
  849. offset += copy;
  850. to += copy;
  851. }
  852. start = end;
  853. }
  854. }
  855. if (!len)
  856. return 0;
  857. fault:
  858. return -EFAULT;
  859. }
  860. /**
  861. * skb_store_bits - store bits from kernel buffer to skb
  862. * @skb: destination buffer
  863. * @offset: offset in destination
  864. * @from: source buffer
  865. * @len: number of bytes to copy
  866. *
  867. * Copy the specified number of bytes from the source buffer to the
  868. * destination skb. This function handles all the messy bits of
  869. * traversing fragment lists and such.
  870. */
  871. int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
  872. {
  873. int i, copy;
  874. int start = skb_headlen(skb);
  875. if (offset > (int)skb->len - len)
  876. goto fault;
  877. if ((copy = start - offset) > 0) {
  878. if (copy > len)
  879. copy = len;
  880. memcpy(skb->data + offset, from, copy);
  881. if ((len -= copy) == 0)
  882. return 0;
  883. offset += copy;
  884. from += copy;
  885. }
  886. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  887. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  888. int end;
  889. BUG_TRAP(start <= offset + len);
  890. end = start + frag->size;
  891. if ((copy = end - offset) > 0) {
  892. u8 *vaddr;
  893. if (copy > len)
  894. copy = len;
  895. vaddr = kmap_skb_frag(frag);
  896. memcpy(vaddr + frag->page_offset + offset - start,
  897. from, copy);
  898. kunmap_skb_frag(vaddr);
  899. if ((len -= copy) == 0)
  900. return 0;
  901. offset += copy;
  902. from += copy;
  903. }
  904. start = end;
  905. }
  906. if (skb_shinfo(skb)->frag_list) {
  907. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  908. for (; list; list = list->next) {
  909. int end;
  910. BUG_TRAP(start <= offset + len);
  911. end = start + list->len;
  912. if ((copy = end - offset) > 0) {
  913. if (copy > len)
  914. copy = len;
  915. if (skb_store_bits(list, offset - start,
  916. from, copy))
  917. goto fault;
  918. if ((len -= copy) == 0)
  919. return 0;
  920. offset += copy;
  921. from += copy;
  922. }
  923. start = end;
  924. }
  925. }
  926. if (!len)
  927. return 0;
  928. fault:
  929. return -EFAULT;
  930. }
  931. EXPORT_SYMBOL(skb_store_bits);
  932. /* Checksum skb data. */
  933. unsigned int skb_checksum(const struct sk_buff *skb, int offset,
  934. int len, unsigned int csum)
  935. {
  936. int start = skb_headlen(skb);
  937. int i, copy = start - offset;
  938. int pos = 0;
  939. /* Checksum header. */
  940. if (copy > 0) {
  941. if (copy > len)
  942. copy = len;
  943. csum = csum_partial(skb->data + offset, copy, csum);
  944. if ((len -= copy) == 0)
  945. return csum;
  946. offset += copy;
  947. pos = copy;
  948. }
  949. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  950. int end;
  951. BUG_TRAP(start <= offset + len);
  952. end = start + skb_shinfo(skb)->frags[i].size;
  953. if ((copy = end - offset) > 0) {
  954. unsigned int csum2;
  955. u8 *vaddr;
  956. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  957. if (copy > len)
  958. copy = len;
  959. vaddr = kmap_skb_frag(frag);
  960. csum2 = csum_partial(vaddr + frag->page_offset +
  961. offset - start, copy, 0);
  962. kunmap_skb_frag(vaddr);
  963. csum = csum_block_add(csum, csum2, pos);
  964. if (!(len -= copy))
  965. return csum;
  966. offset += copy;
  967. pos += copy;
  968. }
  969. start = end;
  970. }
  971. if (skb_shinfo(skb)->frag_list) {
  972. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  973. for (; list; list = list->next) {
  974. int end;
  975. BUG_TRAP(start <= offset + len);
  976. end = start + list->len;
  977. if ((copy = end - offset) > 0) {
  978. unsigned int csum2;
  979. if (copy > len)
  980. copy = len;
  981. csum2 = skb_checksum(list, offset - start,
  982. copy, 0);
  983. csum = csum_block_add(csum, csum2, pos);
  984. if ((len -= copy) == 0)
  985. return csum;
  986. offset += copy;
  987. pos += copy;
  988. }
  989. start = end;
  990. }
  991. }
  992. if (len)
  993. BUG();
  994. return csum;
  995. }
  996. /* Both of above in one bottle. */
  997. unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  998. u8 *to, int len, unsigned int csum)
  999. {
  1000. int start = skb_headlen(skb);
  1001. int i, copy = start - offset;
  1002. int pos = 0;
  1003. /* Copy header. */
  1004. if (copy > 0) {
  1005. if (copy > len)
  1006. copy = len;
  1007. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1008. copy, csum);
  1009. if ((len -= copy) == 0)
  1010. return csum;
  1011. offset += copy;
  1012. to += copy;
  1013. pos = copy;
  1014. }
  1015. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1016. int end;
  1017. BUG_TRAP(start <= offset + len);
  1018. end = start + skb_shinfo(skb)->frags[i].size;
  1019. if ((copy = end - offset) > 0) {
  1020. unsigned int csum2;
  1021. u8 *vaddr;
  1022. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1023. if (copy > len)
  1024. copy = len;
  1025. vaddr = kmap_skb_frag(frag);
  1026. csum2 = csum_partial_copy_nocheck(vaddr +
  1027. frag->page_offset +
  1028. offset - start, to,
  1029. copy, 0);
  1030. kunmap_skb_frag(vaddr);
  1031. csum = csum_block_add(csum, csum2, pos);
  1032. if (!(len -= copy))
  1033. return csum;
  1034. offset += copy;
  1035. to += copy;
  1036. pos += copy;
  1037. }
  1038. start = end;
  1039. }
  1040. if (skb_shinfo(skb)->frag_list) {
  1041. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1042. for (; list; list = list->next) {
  1043. unsigned int csum2;
  1044. int end;
  1045. BUG_TRAP(start <= offset + len);
  1046. end = start + list->len;
  1047. if ((copy = end - offset) > 0) {
  1048. if (copy > len)
  1049. copy = len;
  1050. csum2 = skb_copy_and_csum_bits(list,
  1051. offset - start,
  1052. to, copy, 0);
  1053. csum = csum_block_add(csum, csum2, pos);
  1054. if ((len -= copy) == 0)
  1055. return csum;
  1056. offset += copy;
  1057. to += copy;
  1058. pos += copy;
  1059. }
  1060. start = end;
  1061. }
  1062. }
  1063. if (len)
  1064. BUG();
  1065. return csum;
  1066. }
  1067. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1068. {
  1069. unsigned int csum;
  1070. long csstart;
  1071. if (skb->ip_summed == CHECKSUM_HW)
  1072. csstart = skb->h.raw - skb->data;
  1073. else
  1074. csstart = skb_headlen(skb);
  1075. if (csstart > skb_headlen(skb))
  1076. BUG();
  1077. memcpy(to, skb->data, csstart);
  1078. csum = 0;
  1079. if (csstart != skb->len)
  1080. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1081. skb->len - csstart, 0);
  1082. if (skb->ip_summed == CHECKSUM_HW) {
  1083. long csstuff = csstart + skb->csum;
  1084. *((unsigned short *)(to + csstuff)) = csum_fold(csum);
  1085. }
  1086. }
  1087. /**
  1088. * skb_dequeue - remove from the head of the queue
  1089. * @list: list to dequeue from
  1090. *
  1091. * Remove the head of the list. The list lock is taken so the function
  1092. * may be used safely with other locking list functions. The head item is
  1093. * returned or %NULL if the list is empty.
  1094. */
  1095. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1096. {
  1097. unsigned long flags;
  1098. struct sk_buff *result;
  1099. spin_lock_irqsave(&list->lock, flags);
  1100. result = __skb_dequeue(list);
  1101. spin_unlock_irqrestore(&list->lock, flags);
  1102. return result;
  1103. }
  1104. /**
  1105. * skb_dequeue_tail - remove from the tail of the queue
  1106. * @list: list to dequeue from
  1107. *
  1108. * Remove the tail of the list. The list lock is taken so the function
  1109. * may be used safely with other locking list functions. The tail item is
  1110. * returned or %NULL if the list is empty.
  1111. */
  1112. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1113. {
  1114. unsigned long flags;
  1115. struct sk_buff *result;
  1116. spin_lock_irqsave(&list->lock, flags);
  1117. result = __skb_dequeue_tail(list);
  1118. spin_unlock_irqrestore(&list->lock, flags);
  1119. return result;
  1120. }
  1121. /**
  1122. * skb_queue_purge - empty a list
  1123. * @list: list to empty
  1124. *
  1125. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1126. * the list and one reference dropped. This function takes the list
  1127. * lock and is atomic with respect to other list locking functions.
  1128. */
  1129. void skb_queue_purge(struct sk_buff_head *list)
  1130. {
  1131. struct sk_buff *skb;
  1132. while ((skb = skb_dequeue(list)) != NULL)
  1133. kfree_skb(skb);
  1134. }
  1135. /**
  1136. * skb_queue_head - queue a buffer at the list head
  1137. * @list: list to use
  1138. * @newsk: buffer to queue
  1139. *
  1140. * Queue a buffer at the start of the list. This function takes the
  1141. * list lock and can be used safely with other locking &sk_buff functions
  1142. * safely.
  1143. *
  1144. * A buffer cannot be placed on two lists at the same time.
  1145. */
  1146. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1147. {
  1148. unsigned long flags;
  1149. spin_lock_irqsave(&list->lock, flags);
  1150. __skb_queue_head(list, newsk);
  1151. spin_unlock_irqrestore(&list->lock, flags);
  1152. }
  1153. /**
  1154. * skb_queue_tail - queue a buffer at the list tail
  1155. * @list: list to use
  1156. * @newsk: buffer to queue
  1157. *
  1158. * Queue a buffer at the tail of the list. This function takes the
  1159. * list lock and can be used safely with other locking &sk_buff functions
  1160. * safely.
  1161. *
  1162. * A buffer cannot be placed on two lists at the same time.
  1163. */
  1164. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1165. {
  1166. unsigned long flags;
  1167. spin_lock_irqsave(&list->lock, flags);
  1168. __skb_queue_tail(list, newsk);
  1169. spin_unlock_irqrestore(&list->lock, flags);
  1170. }
  1171. /**
  1172. * skb_unlink - remove a buffer from a list
  1173. * @skb: buffer to remove
  1174. *
  1175. * Place a packet after a given packet in a list. The list locks are taken
  1176. * and this function is atomic with respect to other list locked calls
  1177. *
  1178. * Works even without knowing the list it is sitting on, which can be
  1179. * handy at times. It also means that THE LIST MUST EXIST when you
  1180. * unlink. Thus a list must have its contents unlinked before it is
  1181. * destroyed.
  1182. */
  1183. void skb_unlink(struct sk_buff *skb)
  1184. {
  1185. struct sk_buff_head *list = skb->list;
  1186. if (list) {
  1187. unsigned long flags;
  1188. spin_lock_irqsave(&list->lock, flags);
  1189. if (skb->list == list)
  1190. __skb_unlink(skb, skb->list);
  1191. spin_unlock_irqrestore(&list->lock, flags);
  1192. }
  1193. }
  1194. /**
  1195. * skb_append - append a buffer
  1196. * @old: buffer to insert after
  1197. * @newsk: buffer to insert
  1198. *
  1199. * Place a packet after a given packet in a list. The list locks are taken
  1200. * and this function is atomic with respect to other list locked calls.
  1201. * A buffer cannot be placed on two lists at the same time.
  1202. */
  1203. void skb_append(struct sk_buff *old, struct sk_buff *newsk)
  1204. {
  1205. unsigned long flags;
  1206. spin_lock_irqsave(&old->list->lock, flags);
  1207. __skb_append(old, newsk);
  1208. spin_unlock_irqrestore(&old->list->lock, flags);
  1209. }
  1210. /**
  1211. * skb_insert - insert a buffer
  1212. * @old: buffer to insert before
  1213. * @newsk: buffer to insert
  1214. *
  1215. * Place a packet before a given packet in a list. The list locks are taken
  1216. * and this function is atomic with respect to other list locked calls
  1217. * A buffer cannot be placed on two lists at the same time.
  1218. */
  1219. void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
  1220. {
  1221. unsigned long flags;
  1222. spin_lock_irqsave(&old->list->lock, flags);
  1223. __skb_insert(newsk, old->prev, old, old->list);
  1224. spin_unlock_irqrestore(&old->list->lock, flags);
  1225. }
  1226. #if 0
  1227. /*
  1228. * Tune the memory allocator for a new MTU size.
  1229. */
  1230. void skb_add_mtu(int mtu)
  1231. {
  1232. /* Must match allocation in alloc_skb */
  1233. mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
  1234. kmem_add_cache_size(mtu);
  1235. }
  1236. #endif
  1237. static inline void skb_split_inside_header(struct sk_buff *skb,
  1238. struct sk_buff* skb1,
  1239. const u32 len, const int pos)
  1240. {
  1241. int i;
  1242. memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
  1243. /* And move data appendix as is. */
  1244. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1245. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1246. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1247. skb_shinfo(skb)->nr_frags = 0;
  1248. skb1->data_len = skb->data_len;
  1249. skb1->len += skb1->data_len;
  1250. skb->data_len = 0;
  1251. skb->len = len;
  1252. skb->tail = skb->data + len;
  1253. }
  1254. static inline void skb_split_no_header(struct sk_buff *skb,
  1255. struct sk_buff* skb1,
  1256. const u32 len, int pos)
  1257. {
  1258. int i, k = 0;
  1259. const int nfrags = skb_shinfo(skb)->nr_frags;
  1260. skb_shinfo(skb)->nr_frags = 0;
  1261. skb1->len = skb1->data_len = skb->len - len;
  1262. skb->len = len;
  1263. skb->data_len = len - pos;
  1264. for (i = 0; i < nfrags; i++) {
  1265. int size = skb_shinfo(skb)->frags[i].size;
  1266. if (pos + size > len) {
  1267. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1268. if (pos < len) {
  1269. /* Split frag.
  1270. * We have two variants in this case:
  1271. * 1. Move all the frag to the second
  1272. * part, if it is possible. F.e.
  1273. * this approach is mandatory for TUX,
  1274. * where splitting is expensive.
  1275. * 2. Split is accurately. We make this.
  1276. */
  1277. get_page(skb_shinfo(skb)->frags[i].page);
  1278. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1279. skb_shinfo(skb1)->frags[0].size -= len - pos;
  1280. skb_shinfo(skb)->frags[i].size = len - pos;
  1281. skb_shinfo(skb)->nr_frags++;
  1282. }
  1283. k++;
  1284. } else
  1285. skb_shinfo(skb)->nr_frags++;
  1286. pos += size;
  1287. }
  1288. skb_shinfo(skb1)->nr_frags = k;
  1289. }
  1290. /**
  1291. * skb_split - Split fragmented skb to two parts at length len.
  1292. * @skb: the buffer to split
  1293. * @skb1: the buffer to receive the second part
  1294. * @len: new length for skb
  1295. */
  1296. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1297. {
  1298. int pos = skb_headlen(skb);
  1299. if (len < pos) /* Split line is inside header. */
  1300. skb_split_inside_header(skb, skb1, len, pos);
  1301. else /* Second chunk has no header, nothing to copy. */
  1302. skb_split_no_header(skb, skb1, len, pos);
  1303. }
  1304. void __init skb_init(void)
  1305. {
  1306. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  1307. sizeof(struct sk_buff),
  1308. 0,
  1309. SLAB_HWCACHE_ALIGN,
  1310. NULL, NULL);
  1311. if (!skbuff_head_cache)
  1312. panic("cannot create skbuff cache");
  1313. }
  1314. EXPORT_SYMBOL(___pskb_trim);
  1315. EXPORT_SYMBOL(__kfree_skb);
  1316. EXPORT_SYMBOL(__pskb_pull_tail);
  1317. EXPORT_SYMBOL(alloc_skb);
  1318. EXPORT_SYMBOL(pskb_copy);
  1319. EXPORT_SYMBOL(pskb_expand_head);
  1320. EXPORT_SYMBOL(skb_checksum);
  1321. EXPORT_SYMBOL(skb_clone);
  1322. EXPORT_SYMBOL(skb_clone_fraglist);
  1323. EXPORT_SYMBOL(skb_copy);
  1324. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1325. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1326. EXPORT_SYMBOL(skb_copy_bits);
  1327. EXPORT_SYMBOL(skb_copy_expand);
  1328. EXPORT_SYMBOL(skb_over_panic);
  1329. EXPORT_SYMBOL(skb_pad);
  1330. EXPORT_SYMBOL(skb_realloc_headroom);
  1331. EXPORT_SYMBOL(skb_under_panic);
  1332. EXPORT_SYMBOL(skb_dequeue);
  1333. EXPORT_SYMBOL(skb_dequeue_tail);
  1334. EXPORT_SYMBOL(skb_insert);
  1335. EXPORT_SYMBOL(skb_queue_purge);
  1336. EXPORT_SYMBOL(skb_queue_head);
  1337. EXPORT_SYMBOL(skb_queue_tail);
  1338. EXPORT_SYMBOL(skb_unlink);
  1339. EXPORT_SYMBOL(skb_append);
  1340. EXPORT_SYMBOL(skb_split);