skbuff.c 45 KB

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