skbuff.c 46 KB

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