skbuff.c 41 KB

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