skbuff.c 43 KB

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