skbuff.c 45 KB

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