skbuff.c 45 KB

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