skbuff.c 41 KB

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