skbuff.c 42 KB

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