skbuff.c 49 KB

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