skbuff.c 49 KB

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